app

Local-first trade for farms and co-ops
git clone https://radroots.dev/git/app.git
Log | Files | Refs | README | LICENSE

editor-workflow.ts (4271B)


      1 // HC_TEST_ONLY_QUALIFICATION_PORT: genuine production owners/native IndexedDB
      2 // with controlled exercised inbox observations, never live extension/relay Q.
      3 import { mount, unmount } from 'svelte';
      4 import ListingEditor from '../../../src/lib/components/ListingEditor.svelte';
      5 import { makeSetupFixture, fixturePolicy, inbox } from './inbox-setup.ts';
      6 import { inspectNative } from './listing-editor.ts';
      7 import {
      8   createIdentityViewContext,
      9   mountIdentityView,
     10   connectIdentityView,
     11   probeIdentityViewMessaging,
     12   identityViewSession,
     13   closeIdentityView,
     14   disconnectIdentityView,
     15   IDENTITY_VIEW_CONTEXT
     16 } from '../../../src/lib/runtime/view-context.ts';
     17 import {
     18   createInboxSetupView,
     19   checkInboxSetupView,
     20   unlockInboxSetupView,
     21   stopInboxSetupView,
     22   closeInboxSetupView
     23 } from '../../../src/lib/messaging/inbox-setup-view.ts';
     24 import {
     25   openListingEditor,
     26   editListingEditor,
     27   listingEditorSnapshot,
     28   closeListingEditor,
     29   type ListingEditor as Editor
     30 } from '../../../src/lib/publishing/listing-editor.ts';
     31 import {
     32   openEditorReview,
     33   editorReviewSnapshot,
     34   closeEditorReview,
     35   type EditorReview
     36 } from '../../../src/lib/publishing/editor-workflow.ts';
     37 import type { PublicDraftForm } from '../../../src/lib/contracts/local-records.ts';
     38 export async function createFixture(qualified = true) {
     39   const f = await makeSetupFixture([['relay', inbox]]),
     40     context = createIdentityViewContext();
     41   mountIdentityView(context);
     42   await connectIdentityView(context);
     43   await probeIdentityViewMessaging(context, 'reviewed_self_copy');
     44   const identity = identityViewSession(context);
     45   if (!identity) throw Error('original editor session unavailable');
     46   const setup = createInboxSetupView({
     47     identity,
     48     policy: fixturePolicy(),
     49     lookup: () => Promise.resolve(f.resolve().resolver),
     50     ...(qualified
     51       ? {
     52           observeAccess: (
     53             value: import('../../../src/lib/messaging/readiness.ts').OwnInboxAccessContext
     54           ) => ({
     55             ...value,
     56             readTargets: value.readTargets.slice(),
     57             archiveTargets: value.archiveTargets.slice(),
     58             receive: 'qualified_exercised' as const,
     59             archive: 'qualified_exercised' as const,
     60             current: () => true
     61           })
     62         }
     63       : {})
     64   });
     65   if (!setup) throw Error('original inbox setup unavailable');
     66   await checkInboxSetupView(setup);
     67   await unlockInboxSetupView(setup);
     68   let editor: Editor | undefined, review: EditorReview | undefined;
     69   let mounted: ReturnType<typeof mount> | undefined,
     70     target: HTMLElement | undefined;
     71   return {
     72     owner: f.owner,
     73     counts: f.counts,
     74     inspect: inspectNative,
     75     async open() {
     76       const result = await openListingEditor(context, { mode: 'new' });
     77       if (result.status !== 'ready') throw Error(result.reason);
     78       editor = result.editor;
     79       return listingEditorSnapshot(editor);
     80     },
     81     edit: (form: PublicDraftForm) =>
     82       editor ? editListingEditor(editor, form) : Promise.resolve(false),
     83     async review(form: PublicDraftForm, copied = false) {
     84       if (!editor) throw Error('original editor unavailable');
     85       const result = await openEditorReview(
     86         copied ? { ...editor } : editor,
     87         setup,
     88         form
     89       );
     90       if (result.status === 'review') review = result.review;
     91       return result.status === 'review'
     92         ? {
     93             status: result.status,
     94             snapshot: editorReviewSnapshot(result.review)
     95           }
     96         : result;
     97     },
     98     snapshot: (copied = false) =>
     99       review && editorReviewSnapshot(copied ? { ...review } : review),
    100     stop: () => stopInboxSetupView(setup),
    101     disconnect: () => disconnectIdentityView(context),
    102     render() {
    103       target = document.createElement('div');
    104       document.body.append(target);
    105       mounted = mount(ListingEditor, {
    106         target,
    107         props: { route: { mode: 'new' }, inboxController: setup },
    108         context: new Map([[IDENTITY_VIEW_CONTEXT, context]])
    109       });
    110     },
    111     async close() {
    112       if (mounted) await unmount(mounted);
    113       target?.remove();
    114       if (review) closeEditorReview(review);
    115       if (editor) closeListingEditor(editor);
    116       closeInboxSetupView(setup);
    117       closeIdentityView(context);
    118       f.close();
    119     }
    120   };
    121 }