refactor(app): split the crop group hook, closed reason in drawing refusals
This commit is contained in:
1 parent
1bbf97e65b
commit
7ea9619aa0
3 files changed
+82
-25
No files matched your search
@@ -23,13 +23,24 @@ export interface CropGroup {
|
|||||||
const differs = (a: BoxFields, b: BoxFields) =>
|
const differs = (a: BoxFields, b: BoxFields) =>
|
||||||
BOX_KEYS.some((key) => a[key] !== b[key])
|
BOX_KEYS.some((key) => a[key] !== b[key])
|
||||||
|
|
||||||
|
type Proposal =
|
||||||
|
| { kind: 'invalid'; reason: string }
|
||||||
|
| { kind: 'same' }
|
||||||
|
| { kind: 'change'; box: ViewBox | null }
|
||||||
|
|
||||||
|
function proposalOf(draft: BoxFields, box: ViewBox | null): Proposal {
|
||||||
|
const parsed = parseBox(draft)
|
||||||
|
if (!parsed.ok) return { kind: 'invalid', reason: parsed.reason }
|
||||||
|
return sameBox(parsed.box, box)
|
||||||
|
? { kind: 'same' }
|
||||||
|
: { kind: 'change', box: parsed.box }
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The four `view_box` fields as one group: `apply` proposes all four together under the
|
* The typed fields and the refused flag. A new saved box (undo, reload, restore, an
|
||||||
* geometry check; a refused group stays typed and `refused`. A new saved box (undo, reload,
|
* accepted apply) replaces what was typed.
|
||||||
* restore, an accepted apply) replaces what was typed; unmounting applies a pending group.
|
|
||||||
*/
|
*/
|
||||||
export function useCropGroup(viewId: string, box: ViewBox | null): CropGroup {
|
function useCropDraft(box: ViewBox | null) {
|
||||||
const views = useViewUpdate()
|
|
||||||
const saved = fieldsOf(box)
|
const saved = fieldsOf(box)
|
||||||
const [draft, setDraft] = useState<BoxFields>(saved)
|
const [draft, setDraft] = useState<BoxFields>(saved)
|
||||||
const [seen, setSeen] = useState<BoxFields>(saved)
|
const [seen, setSeen] = useState<BoxFields>(saved)
|
||||||
@@ -39,35 +50,50 @@ export function useCropGroup(viewId: string, box: ViewBox | null): CropGroup {
|
|||||||
setDraft(saved)
|
setDraft(saved)
|
||||||
setRefused(false)
|
setRefused(false)
|
||||||
}
|
}
|
||||||
const apply = (): void => {
|
return { saved, draft, setDraft, refused, setRefused }
|
||||||
const parsed = parseBox(draft)
|
}
|
||||||
if (!parsed.ok) {
|
|
||||||
views.reject('crop', `editor.views.crop.${parsed.reason}`)
|
/** Runs the latest `flush` once, when the component unmounts with `pending` true. */
|
||||||
setRefused(true)
|
function useFlushOnUnmount(pending: boolean, flush: () => void): void {
|
||||||
} else if (sameBox(parsed.box, box)) {
|
const latest = useRef({ pending, flush })
|
||||||
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(() => {
|
useEffect(() => {
|
||||||
latest.current = { draft, saved, apply }
|
latest.current = { pending, flush }
|
||||||
})
|
})
|
||||||
useEffect(
|
useEffect(
|
||||||
() => () => {
|
() => () => {
|
||||||
if (differs(latest.current.draft, latest.current.saved)) {
|
if (latest.current.pending) latest.current.flush()
|
||||||
latest.current.apply()
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
[],
|
[],
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The four `view_box` fields as one group: `apply` proposes all four together under the
|
||||||
|
* geometry check; a refused group stays typed and `refused`. Unmounting applies a pending
|
||||||
|
* group.
|
||||||
|
*/
|
||||||
|
export function useCropGroup(viewId: string, box: ViewBox | null): CropGroup {
|
||||||
|
const views = useViewUpdate()
|
||||||
|
const form = useCropDraft(box)
|
||||||
|
const { saved, draft, setDraft, setRefused } = form
|
||||||
|
const apply = (): void => {
|
||||||
|
const proposal = proposalOf(draft, box)
|
||||||
|
if (proposal.kind === 'invalid') {
|
||||||
|
views.reject('crop', `editor.views.crop.${proposal.reason}`)
|
||||||
|
setRefused(true)
|
||||||
|
} else if (proposal.kind === 'same') {
|
||||||
|
views.clear()
|
||||||
|
setRefused(false)
|
||||||
|
} else {
|
||||||
|
const patch = { view_box: proposal.box }
|
||||||
|
setRefused(!views.update('crop', viewId, patch, true))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
useFlushOnUnmount(differs(draft, saved), apply)
|
||||||
const shown = parseBox(draft)
|
const shown = parseBox(draft)
|
||||||
return {
|
return {
|
||||||
draft,
|
draft,
|
||||||
refused,
|
refused: form.refused,
|
||||||
views,
|
views,
|
||||||
rectangle: shown.ok ? shown.box : box,
|
rectangle: shown.ok ? shown.box : box,
|
||||||
setField: (key, value) => setDraft({ ...draft, [key]: value }),
|
setField: (key, value) => setDraft({ ...draft, [key]: value }),
|
||||||
|
|||||||
@@ -1,10 +1,21 @@
|
|||||||
import type { AnatomicalSheetDocument } from '@faunosteo/core-wasm/types'
|
import type { AnatomicalSheetDocument } from '@faunosteo/core-wasm/types'
|
||||||
import type { Locale } from '@/i18n/types'
|
import type { Locale } from '@/i18n/types'
|
||||||
|
import { rejectionReason } from './drawingReason'
|
||||||
import { elementLabel } from './sheetDocument'
|
import { elementLabel } from './sheetDocument'
|
||||||
import type { GeometryRefusal } from './issues'
|
import type { GeometryRefusal } from './issues'
|
||||||
|
|
||||||
type Translate = (key: string) => string
|
type Translate = (key: string) => string
|
||||||
|
|
||||||
|
function drawingRejectedMessage(t: Translate, detail: string | null): string {
|
||||||
|
const reason = rejectionReason(detail)
|
||||||
|
if (reason === 'not_canonical')
|
||||||
|
return t('editor.upload.err.svg_not_canonical')
|
||||||
|
return t('editor.upload.err.svg_rejected').replace(
|
||||||
|
'{reason}',
|
||||||
|
t(`editor.upload.reason.${reason}`),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The words for a frame change the geometry check refused: the annotations (or views)
|
* The words for a frame change the geometry check refused: the annotations (or views)
|
||||||
* the change would break, by name. Nothing here re-judges the geometry.
|
* the change would break, by name. Nothing here re-judges the geometry.
|
||||||
@@ -20,7 +31,7 @@ export function frameRefusalMessage(
|
|||||||
.map((id) => (doc ? elementLabel(doc, { kind, id }, locale) : id))
|
.map((id) => (doc ? elementLabel(doc, { kind, id }, locale) : id))
|
||||||
.join(', ')
|
.join(', ')
|
||||||
if (refusal.reason === 'drawing_rejected') {
|
if (refusal.reason === 'drawing_rejected') {
|
||||||
return t('editor.upload.err.svg_not_canonical')
|
return drawingRejectedMessage(t, refusal.drawingDetail ?? null)
|
||||||
}
|
}
|
||||||
if (refusal.reason === 'view_unmeasured') {
|
if (refusal.reason === 'view_unmeasured') {
|
||||||
return t('editor.views.frame.view_unmeasured').replace(
|
return t('editor.views.frame.view_unmeasured').replace(
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import ViewsTab from '@/components/editor/tabs/ViewsTab'
|
|||||||
import { cleanSvg } from '@/editor/core/cleaning'
|
import { cleanSvg } from '@/editor/core/cleaning'
|
||||||
import { createHash } from 'node:crypto'
|
import { createHash } from 'node:crypto'
|
||||||
import { getDict } from '@/i18n'
|
import { getDict } from '@/i18n'
|
||||||
|
import { frameRefusalMessage } from '@/editor/frameRefusal'
|
||||||
import { uploadErrorMessage } from '@/editor/uploadMessages'
|
import { uploadErrorMessage } from '@/editor/uploadMessages'
|
||||||
import { MAX_DRAWING_BYTES, attachToView } from '@/editor/drawingUpload'
|
import { MAX_DRAWING_BYTES, attachToView } from '@/editor/drawingUpload'
|
||||||
import { useLocaleStore } from '@/state/localeStore'
|
import { useLocaleStore } from '@/state/localeStore'
|
||||||
@@ -296,3 +297,22 @@ describe('drawing upload flow', () => {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('frame refusal for a rejected drawing', () => {
|
||||||
|
it('phrases not_canonical and a reader reason as closed messages', () => {
|
||||||
|
const t = (key: string) => getDict('fr')[key as never] as string
|
||||||
|
const refusal = (drawingDetail: string) => ({
|
||||||
|
reason: 'drawing_rejected' as const,
|
||||||
|
annotationIds: [],
|
||||||
|
issues: [],
|
||||||
|
viewIds: [],
|
||||||
|
drawingDetail,
|
||||||
|
})
|
||||||
|
expect(
|
||||||
|
frameRefusalMessage(t, refusal('not_canonical'), null, 'fr'),
|
||||||
|
).toMatch(/pas sous la forme enregistrée/)
|
||||||
|
expect(frameRefusalMessage(t, refusal('doctype'), null, 'fr')).toMatch(
|
||||||
|
/DOCTYPE/,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
Reference in new issue
Block a user