Open History to see the last commit
1import { StackActions, useNavigation } from "@react-navigation/native";
2import { useMemo } from "react";
3import { Platform } from "react-native";
4import type { AppNativeStackNavigationOptions } from "../../native/StackHeader";
5import { useNativeWorkspaceColumnsSupported } from "../../native/NativeWorkspaceColumns";
6import { useAdaptiveWorkspaceLayout } from "../layout/AdaptiveWorkspaceLayout";
7import { withNativeGlassHeaderItem } from "../layout/native-glass-header-items";
8import { dispatchHardwareKeyboardCommand } from "../keyboard/hardwareKeyboardCommands";
9import {
10 ThreadGitControls,
11 useThreadGitCenterHeaderItems,
12 useThreadGitRightHeaderItems,
13} from "./ThreadGitControls";
14
15type NativeHeaderItems = ReadonlyArray<Record<string, unknown>>;
16
17export function useThreadHeaderOptions(props: {
18 readonly title: string;
19 readonly subtitle: string;
20 readonly headerColor: string;
21 readonly usesNativeHeaderGlass: boolean;
22 readonly gitControls: Parameters<typeof ThreadGitControls>[0];
23 readonly onReturnToThread?: () => void;
24}) {
25 const usesNativeWorkspaceColumns = useNativeWorkspaceColumnsSupported();
26 const usesDuoHeader = usesNativeWorkspaceColumns && Platform.OS === "ios" && !Platform.isPad;
27 const navigation = useNavigation();
28 const { layout, panes, togglePrimarySidebar } = useAdaptiveWorkspaceLayout();
29 const threadCenterHeaderItems = useThreadGitCenterHeaderItems(props.gitControls);
30 const compactRightHeaderItems = useThreadGitRightHeaderItems(props.gitControls);
31 const splitLeftHeaderItems = useMemo<NativeHeaderItems>(
32 () => [
33 ...(!usesDuoHeader
34 ? [
35 {
36 spacing: 18,
37 type: "spacing" as const,
38 },
39 ]
40 : []),
41 ...(props.onReturnToThread
42 ? [
43 withNativeGlassHeaderItem({
44 accessibilityLabel: "Return to chat",
45 icon: { name: "chevron.left", type: "sfSymbol" as const },
46 identifier: "thread-left-return",
47 onPress: props.onReturnToThread,
48 type: "button" as const,
49 }),
50 ]
51 : []),
52 withNativeGlassHeaderItem({
53 axisBehavior: usesNativeWorkspaceColumns ? "horizontalOnly" : undefined,
54 accessibilityLabel: panes.primarySidebarVisible
55 ? "Maximize content"
56 : "Show thread sidebar",
57 icon: {
58 name: panes.primarySidebarVisible ? "arrow.up.left.and.arrow.down.right" : "sidebar.left",
59 type: "sfSymbol" as const,
60 },
61 identifier: "thread-left-sidebar",
62 label: panes.primarySidebarVisible ? "Maximize content" : "Show thread sidebar",
63 onPress: togglePrimarySidebar,
64 type: "button" as const,
65 }),
66 ...(!usesDuoHeader
67 ? [
68 withNativeGlassHeaderItem({
69 accessibilityLabel: "New task",
70 icon: { name: "square.and.pencil", type: "sfSymbol" as const },
71 identifier: "thread-left-new-task",
72 label: "New task",
73 onPress: () => navigation.navigate("NewTaskSheet", { screen: "NewTask" }),
74 type: "button" as const,
75 }),
76 ]
77 : []),
78 ],
79 [
80 panes.primarySidebarVisible,
81 props.onReturnToThread,
82 navigation,
83 togglePrimarySidebar,
84 usesDuoHeader,
85 usesNativeWorkspaceColumns,
86 ],
87 );
88 const canGoBack = navigation.canGoBack();
89 const compactHomeHeaderItems = useMemo<NativeHeaderItems>(
90 () => [
91 withNativeGlassHeaderItem({
92 accessibilityLabel: "Go to threads list",
93 icon: { name: "list.bullet", type: "sfSymbol" as const },
94 identifier: "thread-left-home",
95 onPress: () => navigation.dispatch(StackActions.replace("Home")),
96 type: "button" as const,
97 }),
98 ],
99 [navigation],
100 );
101
102 const duoRightHeaderItems = useMemo<NativeHeaderItems>(
103 () => [
104 ...threadCenterHeaderItems,
105 { type: "spacing", spacing: 8 },
106 ...(layout.usesSplitView &&
107 Platform.OS === "ios" &&
108 !Platform.isPad &&
109 !panes.primarySidebarVisible
110 ? [
111 withNativeGlassHeaderItem({
112 type: "button" as const,
113 axisBehavior: "verticalPreferred",
114 identifier: "thread-right-search",
115 label: "Search threads",
116 accessibilityLabel: "Search threads",
117 icon: { name: "magnifyingglass", type: "sfSymbol" as const },
118 onPress: () => {
119 dispatchHardwareKeyboardCommand("focusSearch");
120 },
121 }),
122 ]
123 : []),
124 withNativeGlassHeaderItem({
125 type: "button" as const,
126 axisBehavior: "verticalPreferred",
127 identifier: "thread-right-new-task",
128 label: "New task",
129 accessibilityLabel: "New task",
130 icon: { name: "square.and.pencil", type: "sfSymbol" as const },
131 onPress: () => navigation.navigate("NewTaskSheet", { screen: "NewTask" }),
132 }),
133 ],
134 [navigation, threadCenterHeaderItems, layout.usesSplitView, panes.primarySidebarVisible],
135 );
136
137 const options: AppNativeStackNavigationOptions = {
138 headerShown: true,
139 headerTitle: props.title,
140 headerTitleStyle: props.usesNativeHeaderGlass
141 ? {
142 fontSize: 17,
143 fontWeight: "800",
144 }
145 : undefined,
146 title: props.title,
147 headerBackVisible: !layout.usesSplitView,
148 unstable_headerLeftItems: layout.usesSplitView
149 ? () => splitLeftHeaderItems
150 : canGoBack
151 ? undefined
152 : () => compactHomeHeaderItems,
153 unstable_headerRightItems: () =>
154 usesDuoHeader
155 ? duoRightHeaderItems
156 : layout.usesSplitView
157 ? threadCenterHeaderItems
158 : compactRightHeaderItems,
159 unstable_headerToolbarItems: () => [],
160 unstable_headerSubtitle: props.usesNativeHeaderGlass ? props.subtitle : undefined,
161 contentStyle: undefined,
162 };
163 const { environmentId, threadId, gitStatus } = props.gitControls;
164 return {
165 options,
166 // Header item factories are stabilized, so the native header only re-reads them when
167 // this version changes. Keying on the items keeps the Git menu status live; the menu
168 // callbacks also read state the items do not display (a "Push" item runs `push` or
169 // `commit_push` depending on the default ref), so that state is keyed too.
170 optionsVersion: [
171 splitLeftHeaderItems,
172 threadCenterHeaderItems,
173 compactRightHeaderItems,
174 duoRightHeaderItems,
175 usesDuoHeader,
176 environmentId,
177 threadId,
178 gitStatus?.isDefaultRef,
179 gitStatus?.refName,
180 gitStatus?.pr?.url,
181 ],
182 sidebar: false,
183 fallback:
184 !layout.usesSplitView && !props.usesNativeHeaderGlass ? (
185 <ThreadGitControls {...props.gitControls} showActionControls />
186 ) : null,
187 };
188}