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.

9.8 KB
Raw Blame
import { getDisplayName, tryMacroId } from '@core/user';
import { ThrownResultError } from '@core/util/result';
import {
  buildDiffState,
  buildWhoMap,
  diffStates,
} from '@macro-inc/lexical-core';
import { useDocumentPeersQuery } from '@queries/sync/document-peers';
import type { HistorySession, HistoryVersionId } from '@service-sync/client';
import { syncServiceClient } from '@service-sync/client';
import type { SerializedEditorState } from 'lexical';
import { LoroDoc } from 'loro-crdt';
import {
  type Accessor,
  createContext,
  createMemo,
  createResource,
  createSignal,
  type JSX,
  mapArray,
  useContext,
} from 'solid-js';
import { type PeerRecord, peerAuthorId, useAuthorName } from './author';
import { sessionize } from './sessionize';
import { buildTimestampIndex } from './timestampIndex';
import { userColor, userLabel } from './utils';

export type HistoryUser = {
  userId: string;
  displayName: () => string;
  color: string;
};

function describeError(error: unknown): string {
  if (error instanceof ThrownResultError) {
    const [first] = error.errors;
    return first?.description ?? error.message;
  }
  if (error instanceof Error) return error.message;
  return String(error);
}

type HistoryContextValue = {
  isOpen: Accessor<boolean>;
  selectedAt: Accessor<Date | null>;
  isLive: Accessor<boolean>;
  open: () => void;
  enter: (at?: Date) => void;
  exit: () => void;
  requestLoad: () => void;
  sessions: Accessor<readonly HistorySession[]>;
  loading: { sessions: Accessor<boolean>; doc: Accessor<boolean> };
  /** Underlying failure message when the snapshot or peer map didn't load. */
  error: Accessor<string | null>;
  checkoutAt: (ms: number) => SerializedEditorState | null;
  versionIdAt: (ms: number) => HistoryVersionId | null;
  diff: {
    session: Accessor<HistorySession | null>;
    previewState: Accessor<SerializedEditorState | null>;
    view: (session: HistorySession) => void;
    clear: () => void;
  };
  userByPeer: (peerId: string) => HistoryUser;
  userById: (userId: string) => HistoryUser;
};

const HistoryContext = createContext<HistoryContextValue>();

