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, 03:37 UTC
| 1 | import { msg, useGT, useMessages } from "gt-react-native"; |
| 2 | import { useActiveNewTask } from "../../../state/active-new-task"; |
| 3 | import { Toolbar } from "../../../components/ui/Toolbar"; |
| 4 | import { useFocusEffect, useIsFocused, useNavigationState } from "@react-navigation/native"; |
| 5 | import { use, useCallback, useEffect, useRef } from "react"; |
| 6 | import { isDuoDevice } from "../../../lib/device-type"; |
| 7 | import { useDeviceType } from "../../../native/native-layout-metrics"; |
| 8 | import { createNativeHeaderMenu } from "../../../components/ui/nativeHeaderMenu.ios"; |
| 9 | import { useSettingsEnvironmentMenu } from "../../settings/components/SettingsEnvironmentFilterHeader"; |
| 10 | import type { SearchBarCommands } from "react-native-screens"; |
| 11 | |
| 12 | import { NATIVE_LIQUID_GLASS_SUPPORTED } from "../../../native/native-glass"; |
| 13 | import { NativeStackScreenOptions } from "../../../native/StackHeader"; |
| 14 | import { |
| 15 | NativePrimaryColumnContext, |
| 16 | NativeWorkspaceTabActivityContext, |
| 17 | } from "../../../native/v5-workspace-context"; |
| 18 | import { useHardwareKeyboardCommand } from "../../keyboard/hardwareKeyboardCommands"; |
| 19 | import { useAdaptiveWorkspaceLayout, useWorkspacePaneState } from "../AdaptiveWorkspaceLayout"; |
| 20 | import { useWorkspaceTabs } from "./WorkspaceTabsContext"; |
| 21 | import { shouldRestoreNativeSearch } from "./workspace-tabs-state"; |
| 22 | import { useFloatingWorkspaceTabs, useSidebarWorkspaceTabs } from "./useSystemWorkspaceTabBar"; |
| 23 | import { WorkspaceTabToolbarButtons } from "./WorkspaceSectionToolbar"; |
| 24 | import { useWorkspaceCompose } from "./useWorkspaceCompose"; |
| 25 | |
| 26 | const SEARCH_LABELS = { |
| 27 | threads: msg("Search threads"), |
| 28 | pullRequests: msg("Search pull requests"), |
| 29 | issues: msg("Search issues"), |
| 30 | settings: msg("Search settings"), |
| 31 | } as const; |
| 32 | |
| 33 | export function WorkspaceToolbar(props: { |
| 34 | readonly onNewTask: () => void; |
| 35 | readonly sidebar?: boolean; |
| 36 | }) { |
| 37 | const gt = useGT(); |
| 38 | const m = useMessages(); |
| 39 | const workspace = useWorkspaceTabs()!; |
| 40 | const activeNewTask = useActiveNewTask(); |
| 41 | const deviceType = useDeviceType(); |
| 42 | const sidebarTabs = useSidebarWorkspaceTabs(); |
| 43 | const floatingTabs = useFloatingWorkspaceTabs(); |
| 44 | const duoSearch = NATIVE_LIQUID_GLASS_SUPPORTED && isDuoDevice(deviceType); |
| 45 | const bottomSearch = duoSearch && sidebarTabs !== "picker"; |
| 46 | const compose = useWorkspaceCompose(); |
| 47 | // On the Duo, compose sits in the list's bar when folded and the detail column's when unfolded. |
| 48 | const nativeToolbarCompose = |
| 49 | deviceType === "duo-folded" && compose !== null && activeNewTask === null; |
| 50 | const tabActive = use(NativeWorkspaceTabActivityContext)?.active ?? true; |
| 51 | const focused = useIsFocused(); |
| 52 | const { layout, togglePrimarySidebar } = useAdaptiveWorkspaceLayout(); |
| 53 | const { panes } = useWorkspacePaneState(); |
| 54 | const searchRef = useRef<SearchBarCommands>(null); |
| 55 | const settingsMenu = useSettingsEnvironmentMenu(); |
| 56 | // The Settings list takes the selected tab's place, so search follows it. |
| 57 | const selectedTab = workspace.settingsOpen ? "settings" : workspace.state.selectedTab; |
| 58 | const tab = workspace.state.tabs[selectedTab]; |
| 59 | const searchLabel = m(SEARCH_LABELS[selectedTab === "usage" ? "threads" : selectedTab]); |
| 60 | const { isTabActive: isListTabActive } = workspace; |
| 61 | // A retained tab slot stays mounted while another is shown. |
| 62 | const isTabActive = (tab: typeof selectedTab) => |
| 63 | tab === "settings" ? tabActive : isListTabActive(tab); |
| 64 | const foldedSearch = deviceType === "duo-folded"; |
| 65 | const primaryColumn = use(NativePrimaryColumnContext); |
| 66 | // UIKit hosts integrated search outside the sidebar's sliding content. |
| 67 | // Mount it after native appearance so it cannot precede the pane's reveal. |
| 68 | const searchVisible = foldedSearch |
| 69 | ? tab.searchMode !== "resting" |
| 70 | : deviceType !== "duo-unfolded" || primaryColumn?.presented !== false; |
| 71 | const enterSearch = useCallback(() => { |
| 72 | workspace.dispatch({ |
| 73 | type: "scroll", |
| 74 | tab: selectedTab, |
| 75 | offset: workspace.getScrollOffset(selectedTab), |
| 76 | }); |
| 77 | workspace.dispatch({ type: "search", tab: selectedTab }); |
| 78 | if (props.sidebar && !panes.primarySidebarVisible) togglePrimarySidebar(); |
| 79 | }, [workspace, selectedTab, props.sidebar, panes.primarySidebarVisible, togglePrimarySidebar]); |
| 80 | const focusSearch = () => { |
| 81 | if (!isTabActive(selectedTab)) return false; |
| 82 | if (duoSearch || !NATIVE_LIQUID_GLASS_SUPPORTED) enterSearch(); |
| 83 | searchRef.current?.focus(); |
| 84 | return true; |
| 85 | }; |
| 86 | const cancelSearch = () => workspace.cancelSearch(selectedTab); |
| 87 | const searchFocused = () => |
| 88 | tab.searchMode === "resting" |
| 89 | ? enterSearch() |
| 90 | : workspace.dispatch({ type: "search", tab: selectedTab }); |
| 91 | useHardwareKeyboardCommand("focusSearch", focusSearch); |
| 92 | // Home draws the Settings list under the `Settings` route, which holds navigation focus. |
| 93 | const settingsListOnTop = useNavigationState( |
| 94 | (state) => state.routes[state.index]?.name === "Settings", |
| 95 | ); |
| 96 | // The sidebar remains visible while its detail owns navigation focus. |
| 97 | const searchHostVisible = focused || settingsListOnTop || primaryColumn?.presented === true; |
| 98 | const restoreSearch = () => { |
| 99 | // A search cancelled while its screen was covered leaves its text in the native field. |
| 100 | if (tab.searchMode === "resting") searchRef.current?.setText(""); |
| 101 | if ( |
| 102 | searchHostVisible && |
| 103 | searchVisible && |
| 104 | isTabActive(selectedTab) && |
| 105 | shouldRestoreNativeSearch(tab, duoSearch) |
| 106 | ) { |
| 107 | searchRef.current?.setText(tab.query); |
| 108 | // Integrated search collapses to its button when its screen reappears, |
| 109 | // which would hide the query that still filters the list. |
| 110 | if (NATIVE_LIQUID_GLASS_SUPPORTED) { |
| 111 | searchRef.current?.setActive(true); |
| 112 | if (tab.searchMode === "active") searchRef.current?.focus(); |
| 113 | } |
| 114 | } |
| 115 | }; |
| 116 | useFocusEffect( |
| 117 | useCallback(() => { |
| 118 | const frame = requestAnimationFrame(restoreSearch); |
| 119 | return () => cancelAnimationFrame(frame); |
| 120 | }, [restoreSearch]), |
| 121 | ); |
| 122 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- schedules a frame to restore the native search bar's text and focus when its tab becomes active |
| 123 | useEffect(() => { |
| 124 | if (!tabActive) return; |
| 125 | const frame = requestAnimationFrame(restoreSearch); |
| 126 | return () => cancelAnimationFrame(frame); |
| 127 | // oxlint-disable-next-line react/exhaustive-effect-dependencies -- Moving the search bar to the bottom toolbar restores its focus and query. |
| 128 | }, [restoreSearch, tabActive, bottomSearch]); |
| 129 | const composeButton = nativeToolbarCompose ? ( |
| 130 | <Toolbar.Button |
| 131 | identifier="workspace-compose" |
| 132 | accessibilityLabel={compose?.label} |
| 133 | icon="square.and.pencil" |
| 134 | onPress={compose?.onPress} |
| 135 | separateBackground |
| 136 | /> |
| 137 | ) : null; |
| 138 | const searchButton = foldedSearch ? ( |
| 139 | <Toolbar.Button |
| 140 | identifier="workspace-search" |
| 141 | accessibilityLabel={searchVisible ? gt("Close search") : searchLabel} |
| 142 | icon={searchVisible ? "xmark" : "magnifyingglass"} |
| 143 | onPress={searchVisible ? () => searchRef.current?.cancelSearch() : focusSearch} |
| 144 | separateBackground |
| 145 | /> |
| 146 | ) : null; |
| 147 | return ( |
| 148 | <> |
| 149 | <NativeStackScreenOptions |
| 150 | optionsVersion={[selectedTab, tab.searchMode]} |
| 151 | listeners={{ transitionEnd: restoreSearch }} |
| 152 | options={{ |
| 153 | // Without Liquid Glass, Settings keeps its search field out instead of a search button, |
| 154 | // which would take the header's trailing items from the scope filter. |
| 155 | headerSearchBarOptions: |
| 156 | (NATIVE_LIQUID_GLASS_SUPPORTED || |
| 157 | tab.searchMode !== "resting" || |
| 158 | workspace.settingsOpen) && |
| 159 | searchVisible |
| 160 | ? { |
| 161 | ref: searchRef, |
| 162 | autoFocus: tab.searchMode === "active", |
| 163 | autoCapitalize: "none", |
| 164 | hideNavigationBar: false, |
| 165 | hideWhenScrolling: false, |
| 166 | obscureBackground: false, |
| 167 | placement: NATIVE_LIQUID_GLASS_SUPPORTED |
| 168 | ? bottomSearch |
| 169 | ? "integrated" |
| 170 | : "integratedButton" |
| 171 | : "stacked", |
| 172 | allowToolbarIntegration: |
| 173 | NATIVE_LIQUID_GLASS_SUPPORTED && |
| 174 | deviceType !== "phone" && |
| 175 | sidebarTabs !== "picker", |
| 176 | placeholder: searchLabel, |
| 177 | cancelButtonHidden: foldedSearch, |
| 178 | onFocus: searchFocused, |
| 179 | onBlur: () => workspace.dispatch({ type: "expand", tab: selectedTab }), |
| 180 | onSearchButtonPress: () => searchRef.current?.blur(), |
| 181 | onChangeText: ({ nativeEvent }) => |
| 182 | workspace.dispatch({ |
| 183 | type: "query", |
| 184 | query: nativeEvent.text, |
| 185 | tab: selectedTab, |
| 186 | }), |
| 187 | onCancelButtonPress: cancelSearch, |
| 188 | } |
| 189 | : undefined, |
| 190 | }} |
| 191 | /> |
| 192 | {/* UIKit lays trailing items out from the edge inward. */} |
| 193 | {workspace.settingsOpen ? ( |
| 194 | <Toolbar placement="right"> |
| 195 | {searchButton} |
| 196 | {createNativeHeaderMenu(settingsMenu)} |
| 197 | </Toolbar> |
| 198 | ) : ( |
| 199 | <Toolbar placement="right"> |
| 200 | {searchButton} |
| 201 | {nativeToolbarCompose && foldedSearch ? <Toolbar.Spacer width={8} /> : null} |
| 202 | {composeButton} |
| 203 | {compose === null || |
| 204 | isDuoDevice(deviceType) || |
| 205 | (deviceType === "pad" && layout.usesSplitView) || |
| 206 | sidebarTabs === "picker" || |
| 207 | floatingTabs ? null : ( |
| 208 | <Toolbar.Button |
| 209 | accessibilityLabel={compose.label} |
| 210 | icon="square.and.pencil" |
| 211 | onPress={compose.onPress} |
| 212 | /> |
| 213 | )} |
| 214 | {NATIVE_LIQUID_GLASS_SUPPORTED ? null : ( |
| 215 | <Toolbar.Button |
| 216 | accessibilityLabel={searchLabel} |
| 217 | icon="magnifyingglass" |
| 218 | onPress={focusSearch} |
| 219 | /> |
| 220 | )} |
| 221 | </Toolbar> |
| 222 | )} |
| 223 | <Toolbar placement="bottom"> |
| 224 | {sidebarTabs === "toolbar" ? <WorkspaceTabToolbarButtons /> : null} |
| 225 | {bottomSearch && searchVisible ? <Toolbar.SearchBarSlot /> : null} |
| 226 | </Toolbar> |
| 227 | </> |
| 228 | ); |
| 229 | } |