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 { fireAndForget } from '@core/util/fireAndForget';
import {
  type Accessor,
  createEffect,
  createMemo,
  createResource,
  createSignal,
  onCleanup,
} from 'solid-js';
import { match } from 'ts-pattern';
import type {
  EmailConnectivity,
  EmailDraftStorage,
} from '../context/compose-capabilities';
import type { DraftSyncViewState } from '../core/local-draft';

/** Recovery reads only metadata; file blobs are restored once when opening a draft. */
export function createDraftSyncStatus(options: {
  drafts: EmailDraftStorage;
  connectivity: EmailConnectivity;
  draftId: Accessor<string | undefined>;
  localSaveState: Accessor<'saving' | 'saved' | 'failed'>;
  retry(): Promise<unknown>;
  discard(): Promise<unknown>;
}) {
  const [busy, setBusy] = createSignal(false);
  const [actionError, setActionError] = createSignal<string>();
  const [saved, { refetch }] = createResource(
    () => options.drafts.readDraft && options.draftId(),
    async (id) => await options.drafts.readDraft!(id, { attachments: false })
  );
  createEffect(() => {
    const unsubscribe = options.drafts.watchDrafts?.(() => {
      fireAndForget(refetch(), 'email-compose');
    });
    onCleanup(() => unsubscribe?.());
  });
  const local = () =>
    saved.state === 'ready' || saved.state === 'refreshing'
      ? saved.latest?.local
      : undefined;
  const deletePending = () =>
    local()?.status === 'delete-failed' ||
    (local()?.status === 'deleting' &&
      (!local()?.queuedAttemptId ||
        local()?.queuedAttemptId !== local()?.latestAttemptId));
  // Saving is silent online. Offline, edits wait on this device.
  const offlineState = (): DraftSyncViewState | undefined => {
    const draft = local();
    if (!draft || draft.acknowledgedRevision >= draft.revision) return;
    if (!options.connectivity.looksOffline()) return;
    return {
      message: 'Saved on this device; waiting to sync',
      detail: 'Changes stay on this device until you are back online.',
      failed: false,
      canKeepEditing: false,
    };
  };
  const state = createMemo<DraftSyncViewState | undefined>(() => {
    if (!options.drafts.saveLocalDraft || !options.draftId()) return;
    const disk = options.localSaveState();
    const draft = local();
    if (saved.error)
      return {
        message: 'Unable to read local draft status.',
        detail: 'Retry to check the saved draft.',
        failed: true,
        action: 'retry',
        canKeepEditing: false,
      };
    const failed =
      disk === 'failed' ||
      ['failed', 'unconfirmed', 'delete-failed'].includes(
        draft?.status ?? ''
      ) ||
      deletePending();
    const availability = options.drafts.syncAvailability?.() ?? 'ready';
    if (disk !== 'failed' && availability !== 'ready') {
      return {
        message: draft
          ? 'Saved on this device; waiting to sync'
          : 'Draft synchronization unavailable',
        detail:
          availability === 'recovering'
            ? 'Reconnecting to saved changes…'
            : availability === 'recovery-required'
              ? 'Queued changes are preserved. Update or reload Macro to recover local storage.'
              : 'Reconnect or retry to resume draft synchronization.',
        failed: true,
        action: match(availability)
          .with('unavailable', () => 'retry' as const)
          .with('recovery-required', () => 'reload' as const)
          .with('recovering', () => undefined)
          .exhaustive(),
        canKeepEditing: false,
      };
    }
    if (!failed) return offlineState();
    return {
      message:
        disk === 'failed'
          ? 'Changes could not be saved on this device'
          : 'Draft could not be synced',
      detail:
        disk === 'failed'
          ? 'Keep this editor open until saving succeeds.'
          : deletePending()
            ? 'Discard could not be confirmed.'
            : draft?.status === 'unconfirmed'
              ? 'The last save could not be confirmed.'
              : draft?.status === 'failed'
                ? 'The server could not save this draft.'
                : undefined,
      failed,
      action: deletePending() ? 'retry-discard' : 'retry',
      canKeepEditing: deletePending(),
    };
  });
  const run = async (action: () => Promise<unknown>) => {
    if (busy()) return;
    setBusy(true);
    setActionError(undefined);
    try {
      await options.drafts.recoverSynchronization?.();
      await action();
      await refetch();
    } catch (error) {
      setActionError(error instanceof Error ? error.message : String(error));
    } finally {
      setBusy(false);
    }
  };
  return {
    state,
    busy,
    error: () =>
      actionError() ??
      (saved.error
        ? 'Unable to read local draft status. Retry to check the saved draft.'
        : undefined),
    retry: () => {
      fireAndForget(
        run(
          saved.error
            ? async () => {
                await refetch();
              }
            : deletePending()
              ? options.discard
              : options.retry
        ),
        'email-compose'
      );
    },
    keepEditing: () => {
      fireAndForget(run(options.retry), 'email-compose');
    },
  };
}