Edit-only parts (creation forms, submission note, crop group, annotation edit body) mount at the first editable render and stay mounted, hidden and detached from canvases, while read-only. Guards: the viewport hook commits the focused field before the tier crosses 1 024 px, the route releases focus left in a hidden part and treats a store out of step as read-only, delete and discard confirmations close when the editor turns read-only, a drag is dropped while the edit body is hidden. Read-only twins take an id prefix. R-24: no existing assertion rewritten; editor-no-loss.test.tsx and two editor-shell Chromium cases added.
160 lines
5.1 KiB
TypeScript
160 lines
5.1 KiB
TypeScript
import { useCallback, useMemo, useState } from 'react'
|
|
import DeleteDialog, {
|
|
type DeleteTarget,
|
|
} from '@/components/editor/DeleteDialog'
|
|
import EditOnly from '@/components/editor/EditOnly'
|
|
import AddBar from '@/components/editor/findings/AddBar'
|
|
import FindingCreateForm, {
|
|
type PendingFinding,
|
|
} from '@/components/editor/findings/FindingCreateForm'
|
|
import FindingDetail from '@/components/editor/findings/FindingDetail'
|
|
import FindingTree from '@/components/editor/findings/FindingTree'
|
|
import {
|
|
buildItems,
|
|
createdItemId,
|
|
fallbackAfterRemoval,
|
|
isUnder,
|
|
itemForKey,
|
|
visibleItems,
|
|
type FindingItem,
|
|
} from '@/editor/findings/tree'
|
|
import { useFocusRequest } from '@/editor/focusRequest'
|
|
import { useEditorStore } from '@/editor/state/editorStore'
|
|
import { useSheetDocument } from '@/editor/sheetDocument'
|
|
import type { EditorPlaceProps } from '@/editor/tabs'
|
|
import { useT } from '@/i18n/useT'
|
|
import { useLocaleStore } from '@/state/localeStore'
|
|
|
|
/** The tree as the store holds it right now (a commit is already in). */
|
|
function currentItems(): FindingItem[] {
|
|
try {
|
|
const doc = JSON.parse(useEditorStore.getState().documentText)
|
|
return buildItems(doc)
|
|
} catch {
|
|
return []
|
|
}
|
|
}
|
|
|
|
/** The deletion flow: the target, the item to select after it, and the focus left behind. */
|
|
function useFindingDeletion(
|
|
items: readonly FindingItem[],
|
|
selectedId: string | null,
|
|
select: (id: string | null) => void,
|
|
focusItem: (id: string | null) => void,
|
|
) {
|
|
const locale = useLocaleStore((s) => s.locale)
|
|
const [deleting, setDeleting] = useState<DeleteTarget | null>(null)
|
|
const [fallback, setFallback] = useState<string | null>(null)
|
|
const askDelete = (item: FindingItem) => {
|
|
setFallback(fallbackAfterRemoval(items, item))
|
|
setDeleting({ key: item.key, label: item.name[locale] })
|
|
}
|
|
const closeDelete = () => {
|
|
setDeleting(null)
|
|
const gone = !currentItems().some((item) => item.id === selectedId)
|
|
if (gone) {
|
|
select(fallback)
|
|
focusItem(fallback)
|
|
}
|
|
}
|
|
return { deleting, askDelete, closeDelete }
|
|
}
|
|
|
|
/** Constats: the tree zones → categories → nodes, edited one command at a time. */
|
|
export default function FindingsTab({ readOnly, focus }: EditorPlaceProps) {
|
|
const t = useT()
|
|
const doc = useSheetDocument()
|
|
const [selectedId, setSelectedId] = useState<string | null>(null)
|
|
const [expanded, setExpanded] = useState<ReadonlySet<string>>(new Set())
|
|
const [seeded, setSeeded] = useState(false)
|
|
const [pending, setPending] = useState<PendingFinding | null>(null)
|
|
const [focusId, setFocusId] = useState<{ id: string } | null>(null)
|
|
const clearFocusId = useCallback(() => setFocusId(null), [])
|
|
const items = useMemo(() => (doc ? buildItems(doc) : []), [doc])
|
|
if (doc && !seeded) {
|
|
setSeeded(true)
|
|
setExpanded(
|
|
new Set(items.filter((i) => i.kind === 'zone').map((i) => i.id)),
|
|
)
|
|
}
|
|
const selected = items.find((item) => item.id === selectedId)
|
|
const toggle = (id: string, open: boolean) => {
|
|
if (!open && selectedId && isUnder(items, selectedId, id)) {
|
|
setSelectedId(id)
|
|
}
|
|
setExpanded((prev) => {
|
|
const next = new Set(prev)
|
|
if (open) next.add(id)
|
|
else next.delete(id)
|
|
return next
|
|
})
|
|
}
|
|
useFocusRequest(focus, (target) => {
|
|
const found = target.key && itemForKey(items, target.key)
|
|
if (!found) return 'none'
|
|
setExpanded((prev) => new Set([...prev, ...found.ancestors]))
|
|
setSelectedId(found.item.id)
|
|
setFocusId({ id: found.item.id })
|
|
return 'found'
|
|
})
|
|
const created = (id: string, parent: FindingItem | null) => {
|
|
if (!pending) return
|
|
if (parent) toggle(parent.id, true)
|
|
setSelectedId(createdItemId(pending.kind, parent, id))
|
|
setPending(null)
|
|
}
|
|
const { deleting, askDelete, closeDelete } = useFindingDeletion(
|
|
items,
|
|
selectedId,
|
|
setSelectedId,
|
|
(id) => setFocusId(id ? { id } : null),
|
|
)
|
|
return (
|
|
<section
|
|
data-testid="editor-tab-findings"
|
|
aria-label={t('editor.tab.findings')}
|
|
className="min-h-24 space-y-3"
|
|
>
|
|
<AddBar
|
|
selected={selected}
|
|
hasZone={items.some((item) => item.kind === 'zone')}
|
|
readOnly={readOnly}
|
|
onStart={setPending}
|
|
/>
|
|
{pending && (
|
|
<EditOnly readOnly={readOnly}>
|
|
<FindingCreateForm
|
|
key={`${pending.kind}-${pending.parent?.id ?? ''}`}
|
|
pending={pending}
|
|
onCreated={created}
|
|
onCancel={() => setPending(null)}
|
|
/>
|
|
</EditOnly>
|
|
)}
|
|
{doc && items.length === 0 && (
|
|
<p className="text-sm text-[var(--color-ink-muted)]">
|
|
{t('editor.findings.empty')}
|
|
</p>
|
|
)}
|
|
<FindingTree
|
|
visible={visibleItems(items, expanded)}
|
|
selectedId={selected?.id ?? null}
|
|
expanded={expanded}
|
|
onSelect={setSelectedId}
|
|
onToggle={toggle}
|
|
focusId={focusId}
|
|
onFocused={clearFocusId}
|
|
/>
|
|
{selected && (
|
|
<FindingDetail
|
|
key={selected.id}
|
|
item={selected}
|
|
readOnly={readOnly}
|
|
onDelete={() => askDelete(selected)}
|
|
/>
|
|
)}
|
|
<DeleteDialog target={deleting} onClose={closeDelete} />
|
|
</section>
|
|
)
|
|
}
|