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.

5.5 KB
Raw Blame
import { TraceFlow, usePending } from '@app/observability/flow';
import { SidePanel } from '@components/app/side-panel';
import { createMemo, Match, onCleanup, Show, Suspense, Switch } from 'solid-js';
import { CrmListSkeleton } from '../components/list-state';
import { RecordOverview } from '../components/record-overview';
import { useCrmContext } from '../context/crm-context';
import type { CrmContact as CompanyContact } from '../core/contact';
import {
  type CompanySection,
  type CrmRecordScope,
  sameRecordScope,
} from '../core/record';
import { CompanyContactsSection } from './company-contacts-section';
import { CompanyDiscussionSection } from './company-discussion-section';
import { CompanyEmailsSection } from './company-emails-section';
import { CompanyHeader } from './company-header';
import { CompanyListsSection } from './company-list-membership';
import { CompanyPropertiesSection } from './company-properties-section';
import { CompanySharingSection } from './company-sharing-section';
import {
  RecordCallsSection,
  RecordFilesSection,
  RecordReferencesSection,
} from './record-associations';
import { RecordDetailsSection } from './record-details-section';
import { useCompanyQuery } from './use-crm';

/**
 * Root of the company detail view, laid out like a project: the host's top
 * tab row picks the section, Overview shows the company with its description and
 * discussion, and the other sections list its team, emails, files, tasks and
 * calls. Properties, sharing, and references open in the floating
 * information panel.
 */
type CompanyProps = {
  companyId: string;
  section?: CompanySection;
  headerToggle?: boolean;
  onOpenContact?: (contact: CompanyContact) => void;
  onOpenPipeline?: (pipelineId: string) => void;
};

export function Company(props: CompanyProps) {
  return (
    <TraceFlow
      name="crm.company"
      key={() => `${props.companyId}:${props.section ?? 'overview'}`}
      attributes={{
        'company.id': props.companyId,
        'crm.section': props.section ?? 'overview',
      }}
    >
      <CompanyContent {...props} />
    </TraceFlow>
  );
}

function CompanyContent(props: CompanyProps) {
  const context = useCrmContext();
  onCleanup(
    context.registerTaskComposerRecord(() => ({
      type: 'company',
      id: props.companyId,
    }))
  );
  const listsEnabled = context.listsEnabled();
  const pipelinesEnabled = context.pipelinesEnabled();
  const { company, contacts, query } = useCompanyQuery(() => props.companyId);
  usePending('crm.record', () => query.isLoading);
  const section = () => props.section ?? 'overview';
  const scope = createMemo(
    (): CrmRecordScope | undefined => {
      const current = company();
      if (!current) return;
      return {
        type: 'company',
        id: current.id,
        domains: current.domains.map((domain) => domain.domain),
      };
    },
    undefined,
    { equals: sameRecordScope }
  );

  return (
    <SidePanel.Layout
      headerToggle={props.headerToggle}
      floating
      defaultOpen={false}
    >
      <div class="flex size-full min-h-0 min-w-0 flex-col">
        <div class="min-h-0 min-w-0 flex-1">
          <Switch>
            <Match when={section() === 'overview'}>
              <RecordOverview>
                <CompanyHeader company={company()} />
                <Show when={pipelinesEnabled()}>
                  <context.PipelineEntries
                    record={{ type: 'company', id: props.companyId }}
                    onOpenPipeline={props.onOpenPipeline}
                  />
                </Show>
                <CompanyDiscussionSection companyId={props.companyId} />
              </RecordOverview>
            </Match>
            <Match when={section() === 'team'}>
              <CompanyContactsSection
                company={company()}
                contacts={contacts()}
                onOpenContact={props.onOpenContact}
              />
            </Match>
            <Match when={section() === 'emails'}>
              <CompanyEmailsSection company={company()} />
            </Match>
            <Match when={section() === 'files'}>
              <RecordFilesSection scope={scope()} />
            </Match>
            <Match when={section() === 'tasks'}>
              <Suspense fallback={<CrmListSkeleton label="Loading tasks" />}>
                <Show
                  when={scope()}
                  fallback={<CrmListSkeleton label="Loading tasks" />}
                >
                  {(current) => <context.RecordTasks scope={current()} />}
                </Show>
              </Suspense>
            </Match>
            <Match when={section() === 'calls'}>
              <RecordCallsSection scope={scope()} />
            </Match>
          </Switch>
        </div>
      </div>

      <RecordDetailsSection company={company()} />
      <SidePanel.Section
        id="company-properties"
        title="Properties"
        order={30}
        defaultOpen
        render={(renderSection) => (
          <CompanyPropertiesSection
            companyId={props.companyId}
            renderSection={renderSection}
          />
        )}
      />
      <Show when={listsEnabled()}>
        <Suspense>
          <CompanyListsSection companyId={props.companyId} />
        </Suspense>
      </Show>
      <SidePanel.Section id="company-sharing" title="Email sharing" order={50}>
        <CompanySharingSection company={company()} />
      </SidePanel.Section>
      <SidePanel.Section id="company-references" title="References" order={20}>
        <RecordReferencesSection scope={scope()} />
      </SidePanel.Section>
    </SidePanel.Layout>
  );
}