Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions assets/app-notes.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
--color-text-tertiary: #64748b;
--color-danger: #dc2626;
--color-danger-soft: #fef2f2;
--color-danger-solid: #b91c1c;
--color-danger-solid-hover: #991b1b;
--color-control-disabled: #94a3b8;
--color-selection: #dbeafe;
--color-focus-ring: rgba(15, 23, 42, 0.78);
Expand Down
105 changes: 105 additions & 0 deletions components/ClearNotesDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { useId, useLayoutEffect, useRef, useState } from 'react';
import type { RefObject } from 'react';
import { isLocalFileUrl } from '@/lib/annotation-scope';
import type { AnnotationScope } from '@/lib/annotation-scope';
import { getPageDisplayLabel, getSiteDisplayLabel } from '@/lib/page';

interface ClearNotesDialogProps {
readonly url: string;
readonly scope: AnnotationScope;
readonly fallbackFocusRef: RefObject<HTMLElement | null>;
readonly onConfirm: () => Promise<boolean>;
readonly onDismiss: () => void;
}

/** Confirm a destructive clear inside the extension's own viewport. */
export function ClearNotesDialog({ url, scope, fallbackFocusRef, onConfirm, onDismiss }: ClearNotesDialogProps) {
const dialogRef = useRef<HTMLDialogElement>(null);
const confirmingRef = useRef(false);
const [status, setStatus] = useState<'ready' | 'deleting' | 'failed'>('ready');
const titleId = useId();
const descriptionId = useId();
const collection = isLocalFileUrl(url) ? (scope === 'page' ? 'file' : 'folder') : 'site';
const label = collection === 'file' ? getPageDisplayLabel(url) : getSiteDisplayLabel(url);
const deleting = status === 'deleting';

useLayoutEffect(() => {
const dialog = dialogRef.current;
if (!dialog) return;
dialog.showModal();
return () => {
dialog.close();
// On success the original trigger may be disabled or removed.
window.requestAnimationFrame(() => {
if (document.activeElement === document.body) {
fallbackFocusRef.current?.focus({ preventScroll: true });
}
});
};
}, [fallbackFocusRef]);

const dismiss = () => {
if (!confirmingRef.current) onDismiss();
};

const confirm = async () => {
if (confirmingRef.current) return;
confirmingRef.current = true;
setStatus('deleting');
let cleared = false;
try {
cleared = await onConfirm();
} catch {
// Keep the dialog open so the user can retry or cancel.
}
if (!dialogRef.current) return;
if (cleared) {
onDismiss();
} else {
confirmingRef.current = false;
setStatus('failed');
}
};

return (
<dialog
ref={dialogRef}
role="alertdialog"
aria-labelledby={titleId}
aria-describedby={descriptionId}
onCancel={(event) => {
event.preventDefault();
dismiss();
}}
className="fixed inset-0 m-auto max-h-[calc(100%_-_1rem)] w-[calc(100%_-_1rem)] max-w-80 overflow-y-auto overscroll-contain rounded-xl border border-border-subtle bg-surface p-3 text-left shadow-lg backdrop:bg-black/45"
>
<h2 id={titleId} className="text-sm font-semibold text-text-primary">
Delete all {collection} notes?
</h2>
<p id={descriptionId} className="mt-1 text-xs leading-5 text-text-secondary wrap-anywhere">
Notes for {label ? <bdi className="font-medium text-text-primary">{label}</bdi> : `this ${collection}`} will be permanently deleted. This can’t be undone.
</p>
{status === 'failed' && (
<p role="alert" className="mt-2 text-xs text-danger">Couldn’t delete notes. Try again.</p>
)}
<div className="mt-3 flex flex-wrap justify-end gap-2">
<button
type="button"
aria-disabled={deleting}
onClick={dismiss}
className="app-notes-touch-target min-h-9 rounded-lg bg-surface-2 px-3 py-1.5 text-xs font-medium text-text-primary hover:bg-surface-hover aria-disabled:opacity-60"
>
Cancel
</button>
<button
type="button"
aria-disabled={deleting}
onClick={confirm}
className="app-notes-touch-target min-h-9 rounded-lg bg-danger-solid px-3 py-1.5 text-xs font-semibold text-white hover:bg-danger-solid-hover aria-disabled:opacity-60"
>
{deleting ? 'Deleting…' : 'Delete notes'}
</button>
</div>
</dialog>
);
}
2 changes: 1 addition & 1 deletion docs/RELEASE_CHECKLIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Test on Chrome Stable, Firefox Stable, Edge Stable, and current Arc. Test Safari
- [ ] Copying all site notes produces readable Markdown with useful selected/nearby text, not only selectors.
- [ ] Exporting downloads the same complete site-wide Markdown.
- [ ] Hacker News smoke test: annotate two story titles and confirm their titles appear in the export.
- [ ] Clearing site notes removes only that site's notes and requires confirmation.
- [ ] In the popup and notes workspace, the clear confirmation fits inside the extension, focuses **Cancel** first, and dismisses without deleting on Escape. **Delete notes** removes only the named site, file, or folder’s notes.
- [ ] The badge count reflects only the current page.

