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.

3.9 KB
Raw Blame
import type {
  NotificationInboxEntryFieldsFragment,
  NotificationInboxPatchFieldsFragment,
  NotificationInboxRowType,
} from '@service-storage/graphql/generated/graphql';

type WireRow = NotificationInboxEntryFieldsFragment['rows'][number];

type RowFixture = {
  id: string;
  rowType?: NotificationInboxRowType;
  latestAt: string;
  revision?: string;
  unseen?: boolean;
  notificationIds?: string[];
  actor?: WireRow['actor'];
  actions?: WireRow['actions'];
};

/** A document comment notification as the GraphQL fragments select it. */
export function commentNotification(
  docId: string,
  id: string,
  createdAt: string
) {
  return {
    id,
    eventType: 'commented_on_document',
    entityType: 'DOCUMENT',
    targetEntityType: 'DOCUMENT',
    entityId: docId,
    sent: true,
    state: 'UNSEEN',
    createdAt,
    viewedAt: null,
    updatedAt: createdAt,
    senderId: 'macro|[email protected]',
    metadata: {
      __typename: 'GraphqlCommentedOnDocumentMetadata',
      commentedOnDocumentDocumentName: 'Plan',
      commentedOnDocumentOwner: 'macro|[email protected]',
      commentedOnDocumentFileType: 'md',
      commentedOnDocumentSubType: null,
      commentedOnDocumentCommentId: id,
      commentedOnDocumentThreadId: id,
      commentedOnDocumentText: 'looks good',
      commentedOnDocumentSenderProfilePictureUrl: null,
    },
  };
}

/** A document's inbox entry, with the given rows. */
export function inboxEntry(fixture: {
  docId: string;
  sortAt: string;
  rows: RowFixture[];
}): NotificationInboxEntryFieldsFragment {
  return {
    groupId: `document:${fixture.docId}`,
    sortAt: fixture.sortAt,
    entity: {
      __typename: 'GraphqlSoupDocument',
      id: fixture.docId,
      entityType: 'DOCUMENT',
      displayName: 'Plan',
      documentName: 'Plan',
      ownerId: 'macro|[email protected]',
      fileType: 'md',
      projectId: null,
      createdAt: '2026-01-01T00:00:00Z',
      updatedAt: '2026-01-01T00:00:00Z',
      viewedAt: null,
      deletedAt: null,
      subType: null,
      isFavorited: false,
      frecencyScore: null,
      cacheProjection: null,
      properties: [],
    },
    rows: fixture.rows.map((row) => ({
      id: row.id,
      threadId: null,
      rowType: row.rowType ?? 'DISCUSSION_MESSAGES',
      revision: row.revision ?? `rev-${row.id}`,
      latestAt: row.latestAt,
      actor:
        row.actor === undefined
          ? {
              kind: 'USER',
              principalId: 'macro|[email protected]',
              name: null,
            }
          : row.actor,
      actions: row.actions ?? [],
      unseen: row.unseen ?? true,
      count: (row.notificationIds ?? [`n-${row.id}`]).length,
      notifications: (row.notificationIds ?? [`n-${row.id}`]).map((id) =>
        commentNotification(fixture.docId, id, row.latestAt)
      ),
    })),
  } as unknown as NotificationInboxEntryFieldsFragment;
}

export function upsertedPatch(
  entry: NotificationInboxEntryFieldsFragment
): NotificationInboxPatchFieldsFragment {
  return { __typename: 'NotificationInboxEntryUpserted', entry };
}

export function removedPatch(
  groupId: string
): NotificationInboxPatchFieldsFragment {
  return { __typename: 'NotificationInboxEntryRemoved', groupId };
}

/** A page whose read stops at its last entry unless `nextPosition` says
 * otherwise; without a cursor the inbox is exhausted. */
export function inboxPage(
  entries: NotificationInboxEntryFieldsFragment[],
  nextCursor: string | null = null,
  nextPosition: { sortAt: string; entityId: string } | null = nextCursor
    ? lastPosition(entries)
    : null
) {
  return {
    user: {
      id: 'macro|[email protected]',
      notificationInbox: { entries, nextCursor, nextPosition },
    },
  };
}

function lastPosition(entries: NotificationInboxEntryFieldsFragment[]) {
  const last = entries.at(-1);
  if (!last) return null;
  return {
    sortAt: last.sortAt,
    entityId: last.groupId.slice(last.groupId.indexOf(':') + 1),
  };
}