204 lines
6.0 KiB
TypeScript
204 lines
6.0 KiB
TypeScript
import { useRef, useState } from 'react'
|
|
import DeleteDialog, {
|
|
type DeleteTarget,
|
|
} from '@/components/editor/DeleteDialog'
|
|
import Button from '@/components/ui/Button'
|
|
import Notice from '@/components/ui/Notice'
|
|
import NoResult from '@/components/editor/references/NoResult'
|
|
import ReferenceTiles from '@/components/editor/references/ReferenceTiles'
|
|
import ReferenceToolbar from '@/components/editor/references/ReferenceToolbar'
|
|
import {
|
|
useReferenceActions,
|
|
useReferenceLibrary,
|
|
type ReferenceLibraryState,
|
|
} from '@/components/editor/references/useReferenceLibrary'
|
|
import { isSheetView } from '@/editor/references/view'
|
|
import type { EditorPlaceProps, ReferenceLayer } from '@/editor/tabs'
|
|
import { useT } from '@/i18n/useT'
|
|
import { useLocaleStore } from '@/state/localeStore'
|
|
|
|
/** 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
|
|
layer: ReferenceLayer
|
|
}
|
|
|
|
/** « Référence ajoutée : … » with « Afficher »: the list did not move. */
|
|
function AddedNotice({
|
|
citation,
|
|
onShow,
|
|
}: {
|
|
citation: string
|
|
onShow: () => void
|
|
}) {
|
|
const t = useT()
|
|
return (
|
|
<Notice tone="success" className="items-center">
|
|
<span className="flex flex-wrap items-center gap-x-3 gap-y-1">
|
|
{t('editor.references.added').replace('{citation}', () => citation)}
|
|
<Button type="button" variant="secondary" size="sm" onClick={onShow}>
|
|
{t('editor.references.added_show')}
|
|
</Button>
|
|
</span>
|
|
</Notice>
|
|
)
|
|
}
|
|
|
|
/** The chip of the finding the list is filtered by, null for the whole bibliography. */
|
|
function useFilterChip(lib: ReferenceLibraryState, layer: ReferenceLayer) {
|
|
const locale = useLocaleStore((s) => s.locale)
|
|
const { finding } = lib
|
|
if (!finding || !layer.filter) return null
|
|
const key = layer.filter
|
|
return {
|
|
name: finding.name[locale],
|
|
onRemove: () => layer.clearFilter(),
|
|
onBack: () => layer.backToFinding(key),
|
|
}
|
|
}
|
|
|
|
type Actions = ReturnType<typeof useReferenceActions>
|
|
|
|
interface BodyProps {
|
|
lib: ReferenceLibraryState
|
|
act: Actions
|
|
props: Props
|
|
}
|
|
|
|
/** The notice, the empty and no-result lines and the reorder hint. */
|
|
function LibraryNotices(body: BodyProps) {
|
|
const t = useT()
|
|
const { shown } = body.lib
|
|
const reorderable =
|
|
!body.props.readOnly && !isSheetView(shown.view, shown.filtered)
|
|
return (
|
|
<>
|
|
{body.act.notice && (
|
|
<AddedNotice
|
|
citation={body.act.notice.citation}
|
|
onShow={body.act.notice.show}
|
|
/>
|
|
)}
|
|
{body.lib.all.length === 0 && <EmptyLine />}
|
|
{body.lib.all.length > 0 && shown.positions.length === 0 && (
|
|
<NoResult query={shown.view.query} onClear={body.act.clearSearch} />
|
|
)}
|
|
{reorderable && shown.positions.length > 0 && (
|
|
<p className="text-sm text-[var(--color-ink-muted)]">
|
|
{t('editor.references.reorder_hint')}
|
|
</p>
|
|
)}
|
|
</>
|
|
)
|
|
}
|
|
|
|
/** The tiles and the delete dialog they open. */
|
|
function LibraryTiles(body: BodyProps) {
|
|
const [deleting, setDeleting] = useState<DeleteTarget | null>(null)
|
|
const { lib } = body
|
|
return (
|
|
<>
|
|
<ReferenceTiles
|
|
positions={lib.shown.positions}
|
|
all={lib.all}
|
|
keys={lib.keys}
|
|
citers={lib.citers}
|
|
tiles={lib.tiles}
|
|
query={lib.shown.view.query}
|
|
readOnly={body.props.readOnly}
|
|
movable={isSheetView(lib.shown.view, lib.shown.filtered)}
|
|
navigate={body.props.navigate}
|
|
onEdit={body.act.edit}
|
|
onDelete={(reference) =>
|
|
setDeleting({
|
|
key: { kind: 'reference', id: reference.id },
|
|
label: reference.citation,
|
|
})
|
|
}
|
|
/>
|
|
<DeleteDialog target={deleting} onClose={() => setDeleting(null)} />
|
|
</>
|
|
)
|
|
}
|
|
|
|
/**
|
|
* The sheet's bibliography: toolbar (search, sort, filter, count, add), one collapsible
|
|
* tile per reference and the delete dialog. « Modifier » and « Ajouter » open the
|
|
* reference dialog (a layer of the workspace). Ids carry the `tab-` prefix. The citations
|
|
* panel shares only the tile (R2.4).
|
|
*/
|
|
function Library(props: Props) {
|
|
const lib = useReferenceLibrary(props.resetSignal, props.layer)
|
|
const searchRef = useRef<HTMLInputElement>(null)
|
|
const act = useReferenceActions(lib, props.focus, props.layer, searchRef)
|
|
const chip = useFilterChip(lib, props.layer)
|
|
return (
|
|
<div className="space-y-3">
|
|
<ReferenceToolbar
|
|
query={lib.shown.view.query}
|
|
onQuery={(query) => lib.shown.update({ query })}
|
|
onClear={act.clearSearch}
|
|
searchRef={searchRef}
|
|
sort={lib.shown.view.sort}
|
|
onSort={(sort) => lib.shown.update({ sort })}
|
|
shown={lib.shown.matchCount}
|
|
total={lib.shown.total}
|
|
canAdd={!props.readOnly}
|
|
onAdd={act.startAdd}
|
|
filter={chip}
|
|
/>
|
|
{lib.loaded && (
|
|
<>
|
|
<LibraryNotices lib={lib} act={act} props={props} />
|
|
<LibraryTiles lib={lib} act={act} props={props} />
|
|
</>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
/** The fifth tab: the sheet's reference library under its own heading. */
|
|
export default function ReferencesTab({
|
|
readOnly,
|
|
navigate,
|
|
focus,
|
|
libraryReset = 0,
|
|
references,
|
|
}: EditorPlaceProps) {
|
|
const t = useT()
|
|
if (!references)
|
|
throw new Error('Références needs the reference layer of the workspace')
|
|
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}
|
|
layer={references}
|
|
/>
|
|
</section>
|
|
)
|
|
}
|