fix(editor): plurals, trimmed names, default tab, parent drawing confirmation

This commit is contained in:
bchanot committed 2026-11-04 19:40:43 +01:00
1 parent b42c6ae844
commit be4f468790
35 files changed
+385 -63

No files matched your search

@@ -60,6 +60,7 @@ function NameFields(props: Props) {
value={annotation.name?.[locale] ?? ''}
onCommit={rename(locale)}
readOnly={readOnly}
trim
error={edit.fieldError(locale)}
/>
))}
@@ -12,6 +12,7 @@ import {
type EditableGeometry,
type GroupFields,
} from '@/editor/annotations/geometryEdit'
import { usePlural } from '@/editor/plural'
import { useT } from '@/i18n/useT'
/** What applying the group answers: kept, or the words that say why not. */
@@ -208,7 +209,6 @@ function CircleRows(props: RowsProps<Extract<Fields, { type: 'circle' }>>) {
}
function PolygonRows(props: RowsProps<PolygonFields>) {
const t = useT()
const [selected, setSelected] = useState(0)
const { fields } = props
const change = (index: number, axis: 'x' | 'y', value: string) =>
@@ -218,11 +218,11 @@ function PolygonRows(props: RowsProps<PolygonFields>) {
i === index ? { ...row, [axis]: value } : row,
),
})
const count = String(fields.rows.length)
const plural = usePlural()
return (
<div className="space-y-2">
<p className="text-sm text-[var(--color-ink-muted)]">
{t('editor.annotations.geometry.vertices').replace('{count}', count)}
{plural('editor.annotations.geometry.vertices', fields.rows.length)}
</p>
{fields.rows.map((row, index) => (
<VertexRow
@@ -1,4 +1,5 @@
import Button from '@/components/ui/Button'
import { usePlural } from '@/editor/plural'
import { useT } from '@/i18n/useT'
/** While a zone or link is being traced: close (3 vertices at least) or drop it. */
@@ -12,10 +13,11 @@ export default function TracingBar({
onCancel: () => void
}) {
const t = useT()
const plural = usePlural()
return (
<div className="flex flex-wrap items-center gap-2">
<p className="text-sm text-[var(--color-ink-muted)]">
{t('editor.annotations.tracing').replace('{count}', String(count))}
{plural('editor.annotations.tracing', count)}
</p>
<Button type="button" size="sm" disabled={count < 3} onClick={onClose}>
{t('editor.annotations.trace_close')}
@@ -15,6 +15,8 @@ export interface TextFieldProps {
readOnly?: boolean
multiline?: boolean
maxLength?: number
/** Opt-in for names and simple labels: the committed value loses its edge spaces. */
trim?: boolean
hint?: string
/** The refusal phrased by the caller, shown next to the control. */
error?: string
@@ -46,6 +48,7 @@ function EditableText({
onCommit,
multiline,
maxLength,
trim,
hint,
error,
}: TextFieldProps) {
@@ -56,20 +59,23 @@ function EditableText({
setSeen(value)
setDraft(value)
}
const latest = useRef({ draft, value, onCommit })
const latest = useRef({ draft, value, onCommit, trim })
useEffect(() => {
latest.current = { draft, value, onCommit }
latest.current = { draft, value, onCommit, trim }
})
useEffect(
() => () => {
const pending = latest.current
if (pending.draft !== pending.value) pending.onCommit(pending.draft)
const next = pending.trim ? pending.draft.trim() : pending.draft
if (next !== pending.value) pending.onCommit(next)
},
[],
)
const commit = () => {
if (draft !== value) onCommit(draft)
const next = trim ? draft.trim() : draft
if (next !== draft) setDraft(next)
if (next !== value) onCommit(next)
}
const onKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {
if (event.key === 'Enter' && !event.nativeEvent.isComposing) commit()
@@ -9,9 +9,10 @@ import DraftField from '@/components/editor/fields/DraftField'
import SelectField from '@/components/editor/fields/SelectField'
import type { FindingItem, FindingKind } from '@/editor/findings/tree'
import type { IdPrefix } from '@/editor/ids'
import { isBlank } from '@/editor/sheetDocument'
import { elementLabel, isBlank, useSheetDocument } from '@/editor/sheetDocument'
import { useEditCommand } from '@/editor/useEditCommand'
import { useT } from '@/i18n/useT'
import { useLocaleStore } from '@/state/localeStore'
export const CATEGORY_KINDS: readonly FindingCategoryKind[] = [
'dysfunction',
@@ -40,6 +41,7 @@ export default function FindingCreateForm({
onCancel: () => void
}) {
const t = useT()
const title = useFormTitle(pending)
const edit = useEditCommand()
const [fr, setFr] = useState('')
const [en, setEn] = useState('')
@@ -54,15 +56,13 @@ export default function FindingCreateForm({
let createdId = ''
const added = edit.runNew('form', PREFIX[pending.kind], (id) => {
createdId = id
return insertCommand(pending, id, { fr, en }, kind)
return insertCommand(pending, id, { fr: fr.trim(), en: en.trim() }, kind)
})
if (added) onCreated(createdId)
}
return (
<div className="space-y-2 rounded-lg border border-dashed border-[var(--color-border)] p-3">
<p className="text-sm font-medium">
{t(`editor.findings.new.${pending.kind}`)}
</p>
<p className="text-sm font-medium">{title}</p>
<div className="grid gap-3 md:grid-cols-2">
{pending.kind === 'category' && (
<SelectField
@@ -109,6 +109,22 @@ export default function FindingCreateForm({
)
}
/** A category lands at zone level, so its title names the zone, whatever is selected. */
function useFormTitle(pending: PendingFinding): string {
const t = useT()
const doc = useSheetDocument()
const locale = useLocaleStore((s) => s.locale)
if (pending.kind !== 'category' || !doc || !pending.parent) {
return t(`editor.findings.new.${pending.kind}`)
}
const zone = elementLabel(
doc,
{ kind: 'zone', id: pending.parent.zoneId },
locale,
)
return t('editor.findings.new.category_in_zone').replace('{zone}', zone)
}
const PREFIX: Record<FindingKind, IdPrefix> = {
zone: 'zone',
category: 'fcat',
@@ -92,6 +92,7 @@ function Names({
value={item.name[locale]}
onCommit={rename(locale)}
readOnly={readOnly}
trim
error={edit.fieldError(locale)}
/>
))}
@@ -48,6 +48,7 @@ export default function AttributionSection({
value={attribution.license ?? ''}
onCommit={(value) => send('license', { license: nullable(value) })}
readOnly={readOnly}
trim
error={edit.fieldError('license')}
/>
<TextField
@@ -78,7 +79,7 @@ function Authors({
const [draft, setDraft] = useState('')
const add = () => {
if (isBlank(draft)) return
if (onChange([...authors, draft])) setDraft('')
if (onChange([...authors, draft.trim()])) setDraft('')
}
return (
<div className="space-y-2">
@@ -91,6 +91,7 @@ function CategoryRow({
value={category.name[locale]}
onCommit={rename(locale)}
readOnly={readOnly}
trim
error={edit.fieldError(locale)}
/>
))}
@@ -133,7 +134,11 @@ function CategoryForm() {
op: 'insert',
element: {
kind: 'category',
value: { id, name: { fr, en }, color: nullable(color) },
value: {
id,
name: { fr: fr.trim(), en: en.trim() },
color: nullable(color),
},
},
}))
if (added) {
@@ -47,6 +47,7 @@ export default function TextsSection({
value={doc.name[locale]}
onCommit={rename(locale)}
readOnly={readOnly}
trim
error={edit.fieldError(`name-${locale}`)}
/>
))}
@@ -4,6 +4,7 @@ import type {
SheetReport,
StatusCounts,
} from '@faunosteo/core-wasm/types'
import { usePlural } from '@/editor/plural'
import { elementLabel } from '@/editor/sheetDocument'
import { DOCUMENT_BYTES_CAP, groupThousands } from '@/editor/verification'
import { useT } from '@/i18n/useT'
@@ -53,7 +54,7 @@ function GroupRow({ group, label }: { group: ReviewGroup; label: string }) {
export function Completeness({ report }: { report: SheetReport }) {
const t = useT()
const { statuses, vet_referrals, uncited } = report.completeness
const count = (key: string, n: number) => t(key).replace('{count}', String(n))
const count = usePlural()
return (
<section
aria-label={t('editor.verification.completeness')}
@@ -3,6 +3,7 @@ import FormField from '@/components/ui/FormField'
import Textarea from '@/components/ui/Textarea'
import { useEditorStore } from '@/editor/state/editorStore'
import { NOTE_MAX_LENGTH } from '@/editor/verification'
import { usePlural } from '@/editor/plural'
import { useT } from '@/i18n/useT'
import { useSubmission } from './useSubmission'
@@ -17,12 +18,10 @@ function useBlockingHint(
noteBlank: boolean,
): string | null {
const t = useT()
const plural = usePlural()
const unsaved = useEditorStore((s) => s.unsaved || s.staleBase)
if (errorCount > 0) {
return t('editor.verification.hint.errors').replace(
'{count}',
String(errorCount),
)
return plural('editor.verification.hint.errors', errorCount)
}
if (unsaved) return t('editor.verification.hint.unsaved')
return noteBlank ? t('editor.verification.hint.note') : null
@@ -1,4 +1,5 @@
import Badge from '@/components/ui/Badge'
import { usePlural } from '@/editor/plural'
import { useT } from '@/i18n/useT'
interface VerificationSummaryProps {
@@ -14,7 +15,7 @@ export default function VerificationSummary({
toConfirm,
}: VerificationSummaryProps) {
const t = useT()
const count = (key: string, n: number) => t(key).replace('{count}', String(n))
const count = usePlural()
return (
<div className="space-y-2">
<h2 className="text-base font-medium">
@@ -31,7 +31,7 @@ export default function RegionForm() {
kind: 'region',
value: {
code: form.code,
name: { fr: form.fr, en: form.en },
name: { fr: form.fr.trim(), en: form.en.trim() },
count,
count_range: range.range,
},
@@ -70,6 +70,7 @@ export default function RegionRow({
value={region.name[locale]}
onCommit={rename(locale)}
readOnly={readOnly}
trim
error={edit.fieldError(locale)}
/>
))}
@@ -41,7 +41,7 @@ export default function DetailViewForm({
value: {
kind: 'detail',
id,
name: { fr: form.fr, en: form.en },
name: { fr: form.fr.trim(), en: form.en.trim() },
svg_asset_id: null,
view_box: box.box,
parent_view_id: parent,
@@ -0,0 +1,35 @@
import Button from '@/components/ui/Button'
import Dialog from '@/components/ui/Dialog'
import { useT } from '@/i18n/useT'
export interface ParentDrawingDialogProps {
open: boolean
onConfirm: () => void
onCancel: () => void
}
/** Names what « Utiliser l'illustration de la vue parente » does before it commits. */
export default function ParentDrawingDialog({
open,
onConfirm,
onCancel,
}: ParentDrawingDialogProps) {
const t = useT()
return (
<Dialog
open={open}
onClose={onCancel}
title={t('editor.views.drawing.use_parent_title')}
description={t('editor.views.drawing.use_parent_body')}
>
<div className="mt-4 flex flex-wrap justify-end gap-2">
<Button type="button" variant="ghost" onClick={onCancel}>
{t('editor.views.drawing.use_parent_cancel')}
</Button>
<Button type="button" onClick={onConfirm}>
{t('editor.views.drawing.use_parent_confirm')}
</Button>
</div>
</Dialog>
)
}
@@ -1,4 +1,4 @@
import { useRef, type ChangeEvent } from 'react'
import { useRef, useState, type ChangeEvent } from 'react'
import type { SheetView } from '@faunosteo/core-wasm/types'
import ActionButton from '@/components/editor/fields/ActionButton'
import Notice from '@/components/ui/Notice'
@@ -16,6 +16,7 @@ import { useViewUpdate } from '@/editor/useViewUpdate'
import { useT } from '@/i18n/useT'
import { useLocaleStore } from '@/state/localeStore'
import CleaningDialog from './CleaningDialog'
import ParentDrawingDialog from './ParentDrawingDialog'
/**
* A view's drawing: its state, the hint when it is not usable, « Choisir une
@@ -92,6 +93,11 @@ function DrawingActions({
const t = useT()
const upload = useDrawingUpload(view.id)
const views = useViewUpdate()
const [asking, setAsking] = useState(false)
const usesParent = () => {
setAsking(false)
views.update('drawing', view.id, { svg_asset_id: null }, true)
}
return (
<>
<div className="flex flex-wrap items-center gap-2">
@@ -101,14 +107,17 @@ function DrawingActions({
readOnly={false}
variant="ghost"
size="sm"
onClick={() =>
views.update('drawing', view.id, { svg_asset_id: null }, true)
}
onClick={() => setAsking(true)}
>
{t('editor.views.drawing.use_parent')}
</ActionButton>
)}
</div>
<ParentDrawingDialog
open={asking}
onConfirm={usesParent}
onCancel={() => setAsking(false)}
/>
<UploadNotices upload={upload} />
{views.error('drawing') && (
<Notice tone="error">{views.error('drawing')}</Notice>
@@ -35,6 +35,7 @@ export default function ViewNames({
value={view.name[locale]}
onCommit={rename(locale)}
readOnly={readOnly}
trim
error={views.error(locale)}
/>
))}