### Compatibility and quality
Expand Down
47 changes: 25 additions & 22 deletions entrypoints/popup/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
RotateCw,
Trash2,
} from 'lucide-react';
import { ClearNotesDialog } from '@/components/ClearNotesDialog';
import {
collectionContainsStorageKey,
getAnnotationCollectionIdentity,
Expand Down Expand Up @@ -62,7 +63,9 @@ function App() {
const [currentUrl, setCurrentUrl] = useState('');
const [tabId, setTabId] = useState<number | null>(null);
const [isToggling, setIsToggling] = useState(false);
const [isConfirmingClear, setIsConfirmingClear] = useState(false);
const [status, setStatus] = useState('');
const headingRef = useRef<HTMLHeadingElement>(null);
const collectionIdentityRef = useRef<AnnotationCollectionIdentity | null>(null);
const currentUrlRef = useRef('');
const countGenerationRef = useRef(0);
Expand Down Expand Up @@ -235,28 +238,18 @@ function App() {
}
};

const handleClear = async () => {
const localFile = isLocalFileUrl(currentUrl);
if (!confirm(localFile ? 'Remove all notes from this file?' : 'Remove all notes from this site?')) {
return;
}
const scope = resolveAnnotationScope(currentUrl);
const localFile = isLocalFileUrl(currentUrl);

try {
const scope = resolveAnnotationScope(currentUrl);
const result = await clearAnnotationsForScope(currentUrl, scope);
const cleared = scope === 'page' ? result._tag === 'cleared' : result._tag === 'site-cleared';
if (!cleared) {
setStatus('Couldn’t clear notes.');
return;
}
setCount(0);
setStatus(localFile ? 'File notes cleared.' : 'Site notes cleared.');
} catch {
setStatus('Couldn’t clear notes.');
}
};
const handleClear = async (): Promise<boolean> => {
const result = await clearAnnotationsForScope(currentUrl, scope);
const cleared = scope === 'page' ? result._tag === 'cleared' : result._tag === 'site-cleared';
if (!cleared) return false;

const localFile = isLocalFileUrl(currentUrl);
setCount(0);
setStatus(localFile ? 'File notes cleared.' : 'Site notes cleared.');
return true;
};
const copySuccess = localFile ? 'File notes copied.' : 'All site notes copied.';
const defaultFooter = (() => {
switch (availability) {
Expand Down Expand Up @@ -303,7 +296,7 @@ function App() {
<main className="p-2" aria-label="App Notes controls">
<header className="mb-1 flex items-center justify-between px-2 py-1.5">
<div>
<h1 className="text-[13px] font-semibold tracking-[-0.01em] text-text-primary">App Notes</h1>
<h1 ref={headingRef} tabIndex={-1} className="text-[13px] font-semibold tracking-[-0.01em] text-text-primary">App Notes</h1>
<p className="mt-0.5 text-[10px] text-text-tertiary">
{localFile ? 'Notes for this file' : 'Notes for this site'}
</p>
Expand Down Expand Up @@ -351,7 +344,7 @@ function App() {
<MenuItem
icon={<Trash2 size={15} />}
label={localFile ? 'Clear file notes' : 'Clear site notes'}
onClick={handleClear}
onClick={() => setIsConfirmingClear(true)}
danger
disabled={count === 0}
/>
Expand All @@ -362,6 +355,16 @@ function App() {
{status || defaultFooter}
</span>
</footer>

{isConfirmingClear && (
<ClearNotesDialog
url={currentUrl}
scope={scope}
fallbackFocusRef={headingRef}
onConfirm={handleClear}
onDismiss={() => setIsConfirmingClear(false)}
/>
)}
</main>
);
}
Expand Down
46 changes: 27 additions & 19 deletions entrypoints/sidepanel/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Pencil,
Trash2,
} from 'lucide-react';
import { ClearNotesDialog } from '@/components/ClearNotesDialog';
import { LocalFileScopeControl } from '@/components/LocalFileScopeControl';
import {
annotationCollectionsEqual,
Expand Down Expand Up @@ -82,7 +83,9 @@ export function SidePanelApp() {
const [editingId, setEditingId] = useState<string | null>(null);
const [editText, setEditText] = useState('');
const [copied, setCopied] = useState(false);
const [clearTarget, setClearTarget] = useState<PanelSite | null>(null);
const [status, setStatus] = useState('');
const headingRef = useRef<HTMLHeadingElement>(null);
const siteRef = useRef<PanelSite | null>(null);
const viewRef = useRef<PanelView>(launch.view);
const loadGenerationRef = useRef(0);
Expand Down Expand Up @@ -155,6 +158,7 @@ export function SidePanelApp() {
const loadingSite: PanelSite = { tabId, href, identity, scope, annotations: [] };
commitSite(loadingSite);
cancelEditing();
setClearTarget(null);
setStatus('');

if (identity === null) return;
Expand Down Expand Up @@ -350,30 +354,28 @@ export function SidePanelApp() {
}
};

const handleClearAll = async () => {
const currentSite = siteRef.current;
if (!currentSite || !confirm(getCollectionClearPrompt(currentSite.href, currentSite.scope))) {
return;
}
const handleClearAll = async (): Promise<boolean> => {
if (
!clearTarget?.identity
|| !annotationCollectionsEqual(siteRef.current?.identity ?? null, clearTarget.identity)
) return false;

const result = await clearAnnotationsForScope(currentSite.href, currentSite.scope);
const cleared = currentSite.scope === 'page'
const result = await clearAnnotationsForScope(clearTarget.href, clearTarget.scope);
const cleared = clearTarget.scope === 'page'
? result._tag === 'cleared'
: result._tag === 'site-cleared';
if (!cleared) {
setStatus('Couldn’t clear notes.');
return;
}
if (!cleared) return false;

const latestSite = siteRef.current;
if (
latestSite !== null
&& annotationCollectionsEqual(latestSite.identity, currentSite.identity)
&& annotationCollectionsEqual(latestSite.identity, clearTarget.identity)
) {
commitSite({ ...latestSite, annotations: [] });
cancelEditing();
setStatus(getCollectionClearedMessage(currentSite.href, currentSite.scope));
setStatus(getCollectionClearedMessage(clearTarget.href, clearTarget.scope));
}
return true;
};

const handleOpenAnnotation = async (annotation: Annotation) => {
Expand Down Expand Up @@ -449,7 +451,7 @@ export function SidePanelApp() {
<header className="border-b border-border-subtle px-4 py-3.5">
<div className="flex items-center justify-between gap-3">
<div className="min-w-0 flex-1">
<h1 className="text-sm font-semibold tracking-[-0.012em] text-text-primary">App Notes</h1>
<h1 ref={headingRef} tabIndex={-1} className="text-sm font-semibold tracking-[-0.012em] text-text-primary">App Notes</h1>
<p className="mt-0.5 truncate text-xs text-text-secondary">{collectionDescription}</p>
</div>
<button
Expand Down Expand Up @@ -551,7 +553,7 @@ export function SidePanelApp() {
</div>
<button
type="button"
onClick={handleClearAll}
onClick={() => setClearTarget(siteRef.current)}
className="app-notes-pressable app-notes-touch-target mt-2 flex min-h-9 w-full items-center justify-center gap-1.5 rounded-lg text-xs font-medium text-danger transition-[background-color,transform] hover:bg-danger-soft"
>
<Trash2 aria-hidden="true" size={13} />
Expand All @@ -573,6 +575,16 @@ export function SidePanelApp() {
>
{status}
</p>

{clearTarget && (
<ClearNotesDialog
url={clearTarget.href}
scope={clearTarget.scope}
fallbackFocusRef={headingRef}
onConfirm={handleClearAll}
onDismiss={() => setClearTarget(null)}
/>
)}
</main>
);
}
Expand Down Expand Up @@ -913,10 +925,6 @@ function getCollectionClearedMessage(url: string, scope: AnnotationScope): strin
return `${noun[0]?.toUpperCase() ?? ''}${noun.slice(1)} notes cleared.`;
}

function getCollectionClearPrompt(url: string, scope: AnnotationScope): string {
return `Remove all notes from this ${getCollectionNoun(url, scope)}?`;
}

function getExportFilename(url: string, scope: AnnotationScope): string {
const label = isLocalFileUrl(url) && scope === 'page'
? getPageDisplayLabel(url)
Expand Down