fix(editor): repair dialog limits, pending drafts block apply, dialog wording
This commit is contained in:
1 parent
3776a33b79
commit
9c21fd3f86
8 files changed
+197
-52
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { useState } from 'react'
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import FormField, { fieldDescribedBy } from '@/components/ui/FormField'
|
||||
import Button from '@/components/ui/Button'
|
||||
import Input from '@/components/ui/Input'
|
||||
@@ -26,6 +26,8 @@ export interface GeometryGroupProps {
|
||||
onApply: (geometry: EditableGeometry) => ApplyResult
|
||||
/** Fields shown as text: read-only mode or an unmeasured view. */
|
||||
locked: boolean
|
||||
/** Told whenever typed values differ from `geometry` (shown « non appliqué »). */
|
||||
onPending?: (pending: boolean) => void
|
||||
}
|
||||
|
||||
type Fields = GroupFields
|
||||
@@ -62,6 +64,13 @@ function useGroupState(geometry: EditableGeometry) {
|
||||
*/
|
||||
export default function GeometryGroup(props: GeometryGroupProps) {
|
||||
const { state, set } = useGroupState(props.geometry)
|
||||
const pending = changedFieldNames(state.draft, props.geometry).size > 0
|
||||
const report = useRef(props.onPending)
|
||||
report.current = props.onPending
|
||||
useEffect(() => {
|
||||
report.current?.(pending)
|
||||
return () => report.current?.(false)
|
||||
}, [pending])
|
||||
const propose = (next: Fields) => {
|
||||
if (sameAsStored(next, props.geometry))
|
||||
return set({ draft: next, messages: [] })
|
||||
@@ -85,7 +94,7 @@ export default function GeometryGroup(props: GeometryGroupProps) {
|
||||
)
|
||||
}
|
||||
|
||||
interface BodyProps extends Omit<GeometryGroupProps, 'onApply'> {
|
||||
interface BodyProps extends Omit<GeometryGroupProps, 'onApply' | 'onPending'> {
|
||||
draft: Fields
|
||||
pending: Set<string>
|
||||
messages: string[]
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useState } from 'react'
|
||||
import { useCallback, useState } from 'react'
|
||||
import type { Annotation, Geometry } from '@faunosteo/core-wasm/types'
|
||||
import Button from '@/components/ui/Button'
|
||||
import Dialog from '@/components/ui/Dialog'
|
||||
@@ -48,10 +48,8 @@ export default function RepairDialog({ offer, ...actions }: RepairDialogProps) {
|
||||
)
|
||||
}
|
||||
|
||||
/** The temporary state of the form: proposals, typed edits and the last refusal. */
|
||||
function useRepairState(offer: RepairOffer, onApplied: () => void) {
|
||||
const t = useT()
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
/** The listed annotations and their temporary geometries (proposals, then typed edits). */
|
||||
function useRepairGeometries(offer: RepairOffer) {
|
||||
const doc = useSheetDocument()
|
||||
const [annotations] = useState(() =>
|
||||
doc ? listedAnnotations(offer, doc) : [],
|
||||
@@ -59,15 +57,50 @@ function useRepairState(offer: RepairOffer, onApplied: () => void) {
|
||||
const [geometries, setGeometries] = useState(() =>
|
||||
initialGeometries(offer, annotations),
|
||||
)
|
||||
const change = (id: string, geometry: Geometry) =>
|
||||
setGeometries((current) => ({ ...current, [id]: geometry }))
|
||||
return { annotations, geometries, change }
|
||||
}
|
||||
|
||||
/** « Attacher et appliquer »: one batch, the refusal kept for the rows. */
|
||||
function useRepairApply(
|
||||
offer: RepairOffer,
|
||||
geometries: Record<string, Geometry>,
|
||||
onApplied: () => void,
|
||||
) {
|
||||
const t = useT()
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
const [result, setResult] = useState<RepairResult | null>(null)
|
||||
const apply = () => {
|
||||
const applied = applyRepair(offer, geometries, { t, locale })
|
||||
if (applied.ok) onApplied()
|
||||
else setResult(applied)
|
||||
}
|
||||
const change = (id: string, geometry: Geometry) =>
|
||||
setGeometries((current) => ({ ...current, [id]: geometry }))
|
||||
return { annotations, geometries, result, apply, change }
|
||||
return { result, apply }
|
||||
}
|
||||
|
||||
/** The annotations whose geometry group holds typed values not yet in the form. */
|
||||
function usePendingIds() {
|
||||
const [ids, setIds] = useState<ReadonlySet<string>>(new Set())
|
||||
const report = useCallback((id: string, pending: boolean) => {
|
||||
setIds((current) => {
|
||||
if (current.has(id) === pending) return current
|
||||
const next = new Set(current)
|
||||
if (pending) next.add(id)
|
||||
else next.delete(id)
|
||||
return next
|
||||
})
|
||||
}, [])
|
||||
return { pending: ids, report }
|
||||
}
|
||||
|
||||
function useRepairState(offer: RepairOffer, onApplied: () => void) {
|
||||
const geometry = useRepairGeometries(offer)
|
||||
const applied = useRepairApply(offer, geometry.geometries, onApplied)
|
||||
const pending = usePendingIds()
|
||||
const blocked =
|
||||
blockedIds(geometry.geometries).length > 0 || pending.pending.size > 0
|
||||
return { ...geometry, ...applied, report: pending.report, blocked }
|
||||
}
|
||||
|
||||
function RepairForm({
|
||||
@@ -89,6 +122,7 @@ function RepairForm({
|
||||
offer={offer}
|
||||
messages={refused?.byAnnotation[annotation.id]}
|
||||
onChange={(geometry) => state.change(annotation.id, geometry)}
|
||||
onPending={state.report}
|
||||
/>
|
||||
))}
|
||||
</ul>
|
||||
@@ -97,11 +131,7 @@ function RepairForm({
|
||||
<Button type="button" variant="ghost" onClick={onCancel}>
|
||||
{t('editor.views.repair.cancel')}
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
disabled={blockedIds(state.geometries).length > 0}
|
||||
onClick={state.apply}
|
||||
>
|
||||
<Button type="button" disabled={state.blocked} onClick={state.apply}>
|
||||
{t('editor.views.repair.apply')}
|
||||
</Button>
|
||||
</div>
|
||||
@@ -128,6 +158,7 @@ interface RowProps {
|
||||
offer: RepairOffer
|
||||
messages: string[] | undefined
|
||||
onChange: (geometry: Geometry) => void
|
||||
onPending: (id: string, pending: boolean) => void
|
||||
}
|
||||
|
||||
function RepairRow({
|
||||
@@ -136,6 +167,7 @@ function RepairRow({
|
||||
offer,
|
||||
messages,
|
||||
onChange,
|
||||
onPending,
|
||||
}: RowProps) {
|
||||
const t = useT()
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
@@ -149,12 +181,13 @@ function RepairRow({
|
||||
data-testid={`repair-${annotation.id}`}
|
||||
className="space-y-2 rounded-lg border border-[var(--color-border)] p-3"
|
||||
>
|
||||
<h4 className="text-sm font-semibold">{title}</h4>
|
||||
<h3 className="text-sm font-semibold">{title}</h3>
|
||||
{geometry && isEditable(geometry) ? (
|
||||
<GeometryGroup
|
||||
idPrefix={`repair-${annotation.id}`}
|
||||
geometry={geometry}
|
||||
locked={false}
|
||||
onPending={(pending) => onPending(annotation.id, pending)}
|
||||
onApply={(next) => {
|
||||
onChange(next)
|
||||
return { ok: true }
|
||||
|
||||
Reference in new issue
Block a user