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

useThreadHeaderOptions.tsx

171 lines6.0 KB

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 duoRightHeaderItems = useMemo<NativeHeaderItems>(
104 () => [
105 ...threadCenterHeaderItems,
106 { type: "spacing", spacing: 8 },
107 withNativeGlassHeaderItem({
108 type: "button" as const,
109 axisBehavior: "verticalPreferred",
110 identifier: "thread-right-new-task",
111 label: "New task",
112 accessibilityLabel: "New task",
113 icon: { name: "square.and.pencil", type: "sfSymbol" as const },
114 onPress: () => navigation.navigate("NewTaskSheet", { screen: "NewTask" }),
115 }),
116 ],
117 [navigation, threadCenterHeaderItems],
118 );
119​
120 const options: AppNativeStackNavigationOptions = {
121 headerShown: true,
122 headerTitle: props.title,
123 headerTitleStyle: props.usesNativeHeaderGlass
124 ? {
125 fontSize: 17,
126 fontWeight: "800",
127 }
128 : undefined,
129 title: props.title,
130 headerBackVisible: !layout.usesSplitView,
131 unstable_headerLeftItems: layout.usesSplitView
132 ? () => splitLeftHeaderItems
133 : canGoBack
134 ? undefined
135 : () => compactHomeHeaderItems,
136 unstable_headerRightItems: () =>
137 usesDuoHeader
138 ? duoRightHeaderItems
139 : layout.usesSplitView
140 ? threadCenterHeaderItems
141 : compactRightHeaderItems,
142 unstable_headerToolbarItems: () => [],
143 unstable_headerSubtitle: props.usesNativeHeaderGlass ? props.subtitle : undefined,
144 contentStyle: undefined,
145 };
146 const { environmentId, threadId, gitStatus } = props.gitControls;
147 return {
148 options,
149 // Header item factories are stabilized, so the native header only re-reads them when
150 // this version changes. Keying on the items keeps the Git menu status live; the menu
151 // callbacks also read state the items do not display (a "Push" item runs `push` or
152 // `commit_push` depending on the default ref), so that state is keyed too.
153 optionsVersion: [
154 splitLeftHeaderItems,
155 threadCenterHeaderItems,
156 compactRightHeaderItems,
157 duoRightHeaderItems,
158 usesDuoHeader,
159 environmentId,
160 threadId,
161 gitStatus?.isDefaultRef,
162 gitStatus?.refName,
163 gitStatus?.pr?.url,
164 ],
165 sidebar: false,
166 fallback:
167 !layout.usesSplitView && !props.usesNativeHeaderGlass ? (
168 <ThreadGitControls {...props.gitControls} showActionControls />
169 ) : null,
170 };