Skip to content

g1t/apps/web/app/components/command-palette-dialog.tsx

189 lines8,177 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 { Command as Cmdk } from "cmdk";
2import { Code2, CornerDownLeft, Search } from "lucide-react";
3import { Dialog as Primitive } from "radix-ui";
4import { useEffect, useState } from "react";
5import { useFetcher, useNavigate } from "react-router";
6
7import type { SiteHit } from "@g1t/contracts";
8
9import { searchHref } from "../lib/search";
10import { hitIcon } from "./search";
11import type { PaletteCommand } from "./command-palette";
12import { Command, CommandEmpty, CommandGroup, CommandItem, CommandList } from "./ui/combobox";
Fast pages, required checks on the branch, self-hosted runners, honest incidents13import { SkeletonRows } from "./ui/skeleton";
Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look14
15/** How long typing has to pause before results are asked for. */
16const PAUSE_MS = 140;
17
18function hitHint(hit: SiteHit): string {
19 switch (hit.kind) {
20 case "repository":
21 return "Repository";
22 case "issue":
23 return `${hit.repo} #${hit.number}`;
24 case "pull":
25 return `${hit.repo} #${hit.number}`;
26 case "user":
27 return `Person · ${hit.slug}`;
28 case "workspace":
29 return `Workspace · ${hit.slug}`;
30 default:
31 return hit.repo ?? "";
32 }
33}
34
35/**
36 * ⌘K: pages and actions from what the page already knows, and, as someone
37 * types, repositories, issues, pull requests and people from search,
38 * public ones for anyone and private ones for their workspaces' members.
39 * The first row always searches all of g1t for what was typed.
40 */
41export default function CommandPaletteDialog({
42 open,
43 onOpenChange,
44 commands,
45 repo,
Usage, Billing settings and prepaid AI credit; fixes from the UX audit46 onCloseAutoFocus,
Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look47}: {
48 open: boolean;
49 onOpenChange: (open: boolean) => void;
50 commands: PaletteCommand[];
51 /** The repository being looked at, `owner/name`, to offer searching its code. */
52 repo?: string | null;
Usage, Billing settings and prepaid AI credit; fixes from the UX audit53 /** Where focus goes once it closes (command-palette.tsx). */
54 onCloseAutoFocus?: (event: Event) => void;
Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look55}) {
56 const navigate = useNavigate();
57 const fetcher = useFetcher<{ q: string; hits: SiteHit[] }>();
58 const [query, setQuery] = useState("");
59 const trimmed = query.trim();
60
61 useEffect(() => {
62 if (open) setQuery("");
63 }, [open]);
64 useEffect(() => {
65 if (trimmed.length < 2) return;
66 const timer = setTimeout(() => fetcher.load(`/search.json?q=${encodeURIComponent(trimmed)}`), PAUSE_MS);
67 return () => clearTimeout(timer);
68 // `fetcher` changes identity as it loads; only the text should restart it.
69 // eslint-disable-next-line react-hooks/exhaustive-deps
70 }, [trimmed]);
71
72 const hits = trimmed.length >= 2 && fetcher.data?.q === trimmed ? fetcher.data.hits : [];
73 const go = (to: string) => {
74 onOpenChange(false);
75 // The docs and other sites are full page loads.
76 if (/^https?:\/\//.test(to)) window.location.assign(to);
77 else navigate(to);
78 };
79 const words = trimmed.toLowerCase().split(/\s+/).filter(Boolean);
80 const pages = commands
81 .filter((command) => {
82 const text = `${command.label} ${command.hint ?? ""}`.toLowerCase();
83 return words.every((word) => text.includes(word));
84 })
85 .slice(0, trimmed ? 8 : 12);
86 const itemClass = "gap-3 rounded-lg px-3 py-2";
87
88 return (
89 <Primitive.Root open={open} onOpenChange={onOpenChange}>
90 <Primitive.Portal>
91 <Primitive.Overlay className="fixed inset-0 z-50 bg-black/50 backdrop-blur-sm data-[state=open]:animate-fade-in data-[state=closed]:animate-fade-out motion-reduce:animate-none" />
92 <Primitive.Content
93 aria-describedby={undefined}
Usage, Billing settings and prepaid AI credit; fixes from the UX audit94 onCloseAutoFocus={onCloseAutoFocus}
Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look95 className="fixed top-[12vh] left-1/2 z-50 w-[calc(100vw-2rem)] max-w-xl -translate-x-1/2 overflow-hidden rounded-xl border border-line-strong bg-raised shadow-2xl shadow-black/60 outline-none"
96 >
97 <Primitive.Title className="sr-only">Go to or search</Primitive.Title>
98 {/* Filtering is done here: search already filtered its results. */}
99 <Command shouldFilter={false} loop>
100 <div className="flex items-center gap-3 border-b border-line px-4">
101 <Search size={16} className="shrink-0 text-faint" />
102 <Cmdk.Input
103 value={query}
104 onValueChange={setQuery}
105 placeholder="Search g1t, or go to a page…"
106 autoComplete="off"
107 data-1p-ignore
108 className="h-12 grow border-0 bg-transparent text-sm shadow-none outline-none placeholder:text-faint focus-visible:outline-none"
109 />
110 <kbd className="rounded border border-line px-1.5 font-mono text-[0.6875rem] text-faint">esc</kbd>
111 </div>
112 <CommandList className="max-h-[55vh] p-1.5">
113 <CommandEmpty>Nothing matches.</CommandEmpty>
114 {trimmed && (
115 <CommandGroup heading="Search">
116 <CommandItem value="search:all" onSelect={() => go(searchHref(trimmed))} className={itemClass}>
117 <Search />
118 <span className="min-w-0 grow truncate">
119 Search g1t for <span className="font-medium text-fg">“{trimmed}”</span>
120 </span>
121 <CornerDownLeft className="size-3.5! text-faint!" />
122 </CommandItem>
123 <CommandItem value="search:code" onSelect={() => go(searchHref(trimmed, "code"))} className={itemClass}>
124 <Code2 />
125 <span className="min-w-0 grow truncate">
126 Search code for <span className="font-medium text-fg">“{trimmed}”</span>
127 </span>
128 </CommandItem>
129 {repo && (
130 <CommandItem
131 value="search:repo"
132 onSelect={() => go(searchHref(`${trimmed} repo:${repo}`, "code"))}
133 className={itemClass}
134 >
135 <Code2 />
136 <span className="min-w-0 grow truncate">
137 Search <span className="font-mono">{repo}</span> for{" "}
138 <span className="font-medium text-fg">“{trimmed}”</span>
139 </span>
140 </CommandItem>
141 )}
142 </CommandGroup>
143 )}
144 {hits.length > 0 && (
145 <CommandGroup heading="Results">
146 {hits.map((hit) => (
147 <CommandItem
148 key={`${hit.kind}:${hit.url}`}
149 value={`hit:${hit.kind}:${hit.url}`}
150 onSelect={() => go(hit.url)}
151 className={itemClass}
152 >
153 {hitIcon(hit)}
154 <span className="min-w-0 grow truncate">
155 {hit.kind === "repository" ? <span className="font-mono">{hit.title}</span> : hit.title}
156 </span>
157 <span className="shrink-0 truncate text-xs text-faint">{hitHint(hit)}</span>
158 </CommandItem>
159 ))}
160 </CommandGroup>
161 )}
162 {pages.length > 0 && (
163 <CommandGroup heading="Go to">
164 {pages.map((command) => (
165 <CommandItem
166 key={`${command.to}-${command.label}`}
167 value={`page:${command.to}:${command.label}`}
168 onSelect={() => go(command.to)}
169 className={itemClass}
170 >
171 <span className="shrink-0 text-faint">{command.icon}</span>
172 <span className="min-w-0 grow truncate">{command.label}</span>
173 {command.hint && <span className="shrink-0 truncate text-xs text-faint">{command.hint}</span>}
174 </CommandItem>
175 ))}
176 </CommandGroup>
177 )}
178 </CommandList>
179 </Command>
180 {hits.length === 0 && fetcher.state === "loading" && trimmed.length >= 2 && (
Fast pages, required checks on the branch, self-hosted runners, honest incidents181 <div aria-busy="true" className="border-t border-line">
182 <SkeletonRows rows={3} rowClassName="h-9 px-4" />
183 </div>
Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look184 )}
185 </Primitive.Content>
186 </Primitive.Portal>
187 </Primitive.Root>
188 );
189}