feat(app): editor store, undo, recovery copy and geometry accept rules

This commit is contained in:
bchanot committed 2026-11-04 12:13:10 +01:00
1 parent f7e7bf4ee1
commit 7f1d44065b
20 files changed
+2538 -5

No files matched your search

+92 -4
View File
@@ -62,6 +62,35 @@ export type VersionLoad =
}
| { ok: false; failure: CatalogueFailure }
/** One drawing as `downloadSheetDrawing` settled it: bytes, or the closed failure. */
export type DrawingFetch =
| { ok: true; drawing: PreviewDrawing }
| { ok: false; assetId: string; sha256: string; failure: CatalogueFailure }
type DrawingEntry = VersionAnswer['drawings'][number]
/** Never rejects: a drawing Storage does not give is missing, the version answered. */
export async function fetchDrawing(
client: FaunosteoClient,
entry: DrawingEntry,
): Promise<DrawingFetch> {
try {
const bytes = await downloadSheetDrawing(client, entry.sha256)
return {
ok: true,
drawing: { assetId: entry.asset_id, sha256: entry.sha256, bytes },
}
} catch (error) {
const failure = serverFailure(error)
return {
ok: false,
assetId: entry.asset_id,
sha256: entry.sha256,
failure: failure === 'denied' ? 'drawing_missing' : failure,
}
}
}
async function downloadDrawings(
client: FaunosteoClient,
version: VersionAnswer,
@@ -79,16 +108,75 @@ type VersionRead =
| { ok: true; version: VersionAnswer; drawings: PreviewDrawing[] }
| { ok: false; failure: CatalogueFailure }
async function readVersion(
const DraftAnswerSchema = VersionAnswerSchema.extend({
revision: z.number().int().nonnegative(),
})
export type DraftAnswer = z.infer<typeof DraftAnswerSchema>
/** The editor's read: any status, the revision of the same answer, settled drawings. */
export type DraftRead =
| {
ok: true
version: DraftAnswer
revision: number
drawings: DrawingFetch[]
}
| { ok: false; failure: CatalogueFailure }
export interface DraftReadOptions {
draft: true
/** Asset ids the caller already holds: not downloaded again. */
have?: ReadonlySet<string>
}
async function readAnswer<T>(
client: FaunosteoClient,
versionId: string,
): Promise<VersionRead> {
let version: VersionAnswer
schema: z.ZodType<T>,
): Promise<{ ok: true; version: T } | { ok: false; failure: CatalogueFailure }> {
try {
version = parsed(VersionAnswerSchema, await getSheetVersion(client, versionId))
return { ok: true, version: parsed(schema, await getSheetVersion(client, versionId)) }
} catch (error) {
return { ok: false, failure: serverFailure(error) }
}
}
async function readDraft(
client: FaunosteoClient,
versionId: string,
have: ReadonlySet<string>,
): Promise<DraftRead> {
const answer = await readAnswer(client, versionId, DraftAnswerSchema)
if (!answer.ok) return answer
const { version } = answer
const wanted = version.drawings.filter((entry) => !have.has(entry.asset_id))
const drawings = await Promise.all(wanted.map((e) => fetchDrawing(client, e)))
return { ok: true, version, revision: version.revision, drawings }
}
/**
* One version as the server gave it. Catalogue mode: published and deprecated only, every
* drawing or none. Draft mode (the editor): any status, `revision` from the same answer,
* each drawing settled alone so one bad drawing never blocks authoring.
*/
export async function readVersion(
client: FaunosteoClient,
versionId: string,
): Promise<VersionRead>
export async function readVersion(
client: FaunosteoClient,
versionId: string,
options: DraftReadOptions,
): Promise<DraftRead>
export async function readVersion(
client: FaunosteoClient,
versionId: string,
options?: DraftReadOptions,
): Promise<VersionRead | DraftRead> {
if (options) return readDraft(client, versionId, options.have ?? new Set())
const answer = await readAnswer(client, versionId, VersionAnswerSchema)
if (!answer.ok) return answer
const { version } = answer
// The catalogue reads published and deprecated versions only (§11.1).
if (version.status === 'draft') return { ok: false, failure: 'denied' }
try {