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 { resolveFilesystemReadAccess } from "@t3tools/client-runtime/state/filesystem"; |
| 2 | import { environmentSession } from "../../state/session"; |
| 3 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 4 | import { AppBar } from "../../components/ui/AppBar"; |
| 5 | import { ScreenContent } from "../../components/ui/ScreenContent"; |
| 6 | import { useGT, useMessages } from "gt-react-native"; |
| 7 | import { useState } from "react"; |
| 8 | import { SheetPane } from "../../components/ui/SheetPane"; |
| 9 | |
| 10 | import { useEnvironmentQuery } from "../../state/query"; |
| 11 | import { useEnvironmentPresentation } from "../../state/presentation"; |
| 12 | import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider"; |
| 13 | import { FileTreeBrowser } from "./FileTreeBrowser"; |
| 14 | import { useFileTreeEntries } from "./useFileTreeEntries"; |
| 15 | import { preloadWorkspaceFileContents } from "./preload-workspace-file"; |
| 16 | |
| 17 | export function ThreadFileNavigatorPane(props: { |
| 18 | readonly cwd: string; |
| 19 | readonly environmentId: EnvironmentId; |
| 20 | readonly projectName: string; |
| 21 | readonly selectedPath: string | null; |
| 22 | readonly onSelectFile: (path: string) => void; |
| 23 | }) { |
| 24 | const gt = useGT(); |
| 25 | const workspaceKey = JSON.stringify([props.environmentId, props.cwd]); |
| 26 | // The pane stays mounted across thread switches; a query only filters the workspace it was typed in. |
| 27 | const [search, setSearch] = useState({ workspaceKey, query: "" }); |
| 28 | const searchQuery = search.workspaceKey === workspaceKey ? search.query : ""; |
| 29 | const setSearchQuery = (query: string) => setSearch({ workspaceKey, query }); |
| 30 | const { themeAppearance: highlightTheme } = useAppearancePreferences(); |
| 31 | const messages = useMessages(); |
| 32 | const fileAccessSession = useEnvironmentQuery( |
| 33 | environmentSession.sessionStateAtom(props.environmentId), |
| 34 | ); |
| 35 | const fileEnvironment = useEnvironmentPresentation(props.environmentId); |
| 36 | const fileAccess = resolveFilesystemReadAccess( |
| 37 | { |
| 38 | isCatalogReady: fileEnvironment.isReady, |
| 39 | connection: fileEnvironment.presentation?.connection ?? null, |
| 40 | session: fileAccessSession.data, |
| 41 | sessionError: fileAccessSession.error, |
| 42 | }, |
| 43 | messages, |
| 44 | ); |
| 45 | const { canReadFiles } = fileAccess; |
| 46 | const entriesQuery = useFileTreeEntries({ |
| 47 | environmentId: props.environmentId, |
| 48 | cwd: canReadFiles ? props.cwd : null, |
| 49 | searchQuery, |
| 50 | }); |
| 51 | const handlePreviewFile = (relativePath: string) => { |
| 52 | preloadWorkspaceFileContents({ |
| 53 | cwd: props.cwd, |
| 54 | environmentId: props.environmentId, |
| 55 | relativePath, |
| 56 | theme: highlightTheme, |
| 57 | }); |
| 58 | }; |
| 59 | const fileTree = ( |
| 60 | <FileTreeBrowser |
| 61 | key={workspaceKey} |
| 62 | revealSelection={entriesQuery.revealSelection} |
| 63 | expandedPaths={entriesQuery.expandedPaths} |
| 64 | setExpandedPaths={entriesQuery.setExpandedPaths} |
| 65 | entries={entriesQuery.entries} |
| 66 | loadingDirectories={entriesQuery.loadingDirectories} |
| 67 | onLoadDirectory={entriesQuery.loadDirectory} |
| 68 | error={ |
| 69 | canReadFiles |
| 70 | ? entriesQuery.error |
| 71 | : fileAccess.isPending |
| 72 | ? null |
| 73 | : (fileAccess.error ?? gt("This connection cannot read host files.")) |
| 74 | } |
| 75 | isPending={fileAccess.isPending || entriesQuery.isPending} |
| 76 | isRefreshing={entriesQuery.isRefreshing} |
| 77 | searchQuery={searchQuery} |
| 78 | searchResultPaths={entriesQuery.searchResultPaths} |
| 79 | searchTruncated={entriesQuery.searchTruncated} |
| 80 | selectedPath={props.selectedPath ?? entriesQuery.selectedPath} |
| 81 | onPreviewFile={handlePreviewFile} |
| 82 | onRefresh={entriesQuery.refresh} |
| 83 | onSelectFile={(path) => { |
| 84 | entriesQuery.selectPath(path); |
| 85 | props.onSelectFile(path); |
| 86 | }} |
| 87 | /> |
| 88 | ); |
| 89 | |
| 90 | return ( |
| 91 | <SheetPane> |
| 92 | <AppBar |
| 93 | title={gt("Files", { $context: "screen title, workspace files" })} |
| 94 | subtitle={props.projectName} |
| 95 | search={{ |
| 96 | value: searchQuery, |
| 97 | onChangeText: setSearchQuery, |
| 98 | placeholder: gt("Search files"), |
| 99 | }} |
| 100 | refresh={{ accessibilityLabel: gt("Refresh files"), onPress: entriesQuery.refresh }} |
| 101 | /> |
| 102 | <ScreenContent insetHorizontal>{fileTree}</ScreenContent> |
| 103 | </SheetPane> |
| 104 | ); |
| 105 | } |