All files / src/components/chat/CommandPalette CommandPalette.tsx

93.33% Statements 14/15
88.88% Branches 16/18
83.33% Functions 5/6
92.85% Lines 13/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127                                                579x   14x             160x     160x 17x     17x     160x                     245x     245x 245x 245x                                 1x 1x                                                                                                  
import { BuiltInBadge } from "@/components/badge/BuiltInBadge";
import type { CommandEntry } from "@/util/chatCommands";
import { Workflow } from "lucide-react";
import React, { useEffect, useRef } from "react";
 
/**
 * The chat's command palette: the command lines a typed `/` can complete to,
 * grouped by command, from svc-core's catalogue, each with how many inputs
 * its form takes. Keyboard-first — the input keeps focus and owns the keys
 * (↑ ↓ to move, Enter or Tab to choose, Esc to close); a click chooses too.
 * Rendered in the input area's flow, not as an overlay, so the panel's
 * `overflow-hidden` can never clip it.
 */
 
export interface CommandPaletteProps {
  /** The listbox's id; options are `${id}-${index}`, for the input's
   *  `aria-activedescendant`. */
  id: string;
  entries: CommandEntry[];
  activeIndex: number;
  onPick: (entry: CommandEntry) => void;
  onActivate: (index: number) => void;
}
 
export const optionId = (id: string, index: number) => `${id}-${index}`;
 
const CommandPalette: React.FC<CommandPaletteProps> = ({
  id,
  entries,
  activeIndex,
  onPick,
  onActivate,
}) => {
  const listRef = useRef<HTMLDivElement>(null);
 
  // Keep the active option in view as the arrow keys move through a long list.
  useEffect(() => {
    const active = listRef.current?.ownerDocument.getElementById(
      optionId(id, activeIndex),
    );
    active?.scrollIntoView?.({ block: "nearest" });
  }, [id, activeIndex]);
 
  return (
    <div className="rounded-lg border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-900 shadow-sm overflow-hidden">
      <div
        ref={listRef}
        id={id}
        role="listbox"
        aria-label="Commands"
        className="max-h-64 overflow-y-auto py-1"
      >
        {entries.map((entry, index) => {
          const heading =
            index === 0 || entries[index - 1].spec !== entry.spec
              ? entry.spec.label
              : null;
          const active = index === activeIndex;
          const inputs = entry.target.form?.fields.length ?? 0;
          return (
            <React.Fragment key={entry.line}>
              {heading && (
                <div
                  role="presentation"
                  className="px-3 pt-2 pb-1 text-[10px] font-semibold uppercase tracking-wide text-gray-400 dark:text-gray-500"
                >
                  {heading}
                </div>
              )}
              <div
                id={optionId(id, index)}
                role="option"
                aria-selected={active}
                // mousedown, not click: the input keeps focus until the
                // chat moves it to the chosen command's form.
                onMouseDown={(e) => {
                  e.preventDefault();
                  onPick(entry);
                }}
                onMouseEnter={() => onActivate(index)}
                className={`flex cursor-pointer items-start gap-2.5 px-3 py-2 ${
                  active
                    ? "bg-indigo-50 dark:bg-indigo-950/40"
                    : "hover:bg-gray-50 dark:hover:bg-gray-800/60"
                }`}
              >
                <Workflow
                  className="mt-0.5 size-4 shrink-0 text-indigo-500 dark:text-indigo-400"
                  aria-hidden="true"
                />
                <div className="min-w-0 flex-1">
                  <div className="flex items-center gap-1.5">
                    <span className="truncate text-sm font-medium text-gray-800 dark:text-gray-100">
                      {entry.target.name}
                    </span>
                    {entry.target.built_in && <BuiltInBadge size="sm" />}
                  </div>
                  <div className="flex min-w-0 items-baseline gap-1.5">
                    <code className="truncate text-xs text-gray-500 dark:text-gray-400">
                      {entry.line}
                    </code>
                    {inputs > 0 && (
                      <span className="shrink-0 text-[10px] text-gray-400 dark:text-gray-500">
                        {inputs} input{inputs === 1 ? "" : "s"}
                      </span>
                    )}
                  </div>
                  {entry.target.description && (
                    <p className="truncate text-xs text-gray-400 dark:text-gray-500">
                      {entry.target.description}
                    </p>
                  )}
                </div>
              </div>
            </React.Fragment>
          );
        })}
      </div>
      <p className="border-t border-gray-100 dark:border-gray-800 px-3 py-1.5 text-[10px] text-gray-400 dark:text-gray-500">
        ↑ ↓ to move · Enter or Tab to choose · Esc to close
      </p>
    </div>
  );
};
 
export default CommandPalette;