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.

4.1 KB
Raw Blame
import { touchHandler } from '@core/directive/touchHandler';
import { idToDisplayName } from '@core/user';
import { tryMacroId } from '@core/user/macroId';
import { Popover } from '@kobalte/core/popover';
import { Button, cn, HoverCard } from '@ui';
import { createSignal, type JSX, Show } from 'solid-js';

type ReactionChipProps = {
  emoji: string;
  count: number;
  users: string[];
  currentUserId: string | undefined;
  selected?: boolean;
  interactive?: boolean;
  onClick?: (event: MouseEvent) => void;
};

/**
 * Format a list of user display names with Oxford comma style.
 * Current user ("You") is always listed first regardless of input order.
 * e.g. "You", "You and Alice", "You, Alice, and Bob"
 */
export function formatReactorNames(
  userIds: string[],
  currentUserId: string | undefined
): string {
  if (userIds.length === 0) return '';

  // Reactors outside Macro, such as WhatsApp participants, have no name here.
  const isNamed = (id: string) =>
    id === currentUserId || tryMacroId(id) !== undefined;
  const ordered = [
    ...userIds.filter((id) => id === currentUserId),
    ...userIds.filter((id) => id !== currentUserId && isNamed(id)),
  ];
  const others = userIds.filter((id) => !isNamed(id)).length;

  const names = [
    ...ordered.map((id) =>
      id === currentUserId ? 'You' : idToDisplayName(id)
    ),
    ...(others === 0
      ? []
      : [others === 1 ? '1 other person' : `${others} other people`]),
  ];

  if (names.length === 1) return names[0]!;
  if (names.length === 2) return `${names[0]} and ${names[1]}`;

  const allButLast = names.slice(0, -1);
  const last = names[names.length - 1];
  return `${allButLast.join(', ')}, and ${last}`;
}

function ReactionTooltipContent(props: {
  users: string[];
  currentUserId: string | undefined;
  emoji: string;
}): JSX.Element {
  return (
    <span>
      {formatReactorNames(props.users, props.currentUserId)} reacted with{' '}
      <span class="text-base">{props.emoji}</span>
    </span>
  );
}

export function ReactionChip(props: ReactionChipProps) {
  const [showReactors, setShowReactors] = createSignal(false);

  return (
    <Popover
      open={showReactors()}
      onOpenChange={setShowReactors}
      placement="top"
    >
      <Popover.Anchor>
        <HoverCard
          placement="top"
          content={
            <ReactionTooltipContent
              users={props.users}
              currentUserId={props.currentUserId}
              emoji={props.emoji}
            />
          }
        >
          <Button
            data-message-reaction-chip
            data-emoji={props.emoji}
            data-user-reacted={props.selected ? '' : undefined}
            aria-pressed={!!props.selected}
            ref={(el) =>
              touchHandler(el, () => ({
                onLongPress: () => {
                  setShowReactors(true);
                },
                stopTouchStartPropagation: true,
              }))
            }
            size="sm"
            variant="secondary"
            appearance={props.selected ? 'bare' : undefined}
            class={cn(
              'flex flex-row items-center h-7 min-w-7 gap-1',
              props.selected &&
                'border-border-secondary bg-accent-bg text-accent aria-pressed:overlay-active not-touch:not-data-disabled:hover:overlay-accent-bg',
              !props.interactive && 'pointer-events-auto'
            )}
            disabled={!props.interactive}
            onClick={(event) => {
              event.stopPropagation();
              props.onClick?.(event);
            }}
          >
            <span class="text-lg leading-0">{props.emoji}</span>
            <Show when={props.count > 1}>
              <span class="text-xs tabular-nums">{props.count}</span>
            </Show>
          </Button>
        </HoverCard>
      </Popover.Anchor>
      <Popover.Portal>
        <Popover.Content class="menu-surface z-modal p-1.5 text-ink-muted text-xs">
          <ReactionTooltipContent
            users={props.users}
            currentUserId={props.currentUserId}
            emoji={props.emoji}
          />
        </Popover.Content>
      </Popover.Portal>
    </Popover>
  );
}