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

CommandPaletteSettingsItems.tsx

52 lines2.0 KB

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