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.6 KB
Raw Blame
import { SearchBar, ViewShell } from '@app/components/view-shell';
import {
  ListHeader,
  ListHeaderSearch,
} from '@app/components/view-shell/ListHeader';
import { ViewCreateButton } from '@app/components/view-shell/ViewCreateButton';
import { TasksControls } from '@app/features/tasks-view/components/TasksControls';
import { useTasksDataSource } from '@app/features/tasks-view/queries/use-tasks-query';
import { TasksContent } from '@app/features/tasks-view/tasks-content';
import {
  TasksViewProvider,
  useTasksView,
} from '@app/features/tasks-view/tasks-view-context';
import { t } from '@app/lib/i18n';
import { useSplitLayout } from '@components/app/split-layout/layout';
import { isTouchDevice } from '@core/mobile/isTouchDevice';
import EmptyStateTasksGraphic from '@design/empty-state-tasks.svg';
import PlusIcon from '@icon/system/regular/plus.svg';
import { SYSTEM_PROPERTY_IDS } from '@property/identifiers';
import { TagSetsQueryProvider } from '@property/tags/tag-sets-context';
import { Button } from '@ui';
import { type JSX, Show } from 'solid-js';
import {
  CrmListEmpty,
  CrmListError,
  CrmListSearchEmpty,
  CrmListSkeleton,
} from './components/list-state';
import { useCrmContext } from './context/crm-context';
import { useCrmWorkspace } from './context/workspace-context';

/** Production Tasks layouts for the workspace-wide CRM activity scope. */
export function CrmActivityTasks(props: { title?: JSX.Element }) {
  const crm = useCrmContext();
  const layout = useSplitLayout();
  const workspace = useCrmWorkspace();
  return (
    <TagSetsQueryProvider>
      <TasksViewProvider
        initialState={{ tab: 'team-tasks', groupBy: 'date' }}
        restoreEntryState
        scopeKey="crm:activity:tasks"
        onOpenTask={(task, options) => {
          if (
            workspace.host.record.openItem(
              {
                type: 'document',
                id: task.id,
                name: task.fallbackName ?? 'Task',
                fileType: 'md',
                subType: { type: 'task' },
              },
              options?.event
            )
          )
            return true;
          layout.openWithSplit(
            { type: 'task', id: task.id },
            { preferNewSplit: options?.event?.shiftKey }
          );
          return true;
        }}
        onCloseTask={() => {}}
        onCreateTask={(options) => crm.openTaskComposer(options)}
        sourceFactory={(state, options) =>
          useTasksDataSource(state, {
            ...options,
            crm: () => ({ type: 'activity' }),
          })
        }
      >
        <ActivityTasksBody
          title={props.title}
          onCreateTask={crm.openTaskComposer}
        />
      </TasksViewProvider>
    </TagSetsQueryProvider>
  );
}

function ActivityTasksBody(props: {
  title?: JSX.Element;
  onCreateTask: () => void;
}) {
  const { state, setState, source } = useTasksView();
  let listElement: HTMLDivElement | undefined;
  const controls = () => (
    <>
      <TasksControls />
      <Show
        when={!isTouchDevice()}
        fallback={
          <Button variant="secondary" onClick={() => props.onCreateTask()}>
            <PlusIcon class="size-4" />
            New task
          </Button>
        }
      >
        <ViewCreateButton
          aria-label="New task"
          onClick={() => props.onCreateTask()}
        />
      </Show>
    </>
  );
  return (
    <div class="flex size-full min-h-0 flex-col">
      <Show
        when={!isTouchDevice()}
        fallback={
          <ViewShell.Header>
            <div class="flex min-w-0 items-center gap-2 px-4 py-2">
              {controls()}
              <SearchBar
                label={t('crm.searchSalesTasks')}
                placeholder={t('crm.searchSalesTasks')}
                value={state.search}
                onValueChange={(search) => setState('search', search)}
                class="min-w-0 flex-1"
              />
            </div>
          </ViewShell.Header>
        }
      >
        <ListHeader title={props.title ?? t('crm.salesTasks')}>
          {controls()}
          <ListHeaderSearch
            label={t('crm.searchSalesTasks')}
            value={state.search}
            onValueChange={(search) => setState('search', search)}
            onEscape={() => listElement?.focus()}
            registerFocus={() => {}}
          />
        </ListHeader>
      </Show>
      <div class="min-h-0 flex-1">
        <TasksContent
          loadingContent={<CrmListSkeleton label="Loading tasks" />}
          errorContent={
            <CrmListError
              title="Tasks couldn’t be loaded."
              onRetry={() => source.refresh()}
            />
          }
          emptyContent={
            state.search.trim() && source.hasMore() ? (
              <span>Search more results to find matching tasks.</span>
            ) : state.search.trim() ? (
              <CrmListSearchEmpty
                title="No tasks match this search"
                onClearSearch={() => setState('search', '')}
              />
            ) : (
              <CrmListEmpty
                title="No tasks linked yet"
                graphic={EmptyStateTasksGraphic}
                description="Create a task and choose a company or person to keep your follow-ups here."
                action={{ label: 'New task', onClick: props.onCreateTask }}
              />
            )
          }
          ref={(element) => {
            listElement = element;
          }}
          referencePropertyIds={[
            SYSTEM_PROPERTY_IDS.COMPANIES,
            SYSTEM_PROPERTY_IDS.CONTACTS,
          ]}
        />
      </div>
    </div>
  );
}