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

useThreadHeaderOptions.tsx

132 lines5.2 KB

1import { StackActions, useNavigation } from "@react-navigation/native";
2import { useMemo } from "react";
3import type { AppNativeStackNavigationOptions } from "../../native/StackHeader";
4import { useAdaptiveWorkspaceLayout } from "../layout/AdaptiveWorkspaceLayout";
5import { withNativeGlassHeaderItem } from "../layout/native-glass-header-items";
6import {
7 ThreadGitControls,
8 useThreadGitCenterHeaderItems,
9 useThreadGitRightHeaderItems,
10} from "./ThreadGitControls";
11​
12type NativeHeaderItems = ReadonlyArray<Record<string, unknown>>;
13​
14export function useThreadHeaderOptions(props: {
15 readonly title: string;
16 readonly subtitle: string;
17 readonly headerColor: string;
18 readonly usesNativeHeaderGlass: boolean;
19 readonly gitControls: Parameters<typeof ThreadGitControls>[0];
20 readonly onReturnToThread?: () => void;
21}) {
22 const navigation = useNavigation();
23 const { layout, panes, togglePrimarySidebar } = useAdaptiveWorkspaceLayout();
24 const threadCenterHeaderItems = useThreadGitCenterHeaderItems(props.gitControls);
25 const compactRightHeaderItems = useThreadGitRightHeaderItems(props.gitControls);
26 const splitLeftHeaderItems = useMemo<NativeHeaderItems>(
27 () => [
28 {
29 // Match Mail's split-view detail toolbar: the first detail action sits
30 // inside the content pane, not flush against the sidebar divider.
31 spacing: 18,
32 type: "spacing" as const,
33 },
34 ...(props.onReturnToThread
35 ? [
36 withNativeGlassHeaderItem({
37 accessibilityLabel: "Return to chat",
38 icon: { name: "chevron.left", type: "sfSymbol" as const },
39 identifier: "thread-left-return",
40 onPress: props.onReturnToThread,
41 type: "button" as const,
42 }),
43 ]
44 : []),
45 withNativeGlassHeaderItem({
46 accessibilityLabel: panes.primarySidebarVisible
47 ? "Maximize content"
48 : "Show thread sidebar",
49 icon: {
50 name: panes.primarySidebarVisible ? "arrow.up.left.and.arrow.down.right" : "sidebar.left",
51 type: "sfSymbol" as const,
52 },
53 identifier: "thread-left-sidebar",
54 onPress: togglePrimarySidebar,
55 type: "button" as const,
56 }),
57 withNativeGlassHeaderItem({
58 accessibilityLabel: "New task",
59 icon: { name: "square.and.pencil", type: "sfSymbol" as const },
60 identifier: "thread-left-new-task",
61 onPress: () => navigation.navigate("NewTaskSheet", { screen: "NewTask" }),
62 type: "button" as const,
63 }),
64 ],
65 [panes.primarySidebarVisible, props.onReturnToThread, navigation, togglePrimarySidebar],
66 );
67 // Deep links / cold starts land with Thread as the ONLY route, where the
68 // native back button does not render. Provide an explicit Home escape for
69 // that case; when history exists the native back button is used instead.
70 const canGoBack = navigation.canGoBack();
71 const compactHomeHeaderItems = useMemo<NativeHeaderItems>(
72 () => [
73 withNativeGlassHeaderItem({
74 accessibilityLabel: "Go to threads list",
75 icon: { name: "list.bullet", type: "sfSymbol" as const },
76 identifier: "thread-left-home",
77 onPress: () => navigation.dispatch(StackActions.replace("Home")),
78 type: "button" as const,
79 }),
80 ],
81 [navigation],
82 );
83​
84 const options: AppNativeStackNavigationOptions = {
85 headerShown: true,
86 headerTitle: props.title,
87 headerTitleStyle: props.usesNativeHeaderGlass
88 ? {
89 fontSize: 17,
90 fontWeight: "800",
91 }
92 : undefined,
93 title: props.title,
94 headerBackVisible: !layout.usesSplitView,
95 // Compact uses the NATIVE back button when a previous route exists;
96 // deep links / cold starts get an explicit Home button instead.
97 // Split view always uses its custom left items.
98 unstable_headerLeftItems: layout.usesSplitView
99 ? () => splitLeftHeaderItems
100 : canGoBack
101 ? undefined
102 : () => compactHomeHeaderItems,
103 // Search lives in the persistent sidebar, so the split header keeps
104 // the git controls on the RIGHT (no center items — center space is
105 // reserved for future breadcrumbs/status).
106 unstable_headerRightItems: () =>
107 layout.usesSplitView ? threadCenterHeaderItems : compactRightHeaderItems,
108 unstable_headerSubtitle: props.usesNativeHeaderGlass ? props.subtitle : undefined,
109 contentStyle: undefined,
110 };
111 const { environmentId, threadId, gitStatus } = props.gitControls;
112 return {
113 options,
114 // Header item factories are stabilized, so the native header only re-reads them when
115 // this version changes. Keying on the items keeps the Git menu status live; the menu
116 // callbacks also read state the items do not display (a "Push" item runs `push` or
117 // `commit_push` depending on the default ref), so that state is keyed too.
118 optionsVersion: [
119 layout.usesSplitView ? threadCenterHeaderItems : compactRightHeaderItems,
120 environmentId,
121 threadId,
122 gitStatus?.isDefaultRef,
123 gitStatus?.refName,
124 gitStatus?.pr?.url,
125 ],
126 sidebar: false,
127 fallback:
128 !layout.usesSplitView && !props.usesNativeHeaderGlass ? (
129 <ThreadGitControls {...props.gitControls} showActionControls />
130 ) : null,
131 };