The platform for continuously improving AI agents.
import {
createContext,
type ReactNode,
type RefObject,
useCallback,
useContext,
useEffect,
useMemo,
useRef,
} from "react";
import { Button } from "@/components/ui/button";
import { Icon } from "@/components/ui/icons";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import { SearchFieldContext } from "@/components/ui/search-input";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
import { notify } from "@/lib/notify";
import { cn } from "@/lib/utils";
/** Page-level controls (a view switch) that every list toolbar below ends with. */
export const PageToolbarControls = createContext<ReactNode>(null);
/** Toolbars run at 32px or 36px; 28px is for controls nested inside a popover. */
type ListToolbarSize = "default" | "lg";
function copyCurrentUrl() {
if (typeof navigator === "undefined" || !navigator.clipboard) return;
void navigator.clipboard.writeText(window.location.href).then(
() => notify.success("Link copied"),
() => notify.error("Couldn't copy the link")
);
}
/** `/` focuses search, `r` clears filters, `c` copies the page link. */
export function useListShortcuts({
onClearFilters,
searchRef,
shareable = false,
}: {
onClearFilters?: () => void;
searchRef?: RefObject<HTMLInputElement | null>;
shareable?: boolean;
}) {
// Read through a ref so an inline `onClearFilters` doesn't resubscribe the listener.
const latest = useRef({ onClearFilters, searchRef, shareable });
latest.current = { onClearFilters, searchRef, shareable };
useEffect(() => {
const handler = (e: KeyboardEvent) => {
if (e.defaultPrevented || e.isComposing || e.metaKey || e.ctrlKey || e.altKey) return;
if (e.key !== "/" && e.key !== "r" && e.key !== "c") return;
// Only the active dialog's search may handle shortcuts over a covered list.
const target = e.target as HTMLElement | null;
if (target?.isContentEditable) return;
const tag = target?.tagName;
if (tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT") return;
const current = latest.current;
const dialog = Array.from(document.querySelectorAll('[role="dialog"][data-state="open"]')).at(
-1
);
if (dialog && !dialog.contains(current.searchRef?.current ?? null)) return;
if (e.key === "/") {
const el = current.searchRef?.current;
if (!el) return;
e.preventDefault();
el.focus();
el.select();
} else if (e.key === "r") {
if (!current.onClearFilters) return;
e.preventDefault();
current.onClearFilters();
} else if (current.shareable) {
e.preventDefault();
copyCurrentUrl();
}
};
window.addEventListener("keydown", handler);
return () => window.removeEventListener("keydown", handler);
}, []);
}
export interface ListToolbarProps {
primary?: ReactNode;
/** A `SearchInput`. It inherits `size` and the `/` binding from here. */
search?: ReactNode;
filters?: ReactNode;
actions?: ReactNode;
/** Supplying it renders "Clear filters" while `hasActiveFilters`, and binds `r`. */
onClearFilters?: () => void;
hasActiveFilters?: boolean;
/** The page keeps its filter state in the URL: renders "Copy link" and binds `c`. */
shareable?: boolean;
size?: ListToolbarSize;
className?: string;
/** Extra rows below the control row (quick filters, active filter chips). */
children?: ReactNode;
}
export function ListToolbar({
actions,
children,
className,
filters,
hasActiveFilters = false,
onClearFilters,
primary,
search,
shareable = false,
size = "default",
}: ListToolbarProps) {
const searchRef = useRef<HTMLInputElement | null>(null);
const register = useCallback((el: HTMLInputElement | null) => {
searchRef.current = el;
}, []);
const searchContext = useMemo(() => ({ hint: "/", register, size }), [register, size]);
useListShortcuts({ onClearFilters, searchRef, shareable });
const pageControls = useContext(PageToolbarControls);
// Under a page view switch the row stays one line: the primary action leads as on every
// list, search takes the room, the filters fold into one popover and the switch ends it.
if (pageControls)
return (
<div className={cn("flex w-full min-w-0 flex-col gap-3", className)}>
<div className="flex items-center gap-2">
{primary ? <div className="shrink-0">{primary}</div> : null}
<div className="flex min-w-0 flex-1 [&>*]:w-full">
{search ? (
<SearchFieldContext.Provider value={searchContext}>
{search}
</SearchFieldContext.Provider>
) : null}
</div>
{filters ? (
<Popover>
<PopoverTrigger asChild>
<Button className="shrink-0" size={size} variant="secondary">
<Icon.filter />
Filters
{hasActiveFilters && (
<span
aria-label="Filters applied"
className="size-1.5 rounded-xs bg-primary"
role="img"
/>
)}
</Button>
</PopoverTrigger>
<PopoverContent
align="end"
className="flex w-64 flex-col gap-2 p-3 [&_[data-slot=select-trigger]]:w-full"
>
{filters}
{onClearFilters && hasActiveFilters ? (
<Button onClick={onClearFilters} size="sm" variant="secondary">
<Icon.undo />
Clear filters
</Button>
) : null}
</PopoverContent>
</Popover>
) : null}
{actions}
<div className="shrink-0">{pageControls}</div>
</div>
{children}
</div>
);
return (
<TooltipProvider>
<div className={cn("flex w-full min-w-0 flex-col gap-3", className)}>
<div className="flex flex-wrap items-center gap-2">
{primary}
{search ? (
<SearchFieldContext.Provider value={searchContext}>
{search}
</SearchFieldContext.Provider>
) : null}
{filters}
{onClearFilters && hasActiveFilters ? (
<Tooltip>
<TooltipTrigger asChild>
<Button onClick={onClearFilters} size={size} variant="secondary">
<Icon.undo />
Clear filters
</Button>
</TooltipTrigger>
<TooltipContent side="top" sideOffset={6}>
Clear filters (r)
</TooltipContent>
</Tooltip>
) : null}
{shareable ? (
<Tooltip>
<TooltipTrigger asChild>
<Button
aria-label="Copy link"
onClick={copyCurrentUrl}
size={size === "lg" ? "icon-lg" : "icon"}
variant="secondary"
>
<Icon.copy />
</Button>
</TooltipTrigger>
<TooltipContent side="top" sideOffset={6}>
Copy link (c)
</TooltipContent>
</Tooltip>
) : null}
{actions}
</div>
{children}
</div>
</TooltipProvider>
);
}