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.5 KB
Raw Blame
import { ArchivedRecordBadge } from '../components/record-archived-status';
import '@entity/composed/ListEntity.css';
import {
  SwipableRow,
  SwipableRowContext,
} from '@components/app/mobile/SwipableRow';
import { isMobile } from '@core/mobile/isMobile';
import { isTouchDevice } from '@core/mobile/isTouchDevice';
import { createEntityDraggable, Entity, unreadFilterFn } from '@entity';
import { NarrowLayout } from '@entity/composed/list-entity/narrow-layout';
import { SOUP_ROW_CLASS } from '@entity/composed/list-entity/row-geometry';
import {
  type BaseListEntityProps,
  type LayoutProps,
  useListLayout,
} from '@entity/composed/list-entity/shared';
import type { EntityRowConfig } from '@entity/extractors-notification';
import { mergeRefs } from '@solid-primitives/refs';
import { cn } from '@ui/utils/classname';
import {
  type Component,
  type JSX,
  Match,
  Show,
  Switch,
  useContext,
} from 'solid-js';
import { Dynamic } from 'solid-js/web';
import { useCrmContext } from '../context/crm-context';

function MaybeEntityRow(props: {
  entityId: string;
  children: JSX.Element;
  config?: EntityRowConfig;
}) {
  const ctx = useContext(SwipableRowContext);
  return (
    <Show when={isMobile() && ctx} fallback={props.children}>
      <SwipableRow
        id={props.entityId}
        swipeLeftColor={props.config?.swipeLeftColor}
        swipeLeftRevealedComponent={props.config?.swipeLeftRevealedComponent}
        swipeRightColor={props.config?.swipeRightColor}
        swipeRightRevealedComponent={props.config?.swipeRightRevealedComponent}
      >
        {props.children}
      </SwipableRow>
    </Show>
  );
}

/**
 * CRM list row: selection, drag, and swipe chrome around a record-specific
 * grid layout whose columns line up across rows, mirroring `TaskListEntity`.
 */
export function CrmListRow(
  props: BaseListEntityProps & { layout: Component<LayoutProps> }
) {
  const unread = () => unreadFilterFn(props.entity);

  const layoutProps = (): LayoutProps => ({
    entity: props.entity,
    checked: props.checked,
    hideCheckbox: props.hideCheckbox,
    onChecked: props.onChecked,
    unread: unread(),
    isShared: false,
    hasNotifications: false,
    showHitSnippet: false,
    streamState: undefined,
    setSnippetContainerRef: () => {},
    chars: 0,
    get titleTrailing() {
      return (
        <Show when={'hidden' in props.entity && props.entity.hidden}>
          <ArchivedRecordBadge />
        </Show>
      );
    },
    onProjectClick: props.onProjectClick,
  });

  const draggable = createEntityDraggable({
    entity: props.entity,
    splitId: useCrmContext().createNavigation().splitId,
    deferUntilInteraction: () => props.deferInteractions === true,
  });

  const isWide = useListLayout()?.isWide ?? (() => true);

  return (
    <Entity.Root
      role={props.role}
      tabIndex={props.tabIndex}
      onKeyDown={props.onKeyDown}
      entity={props.entity}
      onClick={(e) => {
        if (e.metaKey && props.onChecked) {
          props.onChecked(!props.checked, e.shiftKey);
          return;
        }
        props.onClick?.(e);
      }}
      ref={mergeRefs(props.ref, draggable)}
      class={cn(
        // Carries the --soup-row-* geometry (ListEntity.css) that the narrow
        // layout below reads for its leading edge.
        isWide() ? SOUP_ROW_CLASS.wide : SOUP_ROW_CLASS.narrow,
        'soup-list-entity @container/entity w-[calc(100%-0.5rem)] mr-1 relative group/narrow flex flex-col py-0.5 rounded-xl',
        {
          'min-h-10 mx-(--soup-row-gutter)': !isMobile(),
          'bg-list-selected': props.checked,
          'bg-list-selected-highlighted':
            props.checked && props.highlighted && !isTouchDevice(),
          'bg-list-highlighted':
            props.highlighted && !props.checked && !isTouchDevice(),
          'hover:bg-list-hover':
            !props.highlighted && !props.checked && !isTouchDevice(),
        }
      )}
      onMouseMove={props.onMouseMove}
    >
      <Switch>
        <Match when={isWide()}>
          <MaybeEntityRow
            entityId={props.entity.id}
            config={props.entityRowConfig}
          >
            <Dynamic component={props.layout} {...layoutProps()} />
          </MaybeEntityRow>
        </Match>
        <Match when={true}>
          <MaybeEntityRow
            entityId={props.entity.id}
            config={props.entityRowConfig}
          >
            <NarrowLayout {...layoutProps()} />
          </MaybeEntityRow>
        </Match>
      </Switch>
    </Entity.Root>
  );
}