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.

2.1 KB
Raw Blame
import { Badge, cn } from '@ui';
import { type Component, For, type JSX, Show } from 'solid-js';
import type { Property } from '../types';
import {
  formatPropertyValue,
  getEntityValues,
  getLinkValues,
  getSelectValues,
  isEntityProperty,
  isLinkProperty,
  isSelectProperty,
} from '../utils';
import { PropertyIcon } from './PropertyIcon';

type Props = {
  property: Property;
  class?: string;
  /** Render-prop for each chip. Defaults to a simple label-only chip. */
  renderChip?: (chip: ChipInfo) => JSX.Element;
};

type ChipInfo = {
  /** Stable identifier for the chip (option ID, entity ID, URL, etc.). */
  key: string;
  /** Human-readable label. */
  label: string;
};

/**
 * Renders a property's values as a chip list — for SELECT_* multi, ENTITY
 * (non-user), LINK. Caller controls the chip appearance via `renderChip`.
 *
 * For single-value properties returns null (use <PropertyText /> instead).
 */
export const PropertyChips: Component<Props> = (props) => {
  const chips = (): ChipInfo[] => {
    if (isSelectProperty(props.property)) {
      return getSelectValues(props.property).map((id) => ({
        key: id,
        label: formatPropertyValue(props.property, id),
      }));
    }
    if (isEntityProperty(props.property)) {
      return getEntityValues(props.property).map((ref) => ({
        key: `${ref.entity_type}:${ref.entity_id}`,
        label: ref.entity_id,
      }));
    }
    if (isLinkProperty(props.property)) {
      return getLinkValues(props.property).map((url) => ({
        key: url,
        label: url,
      }));
    }
    return [];
  };

  const renderChip =
    props.renderChip ??
    ((chip: ChipInfo) => (
      <Badge variant="ghost" size="sm" class="max-w-full">
        <Show when={isSelectProperty(props.property)}>
          <PropertyIcon property={props.property} class="size-3 shrink-0" />
        </Show>
        <span class="truncate">{chip.label}</span>
      </Badge>
    ));

  return (
    <div class={cn('flex flex-wrap gap-1', props.class)}>
      <For each={chips()}>{(chip) => renderChip(chip)}</For>
    </div>
  );
};