Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

CommandPaletteSettingsItems.tsx

49 lines2.0 KB

1import { useLocation, useNavigate } from "@tanstack/react-router";
2import { decodeMsg, useGT, useMessages } from "gt-react";
3import { SettingsIcon } from "lucide-react";
4​
5import { ITEM_ICON_CLASS, type CommandPaletteActionItem } from "./CommandPalette.logic";
6import { settingsForThisPlatform, SETTINGS_SECTION_LABELS } from "./settings/settingsSearch";
7import { useAvailableSettingsSearchItems } from "./settings/useAvailableSettingsSearchItems";
8​
9const NO_SETTINGS_ITEMS: ReadonlyArray<CommandPaletteActionItem> = [];
10​
11/**
12 * Every setting as a palette row, so settings get the palette's word starts and typo
13 * tolerance. Its own hook so the compiler keeps the rows across keystrokes. Only a search
14 * lists settings, so opening the palette does not translate them.
15 */
16export function useCommandPaletteSettingsItems(
17 searching: boolean,
18): ReadonlyArray<CommandPaletteActionItem> {
19 const gt = useGT();
20 const messages = useMessages();
21 const navigate = useNavigate();
22 const pathname = useLocation({ select: (location) => location.pathname });
23 const availableSettingsSearchItems = useAvailableSettingsSearchItems();
24 if (!searching) return NO_SETTINGS_ITEMS;
25 return settingsForThisPlatform(availableSettingsSearchItems).map((item) => ({
26 kind: "action",
27 value: `setting:${item.id}`,
28 // Matches the English and the displayed language, like the settings search.
29 searchTerms: [
30 item.title,
31 SETTINGS_SECTION_LABELS[item.to],
32 ...(item.searchTerms ?? []),
33 ].flatMap((term) => [messages(term), decodeMsg(term)]),
34 title: messages(item.title),
35 description: gt("Settings · {section}", {
36 section: messages(SETTINGS_SECTION_LABELS[item.to]),
37 }),
38 ...(item.secondary ? { secondary: true } : {}),
39 icon: <SettingsIcon className={ITEM_ICON_CLASS} />,
40 run: async () => {
41 await navigate({
42 to: item.to,
43 hash: item.targetId ?? item.id,
44 replace: pathname === item.to,
45 hashScrollIntoView: false,
46 });
47 },
48 }));