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.

8.6 KB
Raw Blame
/**
 * Undo/redo support for mutations.
 *
 * @example
 * // 1. Wrap your app with the provider
 * <MutationUndoProvider>
 *   <App />
 * </MutationUndoProvider>
 *
 * // 2. Push an entry directly and bind side effects (toast, highlight, etc.)
 * //    to its lifecycle hooks.
 * const { pushUndo } = useMutationUndoContext();
 * const handle = pushUndo({
 *   undo: async () => api.update(id, previousValue),
 *   redo: async () => api.update(id, nextValue),
 *   onUndone: () => toast.dismiss(toastId),
 *   onRedone: () => showToast(),
 * });
 *
 * // 3. Target this specific entry from UI (e.g. an Undo button):
 * handle.undo({ onError: (e) => toast.failure(e.message) });
 *
 * // 4. Global LIFO shortcuts (cmd+z / shift+cmd+z) use the stack top:
 * const { undo, redo, canUndo, canRedo } = useMutationUndoContext();
 */

import { createAssertedContextProvider } from '@core/context/createContext';
import type { HotkeyGroup } from '@core/hotkey/types';
import { type MutationOptions, useMutation } from '@tanstack/solid-query';
import { createSignal } from 'solid-js';

type UndoHandler<TVariables, TContext> = (
  variables: TVariables,
  context: TContext | undefined
) => Promise<void> | void;

type UndoEntry = {
  id: string;
  undo: () => Promise<void> | void;
  redo?: () => Promise<void> | void;
  label?: string;
  /** Fires after `undo` resolves, regardless of whether it was triggered by
   *  `handle.undo()` or the global `ctx.undo()` LIFO call. */
  onUndone?: () => void;
  /** Fires after `redo` resolves. */
  onRedone?: () => void;
};

type UndoEntryInput = Omit<UndoEntry, 'id'>;

export type UndoHandle = {
  id: string;
  /** Undo this specific entry, even if it is not at the top of the stack. */
  undo: (callbacks?: UndoCallbacks) => Promise<void>;
  /** Remove this entry from both undo and redo stacks. Lifecycle hooks
   *  do not fire. Calling `undo()` after `dispose()` is a no-op. */
  dispose: () => void;
};

type UndoCallbacks = {
  onSuccess?: () => void;
  onError?: (error: Error) => void;
  onSettled?: () => void;
};

type MutationUndoContextValue = {
  pushUndo: (entry: UndoEntryInput) => UndoHandle;
  canUndo: () => boolean;
  canRedo: () => boolean;
  clear: () => void;
  undo: (callbacks?: UndoCallbacks) => Promise<void>;
  redo: (callbacks?: UndoCallbacks) => Promise<void>;
};

const genId = (): string =>
  typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function'
    ? crypto.randomUUID()
    : `undo-${Date.now()}-${Math.random().toString(36).slice(2)}`;

