fix(editor): stale cite choice reset, pending line under the add row

This commit is contained in:
bchanot committed 2026-11-15 20:00:28 +01:00
1 parent 70eb2fcbb4
commit 1d119c3684
6 files changed
+91 -29

No files matched your search

@@ -2,7 +2,7 @@ import { useEffect, useRef } from 'react'
import type { Reference } from '@faunosteo/core-wasm/types'
import Badge from '@/components/ui/Badge'
import Button from '@/components/ui/Button'
import FormField, { fieldDescribedBy } from '@/components/ui/FormField'
import FormField from '@/components/ui/FormField'
import Select from '@/components/ui/Select'
import type { SelectOption } from '@/components/editor/fields/SelectField'
import { useSheetDocument } from '@/editor/sheetDocument'
@@ -37,6 +37,9 @@ export interface DraftListProps {
/** The add choice: its id, so the focus can land on it once the last row is removed. */
const ADD_ID = 'citations-add-reference'
/** The line of a choice not cited at « Enregistrer » (Rev 6), described by the select. */
const PENDING_ID = 'citations-pending-choice'
/** « Citer »: its id, where a refused save puts the focus. */
export const CITE_ID = 'citations-cite-reference'
@@ -131,9 +134,6 @@ function DraftRow({ id, props, onRemove }: RowProps) {
function CiteField({ props }: { props: DraftListProps }) {
const t = useT()
const { choice } = props
const error = choice.pending
? t('editor.citations.pending_choice')
: undefined
const cite = () => {
props.onAdd(choice.chosen)
choice.onChoose('')
@@ -141,18 +141,14 @@ function CiteField({ props }: { props: DraftListProps }) {
}
return (
<div className="min-w-0 flex-1">
<FormField
id={ADD_ID}
label={t('editor.findings.reference_add')}
error={error}
>
<FormField id={ADD_ID} label={t('editor.findings.reference_add')}>
<div className="flex items-center gap-2">
<Select
id={ADD_ID}
value={choice.chosen}
onChange={(event) => choice.onChoose(event.target.value)}
aria-invalid={choice.pending}
aria-describedby={fieldDescribedBy(ADD_ID, undefined, error)}
aria-describedby={choice.pending ? PENDING_ID : undefined}
className="min-w-0 flex-1"
>
{choice.options.map((option) => (
@@ -176,20 +172,35 @@ function CiteField({ props }: { props: DraftListProps }) {
)
}
/** The way to cite an existing reference (edit tier only), and « Nouvelle référence ». */
/**
* The way to cite an existing reference (edit tier only), and « Nouvelle référence »; the
* pending line goes under the whole row, so both buttons stay level with the select.
*/
function AddRow({ props }: { props: DraftListProps }) {
const t = useT()
const pending = props.choice.pending && !props.readOnly
return (
<div className="flex flex-wrap items-end gap-2">
{!props.readOnly && <CiteField props={props} />}
<Button
type="button"
variant="secondary"
disabled={props.readOnly}
onClick={(event) => props.onNew(event.currentTarget)}
>
{t('editor.citations.new_reference')}
</Button>
<div className="space-y-1">
<div className="flex flex-wrap items-end gap-2">
{!props.readOnly && <CiteField props={props} />}
<Button
type="button"
variant="secondary"
disabled={props.readOnly}
onClick={(event) => props.onNew(event.currentTarget)}
>
{t('editor.citations.new_reference')}
</Button>
</div>
{pending && (
<p
id={PENDING_ID}
role="alert"
className="text-sm text-[var(--color-danger)]"
>
{t('editor.citations.pending_choice')}
</p>
)}
</div>
)
}
@@ -161,6 +161,9 @@ function useCiteChoice(ids: readonly string[]) {
const options = useCitableOptions(ids)
const [state, setState] = useState({ id: '', asked: false })
const chosen = options.some((o) => o.value === state.id) ? state.id : ''
// A choice no longer offered is dropped (a render-time adjustment, no effect): its
// reference coming back by a redo never brings it, or its line, back.
if (state.id !== '' && chosen === '') setState({ id: '', asked: false })
return {
options,
chosen,
@@ -30,10 +30,6 @@ export default function CitersList({
// The current family's row says « cette famille » once, without a second badge (Rev 6).
const isCurrent = (citer: CiterItem) =>
current !== undefined && sameKey(citer.key, current)
const ownLabel = elementLabel(
'editor.references.this_finding',
current ?? null,
)
// « Aller à » alone repeats per citer: the name says where each one goes (M6).
const where = (citer: CiterItem) =>
[...citer.trail, citer.name].map((name) => name[locale]).join(' › ')
@@ -53,7 +49,12 @@ export default function CitersList({
{citer.name[locale]}
{isCurrent(citer) && (
<span className="ml-1 rounded-md bg-[var(--color-surface-strong)] px-2 py-0.5 text-xs">
{t(ownLabel)}
{t(
elementLabel(
'editor.references.this_finding',
current ?? null,
),
)}
</span>
)}
{citer.isFamily && !isCurrent(citer) && (
@@ -34,8 +34,10 @@ function useFilterFinding(layer: ReferenceLayer) {
const gone = doc !== null && layer.filter !== null && finding === null
// The vanished finding is the trigger; the move and its wording are the latest ones.
const clear = useRef<() => void>(() => undefined)
const gonePhrase = elementLabel('editor.references.filter_gone', layer.filter)
clear.current = () => layer.clearFilter(t(gonePhrase))
clear.current = () =>
layer.clearFilter(
t(elementLabel('editor.references.filter_gone', layer.filter)),
)
useEffect(() => {
if (gone) clear.current()
}, [gone])