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 { uuidv4 } from "../../lib/uuid"; |
| 2 | import { ThreadUndoNotice } from "./ThreadUndoNotice"; |
| 3 | import { use } from "react"; |
| 4 | import { NativePrimaryColumnContext } from "../../native/v5-workspace-context"; |
| 5 | import { useNavigation } from "@react-navigation/native"; |
| 6 | import { useMemo, useState } from "react"; |
| 7 | import { Platform } from "react-native"; |
| 8 | import { useGT, useMessages } from "gt-react-native"; |
| 9 | |
| 10 | import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell"; |
| 11 | import { pickableProjectGroups } from "@t3tools/client-runtime/state/project-grouping"; |
| 12 | |
| 13 | import { useProjects } from "../../state/entities"; |
| 14 | import { steadyNavigationThreadShellsAtom } from "../../state/threads"; |
| 15 | import { useLaunchingThreads, usePendingNewTasks } from "../../state/use-pending-new-tasks"; |
| 16 | import { useWorkspaceState } from "../../state/workspace"; |
| 17 | import { useSavedRemoteConnections } from "../../state/use-remote-environment-registry"; |
| 18 | import { |
| 19 | useAdaptiveWorkspaceLayout, |
| 20 | useWorkspacePaneState, |
| 21 | } from "../layout/AdaptiveWorkspaceLayout"; |
| 22 | import { AndroidHomeFabLayout } from "./AndroidHomeFab"; |
| 23 | import { HomeScreen } from "./HomeScreen"; |
| 24 | import { useHomeListOptions } from "./home-list-options"; |
| 25 | import { |
| 26 | runWhenHomeCovered, |
| 27 | useAtomValueWhileVisible, |
| 28 | useHomeRouteVisible, |
| 29 | } from "./home-route-visibility"; |
| 30 | import { |
| 31 | buildHomeProjectScopes, |
| 32 | resolveHomeProjectFilterScope, |
| 33 | sortHomeProjectScopes, |
| 34 | } from "./homeThreadList"; |
| 35 | import { usePendingTaskListActions } from "./usePendingTaskListActions"; |
| 36 | import { useThreadListActions } from "./useThreadListActions"; |
| 37 | import { useWorkspaceTabs } from "../layout/workspace/WorkspaceTabsContext"; |
| 38 | import { openSettings } from "../settings/settings-navigation"; |
| 39 | import { useSelectWorkspaceTab } from "../layout/workspace/useSelectWorkspaceTab"; |
| 40 | import { useSelectWorkspaceDetail } from "../layout/workspace/useSelectWorkspaceDetail"; |
| 41 | import { WorkspaceListEmptyDetail } from "../layout/workspace/WorkspaceListEmptyDetail"; |
| 42 | import { WorkspaceListHeader } from "../layout/workspace/WorkspaceListHeader"; |
| 43 | import { buildHomeListFilterMenuItems, homeListFilterEnvironments } from "./home-list-filter-menu"; |
| 44 | |
| 45 | export function HomeRouteScreen() { |
| 46 | const gt = useGT(); |
| 47 | const m = useMessages(); |
| 48 | const nativePrimaryColumn = use(NativePrimaryColumnContext); |
| 49 | const { layout, setPrimarySidebarSearchQuery, startNewTask } = useAdaptiveWorkspaceLayout(); |
| 50 | const { primarySidebarSearchQuery } = useWorkspacePaneState(); |
| 51 | const selectDetail = useSelectWorkspaceDetail(); |
| 52 | const projects = useProjects(); |
| 53 | // Stop rebuilding the streaming thread list while Thread covers Home. |
| 54 | const visible = useHomeRouteVisible(); |
| 55 | const threads = useLaunchingThreads( |
| 56 | useAtomValueWhileVisible(steadyNavigationThreadShellsAtom, visible), |
| 57 | ); |
| 58 | const { environments: workspaceEnvironments, state: catalogState } = useWorkspaceState(); |
| 59 | const { savedConnectionsById } = useSavedRemoteConnections(); |
| 60 | const navigation = useNavigation(); |
| 61 | const selectTab = useSelectWorkspaceTab(); |
| 62 | const workspace = useWorkspaceTabs(); |
| 63 | const [localSearchQuery, setLocalSearchQuery] = useState(""); |
| 64 | const searchQuery = |
| 65 | Platform.OS === "android" |
| 66 | ? primarySidebarSearchQuery |
| 67 | : (workspace?.state.tabs.threads.query ?? localSearchQuery); |
| 68 | const setSearchQuery = (query: string) => { |
| 69 | if (Platform.OS === "android") setPrimarySidebarSearchQuery(query); |
| 70 | else if (workspace) workspace.dispatch({ type: "query", query, tab: "threads" }); |
| 71 | else setLocalSearchQuery(query); |
| 72 | }; |
| 73 | // Split layouts keep the query; only the stacked layout opens the thread over the list. |
| 74 | // Android's search field keeps its own open state, which a cleared query would leave open. |
| 75 | const clearsSearchOnOpen = Platform.OS !== "android" && !layout.usesSplitView; |
| 76 | const selectThreadRoute = (params: ReactNavigation.RootParamList["Thread"]) => { |
| 77 | if (searchQuery !== "" && clearsSearchOnOpen && workspace) |
| 78 | runWhenHomeCovered(() => workspace.cancelSearch("threads")); |
| 79 | selectDetail({ name: "Thread", params }); |
| 80 | }; |
| 81 | const handleSelectThread = (thread: EnvironmentThreadShell) => |
| 82 | selectThreadRoute({ environmentId: String(thread.environmentId), threadId: String(thread.id) }); |
| 83 | const handleNewThreadOnBranch = (thread: EnvironmentThreadShell) => { |
| 84 | selectThreadRoute({ |
| 85 | draft: { |
| 86 | requestId: uuidv4(), |
| 87 | environmentId: String(thread.environmentId), |
| 88 | projectId: String(thread.projectId), |
| 89 | branch: thread.branch, |
| 90 | worktreePath: thread.worktreePath, |
| 91 | }, |
| 92 | }); |
| 93 | }; |
| 94 | |
| 95 | const { |
| 96 | archiveThread, |
| 97 | confirmDeleteThread, |
| 98 | settleThread, |
| 99 | snoozeThread, |
| 100 | unsnoozeThread, |
| 101 | pinThread, |
| 102 | unpinThread, |
| 103 | setThreadAutoSettle, |
| 104 | moveThread, |
| 105 | renameThread, |
| 106 | regenerateThreadTitle, |
| 107 | unsettleThread, |
| 108 | } = useThreadListActions(); |
| 109 | const pendingTasks = usePendingNewTasks(); |
| 110 | const { openPendingTask, confirmDeletePendingTask } = |
| 111 | usePendingTaskListActions(selectThreadRoute); |
| 112 | const environments = homeListFilterEnvironments(workspaceEnvironments); |
| 113 | const availableEnvironmentIds = useMemo( |
| 114 | () => new Set(environments.map((environment) => environment.environmentId)), |
| 115 | [environments], |
| 116 | ); |
| 117 | const { |
| 118 | options: listOptions, |
| 119 | setSelectedEnvironmentId, |
| 120 | setSelectedProjectKey, |
| 121 | } = useHomeListOptions(availableEnvironmentIds); |
| 122 | const selectedEnvironmentId = listOptions.selectedEnvironmentId; |
| 123 | const projectScopes = useMemo( |
| 124 | () => |
| 125 | buildHomeProjectScopes( |
| 126 | { |
| 127 | projects, |
| 128 | environmentId: selectedEnvironmentId, |
| 129 | projectGroupingMode: listOptions.projectGroupingMode, |
| 130 | }, |
| 131 | m, |
| 132 | ), |
| 133 | [listOptions.projectGroupingMode, projects, selectedEnvironmentId, m], |
| 134 | ); |
| 135 | const projectFilterOptions = useMemo( |
| 136 | () => |
| 137 | pickableProjectGroups( |
| 138 | sortHomeProjectScopes({ |
| 139 | scopes: projectScopes, |
| 140 | threads, |
| 141 | pendingTasks: [], |
| 142 | projectSortOrder: "updated_at", |
| 143 | }), |
| 144 | ).map((scope) => ({ |
| 145 | key: scope.key, |
| 146 | label: scope.title, |
| 147 | })), |
| 148 | [projectScopes, threads], |
| 149 | ); |
| 150 | const selectedProjectKey = |
| 151 | resolveHomeProjectFilterScope(projectScopes, projects, listOptions)?.key ?? null; |
| 152 | |
| 153 | if (layout.usesSplitView && !nativePrimaryColumn) { |
| 154 | return ( |
| 155 | <WorkspaceListEmptyDetail |
| 156 | hasConnections={catalogState.hasConnections} |
| 157 | onAddConnection={() => openSettings(navigation, "SettingsEnvironmentNew")} |
| 158 | onStartNewTask={startNewTask} |
| 159 | /> |
| 160 | ); |
| 161 | } |
| 162 | |
| 163 | return ( |
| 164 | <AndroidHomeFabLayout onStartNewTask={startNewTask}> |
| 165 | <> |
| 166 | <WorkspaceListHeader |
| 167 | accessibilityLabel={gt("T3 Code, Threads")} |
| 168 | onOpenEnvironments={() => openSettings(navigation, "SettingsEnvironments")} |
| 169 | search={{ |
| 170 | value: searchQuery, |
| 171 | onChangeText: setSearchQuery, |
| 172 | placeholder: gt("Search", { $context: "search field placeholder" }), |
| 173 | accessibilityLabel: gt("Search threads"), |
| 174 | }} |
| 175 | filter={{ |
| 176 | customized: selectedEnvironmentId !== null || selectedProjectKey !== null, |
| 177 | items: buildHomeListFilterMenuItems( |
| 178 | { |
| 179 | environments, |
| 180 | projects: projectFilterOptions, |
| 181 | selectedEnvironmentId, |
| 182 | selectedProjectKey, |
| 183 | onEnvironmentChange: setSelectedEnvironmentId, |
| 184 | onProjectChange: setSelectedProjectKey, |
| 185 | }, |
| 186 | m, |
| 187 | ), |
| 188 | }} |
| 189 | /> |
| 190 | |
| 191 | <HomeScreen |
| 192 | catalogState={catalogState} |
| 193 | environments={environments} |
| 194 | onAddConnection={() => openSettings(navigation, "SettingsEnvironmentNew")} |
| 195 | onArchiveThread={archiveThread} |
| 196 | onDeleteThread={confirmDeleteThread} |
| 197 | onSettleThread={settleThread} |
| 198 | onSnoozeThread={snoozeThread} |
| 199 | onUnsnoozeThread={unsnoozeThread} |
| 200 | onUnsettleThread={unsettleThread} |
| 201 | onPinThread={pinThread} |
| 202 | onUnpinThread={unpinThread} |
| 203 | onSetThreadAutoSettle={setThreadAutoSettle} |
| 204 | onMoveThread={moveThread} |
| 205 | onRenameThread={renameThread} |
| 206 | onRegenerateThreadTitle={regenerateThreadTitle} |
| 207 | onEnvironmentChange={setSelectedEnvironmentId} |
| 208 | onProjectChange={setSelectedProjectKey} |
| 209 | onOpenSettings={() => selectTab("settings")} |
| 210 | onSearchQueryChange={setSearchQuery} |
| 211 | onSelectThread={handleSelectThread} |
| 212 | onSelectPendingTask={openPendingTask} |
| 213 | onDeletePendingTask={confirmDeletePendingTask} |
| 214 | onNewThreadOnBranch={handleNewThreadOnBranch} |
| 215 | onNewThreadInProject={(project) => { |
| 216 | selectThreadRoute({ |
| 217 | draft: { |
| 218 | requestId: uuidv4(), |
| 219 | environmentId: String(project.environmentId), |
| 220 | projectId: String(project.id), |
| 221 | title: project.title, |
| 222 | }, |
| 223 | }); |
| 224 | }} |
| 225 | onStartNewTask={startNewTask} |
| 226 | pendingTasks={pendingTasks} |
| 227 | projectGroupingMode={listOptions.projectGroupingMode} |
| 228 | projects={projects} |
| 229 | projectSortOrder={listOptions.projectSortOrder} |
| 230 | savedConnectionsById={savedConnectionsById} |
| 231 | searchQuery={searchQuery} |
| 232 | selectedEnvironmentId={selectedEnvironmentId} |
| 233 | selectedProjectKey={selectedProjectKey} |
| 234 | threads={threads} |
| 235 | visible={visible} |
| 236 | /> |
| 237 | <ThreadUndoNotice visible={visible} /> |
| 238 | </> |
| 239 | </AndroidHomeFabLayout> |
| 240 | ); |
| 241 | } |