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.

6.0 KB
Raw Blame
import { isBotPrincipalId } from '@core/constant/macroAgent';
import type { EntityReference } from '@service-properties/generated/schemas/entityReference';
import { cn } from '@ui';
import { type JSX, Show, useContext } from 'solid-js';
import { PropertyRootContext } from '../core/context';
import { usePropertyEntityDisplay } from '../hooks/usePropertyEntityDisplay';
import { usePropertyUserDisplay } from '../hooks/usePropertyUserDisplay';
import type { Property } from '../types';
import {
  formatBoolean,
  formatDate,
  formatNumber,
  formatPropertyValue,
  formatTimeRange,
} from '../utils/formatting';
import {
  getEntityValues,
  getLinkValues,
  getSelectValues,
  hasValue,
  isBooleanProperty,
  isDateProperty,
  isDateRangeProperty,
  isEntityProperty,
  isLinkProperty,
  isNumberProperty,
  isSelectProperty,
  isStringProperty,
} from '../utils/typeGuards';

type Props = {
  property: Property;
  /** Rendered when the property has no value. */
  fallback?: JSX.Element;
  class?: string;
  /** Override the rendered text (overrides default extraction). */
  text?: string;
  /** Resolve a single linked item to its name instead of showing "1 item". */
  resolveSingleEntity?: boolean;
  /** Omit the resolved entity icon when the caller renders it separately. */
  hideIcon?: boolean;
};

/**
 * Renders a property's value as text (single-line, truncate-friendly).
 *
 * Routes by valueType to produce the right representation:
 * - STRING: raw value
 * - NUMBER: formatted number
 * - BOOLEAN: "True"/"False"
 * - DATE: formatted date
 * - DATE_RANGE: formatted range of days
 * - SELECT_*: first option's display label
 * - ENTITY+USER (single): user's first name
 * - ENTITY+USER (multi): "N people"
 * - ENTITY (non-user): "1 item" / "N items"
 * - LINK: first URL
 *
 * Caller is responsible for any container/border/padding.
 */
export function PropertyText(props: Props) {
  const userId = () => {
    if (props.text !== undefined) return undefined;
    if (!isEntityProperty(props.property)) return undefined;
    if (props.property.specificEntityType !== 'USER') return undefined;

    const entities = getEntityValues(props.property);
    return entities.length === 1 ? entities[0].entity_id : undefined;
  };

  const singleEntity = () => {
    if (!props.resolveSingleEntity || props.text !== undefined) return;
    if (!isEntityProperty(props.property)) return;
    if (props.property.specificEntityType === 'USER') return;
    const entities = getEntityValues(props.property);
    return entities.length === 1 ? entities[0] : undefined;
  };

  return (
    <Show
      keyed
      when={singleEntity()?.entity_id}
      fallback={<PropertyTextValue {...props} userId={userId()} />}
    >
      {(entityId) => (
        <EntityPropertyText
          entityId={entityId}
          entityType={singleEntity()?.entity_type}
          class={props.class}
          hideIcon={props.hideIcon}
        />
      )}
    </Show>
  );
}

function EntityPropertyText(props: {
  entityId: string;
  entityType: EntityReference['entity_type'] | undefined;
  class?: string;
  hideIcon?: boolean;
}) {
  const root = useContext(PropertyRootContext);
  const { name, icon } = usePropertyEntityDisplay(
    () => props.entityId,
    () => props.entityType,
    { loadingName: () => root?.pickedEntityName(props.entityId) }
  );
  return (
    <span class={cn('inline-flex min-w-0 items-center gap-1.5', props.class)}>
      <Show when={!props.hideIcon}>
        <span
          aria-hidden="true"
          class="flex size-3.5 shrink-0 items-center justify-center [&_svg]:size-3.5"
        >
          {icon()}
        </span>
      </Show>
      <span class="truncate">{name()}</span>
    </span>
  );
}

function PropertyTextValue(props: Props & { userId?: string }) {
  return (
    <Show
      keyed
      when={props.userId}
      fallback={
        <PrimitivePropertyText
          property={props.property}
          fallback={props.fallback}
          class={props.class}
          text={props.text}
        />
      }
    >
      {(id) => (
        <UserPropertyText
          id={id}
          fallback={props.fallback}
          class={props.class}
        />
      )}
    </Show>
  );
}

function PrimitivePropertyText(props: Props) {
  const text = () => props.text ?? extractText(props.property);
  const empty = () => !text();

  return (
    <Show when={!empty()} fallback={props.fallback ?? null}>
      <span class={cn('truncate', props.class)}>{text()}</span>
    </Show>
  );
}

function UserPropertyText(props: {
  id: string;
  fallback?: JSX.Element;
  class?: string;
}) {
  const { shortName: text } = usePropertyUserDisplay(() => props.id);

  return (
    <Show when={text()} fallback={props.fallback ?? null}>
      <span class={cn('truncate', props.class)}>{text()}</span>
    </Show>
  );
}

function extractText(property: Property): string {
  if (!hasValue(property)) return '';

  if (isStringProperty(property)) return property.value ?? '';
  if (isNumberProperty(property))
    return property.value === null ? '' : formatNumber(property.value);
  if (isBooleanProperty(property))
    return property.value === null ? '' : formatBoolean(property.value);
  if (isDateProperty(property))
    return property.value === null ? '' : formatDate(property.value);
  if (isDateRangeProperty(property))
    return property.value === null ? '' : formatTimeRange(property.value);

  if (isSelectProperty(property)) {
    const first = getSelectValues(property)[0];
    return first ? formatPropertyValue(property, first) : '';
  }

  if (isEntityProperty(property)) {
    const entities = getEntityValues(property);
    if (entities.length === 0) return '';
    if (property.specificEntityType === 'USER') {
      if (entities.some((entity) => isBotPrincipalId(entity.entity_id))) {
        return `${entities.length} assignees`;
      }
      return entities.length === 2 ? '2 people' : `${entities.length} people`;
    }
    return entities.length === 1 ? '1 item' : `${entities.length} items`;
  }

  if (isLinkProperty(property)) {
    return getLinkValues(property)[0] ?? '';
  }

  return '';
}