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

HomeHeader.android.tsx

161 lines5.2 KB

1import type { MenuAction } from "@react-native-menu/menu";
2import { useCallback, useMemo } from "react";
3import { NativeStackScreenOptions } from "../../native/StackHeader";
4import { useThreadListV2Enabled } from "../threads/use-thread-list-v2-enabled";
5import { MaterialThreadListToolbar } from "./MaterialThreadListToolbar";
6import {
7 hasCustomHomeListOptions,
8 PROJECT_SORT_OPTIONS,
9 THREAD_SORT_OPTIONS,
10} from "./home-list-options";
11import type { HomeHeaderProps } from "./HomeHeader.types";
12​
13export type { HomeHeaderEnvironment } from "./HomeHeader.types";
14​
15function checkedMenuState(checked: boolean) {
16 return checked ? ("on" as const) : undefined;
18​
19export function HomeHeader(props: HomeHeaderProps) {
20 // Thread List v2 lays the list out in fixed creation order, so the
21 // sort/group filter controls would be silently ignored — hide them and
22 // key the "customized" icon state off the environment filter alone.
23 const threadListV2Enabled = useThreadListV2Enabled();
24 const hasCustomListOptions = threadListV2Enabled
25 ? props.selectedEnvironmentId !== null || props.selectedProjectKey !== null
26 : hasCustomHomeListOptions(props);
27 const menuActions = useMemo<MenuAction[]>(
28 () => [
29 {
30 id: "environment",
31 title: "Environment",
32 subactions: [
33 {
34 id: "environment:all",
35 title: "All environments",
36 state: checkedMenuState(props.selectedEnvironmentId === null),
37 },
38 ...props.environments.map((environment) => ({
39 id: `environment:${environment.environmentId}`,
40 title: environment.label,
41 state: checkedMenuState(props.selectedEnvironmentId === environment.environmentId),
42 })),
43 ],
44 },
45 ...(props.projects.length === 0
46 ? []
47 : ([
48 {
49 id: "project",
50 title: "Project",
51 subactions: [
52 {
53 id: "project:all",
54 title: "All projects",
55 state: checkedMenuState(props.selectedProjectKey === null),
56 },
57 ...props.projects.map((project) => ({
58 id: `project:${project.key}`,
59 title: project.label,
60 state: checkedMenuState(props.selectedProjectKey === project.key),
61 })),
62 ],
63 },
64 ] satisfies MenuAction[])),
65 ...(threadListV2Enabled
66 ? []
67 : ([
68 {
69 id: "project-sort",
70 title: "Sort projects",
71 subactions: PROJECT_SORT_OPTIONS.map((option) => ({
72 id: `project-sort:${option.value}`,
73 title: option.label,
74 state: checkedMenuState(props.projectSortOrder === option.value),
75 })),
76 },
77 {
78 id: "thread-sort",
79 title: "Sort threads",
80 subactions: THREAD_SORT_OPTIONS.map((option) => ({
81 id: `thread-sort:${option.value}`,
82 title: option.label,
83 state: checkedMenuState(props.threadSortOrder === option.value),
84 })),
85 },
86 ] satisfies MenuAction[])),
87 ],
88 [
89 props.environments,
90 props.projectSortOrder,
91 props.projects,
92 props.selectedEnvironmentId,
93 props.selectedProjectKey,
94 props.threadSortOrder,
95 threadListV2Enabled,
96 ],
97 );
98 const handleMenuAction = useCallback(
99 (event: { nativeEvent: { event: string } }) => {
100 const id = event.nativeEvent.event;
101 if (id === "environment:all") {
102 props.onEnvironmentChange(null);
103 return;
104 }
105​
106 if (id.startsWith("environment:")) {
107 const environmentId = id.slice("environment:".length);
108 const environment = props.environments.find(
109 (candidate) => candidate.environmentId === environmentId,
110 );
111 if (environment) {
112 props.onEnvironmentChange(environment.environmentId);
113 }
114 return;
115 }
116​
117 if (id === "project:all") {
118 props.onProjectChange(null);
119 return;
120 }
121​
122 if (id.startsWith("project:")) {
123 const projectKey = id.slice("project:".length);
124 if (props.projects.some((project) => project.key === projectKey)) {
125 props.onProjectChange(projectKey);
126 }
127 return;
128 }
129​
130 const projectSort = PROJECT_SORT_OPTIONS.find(
131 (option) => id === `project-sort:${option.value}`,
132 );
133 if (projectSort) {
134 props.onProjectSortOrderChange(projectSort.value);
135 return;
136 }
137​
138 const threadSort = THREAD_SORT_OPTIONS.find((option) => id === `thread-sort:${option.value}`);
139 if (threadSort) {
140 props.onThreadSortOrderChange(threadSort.value);
141 return;
142 }
143 },
144 [props],
145 );
146​
147 return (
148 <>
149 <NativeStackScreenOptions options={{ headerShown: false }} />
150 <MaterialThreadListToolbar
151 searchQuery={props.searchQuery}
152 onSearchQueryChange={props.onSearchQueryChange}
153 filterActions={menuActions}
154 filterCustomized={hasCustomListOptions}
155 onFilterAction={handleMenuAction}
156 onOpenSettings={props.onOpenSettings}
157 onOpenEnvironments={props.onOpenEnvironments}
158 />
159 </>
160 );