export function HistoryProvider(props: {
  documentId: Accessor<string>;
  children: JSX.Element;
}) {
  const [isOpen, setIsOpen] = createSignal(false);
  const [selectedAt, setSelectedAt] = createSignal<Date | null>(null);
  const [isLive, setIsLive] = createSignal(true);
  const [diffSession, setDiffSession] = createSignal<HistorySession | null>(
    null
  );
  const [shouldLoad, setShouldLoad] = createSignal(false);
  const requestLoad = () => setShouldLoad(true);

  const open = () => setIsOpen(true);

  const enter = (at?: Date) => {
    setDiffSession(null);
    setSelectedAt(at ?? null);
    setIsLive(at === undefined);
    setIsOpen(true);
  };

  const exit = () => {
    setIsOpen(false);
    setSelectedAt(null);
    setIsLive(true);
    setDiffSession(null);
  };

  // Park the scrubber at the session's end and flip into diff mode. The overlay
  // renders `diffPreviewState` while a diff is active.
  const viewSessionDiff = (session: HistorySession) => {
    setSelectedAt(new Date(session.endMs));
    setIsLive(false);
    setIsOpen(true);
    setDiffSession(session);
  };

  // Download the full snapshot once and drive both the session timeline and
  // local scrubbing from it. The full snapshot (not updates) is required so
  // getAllChanges() carries the per-change timestamp metadata.
  const [historyDoc] = createResource(
    () => (isOpen() || shouldLoad() ? props.documentId() : undefined),
    async (documentId) => {
      const result = await syncServiceClient.getSnapshot({ documentId });
      if (result.isErr()) throw new ThrownResultError(result.error);
      const doc = new LoroDoc();
      doc.import(result.value);
      return doc;
    }
  );

  // Peer -> user mapping for labelling sessions; lightweight JSON.
  const peerMap = useDocumentPeersQuery(() =>
    isOpen() || shouldLoad() ? props.documentId() : ''
  );

  const resolvePeerUser = (
    peers: Map<string, PeerRecord>,
    peer: string
  ): string => peerAuthorId(peer, peers.get(peer));

  // Non-suspending reads of the loaded doc and peer map. Reading the resource
  // (`historyDoc()`) or the query.data while they're still pending bubbles
  // a Suspense trigger. This Provider is mounted above places where a simple
  // suspense on the history UI would work.
  const loadedDoc = (): LoroDoc | undefined =>
    historyDoc.state === 'ready' || historyDoc.state === 'refreshing'
      ? historyDoc.latest
      : undefined;
  const loadedPeers = (): Map<string, PeerRecord> | undefined =>
    peerMap.isSuccess ? peerMap.data : undefined;

  // Either fetch failing leaves the timeline permanently empty, so surface it
  // instead of leaving the UI to guess between "still loading" and "no edits".
  const error = createMemo<string | null>(() => {
    if (historyDoc.error !== undefined) return describeError(historyDoc.error);
    if (peerMap.isError) return describeError(peerMap.error);
    return null;
  });

  // One stable display-name accessor per unique author, batched into one fetch.
  const uniqueUserIds = createMemo(() => {
    const peers = loadedPeers();
    if (!peers) return [];
    return [
      ...new Set(
        [...peers].map(([peer, record]) => peerAuthorId(peer, record))
      ),
    ];
  });
  const userEntries = mapArray(uniqueUserIds, (userId) => {
    const displayName = useAuthorName(
      () => userId,
      (id) => getDisplayName(tryMacroId(id), { emailFallback: 'local-part' })
    );
    return { userId, displayName, color: userColor(userId) };
  });
  const userById = (userId: string): HistoryUser =>
    userEntries().find((e) => e.userId === userId) ?? {
      userId,
      displayName: () => userLabel(userId),
      color: userColor(userId),
    };
  const userByPeer = (peerId: string): HistoryUser =>
    userById(resolvePeerUser(loadedPeers() ?? new Map(), peerId));

  const historyIndex = createMemo(() => {
    const doc = loadedDoc();
    return doc ? buildTimestampIndex(doc) : null;
  });

  // Sessions derived locally from the oplog: one edit event per change, grouped
  // per user.
  const sessions = createMemo<readonly HistorySession[]>(() => {
    const doc = loadedDoc();
    const peers = loadedPeers();
    if (!doc || !peers) return [];

    const events: { userId: string; tMs: number }[] = [];
    for (const [peer, changes] of doc.getAllChanges()) {
      const userId = resolvePeerUser(peers, peer);
      for (const change of changes) {
        events.push({ userId, tMs: change.timestamp * 1000 });
      }
    }

    return sessionize(events);
    // TODO (seamus/wolf): this is expensive. on 55 bones test doc each
    // each inter of the filter loop took avg 6 seconds.
    // const index = historyIndex();
    // if (!index) return sessionize(events);
    // return sessionize(events).filter((s) => {
    //   const before = index.checkoutAt(s.startMs - 1);
    //   const after = index.checkoutAt(s.endMs);
    //   // Can't compute both states (edge frontiers) — keep rather than hide.
    //   if (!before || !after) return true;
    //   return (
    //     // HACK: basically, some deltas are not visually different, and that's confusing
    //     serializedEditorStateToMarkdown(before) !==
    //     serializedEditorStateToMarkdown(after)
    //   );
    // });
  });

  const checkoutAt = (ms: number): SerializedEditorState | null =>
    historyIndex()?.checkoutAt(ms) ?? null;

  const versionIdAt = (ms: number): HistoryVersionId | null =>
    historyIndex()?.versionIdAt(ms) ?? null;

  // Diff a session: the state just before its first edit vs the state at its end,
  // paired by node id. Each changed block is attributed to whoever last edited it
  // (via buildWhoMap over the loro op history), falling back to the session author.
  const diffPreviewState = createMemo<SerializedEditorState | null>(() => {
    const session = diffSession();
    if (!session) return null;
    const before = checkoutAt(session.startMs - 1);
    const after = checkoutAt(session.endMs);
    if (!before || !after) return null;
    // Checking out an arbitrary frontier can yield a structurally-incomplete
    // lexical state (empty root, or a node whose `type` isn't set yet — the same
    // states that break the plain scrubbing preview). Skip and degrade to "no
    // diff" rather than crashing the overlay.
    if (!before.root.children?.length || !after.root.children?.length)
      return null;
    try {
      const doc = loadedDoc();
      const peers = loadedPeers();
      const whoMap =
        doc && peers
          ? buildWhoMap(doc, (peer) => resolvePeerUser(peers, peer))
          : new Map<string, string>();
      const diffs = diffStates(
        before,
        after,
        (id) => whoMap.get(id) ?? session.userId
      );
      return buildDiffState(before, diffs);
    } catch (error) {
      console.warn('[history] could not build the session diff', error);
      return null;
    }
  });

  const value: HistoryContextValue = {
    isOpen,
    selectedAt,
    isLive,
    open,
    enter,
    exit,
    requestLoad,
    sessions,
    loading: {
      // A failed load is not a pending one — `error` owns that state.
      sessions: () =>
        error() == null && (loadedDoc() == null || peerMap.isPending),
      doc: () => error() == null && loadedDoc() == null,
    },
    error,
    checkoutAt,
    versionIdAt,
    userByPeer,
    userById,
    diff: {
      session: diffSession,
      previewState: diffPreviewState,
      view: viewSessionDiff,
      clear: () => {
        setDiffSession(null);
        setSelectedAt(null);
        setIsLive(true);
      },
    },
  };

  return (
    <HistoryContext.Provider value={value}>
      {props.children}
    </HistoryContext.Provider>
  );
}

export function useHistory() {
  const context = useContext(HistoryContext);
  if (!context)
    throw new Error('useHistory must be used inside HistoryProvider');
  return context;
}