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

ComposerCodeBlockLanguagePicker.tsx

102 lines3.4 KB

1import { CheckIcon, ChevronDownIcon } from "lucide-react";
2import { useState } from "react";
3​
4import {
5 CODE_BLOCK_LANGUAGES,
6 type CodeBlockLanguage,
7 codeLanguageEntry,
8 codeLanguageLabel,
9} from "~/composer-code-languages";
10import { syntheticFileNameForLanguageId } from "~/pierre-icons";
11​
12import { Button } from "../ui/button";
13import {
14 Combobox,
15 ComboboxEmpty,
16 ComboboxItem,
17 ComboboxList,
18 ComboboxPopup,
19 ComboboxSearchInput,
20 ComboboxTrigger,
21} from "../ui/combobox";
22import { PierreEntryIcon } from "./PierreEntryIcon";
23​
24function LanguageIcon(props: { language: string; theme: "light" | "dark" }) {
25 const fileName = props.language ? syntheticFileNameForLanguageId(props.language) : "file.txt";
26 return (
27 <PierreEntryIcon pathValue={fileName} kind="file" theme={props.theme} className="size-3.5" />
28 );
30​
31export function ComposerCodeBlockLanguagePicker(props: {
32 language: string;
33 theme: "light" | "dark";
34 disabled: boolean;
35 onChange: (language: string) => void;
36}) {
37 const [query, setQuery] = useState("");
38 const current = codeLanguageEntry(props.language)?.id ?? props.language;
39 const items: CodeBlockLanguage[] = CODE_BLOCK_LANGUAGES.some((entry) => entry.id === current)
40 ? [...CODE_BLOCK_LANGUAGES]
41 : [{ id: current, label: current }, ...CODE_BLOCK_LANGUAGES];
42 const needle = query.trim().toLowerCase();
43 const filteredItems = needle
44 ? items.filter(
45 (entry) => entry.label.toLowerCase().includes(needle) || entry.id.includes(needle),
46 )
47 : items;
48 const selected = items.find((entry) => entry.id === current) ?? null;
49​
50 return (
51 <span className="-ml-1.5 flex min-w-0">
52 <Combobox
53 items={items}
54 filteredItems={filteredItems}
55 autoHighlight
56 itemToStringLabel={(entry) => entry.label}
57 isItemEqualToValue={(a, b) => a.id === b.id}
58 value={selected}
59 onOpenChange={(open) => {
60 if (!open) setQuery("");
61 }}
62 onValueChange={(entry) => {
63 if (entry && entry.id !== current) props.onChange(entry.id);
64 }}
65 >
66 <ComboboxTrigger
67 render={
68 <Button
69 type="button"
70 variant="ghost-muted"
71 size="xs"
72 disabled={props.disabled}
73 aria-label={`Code language: ${codeLanguageLabel(props.language)}`}
74 />
75 }
76 >
77 <LanguageIcon language={current} theme={props.theme} />
78 <span className="max-w-40 truncate">{codeLanguageLabel(props.language)}</span>
79 <ChevronDownIcon className="opacity-60" />
80 </ComboboxTrigger>
81 <ComboboxPopup align="start" className="w-56">
82 <ComboboxSearchInput
83 aria-label="Search languages"
84 placeholder="Search languages"
85 value={query}
86 onChange={(event) => setQuery(event.target.value)}
87 />
88 <ComboboxEmpty>No matching languages.</ComboboxEmpty>
89 <ComboboxList>
90 {(entry: CodeBlockLanguage) => (
91 <ComboboxItem key={entry.id || "plain"} value={entry}>
92 <LanguageIcon language={entry.id} theme={props.theme} />
93 <span className="min-w-0 flex-1 truncate">{entry.label}</span>
94 {entry.id === current ? <CheckIcon className="ml-auto" /> : null}
95 </ComboboxItem>
96 )}
97 </ComboboxList>
98 </ComboboxPopup>
99 </Combobox>
100 </span>
101 );