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.

4.7 KB
Raw Blame
import {
  clause,
  type Facet,
  type FacetClause,
  type FacetOption,
  NIL_UUID,
} from '@app/features/soup/filters';
import {
  type EntityData,
  isGithubPrEntity,
  isTaskEntity,
} from '@entity/types/entity';
import { isWithNotification } from '@entity/types/notification';
import { toNotificationEntity } from '@entity/utils/notification';
import type { NotificationSource } from '@notifications/notification-source';
import { compositeEntity } from '@notifications/types';
import type { HomeReadFilter, HomeTypeFilter } from './types';

export type HomeFacetContext = {
  notificationSource?: NotificationSource;
};

export type HomeFacetOption = FacetOption<EntityData, HomeFacetContext> & {
  label: string;
};

function readClause(seen: boolean): FacetClause {
  return {
    df: clause.eq('documentSeen', seen),
    calf: clause.eq('calendarEventSeen', seen),
    ef: clause.eq('emailSeen', seen),
    chanf: clause.eq('channelSeen', seen),
    cthf: clause.eq('channelThreadSeen', seen),
    cf: clause.eq('chatSeen', seen),
    pf: clause.eq('folderSeen', seen),
    fef: clause.eq('foreignEntitySeen', seen),
    if: clause.eq('initiativeSeen', seen),
  };
}

function readOption(
  id: HomeReadFilter,
  label: string,
  seen: boolean
): HomeFacetOption {
  return {
    id,
    label,
    clause: readClause(seen),
    predicate: (entity, context) => {
      const source = context.notificationSource;
      if (!source) return true;

      if (entity.type === 'email') {
        const unread = !entity.isRead;
        return unread !== seen;
      }

      const notifications =
        (isWithNotification(entity)
          ? entity.notifications?.()
          : source.notificationsByEntity()[
              compositeEntity(toNotificationEntity(entity))
            ]) ?? [];
      const unread = notifications.some(
        (notification) => notification.state === 'unseen'
      );

      return unread !== seen;
    },
  };
}

const typeOptions: Record<HomeTypeFilter, HomeFacetOption> = {
  documents: {
    id: 'documents',
    label: 'Documents',
    clause: {
      df: clause.not(clause.eq('subType', 'task')),
    },
    predicate: (entity) => entity.type === 'document' && !isTaskEntity(entity),
  },
  tasks: {
    id: 'tasks',
    label: 'Tasks',
    clause: { df: clause.eq('subType', 'task') },
    predicate: isTaskEntity,
  },
  email: {
    id: 'email',
    label: 'Email',
    clause: { ef: clause.not(clause.eq('threadId', NIL_UUID)) },
    predicate: (entity) => entity.type === 'email',
  },
  channels: {
    id: 'channels',
    label: 'Channels',
    clause: {
      chanf: clause.not(clause.eq('channelId', NIL_UUID)),
      cthf: clause.not(clause.eq('channelThreadId', NIL_UUID)),
    },
    predicate: (entity) =>
      entity.type === 'channel' ||
      entity.type === 'channel_message' ||
      entity.type === 'channel_thread',
  },
  agents: {
    id: 'agents',
    label: 'Agents',
    clause: {
      cf: clause.not(clause.eq('chatId', NIL_UUID)),
      asf: clause.not(clause.eq('agentSessionId', NIL_UUID)),
    },
    predicate: (entity) =>
      entity.type === 'agent_session' || entity.type === 'chat',
  },
  folders: {
    id: 'folders',
    label: 'Folders',
    clause: { pf: clause.not(clause.eq('folderId', NIL_UUID)) },
    predicate: (entity) => entity.type === 'project',
  },
  initiatives: {
    id: 'initiatives',
    label: 'Projects',
    clause: { if: clause.eq('includeInitiatives', true) },
    predicate: (entity) => entity.type === 'initiative',
  },
  github: {
    id: 'github',
    label: 'GitHub',
    clause: {
      fef: clause.not(clause.eq('foreignEntityRecordId', NIL_UUID)),
    },
    predicate: isGithubPrEntity,
  },
  calendar: {
    id: 'calendar',
    label: 'Calendar',
    clause: {
      calf: clause.not(clause.eq('calendarEventId', NIL_UUID)),
    },
    predicate: (entity) => entity.type === 'calendar_event',
  },
};

type HomeFilterGroup = {
  id: string;
  label: string;
  options: { id: string; label: string }[];
};

export const HOME_TYPE_FILTER_GROUPS: HomeFilterGroup[] = [
  {
    id: 'type',
    label: 'Entity types',
    options: Object.values(typeOptions),
  },
];

export const HOME_FACETS: Facet<
  EntityData,
  HomeFacetContext,
  HomeFacetOption
>[] = [
  {
    id: 'read',
    mode: 'or',
    options: [
      readOption('unread', 'Unread', false),
      readOption('read', 'Read', true),
    ],
  },
  {
    id: 'type',
    mode: 'or',
    restrict: true,
    // Empty selections mean unrestricted in Soup. An explicit option with no
    // allowed targets represents hiding every type, including in server queries.
    options: [
      ...Object.values(typeOptions),
      { id: 'none', label: 'None', clause: {}, predicate: () => false },
    ],
  },
];