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.

8.3 KB
Raw Blame
import { SidePanel } from '@components/app/side-panel';
import type { SplitContent } from '@components/app/split-layout/layoutManager';
import { useChannelName } from '@core/context/channels';
import { fireAndForget } from '@core/util/fireAndForget';
import { openExternalUrl } from '@core/util/url';
import BuildingsIcon from '@icon/system/regular/buildings.svg';
import HashIcon from '@icon/system/regular/hash.svg';
import { Button, cn } from '@ui';
import { type Component, For, type JSX, Match, Show, Switch } from 'solid-js';
import {
  PR_PRIORITY_LABELS,
  type PrLinks,
  priorityTaskName,
} from '../../data/pr-links';
import { PR_ORIGIN_LABELS, type PrOrigin } from '../../data/pr-origin';
import { prLinksTarget, usePrLinksQuery } from '../../queries/pr-links-query';
import { PrOriginIcon, PrPriorityIcon, prOriginDescription } from '../PrLinks';
import { useOpenFromSidePanel } from './open-from-side-panel';
import { useAgentSessionLabel } from './PrAgentSessionsSection';

function LinkRow(props: {
  icon?: Component<{ class?: string }>;
  leading?: JSX.Element;
  /** The item to open, or a handler for one that opens outside Macro. */
  content: SplitContent | (() => void);
  children: JSX.Element;
  class?: string;
}) {
  const open = useOpenFromSidePanel();
  return (
    <button
      type="button"
      class={cn(
        'flex h-6 w-full min-w-0 items-center gap-2 rounded text-left hover:text-ink',
        props.class
      )}
      onClick={(event) => {
        const content = props.content;
        if (typeof content === 'function') content();
        else open(content, event.shiftKey);
      }}
    >
      <Show when={props.icon} fallback={props.leading}>
        {(icon) => {
          const Icon = icon();
          return <Icon class="size-3.5 shrink-0 text-ink-muted" />;
        }}
      </Show>
      <span class="min-w-0 truncate">{props.children}</span>
    </button>
  );
}

function SessionOriginRow(props: { origin: PrOrigin; sessionId: string }) {
  const { label } = useAgentSessionLabel(() => props.sessionId);
  return (
    <LinkRow
      leading={<PrOriginIcon tool={props.origin.tool} class="size-3.5" />}
      content={{ type: 'agent', id: props.sessionId }}
    >
      {label()}
    </LinkRow>
  );
}

/**
 * Where the pull request was started: the Macro agent session by name, the
 * external session link, or just the tool when nothing links to it.
 */
function OriginRow(props: { origin: PrOrigin }) {
  const tool = () => PR_ORIGIN_LABELS[props.origin.tool];
  return (
    <Switch
      fallback={
        <div class="flex h-6 min-w-0 items-center gap-2">
          <PrOriginIcon tool={props.origin.tool} class="size-3.5" />
          <span class="min-w-0 truncate">{tool()}</span>
        </div>
      }
    >
      <Match when={props.origin.sessionId}>
        {(sessionId) => (
          <SessionOriginRow origin={props.origin} sessionId={sessionId()} />
        )}
      </Match>
      <Match when={props.origin.url}>
        {(url) => (
          <LinkRow
            leading={<PrOriginIcon tool={props.origin.tool} class="size-3.5" />}
            content={() => openExternalUrl(url())}
          >
            {tool()}
          </LinkRow>
        )}
      </Match>
    </Switch>
  );
}

function ChannelRow(props: { id: string }) {
  const name = useChannelName(props.id, 'Channel');
  return (
    <LinkRow icon={HashIcon} content={{ type: 'channel', id: props.id }}>
      {name()}
    </LinkRow>
  );
}

function LinkGroup(props: { label: string; children: JSX.Element }) {
  return (
    <div class="flex flex-col gap-0.5">
      <div class="text-ink-placeholder">{props.label}</div>
      {props.children}
    </div>
  );
}

