Files
faunosteo/apps/app/src/components/editor/tabs/ReferencesTab.tsx
T
bchanot d1779928a9 feat(editor): cancel adding a reference by button or Escape
Draft tile gets « Annuler l'ajout »; Escape cancels from non-text targets and
from a pristine text field, never while composing or already handled. Nothing
reaches the document, history untouched, focus back on the add button.
2026-11-13 12:39:57 +01:00

134 lines
4.1 KiB
TypeScript

import { useRef, useState } from 'react'
import DeleteDialog, {
type DeleteTarget,
} from '@/components/editor/DeleteDialog'
import EditOnly from '@/components/editor/EditOnly'
import NewReferenceTile from '@/components/editor/references/NewReferenceTile'
import NoResult from '@/components/editor/references/NoResult'
import ReferenceTiles from '@/components/editor/references/ReferenceTiles'
import ReferenceToolbar from '@/components/editor/references/ReferenceToolbar'
import {
useReferenceActions,
useReferenceLibrary,
} from '@/components/editor/references/useReferenceLibrary'
import { isSheetView } from '@/editor/references/view'
import type { EditorPlaceProps } from '@/editor/tabs'
import { useT } from '@/i18n/useT'
/** The empty line: the sheet has no reference at all. */
function EmptyLine() {
const t = useT()
return (
<p className="text-sm text-[var(--color-ink-muted)]">
{t('editor.reference.empty')}
</p>
)
}
type Props = Pick<EditorPlaceProps, 'readOnly' | 'navigate' | 'focus'> & {
resetSignal: number
}
/**
* The sheet's bibliography: toolbar (search, sort, count, add), one collapsible tile per
* reference, the draft tile and the delete dialog. Ids carry the `tab-` prefix. The
* citations panel shares only the tile (R2.4).
*/
function Library({ readOnly, navigate, focus, resetSignal }: Props) {
const t = useT()
const lib = useReferenceLibrary(resetSignal)
const searchRef = useRef<HTMLInputElement>(null)
const act = useReferenceActions(lib, focus, searchRef)
const [deleting, setDeleting] = useState<DeleteTarget | null>(null)
const { all, shown } = lib
const sheetView = isSheetView(shown.view)
return (
<div className="space-y-3">
<ReferenceToolbar
query={shown.view.query}
onQuery={(query) => shown.update({ query })}
onClear={act.clearSearch}
searchRef={searchRef}
sort={shown.view.sort}
onSort={(sort) => shown.update({ sort })}
shown={shown.matchCount}
total={all.length}
canAdd={!readOnly}
onAdd={act.startAdd}
/>
{lib.loaded && (
<>
{all.length === 0 && <EmptyLine />}
{all.length > 0 && shown.matchCount === 0 && (
<NoResult query={shown.view.query} onClear={act.clearSearch} />
)}
{!readOnly && !sheetView && shown.positions.length > 0 && (
<p className="text-sm text-[var(--color-ink-muted)]">
{t('editor.references.reorder_hint')}
</p>
)}
<ReferenceTiles
positions={shown.positions}
all={all}
keys={lib.keys}
citers={lib.citers}
tiles={lib.tiles}
readOnly={readOnly}
movable={sheetView}
navigate={navigate}
onEdit={act.edit}
onDelete={(reference) =>
setDeleting({
key: { kind: 'reference', id: reference.id },
label: reference.citation,
})
}
/>
<EditOnly readOnly={readOnly}>
{act.draft !== null && (
<NewReferenceTile
focusSerial={act.draft}
onAdded={act.added}
onCancel={act.cancelAdd}
/>
)}
</EditOnly>
</>
)}
<DeleteDialog target={deleting} onClose={() => setDeleting(null)} />
</div>
)
}
/** The fifth tab: the sheet's reference library under its own heading. */
export default function ReferencesTab({
readOnly,
navigate,
focus,
libraryReset = 0,
}: EditorPlaceProps) {
const t = useT()
return (
<section
data-testid="editor-tab-references"
aria-labelledby="editor-references-title"
className="min-h-24 space-y-3"
>
<h2
id="editor-references-title"
tabIndex={-1}
data-place-heading
className="font-serif text-xl outline-none"
>
{t('editor.tab.references')}
</h2>
<Library
readOnly={readOnly}
navigate={navigate}
focus={focus}
resetSignal={libraryReset}
/>
</section>
)
}