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.

1.3 KB
Raw Blame
import type { Accessor } from 'solid-js';
import { type EmailTab, isExcludedEmailFolder } from '../constants';

type FolderRestoreOptions = {
  tab: Accessor<EmailTab>;
  isPending: Accessor<boolean>;
  /** Stable requested-key snapshot, replaced only when selection changes. */
  selection: Accessor<ReadonlySet<string>>;
  clearSelection: () => void;
  restore: (input: {
    folder: 'spam' | 'trash';
    threadIds: string[];
  }) => Promise<{ failedIds: string[] }>;
  onRestored: () => void;
  onFailed: (count: number) => void;
};

/** Restore the captured folder without clearing a newer list selection. */
export function createEmailFolderRestore(options: FolderRestoreOptions) {
  return async (
    threadIds: string[],
    action: { clearSelection?: boolean } = {}
  ) => {
    const folder = options.tab();
    if (!isExcludedEmailFolder(folder) || options.isPending()) return;
    const selection = options.selection();
    try {
      const result = await options.restore({ folder, threadIds });
      if (result.failedIds.length) {
        options.onFailed(result.failedIds.length);
      } else {
        if (
          action.clearSelection !== false &&
          options.selection() === selection
        )
          options.clearSelection();
        options.onRestored();
      }
    } catch {
      options.onFailed(threadIds.length);
    }
  };
}