feat(app): editor views tab, drawing upload and cleaning, guarded crops
This commit is contained in:
1 parent
d163b2ce9a
commit
8343528be9
22 files changed
+2164
-4
No files matched your search
@@ -0,0 +1,53 @@
|
||||
import { useState } from 'react'
|
||||
import {
|
||||
checkFile,
|
||||
uploadDrawing,
|
||||
type Candidate,
|
||||
type Step,
|
||||
type UploadError,
|
||||
} from './drawingUpload'
|
||||
|
||||
export type UploadUi =
|
||||
| { phase: 'idle' }
|
||||
| { phase: 'working' }
|
||||
| { phase: 'cleaning'; candidate: Candidate }
|
||||
| { phase: 'failed'; error: UploadError }
|
||||
| { phase: 'done' }
|
||||
|
||||
export interface DrawingUpload {
|
||||
ui: UploadUi
|
||||
/** A picked file: checked, then sent (clean) or offered for cleaning (cleanable). */
|
||||
start: (file: File) => Promise<void>
|
||||
/** « Nettoyer et utiliser cette version ». */
|
||||
confirmCleaning: () => Promise<void>
|
||||
cancelCleaning: () => void
|
||||
}
|
||||
|
||||
/** The upload of one view's drawing as a small state machine for its section. */
|
||||
export function useDrawingUpload(viewId: string): DrawingUpload {
|
||||
const [ui, setUi] = useState<UploadUi>({ phase: 'idle' })
|
||||
const settle = (result: Step<unknown>) => {
|
||||
if (result.ok) setUi({ phase: 'done' })
|
||||
else if (result.error.code === 'stale') setUi({ phase: 'idle' })
|
||||
else setUi({ phase: 'failed', error: result.error })
|
||||
}
|
||||
const send = async (candidate: Candidate) => {
|
||||
setUi({ phase: 'working' })
|
||||
settle(await uploadDrawing(candidate, viewId))
|
||||
}
|
||||
const start = async (file: File) => {
|
||||
setUi({ phase: 'working' })
|
||||
const checked = await checkFile(file)
|
||||
if (!checked.ok) return settle(checked)
|
||||
if (checked.value.outcome === 'clean') return send(checked.value)
|
||||
setUi({ phase: 'cleaning', candidate: checked.value })
|
||||
}
|
||||
return {
|
||||
ui,
|
||||
start,
|
||||
confirmCleaning: async () => {
|
||||
if (ui.phase === 'cleaning') await send(ui.candidate)
|
||||
},
|
||||
cancelCleaning: () => setUi({ phase: 'idle' }),
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user