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.7 KB
Raw Blame
import BuildingsIcon from '@icon/system/regular/buildings.svg';
import CaretDownIcon from '@icon/system/regular/caret-down.svg';
import CheckIcon from '@icon/system/regular/check.svg';
import SearchIcon from '@icon/system/regular/magnifying-glass.svg';
import type { CollectionNode } from '@kobalte/core';
import { Combobox } from '@kobalte/core/combobox';
import { badgeTriggerClasses, Layer } from '@ui';
import { type Accessor, createSignal, Show } from 'solid-js';
import { Virtualizer, type VirtualizerHandle } from 'virtua/solid';

export type CompanyOption = { id: string; name: string; domain?: string };

const ITEM_HEIGHT = 32;

function CompanyListbox() {
  let handle: VirtualizerHandle | undefined;
  // Kobalte hands back the filtered collection, so scrolling indexes into it.
  let visible: Accessor<Iterable<CollectionNode<CompanyOption>>> | undefined;
  return (
    <Combobox.Listbox<CompanyOption>
      scrollToItem={(key) => {
        const index = Array.from(visible?.() ?? []).findIndex(
          (item) => item.rawValue.id === key
        );
        if (index !== -1) handle?.scrollToIndex(index, { align: 'nearest' });
      }}
      class="max-h-60 overflow-y-auto scrollbar-hidden"
    >
      {(items) => {
        visible = items;
        return (
          <Virtualizer
            ref={(h) => (handle = h)}
            data={[...items()]}
            itemSize={ITEM_HEIGHT}
          >
            {(item) => (
              <Combobox.Item
                item={item}
                class="flex w-full cursor-default items-center gap-1.5 rounded-lg p-1.5 px-2 text-left text-sm data-highlighted:bg-ink/5"
              >
                <span class="flex size-3.5 shrink-0 items-center justify-center text-accent">
                  <Combobox.ItemIndicator>
                    <CheckIcon class="size-3" />
                  </Combobox.ItemIndicator>
                </span>
                <Combobox.ItemLabel class="min-w-0 truncate text-ink">
                  {item.rawValue.name}
                </Combobox.ItemLabel>
                <span class="ml-auto min-w-0 shrink-[2] truncate text-xs text-ink-extra-muted">
                  {item.rawValue.domain}
                </span>
              </Combobox.Item>
            )}
          </Virtualizer>
        );
      }}
    </Combobox.Listbox>
  );
}

/** Searchable single-company picker, matching on name or domain. */
export function CompanySelect(props: {
  id?: string;
  companies: CompanyOption[];
  value: CompanyOption | undefined;
  onChange: (company: CompanyOption) => void;
  loading: boolean;
  disabled?: boolean;
  /** Mount the menu here, e.g. a modal dialog's content, which hides body portals. */
  mount?: HTMLElement;
}) {
  const [search, setSearch] = createSignal('');
  let trigger: HTMLButtonElement | undefined;
  const matches = (option: CompanyOption, query: string) =>
    option.name.toLowerCase().includes(query) ||
    (option.domain?.toLowerCase().includes(query) ?? false);
  const hasMatches = () => {
    const query = search().trim().toLowerCase();
    return props.companies.some((option) => matches(option, query));
  };
  return (
    <Combobox<CompanyOption>
      multiple={false}
      disabled={props.disabled}
      options={props.companies}
      value={props.value ?? null}
      onChange={(option) => option && props.onChange(option)}
      onInputChange={setSearch}
      onOpenChange={(open) => !open && setSearch('')}
      optionValue="id"
      optionTextValue={(option) => `${option.name} ${option.domain ?? ''}`}
      // The trigger shows the selection; keep the search box empty on open.
      optionLabel={() => ''}
      defaultFilter={(option, input) =>
        matches(option, input.trim().toLowerCase())
      }
      virtualized
      allowsEmptyCollection
      placement="bottom-start"
      gutter={4}
    >
      <Combobox.Control>
        <Combobox.Trigger
          ref={trigger}
          id={props.id}
          type="button"
          // Kobalte's default name is "Show suggestions".
          aria-label={
            props.value ? `Company: ${props.value.name}` : 'Choose a company'
          }
          class={badgeTriggerClasses({
            variant: 'outline',
            size: 'sm',
            class: 'max-w-full min-w-0 text-left',
          })}
        >
          <BuildingsIcon class="size-3 shrink-0" />
          <Show
            when={props.value}
            fallback={
              <span class="truncate text-ink-extra-muted">
                {props.loading ? 'Loading companies…' : 'Choose a company'}
              </span>
            }
          >
            {(company) => (
              <span class="flex min-w-0 items-baseline gap-1.5">
                <span class="truncate">{company().name}</span>
                <span class="truncate text-xs text-ink-extra-muted">
                  {company().domain}
                </span>
              </span>
            )}
          </Show>
          <CaretDownIcon class="size-3 shrink-0 text-ink-muted" />
        </Combobox.Trigger>
      </Combobox.Control>
      <Combobox.Portal mount={props.mount}>
        <Layer depth={2}>
          <Combobox.Content
            class="menu-surface z-action-menu w-72 overflow-hidden"
            onCloseAutoFocus={(event) => {
              // The input lives inside this menu and is being removed. Return
              // focus to the trigger so the containing drawer stays open.
              event.preventDefault();
              trigger?.focus({ preventScroll: true });
            }}
          >
            <div class="flex items-center gap-2 border-b border-edge-muted px-3 py-2">
              <SearchIcon class="size-3.5 shrink-0 text-ink-muted" />
              <Combobox.Input
                class="min-w-0 flex-1 bg-transparent text-sm caret-accent outline-none placeholder:text-ink-placeholder"
                placeholder="Search companies..."
              />
            </div>
            {/* Pressing an option must not move focus to the dialog, which
                closes the menu before the click selects it. */}
            <div class="p-1" onMouseDown={(event) => event.preventDefault()}>
              <Show
                when={hasMatches()}
                fallback={
                  <div class="px-2 py-3 text-center text-xs text-ink-muted">
                    {props.loading
                      ? 'Loading companies…'
                      : search().trim()
                        ? `No companies match "${search().trim()}"`
                        : 'No companies yet'}
                  </div>
                }
              >
                <CompanyListbox />
              </Show>
            </div>
          </Combobox.Content>
        </Layer>
      </Combobox.Portal>
    </Combobox>
  );
}