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.

16.0 KB
Raw Blame
import { EntityActivitySectionConditional } from '@app/features/activity/views/entity-activity-section';
import { AskMacroButton } from '@app/features/chat/ChatWithAgentButton';
import {
  EntityPropertiesSection,
  EntityTagsSection,
} from '@app/features/property/side-panel/properties';
import { useFeatureFlag } from '@app/lib/analytics/posthog';
import { useGlobalNotificationSource } from '@components/app/GlobalAppState';
import { HeaderActionGroup } from '@components/app/HeaderActionGroup';
import {
  GithubPullRequestDetailsRows,
  SidePanel,
} from '@components/app/side-panel';
import {
  EntityMetadata,
  EntityMetadataRow,
} from '@components/app/side-panel/EntityMetadata';
import type { SidePanelSectionLayout } from '@components/app/side-panel/SidePanel';
import { useSplitLayout } from '@components/app/split-layout/layout';
import { openDocument } from '@core/component/LexicalMarkdown/component/core/BlockLink';
import { Wordcount } from '@core/component/LexicalMarkdown/component/status/Wordcount';
import { Notifications } from '@core/component/Notifications';
import { References } from '@core/component/References';
import {
  enableProjects,
  USE_MACRO_PR_SUMMARY_BLOCK,
} from '@core/constant/featureFlags';
import { isMobile } from '@core/mobile/isMobile';
import type { Entity } from '@core/types';
import type { DateValue } from '@core/util/date';
import { openExternalUrl } from '@core/util/url';
import { useSplitNavigationHandler } from '@core/util/useSplitNavigationHandler';
import { buildTaskProjectDefaultProperty } from '@entity/extractors-property';
import { useNotificationsForEntity } from '@notifications';
import {
  getBuiltinPropertyIds,
  getDefaultPinnedProperties,
  SYSTEM_PROPERTY_IDS,
} from '@property/constants';
import { queryReadyGate } from '@queries/gate';
import { useAttachmentReferencesQuery } from '@queries/storage/attachment-references';
import { useDocumentMetadataQuery } from '@queries/storage/document-metadata';
import {
  type GithubPullRequestWithDetails,
  useDocumentGithubPullRequestsQuery,
} from '@queries/storage/github-pull-requests';
import type { EntityType as PropertiesEntityType } from '@service-properties/generated/schemas/entityType';
import { createCallback } from '@solid-primitives/rootless';
import { cn } from '@ui';
import { createMemo, For, Show } from 'solid-js';
import { useMarkdownDocument } from '../../context/markdown-document-context';
import { createPinnedProperties } from '../../primitives/create-pinned-properties';
import { DispatchAgentButton } from '../DispatchAgentMenu';
import { useMarkdownName } from '../MarkdownNameProvider';
import { TaskDuplicateMatchesSidePanelSection } from '../TaskDuplicateMatches';

/** Registers markdown header actions, panel sections, and shared metadata. */
export function MarkdownSidePanelSections() {
  const { documentId, kind, permissions } = useMarkdownDocument();
  const canEdit = permissions.canEdit;
  const { displayName } = useMarkdownName();
  const isTask = () => kind() === 'task';
  const entity = (): Entity => ({
    id: documentId(),
    type: 'document',
  });
  const propertiesEntityType = (): PropertiesEntityType =>
    isTask() ? 'TASK' : 'DOCUMENT';

  return (
    <>
      <SidePanel.HeaderActions>
        <div class="flex shrink-0 items-center gap-1">
          <Show when={isTask() && !isMobile()}>
            <DispatchAgentButton showPrimaryLabel />
          </Show>
          <HeaderActionGroup label="Actions">
            <AskMacroButton
              grouped
              entity={{
                type: 'document',
                id: documentId(),
                name: displayName() ?? '',
                fileType: 'md',
              }}
            />
          </HeaderActionGroup>
        </div>
      </SidePanel.HeaderActions>
      <SidePanel.Footer>
        <DetailsSectionContent documentId={documentId()} />
        <Show when={!isTask()}>
          <StatsSectionContent />
        </Show>
      </SidePanel.Footer>
      <EntityTagsSection
        entityId={documentId()}
        entityType={propertiesEntityType()}
        canEdit={canEdit()}
        order={20}
      />
      <SidePanel.Section
        id="properties"
        title="Properties"
        defaultOpen
        order={25}
        render={(renderSection) => (
          <PropertiesSectionContent
            documentId={documentId()}
            isTask={isTask()}
            canEdit={canEdit()}
            documentName={displayName() ?? ''}
            renderSection={renderSection}
          />
        )}
      />
      <EntityActivitySectionConditional
        entityId={documentId()}
        entityType={propertiesEntityType()}
        order={40}
      />
      <GithubSectionConditional documentId={documentId()} isTask={isTask()} />
      <NotificationsSectionConditional entity={entity()} />
      <ReferencesSectionConditional documentId={documentId()} />
      <Show when={isTask()}>
        <TaskDuplicateMatchesSidePanelSection />
      </Show>
    </>
  );
}

