fix(app): closed hash and canonical-form drawing messages, split crop and drawing views

This commit is contained in:
bchanot committed 2026-11-04 13:34:02 +01:00
1 parent 7bf1404946
commit 1bbf97e65b
16 files changed
+487 -248

No files matched your search

@@ -1,18 +1,17 @@
import { useEffect, useRef, useState } from 'react'
import type { ViewBox } from '@faunosteo/core-wasm/types'
import ActionButton from '@/components/editor/fields/ActionButton'
import DraftField from '@/components/editor/fields/DraftField'
import ReadOnlyValue from '@/components/editor/fields/ReadOnlyValue'
import {
BOX_KEYS,
fieldsOf,
parseBox,
sameBox,
type BoxFields,
} from '@/editor/viewBox'
import { useViewUpdate } from '@/editor/useViewUpdate'
import { BOX_KEYS, fieldsOf } from '@/editor/viewBox'
import { useT } from '@/i18n/useT'
import CropCanvas from './CropCanvas'
import { useCropGroup, type CropGroup } from './useCropGroup'
interface CropProps {
viewId: string
parentId: string
box: ViewBox | null
}
/**
* The four `view_box` fields as ONE group: leaving the group or Enter proposes all four
@@ -20,141 +19,91 @@ import CropCanvas from './CropCanvas'
* marked « non appliqué », with the refusal naming what it would break.
*/
export default function CropEditor({
viewId,
parentId,
box,
readOnly,
}: {
viewId: string
parentId: string
box: ViewBox | null
readOnly: boolean
}) {
const t = useT()
if (readOnly) {
const fields = fieldsOf(box)
return (
<div className="grid gap-3 sm:grid-cols-4">
{BOX_KEYS.map((key) => (
<ReadOnlyValue
key={key}
id={`view-${viewId}-crop-${key}`}
label={t(`editor.views.crop.${key}`)}
value={fields[key]}
/>
))}
</div>
)
}
return <EditableCrop viewId={viewId} parentId={parentId} box={box} />
...props
}: CropProps & { readOnly: boolean }) {
return readOnly ? <CropValues {...props} /> : <EditableCrop {...props} />
}
type Gate = { state: 'applied' } | { state: 'refused' }
function EditableCrop({
viewId,
parentId,
box,
}: {
viewId: string
parentId: string
box: ViewBox | null
}) {
function CropValues({ viewId, box }: CropProps) {
const t = useT()
const views = useViewUpdate()
const saved = fieldsOf(box)
const [draft, setDraft] = useState<BoxFields>(saved)
const [seen, setSeen] = useState<BoxFields>(saved)
const [gate, setGate] = useState<Gate>({ state: 'applied' })
// A new saved box (undo, reload, restore, an accepted apply) replaces what was typed.
if (BOX_KEYS.some((key) => seen[key] !== saved[key])) {
setSeen(saved)
setDraft(saved)
setGate({ state: 'applied' })
}
const apply = (fields: BoxFields): void => {
const parsed = parseBox(fields)
if (!parsed.ok) {
views.reject('crop', `editor.views.crop.${parsed.reason}`)
setGate({ state: 'refused' })
} else if (sameBox(parsed.box, box)) {
views.clear()
setGate({ state: 'applied' })
} else {
const done = views.update('crop', viewId, { view_box: parsed.box }, true)
setGate({ state: done ? 'applied' : 'refused' })
}
}
const latest = useRef({ draft, saved, apply })
useEffect(() => {
latest.current = { draft, saved, apply }
})
useEffect(
() => () => {
const pending = latest.current
if (BOX_KEYS.some((key) => pending.draft[key] !== pending.saved[key])) {
pending.apply(pending.draft)
}
},
[],
)
const shown = parseBox(draft)
const rectangle = shown.ok ? shown.box : box
const revert = () => {
setDraft(saved)
setGate({ state: 'applied' })
views.clear()
}
const fields = fieldsOf(box)
return (
<div className="grid gap-3 sm:grid-cols-4">
{BOX_KEYS.map((key) => (
<ReadOnlyValue
key={key}
id={`view-${viewId}-crop-${key}`}
label={t(`editor.views.crop.${key}`)}
value={fields[key]}
/>
))}
</div>
)
}
function EditableCrop({ viewId, parentId, box }: CropProps) {
const group = useCropGroup(viewId, box)
return (
<div className="grid gap-4 md:grid-cols-2">
<div
role="group"
aria-label={t('editor.views.crop.title')}
className="space-y-3"
onBlur={(event) => {
if (!event.currentTarget.contains(event.relatedTarget)) apply(draft)
}}
onKeyDown={(event) => {
if (
event.key === 'Enter' &&
event.target instanceof HTMLInputElement
) {
apply(draft)
}
}}
<CropFields viewId={viewId} group={group} />
<CropCanvas parentId={parentId} box={group.rectangle} />
</div>
)
}
function CropFields({ viewId, group }: { viewId: string; group: CropGroup }) {
const t = useT()
return (
<div
role="group"
aria-label={t('editor.views.crop.title')}
className="space-y-3"
onBlur={(event) => {
if (!event.currentTarget.contains(event.relatedTarget)) group.apply()
}}
onKeyDown={(event) => {
if (event.key === 'Enter' && event.target instanceof HTMLInputElement) {
group.apply()
}
}}
>
<p className="text-sm text-[var(--color-ink-muted)]">
{t('editor.views.crop.hint')}
</p>
<div className="grid grid-cols-2 gap-3">
{BOX_KEYS.map((key) => (
<DraftField
key={key}
id={`view-${viewId}-crop-${key}`}
label={t(`editor.views.crop.${key}`)}
value={group.draft[key]}
onChange={(value) => group.setField(key, value)}
/>
))}
</div>
{group.refused && <CropRefusal group={group} />}
</div>
)
}
function CropRefusal({ group }: { group: CropGroup }) {
const t = useT()
const message = group.views.error('crop')
return (
<div role="alert" className="space-y-2 text-sm">
<p className="font-medium text-[var(--color-danger-strong)]">
{t('editor.views.crop.not_applied')}
</p>
{message && <p>{message}</p>}
<ActionButton
readOnly={false}
variant="ghost"
size="sm"
onClick={group.revert}
>
<p className="text-sm text-[var(--color-ink-muted)]">
{t('editor.views.crop.hint')}
</p>
<div className="grid grid-cols-2 gap-3">
{BOX_KEYS.map((key) => (
<DraftField
key={key}
id={`view-${viewId}-crop-${key}`}
label={t(`editor.views.crop.${key}`)}
value={draft[key]}
onChange={(value) => setDraft({ ...draft, [key]: value })}
/>
))}
</div>
{gate.state === 'refused' && (
<div role="alert" className="space-y-2 text-sm">
<p className="font-medium text-[var(--color-danger-strong)]">
{t('editor.views.crop.not_applied')}
</p>
{views.error('crop') && <p>{views.error('crop')}</p>}
<ActionButton
readOnly={false}
variant="ghost"
size="sm"
onClick={revert}
>
{t('editor.views.crop.revert')}
</ActionButton>
</div>
)}
</div>
<CropCanvas parentId={parentId} box={rectangle} />
{t('editor.views.crop.revert')}
</ActionButton>
</div>
)
}
@@ -2,9 +2,13 @@ import { useRef, 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'
import { drawingStatusOf, drawingStatusText } from '@/editor/drawingStatus'
import {
drawingStatusOf,
drawingStatusText,
type DrawingStatus,
} from '@/editor/drawingStatus'
import { unmeasuredViews } from '@/editor/issues'
import { useDrawingUpload } from '@/editor/useDrawingUpload'
import { useDrawingUpload, type DrawingUpload } from '@/editor/useDrawingUpload'
import { uploadErrorMessage } from '@/editor/uploadMessages'
import { useSheetDocument } from '@/editor/sheetDocument'
import { useEditorStore } from '@/editor/state/editorStore'
@@ -14,9 +18,9 @@ import { useLocaleStore } from '@/state/localeStore'
import CleaningDialog from './CleaningDialog'
/**
* A view's drawing: its state with the core's detail, the hint when it is not usable,
* « Choisir une illustration » (check, clean, upload, attach) and, on a detail view that
* has its own drawing, the way back to the parent's.
* A view's drawing: its state, the hint when it is not usable, « Choisir une
* illustration » (check, clean, upload, attach) and, on a detail view that has its own
* drawing, the way back to the parent's.
*/
export default function ViewDrawing({
view,
@@ -25,88 +29,73 @@ export default function ViewDrawing({
view: SheetView
readOnly: boolean
}) {
const t = useT()
const state = useEditorStore((s) => s.previewState)
const failures = useEditorStore((s) => s.drawingFailures)
if (!state) return null
const status = drawingStatusOf(view, state, failures)
const inherits = view.kind === 'detail' && status.kind === 'none'
const unmeasured = unmeasuredViews(state).has(view.id)
const unusable = status.kind !== 'ok' && !inherits
return (
<div className="space-y-2" data-testid={`view-${view.id}-drawing`}>
<DrawingLines
view={view}
status={status}
unmeasured={unmeasured}
readOnly={readOnly}
/>
{!readOnly && (
<DrawingActions view={view} ownDrawing={status.kind !== 'none'} />
)}
</div>
)
}
function DrawingLines({
view,
status,
unmeasured,
readOnly,
}: {
view: SheetView
status: DrawingStatus
unmeasured: boolean
readOnly: boolean
}) {
const t = useT()
const inherits = view.kind === 'detail' && status.kind === 'none'
const unusable = status.kind !== 'ok' && !inherits
const muted = 'text-sm text-[var(--color-ink-muted)]'
return (
<>
<p className="text-sm font-medium">{t('editor.views.drawing')}</p>
<p className="text-sm text-[var(--color-ink-muted)]">
<p className={muted}>
{inherits
? t('editor.views.drawing.inherited')
: drawingStatusText(t, status)}
</p>
{!readOnly && (unusable || unmeasured) && (
<p className="text-sm text-[var(--color-ink-muted)]">
{t('editor.views.drawing.hint')}
</p>
<p className={muted}>{t('editor.views.drawing.hint')}</p>
)}
{unmeasured && (
<p className="text-sm text-[var(--color-ink-muted)]">
{t('editor.views.drawing.unmeasured')}
</p>
<p className={muted}>{t('editor.views.drawing.unmeasured')}</p>
)}
<DrawingActions
view={view}
readOnly={readOnly}
ownDrawing={status.kind !== 'none'}
/>
</div>
</>
)
}
function DrawingActions({
view,
readOnly,
ownDrawing,
}: {
view: SheetView
readOnly: boolean
ownDrawing: boolean
}) {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
const doc = useSheetDocument()
const input = useRef<HTMLInputElement | null>(null)
const upload = useDrawingUpload(view.id)
const views = useViewUpdate()
const pick = (event: ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0]
event.target.value = ''
if (file) void upload.start(file)
}
const failure =
upload.ui.phase === 'failed'
? uploadErrorMessage(t, upload.ui.error, { doc, locale })
: null
if (readOnly) return null
return (
<>
<div className="flex flex-wrap items-center gap-2">
<input
ref={input}
type="file"
accept=".svg,image/svg+xml"
className="sr-only"
tabIndex={-1}
aria-label={t('editor.views.drawing.file')}
data-testid={`view-${view.id}-file`}
onChange={pick}
/>
<ActionButton
readOnly={false}
variant="secondary"
size="sm"
disabled={upload.ui.phase === 'working'}
onClick={() => input.current?.click()}
>
{t('editor.views.drawing.choose')}
</ActionButton>
<FilePicker viewId={view.id} upload={upload} />
{view.kind === 'detail' && ownDrawing && (
<ActionButton
readOnly={false}
@@ -120,13 +109,7 @@ function DrawingActions({
</ActionButton>
)}
</div>
{upload.ui.phase === 'working' && (
<Notice tone="info">{t('editor.upload.working')}</Notice>
)}
{upload.ui.phase === 'done' && (
<Notice tone="success">{t('editor.upload.done')}</Notice>
)}
{failure && <Notice tone="error">{failure}</Notice>}
<UploadNotices upload={upload} />
{views.error('drawing') && (
<Notice tone="error">{views.error('drawing')}</Notice>
)}
@@ -138,3 +121,64 @@ function DrawingActions({
</>
)
}
function FilePicker({
viewId,
upload,
}: {
viewId: string
upload: DrawingUpload
}) {
const t = useT()
const input = useRef<HTMLInputElement | null>(null)
const pick = (event: ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0]
event.target.value = ''
if (file) void upload.start(file)
}
return (
<>
<input
ref={input}
type="file"
accept=".svg,image/svg+xml"
className="sr-only"
tabIndex={-1}
aria-label={t('editor.views.drawing.file')}
data-testid={`view-${viewId}-file`}
onChange={pick}
/>
<ActionButton
readOnly={false}
variant="secondary"
size="sm"
disabled={upload.ui.phase === 'working'}
onClick={() => input.current?.click()}
>
{t('editor.views.drawing.choose')}
</ActionButton>
</>
)
}
function UploadNotices({ upload }: { upload: DrawingUpload }) {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
const doc = useSheetDocument()
const { ui } = upload
const failure =
ui.phase === 'failed'
? uploadErrorMessage(t, ui.error, { doc, locale })
: null
return (
<>
{ui.phase === 'working' && (
<Notice tone="info">{t('editor.upload.working')}</Notice>
)}
{ui.phase === 'done' && (
<Notice tone="success">{t('editor.upload.done')}</Notice>
)}
{failure && <Notice tone="error">{failure}</Notice>}
</>
)
}
@@ -0,0 +1,81 @@
import { useEffect, useRef, useState } from 'react'
import type { ViewBox } from '@faunosteo/core-wasm/types'
import {
BOX_KEYS,
fieldsOf,
parseBox,
sameBox,
type BoxFields,
} from '@/editor/viewBox'
import { useViewUpdate, type ViewUpdateApi } from '@/editor/useViewUpdate'
export interface CropGroup {
draft: BoxFields
refused: boolean
views: ViewUpdateApi
/** The rectangle to draw: the typed group when it parses, else the saved box. */
rectangle: ViewBox | null
setField: (key: keyof BoxFields, value: string) => void
apply: () => void
revert: () => void
}
const differs = (a: BoxFields, b: BoxFields) =>
BOX_KEYS.some((key) => a[key] !== b[key])
/**
* The four `view_box` fields as one group: `apply` proposes all four together under the
* geometry check; a refused group stays typed and `refused`. A new saved box (undo, reload,
* restore, an accepted apply) replaces what was typed; unmounting applies a pending group.
*/
export function useCropGroup(viewId: string, box: ViewBox | null): CropGroup {
const views = useViewUpdate()
const saved = fieldsOf(box)
const [draft, setDraft] = useState<BoxFields>(saved)
const [seen, setSeen] = useState<BoxFields>(saved)
const [refused, setRefused] = useState(false)
if (differs(seen, saved)) {
setSeen(saved)
setDraft(saved)
setRefused(false)
}
const apply = (): void => {
const parsed = parseBox(draft)
if (!parsed.ok) {
views.reject('crop', `editor.views.crop.${parsed.reason}`)
setRefused(true)
} else if (sameBox(parsed.box, box)) {
views.clear()
setRefused(false)
} else {
const done = views.update('crop', viewId, { view_box: parsed.box }, true)
setRefused(!done)
}
}
const latest = useRef({ draft, saved, apply })
useEffect(() => {
latest.current = { draft, saved, apply }
})
useEffect(
() => () => {
if (differs(latest.current.draft, latest.current.saved)) {
latest.current.apply()
}
},
[],
)
const shown = parseBox(draft)
return {
draft,
refused,
views,
rectangle: shown.ok ? shown.box : box,
setField: (key, value) => setDraft({ ...draft, [key]: value }),
apply,
revert: () => {
setDraft(saved)
setRefused(false)
views.clear()
},
}
}