function priorityDetail(links: PrLinks) {
  const { priority } = links;
  if (priority.id === 'none') return 'Link a task with a priority to set one';
  if (priority.source === 'label') return 'From a GitHub label';
  const task = priorityTaskName(links);
  return task ? `From ${task}` : 'From a linked task';
}

/**
 * The tickets, customers, and channels a pull request links to, and the
 * priority they give it. Agent sessions have their own section.
 */
export function PrLinkedWorkSection(props: {
  pullRequest?: {
    owner: string;
    repo: string;
    number: number;
    labels?: readonly { name: string }[] | null;
    description?: string | null;
    authorLogin?: string | null;
  };
}) {
  const target = () =>
    props.pullRequest ? prLinksTarget(props.pullRequest) : undefined;
  const query = usePrLinksQuery(() => {
    const current = target();
    return current ? [current] : [];
  });
  const links = () => {
    const current = target();
    return current ? query.linksFor(current.url) : undefined;
  };

  return (
    <SidePanel.Section id="pr-linked-work" title="Linked work" order={25}>
      <Switch>
        <Match when={!props.pullRequest}>
          <div class="text-ink-placeholder" role="status">
            Loading pull request details…
          </div>
        </Match>
        <Match when={query.isError()}>
          <div class="text-ink-placeholder">Linked work couldn’t be loaded</div>
          <Button
            variant="ghost"
            size="xs"
            onClick={() => fireAndForget(query.retry(), 'pr-side-panel')}
          >
            Retry
          </Button>
        </Match>
        <Match when={links()}>
          {(current) => (
            <div class="flex flex-col gap-3 text-xs">
              <Show when={current().origin}>
                {(origin) => (
                  <LinkGroup label="Started from">
                    <OriginRow origin={origin()} />
                    <div class="text-ink-placeholder">
                      {prOriginDescription(origin())}
                    </div>
                  </LinkGroup>
                )}
              </Show>
              <LinkGroup label="Priority">
                <div class="flex h-6 items-center gap-2">
                  <PrPriorityIcon
                    priority={current().priority.id}
                    class="shrink-0"
                  />
                  <span class="text-ink">
                    {PR_PRIORITY_LABELS[current().priority.id]}
                  </span>
                </div>
                <div class="text-ink-placeholder">
                  {priorityDetail(current())}
                </div>
              </LinkGroup>
              <LinkGroup label="Tickets">
                <Show
                  when={current().tasks.length > 0}
                  fallback={
                    <div class="text-ink-placeholder">
                      Mention a task in the pull request to link it
                    </div>
                  }
                >
                  <For each={current().tasks}>
                    {(task) => (
                      <LinkRow
                        leading={
                          <PrPriorityIcon
                            priority={task.priority}
                            class="shrink-0"
                          />
                        }
                        content={{ type: 'md', id: task.id }}
                        class={cn(task.closed && 'text-ink-muted line-through')}
                      >
                        {task.name || 'Untitled task'}
                      </LinkRow>
                    )}
                  </For>
                </Show>
              </LinkGroup>
              <Show when={current().companyIds.length > 0}>
                <LinkGroup label="Customers">
                  <For each={current().companyIds}>
                    {(id) => (
                      <LinkRow
                        icon={BuildingsIcon}
                        content={{ type: 'company', id }}
                      >
                        {query.companyName(id) ?? 'Customer'}
                      </LinkRow>
                    )}
                  </For>
                </LinkGroup>
              </Show>
              <Show when={current().channelIds.length > 0}>
                <LinkGroup label="Channels">
                  <For each={current().channelIds}>
                    {(id) => <ChannelRow id={id} />}
                  </For>
                </LinkGroup>
              </Show>
            </div>
          )}
        </Match>
        <Match when={true}>
          <div class="text-ink-placeholder" role="status">
            Loading linked work…
          </div>
        </Match>
      </Switch>
    </SidePanel.Section>
  );
}