209 lines
6.4 KiB
TypeScript
209 lines
6.4 KiB
TypeScript
import type { RefObject } from 'react'
|
||
import type { LocalizedText, Reference } from '@faunosteo/core-wasm/types'
|
||
import Icon from '@/components/ui/Icon'
|
||
import { targetOfKey, type EditorTarget } from '@/editor/navigation'
|
||
import { usePlural } from '@/editor/plural'
|
||
import type { CitationEntry } from '@/editor/references/panel'
|
||
import { useT } from '@/i18n/useT'
|
||
import { useLocaleStore } from '@/state/localeStore'
|
||
import ReferenceTile from './ReferenceTile'
|
||
import { useCitationsPanel, type PanelHooks } from './useCitationsPanel'
|
||
|
||
interface Props extends PanelHooks {
|
||
/** ✕ (Escape and the scrim close the sheet itself): a close the tile in edit may refuse. */
|
||
onClose: () => void
|
||
/** « Tout afficher dans l'onglet Références ». */
|
||
onShowAll: () => void
|
||
/** « Aller à » a citing finding or family: closes the panel, then moves. */
|
||
navigate: (target: EditorTarget) => void
|
||
/** « Modifier »: the reference dialog over the sheet, `opener` is where the focus returns. */
|
||
onEdit: (id: string, opener: HTMLElement) => void
|
||
}
|
||
|
||
const RING =
|
||
'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-accent)]'
|
||
|
||
interface HeaderProps {
|
||
onClose: () => void
|
||
headingRef: RefObject<HTMLHeadingElement | null>
|
||
}
|
||
|
||
function PanelHeader({ onClose, headingRef }: HeaderProps) {
|
||
const t = useT()
|
||
return (
|
||
<div className="flex items-center justify-between gap-2">
|
||
<h2
|
||
ref={headingRef}
|
||
id="editor-citations-title"
|
||
tabIndex={-1}
|
||
data-place-heading
|
||
className="flex items-center gap-2 font-serif text-xl outline-none"
|
||
>
|
||
<Icon name="book" size={20} />
|
||
{t('editor.references.panel_title')}
|
||
</h2>
|
||
<button
|
||
type="button"
|
||
aria-label={t('editor.references.close')}
|
||
onClick={onClose}
|
||
className={`inline-flex h-11 w-11 items-center justify-center rounded-md ${RING}`}
|
||
>
|
||
<Icon name="close" size={18} />
|
||
</button>
|
||
</div>
|
||
)
|
||
}
|
||
|
||
/** « Zone › Famille › **Constat** »: where the finding sits, the finding itself in bold. */
|
||
function FindingPath({ path }: { path: readonly LocalizedText[] }) {
|
||
const locale = useLocaleStore((s) => s.locale)
|
||
const names = path.map((name) => name[locale])
|
||
const own = names.pop()
|
||
return (
|
||
<p className="text-sm">
|
||
<span className="text-[var(--color-ink-muted)]">
|
||
{names.map((name) => `${name} › `).join('')}
|
||
</span>
|
||
<b className="font-semibold">{own}</b>
|
||
</p>
|
||
)
|
||
}
|
||
|
||
function ShowAllLink({ onShowAll }: { onShowAll: () => void }) {
|
||
const t = useT()
|
||
return (
|
||
<button
|
||
type="button"
|
||
onClick={onShowAll}
|
||
className={`inline-flex min-h-11 items-center gap-1 text-sm text-[var(--color-accent-strong)] underline ${RING}`}
|
||
>
|
||
{t('editor.references.panel_show_all')}
|
||
<Icon name="chevron-right" size={14} />
|
||
</button>
|
||
)
|
||
}
|
||
|
||
/** « {n} références citées par ce constat » and the way to the whole library. */
|
||
function Summary({
|
||
count,
|
||
onShowAll,
|
||
}: {
|
||
count: number
|
||
onShowAll: () => void
|
||
}) {
|
||
const plural = usePlural()
|
||
return (
|
||
<div className="flex flex-wrap items-center justify-between gap-x-3">
|
||
<p className="text-sm text-[var(--color-ink-muted)]">
|
||
{count > 0 && plural('editor.references.panel_count', count)}
|
||
</p>
|
||
<ShowAllLink onShowAll={onShowAll} />
|
||
</div>
|
||
)
|
||
}
|
||
|
||
/** A cited id the sheet no longer holds (P30): named; only the edit form removes it. */
|
||
function MissingRow({ entry }: { entry: CitationEntry }) {
|
||
const t = useT()
|
||
return (
|
||
<li className="rounded-lg border border-[var(--color-border)] p-3 text-sm">
|
||
{t('editor.references.missing')}
|
||
<span className="ml-2 text-[var(--color-ink-muted)]">{entry.id}</span>
|
||
</li>
|
||
)
|
||
}
|
||
|
||
type Panel = ReturnType<typeof useCitationsPanel>
|
||
|
||
/** One cited reference as a tile (« Modifier »). */
|
||
function EntryTile({
|
||
entry,
|
||
reference,
|
||
panel,
|
||
props,
|
||
}: {
|
||
entry: CitationEntry
|
||
reference: Reference
|
||
panel: Panel
|
||
props: Props
|
||
}) {
|
||
const { tiles, keys, citers, entries } = panel
|
||
const { readOnly, citedBy, navigate } = props
|
||
const key = keys[entry.index]
|
||
return (
|
||
<ReferenceTile
|
||
host="panel"
|
||
panel={{ current: citedBy }}
|
||
reference={reference}
|
||
position={entry.index}
|
||
count={entries.length}
|
||
citers={citers.get(reference.id) ?? []}
|
||
open={tiles.openKeys.has(key)}
|
||
readOnly={readOnly}
|
||
movable={false}
|
||
on={{
|
||
toggle: () => tiles.toggle(key),
|
||
edit: (opener) => props.onEdit(reference.id, opener),
|
||
goTo: (target) => navigate(targetOfKey(target)),
|
||
}}
|
||
/>
|
||
)
|
||
}
|
||
|
||
/** The cited references as tiles, the dangling ids as rows. */
|
||
function Entries({ panel, props }: { panel: Panel; props: Props }) {
|
||
return (
|
||
<ul
|
||
ref={panel.list}
|
||
onFocusCapture={panel.trackFocus}
|
||
className="space-y-3"
|
||
>
|
||
{panel.entries.map((entry) =>
|
||
entry.reference ? (
|
||
<EntryTile
|
||
key={panel.keys[entry.index]}
|
||
entry={entry}
|
||
reference={entry.reference}
|
||
panel={panel}
|
||
props={props}
|
||
/>
|
||
) : (
|
||
<MissingRow key={`${entry.index}-${entry.id}`} entry={entry} />
|
||
),
|
||
)}
|
||
</ul>
|
||
)
|
||
}
|
||
|
||
/**
|
||
* The citations of one finding: the references it cites as tiles, « Tout afficher » for
|
||
* the whole library. No search, add, remove or order (P7): citing is the edit form's. The content of the side sheet
|
||
* (CitationsSheet), which is the modal; opens from a finding's trigger only.
|
||
*/
|
||
export default function CitationsPanel(props: Props) {
|
||
const { onClose, onShowAll } = props
|
||
const t = useT()
|
||
const panel = useCitationsPanel(props)
|
||
const count = panel.entries.length
|
||
return (
|
||
<section
|
||
id="editor-citations-panel"
|
||
data-testid="editor-citations-panel"
|
||
aria-labelledby="editor-citations-title"
|
||
className="flex h-full flex-col gap-3 border-l border-[var(--color-border)] bg-[var(--color-surface)] p-4 shadow-xl"
|
||
>
|
||
<PanelHeader onClose={onClose} headingRef={panel.heading} />
|
||
<FindingPath path={panel.path} />
|
||
<Summary count={count} onShowAll={onShowAll} />
|
||
<div className="min-h-0 flex-1 space-y-3 overflow-y-auto pr-2">
|
||
{count === 0 && (
|
||
<p className="text-sm text-[var(--color-ink-muted)]">
|
||
{t('editor.references.panel_empty')}
|
||
</p>
|
||
)}
|
||
<Entries panel={panel} props={props} />
|
||
</div>
|
||
</section>
|
||
)
|
||
}
|