Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

6.0 KB
Raw Blame
// Presentational autocomplete listbox: a pure ARIA listbox over a
// CompletionPlan. No context, no data fetching — it renders sections/options
// and reports picks/highlights to the parent. The live composer wraps it in
// AutocompletePopover (positioning); Storybook renders it directly.

import * as React from "react";
import { Bookmark, Check, Clock, Parentheses, Search } from "@langfuse/icons";

import { cn } from "@/src/utils/tailwind";
import { ScoreTag } from "@/src/components/ScoreTag";
import type {
  CompletionOption,
  CompletionPlan,
} from "@/src/features/search-bar/lib/completions";
import { optionDomId } from "@/src/features/search-bar/components/presentation";

function OptionIcon({ kind }: { kind: CompletionOption["kind"] }) {
  const cls = "flex-none opacity-55";
  if (kind === "recent") return <Clock __rawClassName={cls} aria-hidden />;
  if (kind === "preset") return <Bookmark __rawClassName={cls} aria-hidden />;
  if (kind === "operator" || kind === "pattern")
    return <Parentheses __rawClassName={cls} aria-hidden />;
  return <Search __rawClassName={cls} aria-hidden />;
}

export type AutocompleteListboxProps = {
  plan: CompletionPlan;
  highlightedId: string | null;
  onPick?: (option: CompletionOption) => void;
  onHighlight?: (id: string) => void;
  listboxId?: string;
};

export function AutocompleteListbox({
  plan,
  highlightedId,
  onPick,
  onHighlight,
  listboxId = "search-bar-listbox",
}: AutocompleteListboxProps) {
  // Hover may only highlight on REAL pointer movement. When the list
  // re-renders under a stationary mouse (typing/pasting grows the popover),
  // Chromium fires synthetic mouseover events — honoring those would arm
  // Enter with an option the user never chose (keyboard keeps authority).
  const lastPointer = React.useRef<{ x: number; y: number } | null>(null);

  // Keyboard navigation only moves the highlight id; the listbox is scrollable
  // and the empty-stage plan has ~50 options, so the active option can fall
  // outside the viewport. Scroll it back into view (the UA does not do this for
  // aria-activedescendant). `nearest` avoids jumping when already visible.
  const optionRefs = React.useRef(new Map<string, HTMLDivElement>());
  React.useEffect(() => {
    if (highlightedId === null) return;
    optionRefs.current.get(highlightedId)?.scrollIntoView?.({
      block: "nearest",
    });
  }, [highlightedId]);

  const highlightOnMove = onHighlight
    ? (id: string) => (e: React.MouseEvent) => {
        const last = lastPointer.current;
        if (last !== null && last.x === e.clientX && last.y === e.clientY)
          return;
        lastPointer.current = { x: e.clientX, y: e.clientY };
        onHighlight(id);
      }
    : undefined;
  return (
    <div
      id={listboxId}
      role="listbox"
      aria-label="Search suggestions"
      data-testid="search-bar-autocomplete"
      data-stage={plan.stage}
      className={cn(
        "ph-no-capture w-max max-w-[calc(100vw-16px)] min-w-[min(420px,calc(100vw-16px))]",
        "max-h-[min(420px,40vh)] overflow-x-hidden overflow-y-auto",
        "bg-popover text-popover-foreground rounded-md border py-1 shadow-md",
      )}
    >
      {plan.loading && (
        <div
          data-testid="search-bar-autocomplete-loading"
          className="text-muted-foreground mx-1 flex min-h-8 items-center gap-2 px-3 text-xs"
        >
          Loading values…
        </div>
      )}
      {!plan.loading && plan.sections.length === 0 && (
        <div className="text-muted-foreground mx-1 flex min-h-8 items-center gap-2 px-3 text-xs">
          No suggestions
        </div>
      )}
      {plan.sections.map((sec, i) => (
        <div
          key={sec.title}
          role="group"
          aria-label={sec.title}
          className={cn(i > 0 && "mt-1.5 border-t pt-1.5")}
        >
          <div
            data-testid="search-bar-autocomplete-section"
            className="text-muted-foreground px-3 pt-1.5 pb-1 text-[10px] tracking-[0.06em] uppercase"
          >
            {sec.title}
          </div>
          {sec.options.map((o) => (
            <div
              key={o.id}
              ref={(el) => {
                const refs = optionRefs.current;
                if (el) refs.set(o.id, el);
                else refs.delete(o.id);
              }}
              id={optionDomId(listboxId, o.id)}
              role="option"
              aria-selected={o.id === highlightedId}
              data-option-id={o.id}
              className={cn(
                "mx-1 flex min-h-8 cursor-pointer items-center gap-2 rounded-sm px-2",
                "font-mono text-xs leading-[1.4]",
                o.id === highlightedId
                  ? "bg-accent text-accent-foreground"
                  : "hover:bg-accent/50",
              )}
              // mousedown must not blur the composer input mid-pick
              onMouseDown={(e) => e.preventDefault()}
              onClick={onPick ? () => onPick(o) : undefined}
              onMouseMove={highlightOnMove?.(o.id)}
            >
              <OptionIcon kind={o.kind} />
              <span
                className={cn(
                  "max-w-[480px] min-w-0 flex-none truncate",
                  o.kind === "pattern" && o.textClassName,
                )}
                title={o.label}
              >
                {o.label}
              </span>
              {o.kind === "field" &&
                o.scoreLevels?.map((level) => (
                  <ScoreTag key={level} level={level} />
                ))}
              {o.kind === "value" && o.active && (
                <Check
                  size="base"
                  __rawClassName="text-foreground/80 flex-none"
                  aria-label="selected"
                />
              )}
              {"detail" in o && o.detail !== undefined && (
                <span className="text-muted-foreground ml-auto pl-6 font-sans text-[11px]">
                  {o.detail}
                </span>
              )}
            </div>
          ))}
        </div>
      ))}
    </div>
  );
}