Open History to see the last commit
1import { StackActions, useNavigation } from "@react-navigation/native";
2import { useMemo } from "react";
3import { isDuoDevice } from "../../lib/device-type";
4import type { AppNativeStackNavigationOptions } from "../../native/StackHeader";
5import { useDeviceType } from "../../native/native-layout-metrics";
6import { useNativeWorkspaceColumnsSupported } from "../../native/NativeWorkspaceColumns";
7import { useAdaptiveWorkspaceLayout } from "../layout/AdaptiveWorkspaceLayout";
8import { withNativeGlassHeaderItem } from "../layout/native-glass-header-items";
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 deviceType = useDeviceType();
27 const usesDuoHeader = usesNativeWorkspaceColumns && isDuoDevice(deviceType);
28 const navigation = useNavigation();
29 const { layout, panes, togglePrimarySidebar } = useAdaptiveWorkspaceLayout();
30 const threadCenterHeaderItems = useThreadGitCenterHeaderItems(props.gitControls);
31 const compactRightHeaderItems = useThreadGitRightHeaderItems(props.gitControls);
32 const splitLeftHeaderItems = useMemo<NativeHeaderItems>(
33 () => [
34 ...(!usesDuoHeader
35 ? [
36 {
37 spacing: 18,
38 type: "spacing" as const,
39 },
40 ]
41 : []),
42 ...(props.onReturnToThread
43 ? [
44 withNativeGlassHeaderItem({
45 accessibilityLabel: "Return to chat",
46 icon: { name: "chevron.left", type: "sfSymbol" as const },
47 identifier: "thread-left-return",
48 onPress: props.onReturnToThread,
49 type: "button" as const,
50 }),
51 ]
52 : []),
53 withNativeGlassHeaderItem({
54 axisBehavior: usesNativeWorkspaceColumns ? "horizontalOnly" : undefined,
55 accessibilityLabel: panes.primarySidebarVisible
56 ? "Maximize content"
57 : "Show thread sidebar",
58 icon: {
59 name: panes.primarySidebarVisible ? "arrow.up.left.and.arrow.down.right" : "sidebar.left",
60 type: "sfSymbol" as const,
61 },
62 identifier: "thread-left-sidebar",
63 label: panes.primarySidebarVisible ? "Maximize content" : "Show thread sidebar",
64 onPress: togglePrimarySidebar,
65 type: "button" as const,
66 }),
67 ...(!usesDuoHeader
68 ? [
69 withNativeGlassHeaderItem({
70 accessibilityLabel: "New task",
71 icon: { name: "square.and.pencil", type: "sfSymbol" as const },
72 identifier: "thread-left-new-task",
73 label: "New task",
74 onPress: () => navigation.navigate("NewTaskSheet", { screen: "NewTask" }),
75 type: "button" as const,
76 }),
77 ]
78 : []),
79 ],
80 [
81 panes.primarySidebarVisible,
82 props.onReturnToThread,
83 navigation,
84 togglePrimarySidebar,
85 usesDuoHeader,
86 usesNativeWorkspaceColumns,
87 ],
88 );
89 const canGoBack = navigation.canGoBack();
90 const compactHomeHeaderItems = useMemo<NativeHeaderItems>(
91 () => [
92 withNativeGlassHeaderItem({
93 accessibilityLabel: "Go to threads list",
94 icon: { name: "list.bullet", type: "sfSymbol" as const },
95 identifier: "thread-left-home",
96 onPress: () => navigation.dispatch(StackActions.replace("Home")),
97 type: "button" as const,
98 }),
99 ],
100 [navigation],
101 );
102
103 const options: AppNativeStackNavigationOptions = {
104 headerShown: true,
105 headerTitle: props.title,
106 headerTitleStyle: props.usesNativeHeaderGlass
107 ? {
108 fontSize: 17,
109 fontWeight: "800",
110 }
111 : undefined,
112 title: props.title,
113 headerBackVisible: !layout.usesSplitView,
114 unstable_headerLeftItems: layout.usesSplitView
115 ? () => splitLeftHeaderItems
116 : canGoBack
117 ? undefined
118 : () => compactHomeHeaderItems,
119 // The workspace navigator adds compose to the Duo detail column's bar.
120 unstable_headerRightItems: () =>
121 usesDuoHeader || layout.usesSplitView ? threadCenterHeaderItems : compactRightHeaderItems,
122 unstable_headerToolbarItems: () => [],
123 unstable_headerSubtitle: props.usesNativeHeaderGlass ? props.subtitle : undefined,
124 contentStyle: undefined,
125 };
126 const { environmentId, threadId, gitStatus } = props.gitControls;
127 return {
128 options,
129 // Header item factories are stabilized, so the native header only re-reads them when
130 // this version changes. Keying on the items keeps the Git menu status live; the menu
131 // callbacks also read state the items do not display (a "Push" item runs `push` or
132 // `commit_push` depending on the default ref), so that state is keyed too.
133 optionsVersion: [
134 splitLeftHeaderItems,
135 threadCenterHeaderItems,
136 compactRightHeaderItems,
137 usesDuoHeader,
138 environmentId,
139 threadId,
140 gitStatus?.isDefaultRef,
141 gitStatus?.refName,
142 gitStatus?.pr?.url,
143 ],
144 sidebar: false,
145 fallback:
146 !layout.usesSplitView && !props.usesNativeHeaderGlass ? (
147 <ThreadGitControls {...props.gitControls} showActionControls />
148 ) : null,
149 };
150}