Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

5.1 KB
Raw Blame
import { toast } from '@core/component/Toast/Toast';
import { ToastRegion } from '@core/component/Toast/ToastRegion';
import ExclamationIcon from '@icon/system/regular/exclamation-mark.svg?component-solid';
import { createSignal, Show } from 'solid-js';
import type {
  EmailDraftStorage,
  SaveEmailDraft,
} from '../context/compose-capabilities';
import { decodeBase64Utf8 } from '../core/decode-base64';
import { createEmailComposer } from '../primitives/email-composer';
import { createEmailEditor, setEmailEditorText } from '../tests/editor';

/** Real controller and toast UI, with an injected durable-save transport. */
function RecoveryEditor() {
  const [saved, setSaved] = createSignal<SaveEmailDraft[]>([]);
  let changed: Parameters<NonNullable<EmailDraftStorage['watchDrafts']>>[0] =
    () => {};
  const editor = createEmailEditor();
  const state = createEmailComposer({
    drafts: {
      transferDraft: async () => ({
        draftId: 'moved-draft',
        threadId: 'moved-thread',
        attachments: [],
      }),
      readDraft: async () => undefined,
      watchDrafts: (callback) => {
        changed = callback;
        return () => {
          changed = () => {};
        };
      },
      saveDraft: async (input) => {
        setSaved((previous) => [...previous, input]);
        return {
          draftId: input.clientHandles?.draftId,
          threadId: input.clientHandles?.threadId,
          inboxId: 'inbox',
          persistence: 'queued',
        };
      },
      deleteDraft: async () => {},
      restoreDraft: async () => {},
    },
    attachmentStorage: {
      uploadAttachments: async () => {},
      addForwardedAttachments: async () => {},
      completeAttachment: async () => {},
      removeAttachment: async () => {},
      removeForwardedAttachment: async () => {},
    },
    delivery: {
      sendMessage: async () => ({ inboxId: 'inbox' }),
      unschedule: async () => {},
      schedule: async () => {},
      archive: async () => {},
      undoSend: async () => {},
    },
    draftLifecycle: {
      observe: () => ({
        state: () => undefined,
        refresh: async () => undefined,
      }),
    },
    notices: {
      feedback: {
        ...toast,
        failure: (message, options) => {
          if (options?.persistent)
            return toast.custom(
              {
                title: message,
                content: () => options.subtext,
                icon: ExclamationIcon,
                color: 'var(--color-failure)',
                actions: options.actions,
              },
              { persistent: true }
            );
          toast.failure(message, options);
          return undefined;
        },
      },
      blockingNotice: async () => {},
      confirm: async () => false,
      reportError: () => {},
    },
    accounts: {
      inboxes: () => [
        { id: 'inbox', email_address: '[email protected]', settings: {} },
      ],
      loading: () => false,
      failed: () => false,
      primaryId: () => 'inbox',
    },
    connectivity: { looksOffline: () => false },
    editorFiles: { uploadDocuments: async () => [], makePublic: () => {} },
    viewerEmail: () => '[email protected]',
    hasPaidAccess: () => true,
    recipients: () => [],
    recipientName: (id) => id,
    initialTo: ['[email protected]'],
  });
  state.context.captureEditor(editor);
  state.context.onContentChange('');
  return (
    <section aria-label="Draft recovery">
      <label>
        Message
        <textarea
          aria-label="Message"
          class="block border border-edge-muted p-2"
          onInput={(event) => {
            const text = event.currentTarget.value;
            setEmailEditorText(editor, text);
            state.context.onContentChange(text);
          }}
        />
      </label>
      <button
        onClick={() =>
          changed({
            mutationUuid: saved().at(-1)?.clientHandles?.draftId,
            failed: true,
          })
        }
      >
        Reject background save
      </button>
      <button
        onClick={() =>
          changed({
            mutationUuid: saved().at(-1)?.clientHandles?.draftId,
            failed: true,
            code: 'DRAFT_ALREADY_SENT',
          })
        }
      >
        Report already sent
      </button>
      <button onClick={() => toast.success('Copied')}>
        Show transient notice
      </button>
      <output data-testid="save-count">{saved().length}</output>
      <output data-testid="has-draft">
        {String(state.context.hasDraft())}
      </output>
      <output data-testid="saved-draft-ids">
        {JSON.stringify(saved().map((input) => input.clientHandles?.draftId))}
      </output>
      <output data-testid="saved-body">
        {decodeBase64Utf8(saved().at(-1)?.draft.body_html ?? '')}
      </output>
    </section>
  );
}

export function DraftRecoveryFixture() {
  const [open, setOpen] = createSignal(true);
  return (
    <main class="min-h-screen bg-surface p-4 text-ink">
      <button onClick={() => setOpen(false)}>Close composer</button>
      <Show when={open()}>
        <RecoveryEditor />
      </Show>
      <ToastRegion />
    </main>
  );
}