|
| 1 | +import { CommandLineIcon } from "@heroicons/react/20/solid"; |
| 2 | +import * as Ariakit from "@ariakit/react"; |
| 3 | +import { type ReactNode, useMemo } from "react"; |
| 4 | +import { AppliedFilter } from "~/components/primitives/AppliedFilter"; |
| 5 | +import { |
| 6 | + ComboBox, |
| 7 | + SelectItem, |
| 8 | + SelectList, |
| 9 | + SelectPopover, |
| 10 | + SelectProvider, |
| 11 | + SelectTrigger, |
| 12 | +} from "~/components/primitives/Select"; |
| 13 | +import { useSearchParams } from "~/hooks/useSearchParam"; |
| 14 | +import { appliedSummary, FilterMenuProvider } from "~/components/runs/v3/SharedFilters"; |
| 15 | + |
| 16 | +const shortcut = { key: "n" }; |
| 17 | + |
| 18 | +interface OperationsFilterProps { |
| 19 | + possibleOperations: string[]; |
| 20 | +} |
| 21 | + |
| 22 | +/** Pretty-print an operation ID like "ai.generateText.doGenerate" → "generateText" */ |
| 23 | +function formatOperation(op: string): string { |
| 24 | + const parts = op.split("."); |
| 25 | + // ai.generateText.doGenerate → generateText |
| 26 | + // ai.streamText.doStream → streamText |
| 27 | + if (parts.length >= 2 && parts[0] === "ai") { |
| 28 | + return parts[1]; |
| 29 | + } |
| 30 | + return op; |
| 31 | +} |
| 32 | + |
| 33 | +export function OperationsFilter({ possibleOperations }: OperationsFilterProps) { |
| 34 | + const { values, replace, del } = useSearchParams(); |
| 35 | + const selectedOperations = values("operations"); |
| 36 | + |
| 37 | + if (selectedOperations.length === 0 || selectedOperations.every((v) => v === "")) { |
| 38 | + return ( |
| 39 | + <FilterMenuProvider> |
| 40 | + {(search, setSearch) => ( |
| 41 | + <OperationsDropdown |
| 42 | + trigger={ |
| 43 | + <SelectTrigger |
| 44 | + icon={<CommandLineIcon className="size-4" />} |
| 45 | + variant="secondary/small" |
| 46 | + shortcut={shortcut} |
| 47 | + tooltipTitle="Filter by operation" |
| 48 | + > |
| 49 | + <span className="ml-0.5">Operations</span> |
| 50 | + </SelectTrigger> |
| 51 | + } |
| 52 | + searchValue={search} |
| 53 | + clearSearchValue={() => setSearch("")} |
| 54 | + possibleOperations={possibleOperations} |
| 55 | + /> |
| 56 | + )} |
| 57 | + </FilterMenuProvider> |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + return ( |
| 62 | + <FilterMenuProvider> |
| 63 | + {(search, setSearch) => ( |
| 64 | + <OperationsDropdown |
| 65 | + trigger={ |
| 66 | + <Ariakit.Select render={<div className="group cursor-pointer focus-custom" />}> |
| 67 | + <AppliedFilter |
| 68 | + label="Operation" |
| 69 | + icon={<CommandLineIcon className="size-4" />} |
| 70 | + value={appliedSummary(selectedOperations.map(formatOperation))} |
| 71 | + onRemove={() => del(["operations"])} |
| 72 | + variant="secondary/small" |
| 73 | + /> |
| 74 | + </Ariakit.Select> |
| 75 | + } |
| 76 | + searchValue={search} |
| 77 | + clearSearchValue={() => setSearch("")} |
| 78 | + possibleOperations={possibleOperations} |
| 79 | + /> |
| 80 | + )} |
| 81 | + </FilterMenuProvider> |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | +function OperationsDropdown({ |
| 86 | + trigger, |
| 87 | + clearSearchValue, |
| 88 | + searchValue, |
| 89 | + onClose, |
| 90 | + possibleOperations, |
| 91 | +}: { |
| 92 | + trigger: ReactNode; |
| 93 | + clearSearchValue: () => void; |
| 94 | + searchValue: string; |
| 95 | + onClose?: () => void; |
| 96 | + possibleOperations: string[]; |
| 97 | +}) { |
| 98 | + const { values, replace } = useSearchParams(); |
| 99 | + |
| 100 | + const handleChange = (values: string[]) => { |
| 101 | + clearSearchValue(); |
| 102 | + replace({ operations: values }); |
| 103 | + }; |
| 104 | + |
| 105 | + const filtered = useMemo(() => { |
| 106 | + const q = searchValue.toLowerCase(); |
| 107 | + return possibleOperations.filter( |
| 108 | + (op) => op.toLowerCase().includes(q) || formatOperation(op).toLowerCase().includes(q) |
| 109 | + ); |
| 110 | + }, [searchValue, possibleOperations]); |
| 111 | + |
| 112 | + return ( |
| 113 | + <SelectProvider value={values("operations")} setValue={handleChange} virtualFocus={true}> |
| 114 | + {trigger} |
| 115 | + <SelectPopover |
| 116 | + className="min-w-0 max-w-[min(360px,var(--popover-available-width))]" |
| 117 | + hideOnEscape={() => { |
| 118 | + if (onClose) { |
| 119 | + onClose(); |
| 120 | + return false; |
| 121 | + } |
| 122 | + return true; |
| 123 | + }} |
| 124 | + > |
| 125 | + <ComboBox placeholder="Filter by operation..." value={searchValue} /> |
| 126 | + <SelectList> |
| 127 | + {filtered.map((op) => ( |
| 128 | + <SelectItem key={op} value={op} icon={<CommandLineIcon className="size-4" />}> |
| 129 | + {formatOperation(op)} |
| 130 | + </SelectItem> |
| 131 | + ))} |
| 132 | + {filtered.length === 0 && <SelectItem disabled>No operations found</SelectItem>} |
| 133 | + </SelectList> |
| 134 | + </SelectPopover> |
| 135 | + </SelectProvider> |
| 136 | + ); |
| 137 | +} |
0 commit comments