Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

4.0 KB
Raw Blame
/**
 * NodePalette — the editable Workflow Canvas's insert palette (issue B5b.2 /
 * Phase 3a). Lists the tinyflows node kinds as {@link PaletteEntry}s grouped
 * into labelled sections (Triggers / Actions / Logic). `tool_call` splits into
 * two entries — an "App action" (Composio OAuth) and a "Tool" (native OpenHuman)
 * — so the two are distinct nodes in the palette. Two ways to add:
 *
 *  - **click** an entry → `onAdd(entry)` (the canvas drops it at a default
 *    position). Keyboard-accessible and the path the unit tests drive.
 *  - **drag** an entry onto the canvas → sets the entry `key` on a
 *    `application/tinyflows-node` payload the canvas's `onDrop` resolves.
 */
import { memo } from 'react';

import { NodeKindTile } from '../../../lib/flows/nodeKindIcons';
import {
  NODE_GROUP_ORDER,
  PALETTE_ENTRIES_BY_GROUP,
  type PaletteEntry,
} from '../../../lib/flows/nodeKindMeta';
import { useT } from '../../../lib/i18n/I18nContext';
import { Button } from '../../ui';

/** dataTransfer MIME key for a palette drag — read by the canvas `onDrop`. */
export const PALETTE_DND_MIME = 'application/tinyflows-node';

interface NodePaletteProps {
  /** Add a node from the given palette entry at the canvas's default position. */
  onAdd: (entry: PaletteEntry) => void;
  /**
   * `overlay` (default) floats over the canvas's top-right corner; `panel`
   * fills a host side panel, with the entries in a two-column grid.
   */
  variant?: 'overlay' | 'panel';
}

function NodePalette({ onAdd, variant = 'overlay' }: NodePaletteProps) {
  const { t } = useT();
  const panel = variant === 'panel';

  return (
    <aside
      className={
        panel
          ? 'flex h-full min-h-0 flex-col'
          : 'pointer-events-auto absolute right-3 top-14 z-10 flex max-h-[calc(100%-4rem)] w-48 flex-col overflow-hidden rounded-xl border border-line bg-surface/95 shadow-xs backdrop-blur'
      }
      data-testid="flow-node-palette"
      aria-label={t('flows.palette.title')}>
      {panel && (
        <p className="px-4 pt-3 text-xs leading-relaxed text-content-muted">
          {t('flows.palette.panelHint')}
        </p>
      )}
      <div
        className={
          panel
            ? 'flex flex-col gap-4 overflow-y-auto p-4'
            : 'flex flex-col gap-2 overflow-y-auto p-2'
        }>
        {NODE_GROUP_ORDER.map(group => (
          <div key={group} className={panel ? 'grid grid-cols-2 gap-1.5' : 'flex flex-col gap-1'}>
            <div
              className={
                panel
                  ? 'col-span-2 text-[11px] font-semibold uppercase tracking-wide text-content-faint'
                  : 'px-1 text-[10px] font-semibold uppercase tracking-wide text-content-faint'
              }>
              {t(`flows.palette.group.${group}`)}
            </div>
            {PALETTE_ENTRIES_BY_GROUP[group].map(entry => {
              const label = t(entry.labelKey, entry.kind);
              return (
                <Button
                  key={entry.key}
                  type="button"
                  variant="secondary"
                  draggable
                  data-testid={`flow-palette-item-${entry.key}`}
                  data-node-kind={entry.kind}
                  onClick={() => onAdd(entry)}
                  onDragStart={event => {
                    event.dataTransfer.setData(PALETTE_DND_MIME, entry.key);
                    event.dataTransfer.effectAllowed = 'copy';
                  }}
                  title={t('flows.palette.addNode').replace('{kind}', label)}
                  className="h-auto justify-start gap-2 px-2 py-1.5 text-left text-xs font-normal">
                  {/* Same tile as the canvas card, scaled down — the swatch the
                      user picks here is the swatch that lands on the graph. */}
                  <NodeKindTile kind={entry.kind} size="sm" />
                  <span className="truncate">{label}</span>
                </Button>
              );
            })}
          </div>
        ))}
      </div>
    </aside>
  );
}

export default memo(NodePalette);