feat(app): editor Général, Références and Vertèbres tabs, delete with dependents
This commit is contained in:
1 parent
6aba9711f0
commit
d163b2ce9a
24 files changed
+2239
-12
No files matched your search
@@ -0,0 +1,33 @@
|
||||
import type { CommitOutcome } from './state/editorTypes'
|
||||
|
||||
type Translate = (key: string) => string
|
||||
|
||||
const EDIT_KINDS: ReadonlySet<string> = new Set([
|
||||
'unknown_key',
|
||||
'duplicate_key',
|
||||
'invalid_target',
|
||||
'id_changed',
|
||||
'invalid_position',
|
||||
'too_deep',
|
||||
'has_dependents',
|
||||
])
|
||||
|
||||
/**
|
||||
* The words for a command that was not kept: the core's closed kind, or the store's
|
||||
* block. Never the core's detail text (it can echo document content).
|
||||
*/
|
||||
export function refusalMessage(
|
||||
t: Translate,
|
||||
outcome: Exclude<CommitOutcome, { ok: true }>,
|
||||
): string {
|
||||
if (outcome.reason === 'blocked') {
|
||||
return outcome.kind === 'recovery.pending'
|
||||
? t('editor.refusal.recovery')
|
||||
: t('editor.refusal.blocked')
|
||||
}
|
||||
if (outcome.reason !== 'core') return t('editor.refusal.blocked')
|
||||
const { kind } = outcome.failure
|
||||
return EDIT_KINDS.has(kind)
|
||||
? t(`editor.refusal.core.${kind}`)
|
||||
: t('editor.refusal.core.other').replace('{kind}', kind)
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
import { useMemo } from 'react'
|
||||
import type {
|
||||
AnatomicalSheetDocument,
|
||||
ElementKey,
|
||||
LocalizedOptionalText,
|
||||
SheetTexts,
|
||||
} from '@faunosteo/core-wasm/types'
|
||||
import type { Locale } from '@/i18n/types'
|
||||
import { useEditorStore } from './state/editorStore'
|
||||
|
||||
/** The edited document, read from the store's text; null while nothing is loaded. */
|
||||
export function useSheetDocument(): AnatomicalSheetDocument | null {
|
||||
const text = useEditorStore((s) => s.documentText)
|
||||
return useMemo(() => {
|
||||
try {
|
||||
return text === '' ? null : (JSON.parse(text) as AnatomicalSheetDocument)
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}, [text])
|
||||
}
|
||||
|
||||
export const isBlank = (value: string): boolean => value.trim() === ''
|
||||
|
||||
/** A blank optional text is absent (LocalizedOptionalText omits the key, never ""). */
|
||||
export function optionalText(
|
||||
current: LocalizedOptionalText,
|
||||
locale: Locale,
|
||||
value: string,
|
||||
): LocalizedOptionalText {
|
||||
const next = { ...current }
|
||||
if (isBlank(value)) delete next[locale]
|
||||
else next[locale] = value
|
||||
return next
|
||||
}
|
||||
|
||||
/** A blank optional field is null where the type is nullable. */
|
||||
export const nullable = (value: string): string | null =>
|
||||
isBlank(value) ? null : value
|
||||
|
||||
export function textsOf(doc: AnatomicalSheetDocument): SheetTexts {
|
||||
return {
|
||||
name: doc.name,
|
||||
description: doc.description,
|
||||
scope_disclaimer: doc.scope_disclaimer,
|
||||
attribution: doc.attribution,
|
||||
}
|
||||
}
|
||||
|
||||
interface Named {
|
||||
id: string
|
||||
name: { fr: string; en: string }
|
||||
children: readonly Named[]
|
||||
}
|
||||
|
||||
function findNode(
|
||||
nodes: readonly Named[],
|
||||
path: readonly string[],
|
||||
): Named | undefined {
|
||||
const [head, ...rest] = path
|
||||
const node = nodes.find((n) => n.id === head)
|
||||
if (!node || rest.length === 0) return node
|
||||
return findNode(node.children, rest)
|
||||
}
|
||||
|
||||
function localized(
|
||||
name: { fr: string; en: string } | null | undefined,
|
||||
locale: Locale,
|
||||
): string | undefined {
|
||||
return name?.[locale]
|
||||
}
|
||||
|
||||
/** The name an element shows in a list or a dependents message; its id as a last resort. */
|
||||
export function elementLabel(
|
||||
doc: AnatomicalSheetDocument,
|
||||
key: ElementKey,
|
||||
locale: Locale,
|
||||
): string {
|
||||
switch (key.kind) {
|
||||
case 'sheet':
|
||||
return doc.name[locale]
|
||||
case 'reference':
|
||||
return doc.references.find((r) => r.id === key.id)?.citation ?? key.id
|
||||
case 'region':
|
||||
return (
|
||||
localized(
|
||||
doc.vertebral_config.regions.find((r) => r.code === key.code)?.name,
|
||||
locale,
|
||||
) ?? key.code
|
||||
)
|
||||
case 'category':
|
||||
return (
|
||||
localized(doc.categories.find((c) => c.id === key.id)?.name, locale) ??
|
||||
key.id
|
||||
)
|
||||
case 'view':
|
||||
return (
|
||||
localized(doc.views.find((v) => v.id === key.id)?.name, locale) ??
|
||||
key.id
|
||||
)
|
||||
case 'annotation':
|
||||
return (
|
||||
localized(doc.annotations.find((a) => a.id === key.id)?.name, locale) ??
|
||||
key.id
|
||||
)
|
||||
case 'zone':
|
||||
return (
|
||||
localized(
|
||||
doc.finding_zones.find((z) => z.id === key.id)?.name,
|
||||
locale,
|
||||
) ?? key.id
|
||||
)
|
||||
case 'finding':
|
||||
return findingLabel(doc, key, locale)
|
||||
}
|
||||
}
|
||||
|
||||
function findingLabel(
|
||||
doc: AnatomicalSheetDocument,
|
||||
key: Extract<ElementKey, { kind: 'finding' }>,
|
||||
locale: Locale,
|
||||
): string {
|
||||
const zone = doc.finding_zones.find((z) => z.id === key.zone_id)
|
||||
const found = zone ? findNode(zone.categories, key.path) : undefined
|
||||
return (
|
||||
localized(found?.name, locale) ??
|
||||
key.path[key.path.length - 1] ??
|
||||
key.zone_id
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import { useState } from 'react'
|
||||
import type { EditCommand } from '@faunosteo/core-wasm/types'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import { newElementId, type IdPrefix } from './ids'
|
||||
import { refusalMessage } from './refusal'
|
||||
import { useEditorStore } from './state/editorStore'
|
||||
|
||||
interface FieldError {
|
||||
field: string
|
||||
message: string
|
||||
}
|
||||
|
||||
export interface EditCommandApi {
|
||||
/** The refusal phrased for `field`, to show next to its control. */
|
||||
fieldError: (field: string) => string | undefined
|
||||
/** Runs one command through the store; a refusal is kept for `field`. */
|
||||
run: (field: string, command: EditCommand) => boolean
|
||||
/** Inserts under a fresh id; one regeneration when the core answers `duplicate_key`. */
|
||||
runNew: (
|
||||
field: string,
|
||||
prefix: IdPrefix,
|
||||
build: (id: string) => EditCommand,
|
||||
) => boolean
|
||||
/** A refusal that never reached the core (a required field left blank). */
|
||||
reject: (field: string, messageKey: string) => false
|
||||
clear: () => void
|
||||
}
|
||||
|
||||
/** The write path of an editor panel: commit once, keep the refusal next to the control. */
|
||||
export function useEditCommand(): EditCommandApi {
|
||||
const t = useT()
|
||||
const [error, setError] = useState<FieldError | null>(null)
|
||||
|
||||
const attempt = (command: EditCommand) =>
|
||||
useEditorStore.getState().commit(command)
|
||||
|
||||
const run = (field: string, command: EditCommand): boolean => {
|
||||
const outcome = attempt(command)
|
||||
setError(outcome.ok ? null : { field, message: refusalMessage(t, outcome) })
|
||||
return outcome.ok
|
||||
}
|
||||
|
||||
const runNew: EditCommandApi['runNew'] = (field, prefix, build) => {
|
||||
let outcome = attempt(build(newElementId(prefix)))
|
||||
const clash =
|
||||
!outcome.ok &&
|
||||
outcome.reason === 'core' &&
|
||||
outcome.failure.kind === 'duplicate_key'
|
||||
if (clash) outcome = attempt(build(newElementId(prefix)))
|
||||
setError(outcome.ok ? null : { field, message: refusalMessage(t, outcome) })
|
||||
return outcome.ok
|
||||
}
|
||||
|
||||
return {
|
||||
fieldError: (field) => (error?.field === field ? error.message : undefined),
|
||||
run,
|
||||
runNew,
|
||||
reject: (field, messageKey) => {
|
||||
setError({ field, message: t(messageKey) })
|
||||
return false
|
||||
},
|
||||
clear: () => setError(null),
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user