import type { ElementKey, Reference } from '@faunosteo/core-wasm/types'
import ActionButton from '@/components/editor/fields/ActionButton'
import Icon from '@/components/ui/Icon'
import { usePlural } from '@/editor/plural'
import type { CiterItem } from '@/editor/references/citations'
import { useT } from '@/i18n/useT'
import CitersList from './CitersList'
import ReferenceFacts from './ReferenceFacts'
import type { ReferenceHost } from './referenceTypes'
/** What a tile asks of the library that owns its state. */
export interface TileHandlers {
toggle: () => void
/** « Modifier »: opens the reference dialog; `opener` is where the focus returns. */
edit: (opener: HTMLElement) => void
/** « Supprimer »: the Références tab only. */
remove?: () => void
/** « Monter » / « Descendre »: the Références tab only. */
move?: (position: number) => void
goTo: (key: ElementKey) => void
}
/** What a tile of the citations panel adds: the finding it belongs to. */
export interface PanelTile {
current: ElementKey
}
export interface TileProps {
host: ReferenceHost
/** Set for the citations panel: « ce constat » marks. */
panel?: PanelTile
reference: Reference
/** Position in the whole bibliography, not in a filtered list. */
position: number
count: number
citers: readonly CiterItem[]
open: boolean
readOnly: boolean
/** ↑ / ↓ only while neither the search nor a sort narrows or reorders the list. */
movable: boolean
/** The tab's pinned tile the current search misses: it says so under its header. */
outsideSearch?: boolean
on: TileHandlers
}
/** DOM id prefix of a tile: host, position and id keep duplicates distinct (P31). */
export const tileBase = (
host: ReferenceHost,
position: number,
reference: Reference,
) => `${host}-reference-${position}-${reference.id}`
/** The meta column (type, count) from 640 px: the panel gives it a fixed 132 px. */
const META_COLUMN = {
tab: 'sm:grid-cols-[1.25rem_1fr_auto]',
panel: 'sm:grid-cols-[1.25rem_1fr_8.25rem]',
} as const
const BUTTON_RING =
'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-accent)]'
/** The disclosure button: citation (two lines when closed), type and how often it is cited. */
function TileHeader({ base, props }: { base: string; props: TileProps }) {
const t = useT()
const plural = usePlural()
const { reference, open, citers } = props
const cited = citers.length
const key = JSON.stringify({ kind: 'reference', id: reference.id })
return (
)
}
/** The panel's one action (P7): « Modifier ». Removing a citation is the edit form's. */
function PanelActions({ props }: { props: TileProps }) {
const t = useT()
return (
{t('editor.references.delete_hint')}
)} > ) } /** * One reference as a collapsible tile (boards 23, 24): closed, or open (facts, citers, * actions). « Modifier » opens the reference dialog; no field is edited in the tile * (R2F-5). Read-only: the facts and the citers' « Aller à » only, no control that changes * the document. */ export default function ReferenceTile(props: TileProps) { const { host, reference, position, open, on } = props const t = useT() const base = tileBase(host, position, reference) return ({t('editor.references.outside_search')}
)} {open && (