fix(app): roll the preview back on a throwing accept or a non-trap failure

This commit is contained in:
bchanot committed 2026-11-04 11:50:59 +01:00
1 parent 173be5949a
commit bc43a1b86b
3 files changed
+85 -34

No files matched your search

+26 -20
View File
@@ -28,22 +28,17 @@ export interface CleaningPreview {
const NAME = { fr: 'Aperçu', en: 'Preview' }
/**
* A throwaway draft holding only the cleaned drawing, so the dialog renders the core's
* composed tree and never the original file. The caller frees `preview` on close.
*/
export function composeCleaningPreview(
drawing: StoredDrawing,
): CoreOutcome<CleaningPreview> {
const runtime = coreRuntime()
const empty = runtime.call<unknown>(
/** The throwaway document: the empty sheet plus one detail view naming the drawing. */
function cleaningDocument(
viewId: string,
assetId: string,
): CoreOutcome<string> {
const empty = coreRuntime().call<unknown>(
'sheet.empty',
JSON.stringify({ species_id: 'preview', name: NAME }),
)
if (!empty.ok) return empty
const assetId = generateUuid()
const viewId = newElementId('view')
const edited = applyEdit(JSON.stringify(empty.value), {
return applyEdit(JSON.stringify(empty.value), {
op: 'insert',
element: {
kind: 'view',
@@ -57,17 +52,28 @@ export function composeCleaningPreview(
},
},
})
if (!edited.ok) return edited
}
/**
* A throwaway draft holding only the cleaned drawing, so the dialog renders the core's
* composed tree and never the original file. The caller frees `preview` on close.
*/
export function composeCleaningPreview(
drawing: StoredDrawing,
): CoreOutcome<CleaningPreview> {
const assetId = generateUuid()
const viewId = newElementId('view')
const document = cleaningDocument(viewId, assetId)
if (!document.ok) return document
const preview = new CataloguePreview(
runtime,
{ status: 'draft', document: edited.value },
coreRuntime(),
{ status: 'draft', document: document.value },
[],
)
const bytes = new TextEncoder().encode(drawing.stored)
const added = preview.addDrawing({ assetId, sha256: drawing.sha256, bytes })
if (!added.ok) {
preview.free()
return added
}
return { ok: true, value: { preview, viewId, scope: `fv.${viewId}.` } }
if (added.ok)
return { ok: true, value: { preview, viewId, scope: `fv.${viewId}.` } }
preview.free()
return added
}