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.0 KB
Raw Blame
import type { EntityData } from '@entity';
import {
  type Accessor,
  createContext,
  type Setter,
  useContext,
} from 'solid-js';
import type { EntityActionListState } from '../../next-soup/actions/entity-action-context';
import type { SoupState } from '../../next-soup/create-soup-state';
import type { QueryStore } from '../../next-soup/filters/filter-store/query-store';
import type { CrmRecordRef, CrmRecordSection } from '../core/record';

/** A new `seek` asks to show the same message again. */
export type CrmCommentTarget = { commentId: string; seek: string };

/** Records opened inside the workspace; the route holds them. */
export type CrmRecordNavigation = {
  /** Records opened from the list, oldest first; empty on the list. */
  records: Accessor<readonly CrmRecordRef[]>;
  /** The shown record's tab; none is its Overview. */
  section: Accessor<CrmRecordSection | undefined>;
  /** A Discussion message of the shown record to scroll to. */
  commentTarget: Accessor<CrmCommentTarget | undefined>;
  /** Shows the last record of `trail` at its Overview. */
  open(trail: readonly CrmRecordRef[]): void;
  setSection(section: CrmRecordSection): void;
  item: Accessor<
    | {
        itemType: 'document' | 'email' | 'call';
        itemId: string;
        itemFileType?: string;
        itemSubType?: 'task' | 'snippet' | 'skill';
        name: string;
      }
    | undefined
  >;
  openItem(
    entity:
      | EntityData
      | Pick<
          Extract<EntityData, { type: 'document' }>,
          'type' | 'id' | 'name' | 'fileType' | 'subType'
        >,
    event?: MouseEvent | KeyboardEvent
  ): boolean;
  closeItem(): void;
  /** Returns to the list the records were opened from. */
  close(): void;
};

/** The Customers location: a list tab, and the records opened from it. */
export type CrmRoute = CrmRecordNavigation & {
  tab: Accessor<string>;
  /** Shows the list of `tab`, closing any open record. */
  setTab(tab: string, options?: { replace?: boolean }): void;
};

export type PeopleVisibility = {
  canShowRemoved: Accessor<boolean>;
  showRemoved: Accessor<boolean>;
  removedTeamId: Accessor<string | undefined>;
  setShowRemoved(value: boolean): void;
};

/** Collection capabilities consumed by CRM views, supplied by the host adapter. */
export type CrmWorkspace = {
  peopleVisibility: PeopleVisibility;
  host: {
    scopeId: string;
    isActive: Accessor<boolean>;
    focus(): void;
    captureEntryState(): void;
    openCompany(entity: EntityData, newSplit: boolean): void;
    /** CRM settings, shown in the workspace over its list or record. */
    settings: {
      isOpen: Accessor<boolean>;
      open(): void;
      close(): void;
    };
    openTeamSettings(): void;
    record: CrmRecordNavigation;
  };

  soup: EntityActionListState & {
    selection: Pick<SoupState['selection'], 'clear' | 'selected'>;
    predicates: Pick<
      SoupState['predicates'],
      'set' | 'isActive' | 'toggle' | 'andIds' | 'orIds'
    >;
    sort: Pick<SoupState['sort'], 'setAll' | 'active' | 'flip'>;
    grouping: Pick<SoupState['grouping'], 'activeGroupId' | 'setActiveGroupId'>;
  };
  source: {
    error: Accessor<Error | undefined | null>;
    data: Accessor<EntityData[]>;
    isLoading: Accessor<boolean>;
    isFetching: Accessor<boolean>;
    hasNextPage: Accessor<boolean>;
    fetchNextPage(): Promise<void>;
  };
  queryFilters: Pick<QueryStore, 'state' | 'set' | 'replace'>;
  searchText: Accessor<string>;
  setSearchText(value: string): void;
  activeTab: Accessor<string | undefined>;
  setActiveTab: Setter<string | undefined>;
  stageFilter: Accessor<string[]>;
  setStageFilter: Setter<string[]>;
  ownerFilter: Accessor<string[]>;
  setOwnerFilter: Setter<string[]>;
};
const Context = createContext<CrmWorkspace>();
export const CrmWorkspaceProvider = Context.Provider;
export const useOptionalCrmWorkspace = () => useContext(Context);
export function useCrmWorkspace(): CrmWorkspace {
  const value = useOptionalCrmWorkspace();
  if (!value)
    throw new Error('CRM workspace views require a CrmWorkspaceProvider');
  return value;
}