export const [MutationUndoProvider, useMutationUndoContext] =
  createAssertedContextProvider(
    'MutationUndo',
    (): MutationUndoContextValue => {
      const [undoStack, setUndoStack] = createSignal<UndoEntry[]>([]);
      const [redoStack, setRedoStack] = createSignal<UndoEntry[]>([]);
      const disposed = new WeakSet<UndoEntry>();
      let generation = 0;
      let redoGeneration = 0;

      const runUndo = async (
        entry: UndoEntry,
        callbacks?: UndoCallbacks
      ): Promise<void> => {
        const current = undoStack();
        const prevPos = current.indexOf(entry);
        // Already removed (e.g. handle.undo() called twice, or entry was
        // cleared by a new pushUndo after redoStack reset).
        if (prevPos < 0) return;

        const startedGeneration = generation;
        const startedRedoGeneration = redoGeneration;
        setUndoStack(current.filter((e) => e !== entry));
        try {
          await entry.undo();
          if (disposed.has(entry) || generation !== startedGeneration) return;
          if (entry.redo && redoGeneration === startedRedoGeneration) {
            setRedoStack((prev) => [...prev, entry]);
          }
          entry.onUndone?.();
          callbacks?.onSuccess?.();
        } catch (err) {
          if (disposed.has(entry) || generation !== startedGeneration) return;
          // Restore at original position so a non-top undo that fails doesn't
          // reorder the stack.
          setUndoStack((prev) => {
            const next = [...prev];
            next.splice(Math.min(prevPos, next.length), 0, entry);
            return next;
          });
          callbacks?.onError?.(
            err instanceof Error ? err : new Error(String(err))
          );
        } finally {
          callbacks?.onSettled?.();
        }
      };

      const runRedo = async (
        entry: UndoEntry,
        callbacks?: UndoCallbacks
      ): Promise<void> => {
        const startedGeneration = generation;
        const startedRedoGeneration = redoGeneration;
        // A new action retires the redo branch, including requests already in
        // flight. Their completion must not repopulate either stack or UI.
        const isRetired = () =>
          disposed.has(entry) ||
          generation !== startedGeneration ||
          redoGeneration !== startedRedoGeneration;
        try {
          if (entry.redo) {
            await entry.redo();
          }
          if (isRetired()) return;
          setUndoStack((prev) => [...prev, entry]);
          entry.onRedone?.();
          callbacks?.onSuccess?.();
        } catch (err) {
          if (isRetired()) return;
          setRedoStack((prev) => [...prev, entry]);
          callbacks?.onError?.(
            err instanceof Error ? err : new Error(String(err))
          );
        } finally {
          callbacks?.onSettled?.();
        }
      };

      return {
        pushUndo: (input) => {
          const entry: UndoEntry = { ...input, id: genId() };
          redoGeneration += 1;
          setUndoStack((prev) => [...prev, entry]);
          setRedoStack([]);
          return {
            id: entry.id,
            undo: (callbacks) => runUndo(entry, callbacks),
            dispose: () => {
              disposed.add(entry);
              setUndoStack((prev) => prev.filter((e) => e !== entry));
              setRedoStack((prev) => prev.filter((e) => e !== entry));
            },
          };
        },

        canUndo: () => undoStack().length > 0,
        canRedo: () => redoStack().length > 0,

        clear: () => {
          generation += 1;
          redoGeneration += 1;
          setUndoStack([]);
          setRedoStack([]);
        },

        undo: async (callbacks) => {
          const entry = undoStack().at(-1);
          if (!entry) return;
          await runUndo(entry, callbacks);
        },

        redo: async (callbacks) => {
          const stack = redoStack();
          const entry = stack.at(-1);
          if (!entry) return;
          setRedoStack(stack.slice(0, -1));
          await runRedo(entry, callbacks);
        },
      };
    }
  );

type UndoLifecycle = {
  onUndone?: () => void;
  onRedone?: () => void;
};

export function useUndoableMutation<
  TData = unknown,
  TError = Error,
  TVariables = void,
  TContext = unknown,
>(
  options: () => MutationOptions<TData, TError, TVariables, TContext> & {
    undoFn?: UndoHandler<TVariables, TContext>;
    redoFn?: UndoHandler<TVariables, TContext>;
    /** Static label, or derived per entry from the mutation variables. */
    undoLabel?: string | ((variables: TVariables) => string);
    /** Fires after the entry is pushed onto the undo stack. Returned
     *  lifecycle hooks (onUndone/onRedone) will run on this specific
     *  entry — regardless of whether it is undone via the returned
     *  handle, the global LIFO `ctx.undo()`, or by a later `ctx.redo()`. */
    onPushed?: (
      handle: UndoHandle,
      variables: TVariables,
      context: TContext | undefined
    ) => UndoLifecycle | void;
    /** When provided, each pushed undo entry is disposed (silently
     *  removed from both stacks) when the group is disposed. */
    hotkeyGroup?: HotkeyGroup;
  }
) {
  const { pushUndo } = useMutationUndoContext();

  return useMutation(() => {
    const {
      undoFn,
      redoFn,
      undoLabel,
      onPushed,
      hotkeyGroup,
      onSuccess,
      ...opts
    } = options();
    return {
      ...opts,
      onSuccess: (data, variables, context, mutation) => {
        if (undoFn) {
          const lifecycleRef: { current?: UndoLifecycle } = {};
          const handle = pushUndo({
            undo: () => undoFn(variables, context),
            redo: redoFn ? () => redoFn(variables, context) : undefined,
            label:
              typeof undoLabel === 'function'
                ? undoLabel(variables)
                : undoLabel,
            onUndone: () => lifecycleRef.current?.onUndone?.(),
            onRedone: () => lifecycleRef.current?.onRedone?.(),
          });
          hotkeyGroup?.addDisposer(handle.dispose);
          lifecycleRef.current =
            onPushed?.(handle, variables, context) ?? undefined;
        }
        onSuccess?.(data, variables, context, mutation);
      },
    };
  });
}