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