Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

WorkspaceToolbar.ios.tsx

212 lines8.3 KB

1import { useActiveNewTask } from "../../state/active-new-task";
2import { Toolbar } from "../../components/ui/Toolbar";
3import { useFocusEffect, useIsFocused } from "@react-navigation/native";
4import { use, useCallback, useEffect, useRef } from "react";
5import { Platform } from "react-native";
6import { isDuoDevice } from "../../lib/device-type";
7import { useDeviceType } from "../../native/native-layout-metrics";
8import type { SearchBarCommands } from "react-native-screens";
9​
10import { NATIVE_LIQUID_GLASS_SUPPORTED } from "../../native/native-glass";
11import { NativeStackScreenOptions } from "../../native/StackHeader";
12import {
13 NativePrimaryColumnContext,
14 NativeWorkspaceTabActivityContext,
15} from "../../native/v5-workspace-context";
16import { useHardwareKeyboardCommand } from "../keyboard/hardwareKeyboardCommands";
17import { useAdaptiveWorkspaceLayout } from "../layout/AdaptiveWorkspaceLayout";
18import { useWorkspaceTabs } from "./WorkspaceTabsContext";
19import { useCompactWorkspaceTabs, useSidebarWorkspaceTabs } from "./useSystemWorkspaceTabBar";
20import { WorkspaceTabToolbarButtons } from "./WorkspaceSectionToolbar";
21​
22export function WorkspaceToolbar(props: {
23 readonly onNewTask: () => void;
24 readonly sidebar?: boolean;
25}) {
26 const workspace = useWorkspaceTabs()!;
27 const activeNewTask = useActiveNewTask();
28 const deviceType = useDeviceType();
29 const sidebarTabs = useSidebarWorkspaceTabs();
30 const compactTabs = useCompactWorkspaceTabs();
31 const duoSearch = NATIVE_LIQUID_GLASS_SUPPORTED && isDuoDevice(deviceType);
32 const bottomSearch = duoSearch && sidebarTabs !== "picker";
33 // On the Duo, compose belongs to the threads tab, in the detail column's bar when unfolded.
34 const nativeToolbarCompose =
35 Platform.OS === "ios" &&
36 deviceType === "duo-folded" &&
37 workspace.state.selectedTab === "threads" &&
38 activeNewTask === null;
39 const { isTabActive } = workspace;
40 const tabActive = use(NativeWorkspaceTabActivityContext)?.active ?? true;
41 const focused = useIsFocused();
42 const { panes, togglePrimarySidebar } = useAdaptiveWorkspaceLayout();
43 const searchRef = useRef<SearchBarCommands>(null);
44 const tab = workspace.state.tabs[workspace.state.selectedTab];
45 const selectedTab = workspace.state.selectedTab;
46 const foldedSearch = deviceType === "duo-folded";
47 const primaryColumn = use(NativePrimaryColumnContext);
48 // UIKit hosts integrated search outside the sidebar's sliding content.
49 // Mount it after native appearance so it cannot precede the pane's reveal.
50 const searchVisible = foldedSearch
51 ? tab.searchMode !== "resting"
52 : deviceType !== "duo-unfolded" || primaryColumn?.presented !== false;
53 const enterSearch = useCallback(() => {
54 workspace.dispatch({
55 type: "scroll",
56 tab: selectedTab,
57 offset: workspace.getScrollOffset(selectedTab),
58 });
59 workspace.dispatch({ type: "search", tab: selectedTab });
60 if (props.sidebar && !panes.primarySidebarVisible) togglePrimarySidebar();
61 }, [workspace, selectedTab, props.sidebar, panes.primarySidebarVisible, togglePrimarySidebar]);
62 const focusSearch = useCallback(() => {
63 if (!workspace.isTabActive(selectedTab)) return false;
64 if (duoSearch || !NATIVE_LIQUID_GLASS_SUPPORTED) enterSearch();
65 searchRef.current?.focus();
66 return true;
67 }, [workspace, selectedTab, enterSearch, duoSearch]);
68 const cancelSearch = useCallback(() => {
69 workspace.recordScrollOffset(selectedTab, tab.searchEntryOffset);
70 workspace.dispatch({ type: "cancel", tab: selectedTab });
71 }, [workspace, selectedTab, tab.searchEntryOffset]);
72 const searchFocused = useCallback(
73 () =>
74 tab.searchMode === "resting"
75 ? enterSearch()
76 : workspace.dispatch({ type: "search", tab: selectedTab }),
77 [tab.searchMode, enterSearch, workspace, selectedTab],
78 );
79 useHardwareKeyboardCommand("focusSearch", focusSearch);
80 // The sidebar remains visible while its detail owns navigation focus.
81 const searchHostVisible = focused || primaryColumn?.presented === true;
82 const restoreSearch = useCallback(() => {
83 if (
84 searchHostVisible &&
85 searchVisible &&
86 isTabActive(selectedTab) &&
87 tab.searchMode !== "resting"
88 ) {
89 searchRef.current?.setText(tab.query);
90 if (duoSearch) {
91 searchRef.current?.setActive(true);
92 if (tab.searchMode === "active") searchRef.current?.focus();
93 }
94 }
95 }, [
96 searchHostVisible,
97 duoSearch,
98 isTabActive,
99 selectedTab,
100 tab.searchMode,
101 tab.query,
102 searchVisible,
103 ]);
104 useFocusEffect(
105 useCallback(() => {
106 const frame = requestAnimationFrame(restoreSearch);
107 return () => cancelAnimationFrame(frame);
108 }, [restoreSearch]),
109 );
110 useEffect(() => {
111 if (!tabActive) return;
112 const frame = requestAnimationFrame(restoreSearch);
113 return () => cancelAnimationFrame(frame);
114 }, [restoreSearch, tabActive, bottomSearch]);
115 const composeButton = nativeToolbarCompose ? (
116 <Toolbar.Button
117 identifier="workspace-compose"
118 accessibilityLabel="New chat"
119 icon="square.and.pencil"
120 onPress={props.onNewTask}
121 separateBackground
122 />
123 ) : null;
124 const searchButton = foldedSearch ? (
125 <Toolbar.Button
126 identifier="workspace-search"
127 accessibilityLabel={
128 searchVisible
129 ? "Close search"
130 : selectedTab === "pullRequests"
131 ? "Search pull requests"
132 : "Search threads"
133 }
134 icon={searchVisible ? "xmark" : "magnifyingglass"}
135 onPress={searchVisible ? () => searchRef.current?.cancelSearch() : focusSearch}
136 separateBackground
137 />
138 ) : null;
139 return (
140 <>
141 <NativeStackScreenOptions
142 optionsVersion={[selectedTab, tab.searchMode]}
143 listeners={{ transitionEnd: restoreSearch }}
144 options={{
145 headerSearchBarOptions:
146 (NATIVE_LIQUID_GLASS_SUPPORTED || tab.searchMode !== "resting") && searchVisible
147 ? {
148 ref: searchRef,
149 autoFocus: tab.searchMode === "active",
150 autoCapitalize: "none",
151 hideNavigationBar: false,
152 hideWhenScrolling: false,
153 obscureBackground: false,
154 placement: NATIVE_LIQUID_GLASS_SUPPORTED
155 ? bottomSearch
156 ? "integrated"
157 : "integratedButton"
158 : "stacked",
159 allowToolbarIntegration:
160 NATIVE_LIQUID_GLASS_SUPPORTED &&
161 deviceType !== "phone" &&
162 sidebarTabs !== "picker",
163 placeholder:
164 selectedTab === "pullRequests" ? "Search pull requests" : "Search threads",
165 cancelButtonHidden: foldedSearch,
166 onFocus: searchFocused,
167 onBlur: () => workspace.dispatch({ type: "expand", tab: selectedTab }),
168 onSearchButtonPress: () => searchRef.current?.blur(),
169 onChangeText: ({ nativeEvent }) =>
170 workspace.dispatch({
171 type: "query",
172 query: nativeEvent.text,
173 tab: selectedTab,
174 }),
175 onCancelButtonPress: cancelSearch,
176 }
177 : undefined,
178 }}
179 />
180 {/* UIKit lays trailing items out from the edge inward. */}
181 <Toolbar placement="right">
182 {searchButton}
183 {nativeToolbarCompose && foldedSearch ? <Toolbar.Spacer width={8} /> : null}
184 {composeButton}
185 {selectedTab !== "threads" ||
186 isDuoDevice(deviceType) ||
187 deviceType === "pad" ||
188 sidebarTabs === "picker" ||
189 compactTabs ? null : (
190 <Toolbar.Button
191 accessibilityLabel="New chat"
192 icon="square.and.pencil"
193 onPress={props.onNewTask}
194 />
195 )}
196 {NATIVE_LIQUID_GLASS_SUPPORTED ? null : (
197 <Toolbar.Button
198 accessibilityLabel={
199 selectedTab === "pullRequests" ? "Search pull requests" : "Search threads"
200 }
201 icon="magnifyingglass"
202 onPress={focusSearch}
203 />
204 )}
205 </Toolbar>
206 <Toolbar placement="bottom">
207 {sidebarTabs === "toolbar" ? <WorkspaceTabToolbarButtons /> : null}
208 {bottomSearch && searchVisible ? <Toolbar.SearchBarSlot /> : null}
209 </Toolbar>
210 </>
211 );