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.0 KB
Raw Blame
import { createCrmUserCardAction } from '@app/features/crm/crm-user-action';
import { createBotSettingsUserCardAction } from '@app/features/settings/bot-settings-user-action';
import { useSplitLayout } from '@components/app/split-layout/layout';
import { toast } from '@core/component/Toast/Toast';
import { isBotPrincipalId } from '@core/constant/macroAgent';
import { useUserId } from '@core/context/user';
import { useIsConnectedSecondaryInbox } from '@core/user';
import { useOpenDirectMessage } from '@core/util/open-direct-message';
import WideChat from '@icon/system/regular/chat.svg';
import CopyIcon from '@icon/system/regular/copy.svg';
import WideTask from '@icon/system/regular/list-checks.svg';
import {
  type Accessor,
  type Component,
  type ComponentProps,
  createMemo,
} from 'solid-js';

/** The person a user card describes. */
export type UserCardTarget = {
  displayName: string;
  email?: string;
  id?: string;
  isDeleted?: boolean;
  photoUrl?: string;
};

type UserCardActionId =
  | 'copy-email'
  | 'copy-name'
  | 'open-contact'
  | 'open-settings'
  | 'dm'
  | 'assign-task';

export type UserCardAction = {
  id: UserCardActionId;
  label: string;
  icon: Component<ComponentProps<'svg'>>;
  /** Writes to the clipboard: confirm in place instead of dismissing the card. */
  copies?: boolean;
  onSelect: (event: MouseEvent) => void | Promise<void>;
};

function copyableName(
  displayName: string,
  email: string | undefined
): string | undefined {
  const name = displayName.trim();
  if (!name) return undefined;
  if (name.toLowerCase() === 'me') return undefined;
  if (email && name.toLowerCase() === email.toLowerCase()) return undefined;
  const localPart = email?.split('@')[0];
  if (localPart && name.toLowerCase() === localPart.toLowerCase()) {
    return undefined;
  }
  return name;
}

/**
 * The actions a user card offers, shared by every presentation of it: the
 * hover card on pointer devices and the bottom sheet on touch ones.
 *
 * Dismissal is left to the caller — a hover card stays open to show that a
 * copy landed, a sheet closes after a successful action.
 */
export function useUserCardActions(
  target: Accessor<UserCardTarget>
): Accessor<UserCardAction[]> {
  const currentUserId = useUserId();
  const isConnectedSecondaryInbox = useIsConnectedSecondaryInbox();
  const { popoverSplit } = useSplitLayout();
  const openDm = useOpenDirectMessage();

  const userId = () => target().id;
  const canTreatAsUser = () =>
    !!userId() && !target().isDeleted && !isConnectedSecondaryInbox(userId());
  // An agent is mentioned like a person and hovers like one, but there is
  // nobody on the other end of a direct message to it: an agent answers where
  // it was mentioned, and a DM channel it never reads would look like a
  // conversation that is simply being ignored.
  const canDirectMessage = () =>
    canTreatAsUser() && !isBotPrincipalId(userId());

  const contactAction = createCrmUserCardAction(() => target().email);
  const settingsAction = createBotSettingsUserCardAction(userId);

  const copyAction = (
    id: UserCardActionId,
    label: string,
    value: string,
    toastMessage: string
  ): UserCardAction => ({
    id,
    label,
    icon: CopyIcon,
    copies: true,
    onSelect: async (event) => {
      event.stopPropagation();
      try {
        await navigator.clipboard.writeText(value);
      } catch (error) {
        toast.failure('Failed to copy to clipboard');
        throw error;
      }
      toast.success(toastMessage);
    },
  });

  const openDirectMessage = async (event: MouseEvent) => {
    event.preventDefault();
    event.stopPropagation();
    const recipientId = userId();
    if (!recipientId) return;
    await openDm(recipientId, { preferNewSplit: event.shiftKey });
  };

  const openTaskComposer = (event: MouseEvent) => {
    event.preventDefault();
    event.stopPropagation();
    const assigneeId = userId();
    if (!assigneeId) return;
    popoverSplit({
      type: 'component',
      id: 'task-compose',
      params: { initialAssigneeIds: [assigneeId] },
    });
  };

  return createMemo(() => {
    const { displayName, email } = target();
    const actions: UserCardAction[] = [];

    if (email) {
      actions.push(
        copyAction('copy-email', 'Copy email', email, 'Email copied')
      );
    }

    const name = copyableName(displayName, email);
    if (name) {
      actions.push(copyAction('copy-name', 'Copy name', name, 'Name copied'));
    }

    const contact = contactAction();
    if (contact) actions.push(contact);

    const settings = settingsAction();
    if (settings) actions.push(settings);

    if (canDirectMessage() && userId() !== currentUserId()) {
      actions.push({
        id: 'dm',
        label: 'DM',
        icon: WideChat,
        onSelect: openDirectMessage,
      });
    }

    if (canTreatAsUser()) {
      actions.push({
        id: 'assign-task',
        label: 'Assign task',
        icon: WideTask,
        onSelect: openTaskComposer,
      });
    }

    return actions;
  });
}