pr_01m47d15m3e54sn21z27rpy5n9/apps/web/app/components/command-palette.tsx

51 lines1,683 bytesCodeBlame

Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.

Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look1import { type ReactNode, Suspense, lazy, useEffect, useRef, useState } from "react";
Search across all of g1t, Explore, and a command palette2
Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look3/*
4 * The ⌘K palette. The dialog itself, with its search and its dependencies,
5 * loads the first time it is opened, so pages that never open it do not
6 * carry it.
7 */
8const Dialog = lazy(() => import("./command-palette-dialog"));
Search across all of g1t, Explore, and a command palette9
10/** A page or action the palette can jump to. */
11export type PaletteCommand = { label: string; hint?: string; to: string; icon: ReactNode };
12
13/** Opens and closes the palette on ⌘K or Ctrl-K, from anywhere on the page. */
14export function usePaletteShortcut(toggle: () => void) {
15 const latest = useRef(toggle);
16 latest.current = toggle;
17 useEffect(() => {
18 const onKey = (event: KeyboardEvent) => {
19 if ((event.metaKey || event.ctrlKey) && event.key.toLowerCase() === "k") {
20 event.preventDefault();
21 latest.current();
22 }
23 };
24 window.addEventListener("keydown", onKey);
25 return () => window.removeEventListener("keydown", onKey);
26 }, []);
27}
28
29/**
30 * ⌘K: pages and actions from what the page already knows, and, as someone
Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look31 * types, repositories, issues, pull requests and people from search
32 * (command-palette-dialog.tsx).
Search across all of g1t, Explore, and a command palette33 */
Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look34export function CommandPalette(props: {
Search across all of g1t, Explore, and a command palette35 open: boolean;
36 onOpenChange: (open: boolean) => void;
37 commands: PaletteCommand[];
38 /** The repository being looked at, `owner/name`, to offer searching its code. */
39 repo?: string | null;
40}) {
Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look41 const [wanted, setWanted] = useState(props.open);
Search across all of g1t, Explore, and a command palette42 useEffect(() => {
Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look43 if (props.open) setWanted(true);
44 }, [props.open]);
45 if (!wanted) return null;
Search across all of g1t, Explore, and a command palette46 return (
Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look47 <Suspense fallback={null}>
48 <Dialog {...props} />
49 </Suspense>
Search across all of g1t, Explore, and a command palette50 );
51}