The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { thrownErrorMessage } from "@t3tools/client-runtime/errors"; |
| 2 | import { m as resolveMessage } from "~/i18n/messages"; |
| 3 | import { useAtomValue } from "@effect/atom-react"; |
| 4 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 5 | import { isWindowsAbsolutePath } from "@t3tools/shared/path"; |
| 6 | import { msg, useGT, useMessages } from "gt-react"; |
| 7 | import { useState } from "react"; |
| 8 | |
| 9 | import type { MessageResolver } from "~/i18n/messages"; |
| 10 | import { primaryServerKeybindingsAtom } from "~/state/server"; |
| 11 | import { useTheme } from "~/hooks/useTheme"; |
| 12 | import { getLocalFileManagerName, isWindowsPlatform } from "~/lib/utils"; |
| 13 | import { CommandPaletteContent } from "../CommandPaletteContent"; |
| 14 | import type { CommandPaletteActionItem } from "../CommandPalette.logic"; |
| 15 | import { CommandPaletteResults } from "../CommandPaletteResults"; |
| 16 | import { PierreEntryIcon } from "../chat/PierreEntryIcon"; |
| 17 | import { |
| 18 | getProjectFilePickerMatches, |
| 19 | PROJECT_FILE_PICKER_RESULT_LIMIT, |
| 20 | } from "../files/ProjectFilePicker.logic"; |
| 21 | import { useProjectFilePickerQuery } from "../files/projectFilesQueryState"; |
| 22 | import { CommandDialog, CommandDialogPopup, CommandFooterAction } from "../ui/command"; |
| 23 | import { toastManager } from "../ui/toast"; |
| 24 | import { ProjectFaviconThumbnail } from "./ProjectFaviconThumbnail"; |
| 25 | |
| 26 | function emptyMessage( |
| 27 | query: string, |
| 28 | error: string | null, |
| 29 | isPending: boolean, |
| 30 | resolve: MessageResolver, |
| 31 | ): string { |
| 32 | if (error) return error; |
| 33 | if (isPending) |
| 34 | return query.trim() |
| 35 | ? resolve(msg("Searching project files…")) |
| 36 | : resolve(msg("Indexing project files…")); |
| 37 | return query.trim() |
| 38 | ? resolve(msg("No matching image files.")) |
| 39 | : resolve(msg("No image files found.")); |
| 40 | } |
| 41 | export function canPickExternalProjectFavicon(cwd: string, platform: string): boolean { |
| 42 | return !isWindowsPlatform(platform) || isWindowsAbsolutePath(cwd); |
| 43 | } |
| 44 | |
| 45 | export function ProjectFaviconPickerDialog(props: { |
| 46 | readonly cwd: string; |
| 47 | readonly environmentId: EnvironmentId; |
| 48 | readonly onOpenChange: (open: boolean) => void; |
| 49 | readonly onPickExternal?: () => Promise<string | null>; |
| 50 | readonly onSelect: (path: string) => void; |
| 51 | readonly open: boolean; |
| 52 | readonly projectName: string; |
| 53 | }) { |
| 54 | const gt = useGT(); |
| 55 | const m = useMessages(); |
| 56 | const [query, setQuery] = useState(""); |
| 57 | const [highlightedItemValue, setHighlightedItemValue] = useState<string | null>(null); |
| 58 | const [isPickingExternal, setIsPickingExternal] = useState(false); |
| 59 | const result = useProjectFilePickerQuery( |
| 60 | props.environmentId, |
| 61 | props.cwd, |
| 62 | query, |
| 63 | PROJECT_FILE_PICKER_RESULT_LIMIT, |
| 64 | { imageOnly: true }, |
| 65 | ); |
| 66 | const { resolvedTheme } = useTheme(); |
| 67 | const keybindings = useAtomValue(primaryServerKeybindingsAtom); |
| 68 | const { onPickExternal: pickExternal, onSelect } = props; |
| 69 | const fileManagerName = getLocalFileManagerName( |
| 70 | typeof navigator === "undefined" ? "" : navigator.platform, |
| 71 | m, |
| 72 | ); |
| 73 | const items: CommandPaletteActionItem[] = getProjectFilePickerMatches( |
| 74 | result.entries, |
| 75 | result.matchedQuery, |
| 76 | ).map((match) => ({ |
| 77 | kind: "action", |
| 78 | value: `project-favicon:${match.path}`, |
| 79 | searchTerms: [match.name, match.path], |
| 80 | title: match.name, |
| 81 | description: match.path, |
| 82 | icon: ( |
| 83 | <ProjectFaviconThumbnail |
| 84 | cwd={props.cwd} |
| 85 | environmentId={props.environmentId} |
| 86 | fallback={<PierreEntryIcon pathValue={match.path} kind="file" theme={resolvedTheme} />} |
| 87 | path={match.path} |
| 88 | /> |
| 89 | ), |
| 90 | run: async () => onSelect(match.path), |
| 91 | })); |
| 92 | |
| 93 | return ( |
| 94 | <CommandDialog open={props.open} onOpenChange={props.onOpenChange}> |
| 95 | {props.open ? ( |
| 96 | <CommandDialogPopup |
| 97 | aria-label={gt("Choose project icon")} |
| 98 | className="overflow-hidden" |
| 99 | onBackdropPointerDown={() => props.onOpenChange(false)} |
| 100 | > |
| 101 | <CommandPaletteContent |
| 102 | aria-label={gt("Choose project icon")} |
| 103 | autoHighlight="always" |
| 104 | escapeLabel={gt("Close")} |
| 105 | footerActionLabel={gt("Select icon")} |
| 106 | footerTrailing={ |
| 107 | pickExternal ? ( |
| 108 | <CommandFooterAction |
| 109 | disabled={isPickingExternal} |
| 110 | onClick={() => { |
| 111 | setIsPickingExternal(true); |
| 112 | void pickExternal() |
| 113 | .then((path) => { |
| 114 | if (!path) return; |
| 115 | props.onOpenChange(false); |
| 116 | onSelect(path); |
| 117 | }) |
| 118 | .catch((error: unknown) => { |
| 119 | toastManager.add({ |
| 120 | type: "error", |
| 121 | title: gt("Could not open image picker"), |
| 122 | description: |
| 123 | error instanceof Error |
| 124 | ? (thrownErrorMessage(error, resolveMessage) ?? error.message) |
| 125 | : gt("An error occurred."), |
| 126 | }); |
| 127 | }) |
| 128 | .finally(() => setIsPickingExternal(false)); |
| 129 | }} |
| 130 | > |
| 131 | {gt("Open in {fileManager}", { fileManager: fileManagerName })} |
| 132 | </CommandFooterAction> |
| 133 | ) : null |
| 134 | } |
| 135 | inputProps={{ placeholder: gt("Search image files…") }} |
| 136 | mode="none" |
| 137 | onItemHighlighted={(value) => { |
| 138 | setHighlightedItemValue(typeof value === "string" ? value : null); |
| 139 | }} |
| 140 | onValueChange={(value) => { |
| 141 | setHighlightedItemValue(null); |
| 142 | setQuery(value); |
| 143 | }} |
| 144 | panelSize="tall-list" |
| 145 | testId="project-favicon-picker" |
| 146 | value={query} |
| 147 | > |
| 148 | <CommandPaletteResults |
| 149 | groups={ |
| 150 | items.length > 0 |
| 151 | ? [{ value: "project-favicon-files", label: props.projectName, items }] |
| 152 | : [] |
| 153 | } |
| 154 | highlightedItemValue={highlightedItemValue} |
| 155 | isActionsOnly={false} |
| 156 | keybindings={keybindings} |
| 157 | onExecuteItem={(item) => { |
| 158 | if (item.kind !== "action") return; |
| 159 | props.onOpenChange(false); |
| 160 | void item.run(); |
| 161 | }} |
| 162 | emptyStateMessage={emptyMessage(query, result.error, result.isPending, m)} |
| 163 | /> |
| 164 | </CommandPaletteContent> |
| 165 | </CommandDialogPopup> |
| 166 | ) : null} |
| 167 | </CommandDialog> |
| 168 | ); |
| 169 | } |