// ─────────────────────────────────────────────────────────────────────────────
// Details Section
// ─────────────────────────────────────────────────────────────────────────────

function DetailsSectionContent(props: { documentId: string }) {
  const query = useDocumentMetadataQuery(() => props.documentId);
  const metadata = createMemo(() => query.data);

  return (
    <DetailsGrid
      owner={() => metadata()?.owner}
      folder={() => {
        const id = metadata()?.projectId;
        const name = metadata()?.projectName;
        return id && name ? { id, name } : undefined;
      }}
      createdAt={() => metadata()?.createdAt}
      updatedAt={() => metadata()?.updatedAt}
    />
  );
}

function DetailsGrid(props: {
  owner: () => string | undefined;
  folder: () => { id: string; name: string } | undefined;
  createdAt: () => DateValue | null | undefined;
  updatedAt: () => DateValue | null | undefined;
}) {
  return (
    <EntityMetadata
      ownerId={props.owner()}
      createdAt={props.createdAt()}
      updatedAt={props.updatedAt()}
    >
      <Show when={props.folder()}>
        {(folder) => (
          <EntityMetadataRow label="Folder">
            <FolderLink projectId={folder().id} projectName={folder().name} />
          </EntityMetadataRow>
        )}
      </Show>
    </EntityMetadata>
  );
}

function FolderLink(props: { projectId: string; projectName: string }) {
  const open = createCallback(() => {
    openDocument('project', props.projectId, undefined, true);
  });
  const navHandlers = useSplitNavigationHandler<HTMLSpanElement>(open);
  return (
    <span
      {...navHandlers}
      class="pointer-events-auto min-w-0 truncate py-0.5 rounded-xs text-ink hover:bg-hover focus:bg-active"
    >
      <span class="underline decoration-current/20 decoration-[max(1px,0.1em)] underline-offset-2">
        {props.projectName}
      </span>
    </span>
  );
}

// ─────────────────────────────────────────────────────────────────────────────
// Properties Section
// ─────────────────────────────────────────────────────────────────────────────

function PropertiesSectionContent(props: {
  documentId: string;
  isTask: boolean;
  canEdit: boolean;
  documentName: string;
  renderSection: SidePanelSectionLayout;
}) {
  const { state } = useMarkdownDocument();
  const mdData = state.editor.md;

  const entityType: PropertiesEntityType = props.isTask ? 'TASK' : 'DOCUMENT';

  const projects = useFeatureFlag(enableProjects);
  const defaultTitlePropertyDefinitionIds = () =>
    props.isTask
      ? [
          ...getDefaultPinnedProperties('task'),
          ...(projects().enabled ? [SYSTEM_PROPERTY_IDS.PROJECT] : []),
        ]
      : [];
  const pins = createPinnedProperties(
    () => mdData.editor,
    defaultTitlePropertyDefinitionIds
  );

  return (
    <EntityPropertiesSection
      entityId={props.documentId}
      entityType={entityType}
      canEdit={props.canEdit}
      documentName={props.documentName}
      defaultPinnedPropertyIds={() =>
        props.isTask ? getDefaultPinnedProperties('task') : []
      }
      requiredPropertyDefinitionIds={
        props.isTask ? getBuiltinPropertyIds('task') : undefined
      }
      pinnedPropertyIds={pins.ids}
      defaultTitlePropertyDefinitionIds={defaultTitlePropertyDefinitionIds}
      pinnedPropertyDefinitionOrder={PINNED_ORDER}
      onPropertyPinned={mdData.editor ? pins.pin : undefined}
      onPropertyUnpinned={pins.unpin}
      showTags={false}
      renderSection={props.renderSection}
      // A task joins a project through its Project property, edited like the others.
      defaultProperties={() =>
        props.isTask && projects().enabled
          ? [buildTaskProjectDefaultProperty()]
          : []
      }
    />
  );
}

// Side-panel ordering: Status, Priority, Assignees pinned to the top so the
// most-frequently scanned task properties always sit in the same place; the
// remaining properties keep their incoming order below.
const PINNED_ORDER: readonly string[] = [
  SYSTEM_PROPERTY_IDS.STATUS,
  SYSTEM_PROPERTY_IDS.PRIORITY,
  SYSTEM_PROPERTY_IDS.ASSIGNEES,
  SYSTEM_PROPERTY_IDS.PROJECT,
];

