Files
bea-edit/src/lib/pending-files.ts
T
2026-10-28 16:09:53 +01:00

38 lines
1.2 KiB
TypeScript

/** Routes that can receive a handoff from another route. */
export type PendingFileTarget = 'bea' | 'pagination';
type QueuedFile = { file: File; target: PendingFileTarget };
let queue: QueuedFile[] = [];
/**
* In-memory handoff between routes (start page → beA workspace, merge →
* pagination). Nothing is uploaded or persisted; the receiving route takes and
* clears the files queued for its own target on mount.
*
* The target keeps a queued ZIP from being consumed — and silently dropped — by
* a tool that only accepts PDFs.
*/
export const queuePendingFiles = (files: File[], target: PendingFileTarget) => {
queue = [...queue, ...files.map((file) => ({ file, target }))];
};
/** Takes (and clears) the files queued for `target`; other targets stay queued. */
export const takePendingFiles = (target: PendingFileTarget): File[] => {
const taken: File[] = [];
const remaining: QueuedFile[] = [];
for (const entry of queue) {
if (entry.target === target) taken.push(entry.file);
else remaining.push(entry);
}
queue = remaining;
return taken;
};
/** Test/storybook helper: forget everything that is still queued. */
export const clearPendingFiles = () => {
queue = [];
};