diff --git a/assets/app-notes.css b/assets/app-notes.css index 8a403ca..03d623b 100644 --- a/assets/app-notes.css +++ b/assets/app-notes.css @@ -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); diff --git a/components/ClearNotesDialog.tsx b/components/ClearNotesDialog.tsx new file mode 100644 index 0000000..ea33e82 --- /dev/null +++ b/components/ClearNotesDialog.tsx @@ -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; + readonly onConfirm: () => Promise; + 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(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 ( + { + 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" + > +

+ Delete all {collection} notes? +

+

+ Notes for {label ? {label} : `this ${collection}`} will be permanently deleted. This can’t be undone. +

+ {status === 'failed' && ( +

Couldn’t delete notes. Try again.

+ )} +
+ + +
+
+ ); +} diff --git a/docs/RELEASE_CHECKLIST.md b/docs/RELEASE_CHECKLIST.md index 3861126..8ff6332 100644 --- a/docs/RELEASE_CHECKLIST.md +++ b/docs/RELEASE_CHECKLIST.md @@ -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 diff --git a/entrypoints/popup/App.tsx b/entrypoints/popup/App.tsx index 880ad6a..82114fc 100644 --- a/entrypoints/popup/App.tsx +++ b/entrypoints/popup/App.tsx @@ -8,6 +8,7 @@ import { RotateCw, Trash2, } from 'lucide-react'; +import { ClearNotesDialog } from '@/components/ClearNotesDialog'; import { collectionContainsStorageKey, getAnnotationCollectionIdentity, @@ -62,7 +63,9 @@ function App() { const [currentUrl, setCurrentUrl] = useState(''); const [tabId, setTabId] = useState(null); const [isToggling, setIsToggling] = useState(false); + const [isConfirmingClear, setIsConfirmingClear] = useState(false); const [status, setStatus] = useState(''); + const headingRef = useRef(null); const collectionIdentityRef = useRef(null); const currentUrlRef = useRef(''); const countGenerationRef = useRef(0); @@ -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 => { + 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) { @@ -303,7 +296,7 @@ function App() {
-

App Notes

+

App Notes

{localFile ? 'Notes for this file' : 'Notes for this site'}

@@ -351,7 +344,7 @@ function App() { } label={localFile ? 'Clear file notes' : 'Clear site notes'} - onClick={handleClear} + onClick={() => setIsConfirmingClear(true)} danger disabled={count === 0} /> @@ -362,6 +355,16 @@ function App() { {status || defaultFooter} + + {isConfirmingClear && ( + setIsConfirmingClear(false)} + /> + )}
); } diff --git a/entrypoints/sidepanel/App.tsx b/entrypoints/sidepanel/App.tsx index 0cf8542..66c571f 100644 --- a/entrypoints/sidepanel/App.tsx +++ b/entrypoints/sidepanel/App.tsx @@ -12,6 +12,7 @@ import { Pencil, Trash2, } from 'lucide-react'; +import { ClearNotesDialog } from '@/components/ClearNotesDialog'; import { LocalFileScopeControl } from '@/components/LocalFileScopeControl'; import { annotationCollectionsEqual, @@ -82,7 +83,9 @@ export function SidePanelApp() { const [editingId, setEditingId] = useState(null); const [editText, setEditText] = useState(''); const [copied, setCopied] = useState(false); + const [clearTarget, setClearTarget] = useState(null); const [status, setStatus] = useState(''); + const headingRef = useRef(null); const siteRef = useRef(null); const viewRef = useRef(launch.view); const loadGenerationRef = useRef(0); @@ -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; @@ -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 => { + 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) => { @@ -449,7 +451,7 @@ export function SidePanelApp() {
-

App Notes

+

App Notes

{collectionDescription}