// ─────────────────────────────────────────────────────────────────────────────
// Stats Section
// ─────────────────────────────────────────────────────────────────────────────

function StatsSectionContent() {
  const { state } = useMarkdownDocument();
  const md = state.editor.md;

  return (
    <Show when={md.wordcountStats}>
      {(stats) => (
        <Wordcount.Root stats={stats()}>
          <div class="mt-2 flex flex-col gap-2">
            <EntityMetadataRow label="Words">
              <Wordcount.Words />
            </EntityMetadataRow>
            <EntityMetadataRow label="Characters">
              <Wordcount.Characters />
            </EntityMetadataRow>
          </div>
        </Wordcount.Root>
      )}
    </Show>
  );
}

// ─────────────────────────────────────────────────────────────────────────────
// Notifications Section (conditional)
// ─────────────────────────────────────────────────────────────────────────────

function NotificationsSectionConditional(props: { entity: Entity }) {
  const notificationSource = useGlobalNotificationSource();
  const notifications = useNotificationsForEntity(
    notificationSource,
    props.entity
  );
  const count = createMemo(() => notifications().length);
  const unreadCount = createMemo(
    () => notifications().filter((n) => n.state === 'unseen').length
  );

  return (
    <Show when={count() > 0}>
      <SidePanel.Section
        id="notifications"
        title={
          <SidePanel.CountTitle label="Notifications" count={unreadCount()} />
        }
        order={40}
      >
        <div class="text-xs">
          <Notifications
            entity={props.entity}
            notificationSource={notificationSource}
          />
        </div>
      </SidePanel.Section>
    </Show>
  );
}

// ─────────────────────────────────────────────────────────────────────────────
// References Section (conditional)
// ─────────────────────────────────────────────────────────────────────────────

function ReferencesSectionConditional(props: { documentId: string }) {
  const references = useAttachmentReferencesQuery(
    () => props.documentId,
    () => 'document'
  );

  const count = () => (queryReadyGate(references) ? references.data.length : 0);

  return (
    <Show when={count() > 0}>
      <SidePanel.Section
        id="references"
        title={<SidePanel.CountTitle label="References" count={count()} />}
        order={33}
      >
        <div class="text-xs">
          <References documentId={props.documentId} />
        </div>
      </SidePanel.Section>
    </Show>
  );
}

// ─────────────────────────────────────────────────────────────────────────────
// GitHub Section (conditional)
// ─────────────────────────────────────────────────────────────────────────────

function GithubSectionConditional(props: {
  documentId: string;
  isTask: boolean;
}) {
  const query = useDocumentGithubPullRequestsQuery(
    props.documentId,
    props.isTask
  );
  const { openWithSplit } = useSplitLayout();

  const pullRequests = createMemo((): GithubPullRequestWithDetails[] => {
    if (!props.isTask || query.isLoading || query.isError) return [];
    return query.data?.pullRequests ?? [];
  });
  const count = createMemo(() => pullRequests().length);

  return (
    <Show when={count() > 0}>
      <SidePanel.Section
        id="github"
        title={<SidePanel.CountTitle label="GitHub" count={count()} />}
        order={35}
      >
        <div class="flex flex-col gap-4">
          <For each={pullRequests()}>
            {(pr, index) => {
              const title = () => pr.name?.trim() || pr.displayName;
              const openPullRequest = () => {
                if (USE_MACRO_PR_SUMMARY_BLOCK && pr.foreignEntityId) {
                  openWithSplit(
                    {
                      type: 'pr',
                      id: pr.foreignEntityId,
                    },
                    { referredFrom: null }
                  );
                  return;
                }
                openExternalUrl(pr.url);
              };

              return (
                <div
                  class={cn(
                    'flex flex-col',
                    index() > 0 && 'border-t border-edge-muted pt-4'
                  )}
                >
                  <SidePanel.Grid class="auto-rows-[minmax(1.75rem,auto)]">
                    <span class="text-ink-muted truncate self-start pt-[0.3125rem]">
                      PR
                    </span>
                    <div class="min-w-0 max-w-full overflow-hidden self-start py-0.5">
                      <button
                        type="button"
                        class="block min-w-0 max-w-full text-left whitespace-normal wrap-break-word leading-snug underline decoration-current/20 decoration-[max(1px,0.1em)] underline-offset-2 hover:decoration-current"
                        title={title()}
                        onClick={openPullRequest}
                      >
                        {title()}
                      </button>
                    </div>
                    <GithubPullRequestDetailsRows enrichment={pr} />
                  </SidePanel.Grid>
                </div>
              );
            }}
          </For>
        </div>
      </SidePanel.Section>
    </Show>
  );
}