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.4 KB
Raw Blame
import { usePropertyEditor } from '@property/hooks/usePropertyEditor';
import { selectablePropertyOptions } from '@property/utils/select-options';
import {
  useAddPropertyOptionMutation,
  usePropertyOptionsQuery,
} from '@queries/properties/options';
import { usablePropertyOptions } from '@queries/properties/options-data';
import { onMount, Show } from 'solid-js';
import { SYSTEM_PROPERTY_IDS } from '../../constants';
import { useProperty } from '../../core/context';
import { PropertyEmptyIcon } from '../../extractors/PropertyEmpty';
import type { PropertyApiValues, SelectProperty } from '../../types';
import { formatOptionValue, isSelectProperty } from '../../utils';
import { PropertyOptionSelector } from '../selectors/PropertyOptionSelector';
import { EditorPopover } from './EditorPopover';

/**
 * Popover dropdown for SELECT_STRING / SELECT_NUMBER. Loads options via
 * usePropertyOptionsQuery, tracks local selection, saves accumulated state
 * on close (matches existing modal UX — single-select closes-on-pick,
 * multi-select stays open until Done/click-out/ESC).
 */
export function SelectEditor() {
  const ctx = useProperty();
  return (
    <Show when={ctx.editorOpen() && isSelectProperty(ctx.property())}>
      <SelectEditorBody />
    </Show>
  );
}

function SelectEditorBody() {
  const ctx = useProperty();
  const property = ctx.property() as SelectProperty;

  const optionsQuery = usePropertyOptionsQuery(
    () => property.propertyDefinitionId
  );
  const addOptionMutation = useAddPropertyOptionMutation({});

  const options = () =>
    selectablePropertyOptions(
      property,
      usablePropertyOptions(optionsQuery, property.options)
    );

  const isLoading = () => optionsQuery.isLoading || addOptionMutation.isPending;

  const editor = usePropertyEditor(
    property,
    options,
    addOptionMutation.mutateAsync
  );

  onMount(() => {
    editor.initializeSelectedOptions();
  });

  const closeAndSave = async () => {
    // Snapshot the selection before closing unmounts this editor. Dismissal
    // must not wait for the network or close a newer editor after a slow save.
    const hasChanges = editor.hasChanges();
    const arr = Array.from(editor.selectedOptions());
    ctx.closeEditor();
    if (!hasChanges) return;

    const apiValues: PropertyApiValues = {
      valueType: property.valueType,
      values: arr.length > 0 ? arr : null,
    };
    try {
      await ctx.onSave?.(property, apiValues);
    } catch {
      // mutation onError owns toast
    }
  };

  const canAddOption = (query: string) => {
    if (property.isSystemProperty) return false;
    if (property.valueType === 'SELECT_STRING') return true;
    if (property.valueType === 'SELECT_NUMBER') {
      const n = parseFloat(query);
      return !Number.isNaN(n) && Number.isFinite(n);
    }
    return false;
  };

  return (
    <EditorPopover onClose={closeAndSave}>
      <Show when={!isLoading()}>
        <PropertyOptionSelector
          config={{
            isMultiSelect: property.isMultiSelect,
            placeholder: `${property.isMultiSelect ? 'Add' : 'Change'} ${property.displayName.toLowerCase()}...`,
            inputType:
              property.valueType === 'SELECT_NUMBER' ? 'number' : 'text',
            canAddOption: property.isSystemProperty ? undefined : canAddOption,
          }}
          options={options().map((opt) => ({
            id: opt.id,
            label: formatOptionValue(opt),
          }))}
          optionIconClass={
            property.propertyDefinitionId === SYSTEM_PROPERTY_IDS.STATUS ||
            property.propertyDefinitionId === SYSTEM_PROPERTY_IDS.PRIORITY
              ? 'size-4'
              : undefined
          }
          isLoading={false}
          error={null}
          selectedOptions={editor.selectedOptions}
          onToggleOption={editor.toggleOption}
          onAddOption={property.isSystemProperty ? undefined : editor.addOption}
          clearOption={
            !property.isMultiSelect && !property.isRequired
              ? {
                  label: `No ${property.displayName.toLowerCase()}`,
                  icon: (
                    <PropertyEmptyIcon
                      propertyDefinitionId={property.propertyDefinitionId}
                      class="size-4 text-ink-extra-muted"
                    />
                  ),
                  onClear: editor.clearOptions,
                }
              : undefined
          }
          onClose={closeAndSave}
        />
      </Show>
    </EditorPopover>
  );
}