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.

2.8 KB
Raw Blame
import type { Message as EntityMessage } from '@service-storage/messages';

type ThreadPreviewState = {
  preview: EntityMessage[];
  reply_count: number;
  latest_reply_at?: string | null;
};

type ThreadPreviewReplySnapshot = {
  previewIndex: number;
  reply: EntityMessage;
};

export function insertReplyIntoThreadPreview(
  thread: ThreadPreviewState,
  reply: EntityMessage
): ThreadPreviewState {
  if (thread.preview.some((previewReply) => previewReply.id === reply.id)) {
    return thread;
  }

  return {
    ...thread,
    latest_reply_at: reply.created_at,
    reply_count: thread.reply_count + 1,
    preview: [...thread.preview, reply],
  };
}

export function removeReplyFromThreadPreview(
  thread: ThreadPreviewState,
  replyId: string
): ThreadPreviewState {
  const nextPreview = thread.preview.filter((reply) => reply.id !== replyId);
  const didRemovePreview = nextPreview.length !== thread.preview.length;
  if (!didRemovePreview && thread.reply_count === 0) {
    return thread;
  }

  return {
    ...thread,
    latest_reply_at: didRemovePreview
      ? (nextPreview.at(-1)?.created_at ?? null)
      : thread.latest_reply_at,
    reply_count: Math.max(thread.reply_count - 1, 0),
    preview: nextPreview,
  };
}

export function replaceReplyCreatedAtInThreadPreview(
  thread: ThreadPreviewState,
  replyIds: readonly string[],
  createdAt: string
): ThreadPreviewState {
  if (replyIds.length === 0) return thread;
  const ids = new Set(replyIds);
  let didChange = false;
  const preview = thread.preview.map((reply) => {
    if (!ids.has(reply.id) || reply.created_at === createdAt) return reply;
    didChange = true;
    return { ...reply, created_at: createdAt };
  });

  return didChange ? { ...thread, preview } : thread;
}

export function captureThreadPreviewReplySnapshot(
  thread: ThreadPreviewState,
  replyId: string
): ThreadPreviewReplySnapshot | undefined {
  const previewIndex = thread.preview.findIndex(
    (reply) => reply.id === replyId
  );
  if (previewIndex === -1) return undefined;

  return {
    previewIndex,
    reply: thread.preview[previewIndex],
  };
}

export function restoreReplyToThreadPreview(
  thread: ThreadPreviewState,
  snapshot?: ThreadPreviewReplySnapshot,
  replyCreatedAt?: string
): ThreadPreviewState {
  if (
    snapshot &&
    thread.preview.some((reply) => reply.id === snapshot.reply.id)
  ) {
    return thread;
  }

  const preview = [...thread.preview];
  if (snapshot) {
    preview.splice(snapshot.previewIndex, 0, snapshot.reply);
  }

  return {
    ...thread,
    preview,
    reply_count: thread.reply_count + 1,
    latest_reply_at:
      [thread.latest_reply_at, replyCreatedAt]
        .filter((value): value is string => !!value)
        .sort()
        .at(-1) ??
      preview.at(-1)?.created_at ??
      null,
  };
}