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

MaterialThreadListToolbar.tsx

143 lines5.2 KB

1import { useCallback, useEffect, useRef, useState, type ComponentProps } from "react";
2import { BackHandler, Keyboard, type TextInput, View, type LayoutChangeEvent } from "react-native";
3import { useSafeAreaInsets } from "react-native-safe-area-context";
4import type { MenuAction } from "@react-native-menu/menu";
5​
6import { AndroidHeaderIconButton } from "../../components/AndroidScreenHeader";
7import { CompactBrandTitle } from "../../components/CompactBrandTitle";
8import { MaterialFloatingActionButton } from "../../components/MaterialFloatingActionButton";
9import { AndroidAnchoredMenu } from "../../components/AndroidAnchoredMenu";
10import { ControlPillMenu } from "../../components/ControlPill";
11import { MaterialSearchField } from "../../components/MaterialSearchField";
12import { useHardwareKeyboardCommand } from "../keyboard/hardwareKeyboardCommands";
13import { WorkspaceConnectionTitle } from "./WorkspaceConnectionTitle";
14import { useWorkspaceState } from "../../state/workspace";
15import { useMaterialToolbarHeight } from "../../components/useMaterialToolbarHeight";
16​
17/** One toolbar height for the compact list and expanded sidebar, including search. */
18export function MaterialThreadListToolbar(props: {
19 readonly searchQuery: string;
20 readonly onSearchQueryChange: (query: string) => void;
21 readonly filterActions: MenuAction[];
22 readonly filterCustomized: boolean;
23 readonly onFilterAction: NonNullable<ComponentProps<typeof ControlPillMenu>["onPressAction"]>;
24 readonly onOpenSettings: () => void;
25 readonly onOpenEnvironments: () => void;
26 readonly sidebar?: boolean;
27 readonly onLayout?: (event: LayoutChangeEvent) => void;
28 readonly onRequestVisibility?: () => void;
29}) {
30 const insets = useSafeAreaInsets();
31 const toolbarHeight = useMaterialToolbarHeight();
32 const { state } = useWorkspaceState();
33 const { onRequestVisibility, onSearchQueryChange } = props;
34 const searchRef = useRef<TextInput>(null);
35 const [searchOpen, setSearchOpen] = useState(false);
36 const searching = searchOpen || props.searchQuery.length > 0;
37 const openSearch = useCallback(() => {
38 onRequestVisibility?.();
39 setSearchOpen(true);
40 searchRef.current?.focus();
41 return true;
42 }, [onRequestVisibility]);
43 useHardwareKeyboardCommand("focusSearch", openSearch);
44​
45 const closeSearch = useCallback(() => {
46 onSearchQueryChange("");
47 setSearchOpen(false);
48 Keyboard.dismiss();
49 }, [onSearchQueryChange]);
50​
51 useEffect(() => {
52 if (!searching) return;
53 const subscription = BackHandler.addEventListener("hardwareBackPress", () => {
54 closeSearch();
55 return true;
56 });
57 return () => subscription.remove();
58 }, [closeSearch, searching]);
59​
60 const filterIcon = props.filterCustomized
61 ? "line.3.horizontal.decrease.circle.fill"
62 : "line.3.horizontal.decrease.circle";
63 const searchField = (
64 <MaterialSearchField
65 inputRef={searchRef}
66 accessibilityLabel="Search threads"
67 clearAccessibilityLabel="Clear search"
68 placeholder="Search"
69 value={props.searchQuery}
70 onChangeText={onSearchQueryChange}
71 />
72 );
73​
74 return (
75 <>
76 <View
77 onLayout={props.onLayout}
78 className={
79 props.sidebar
80 ? "absolute inset-x-0 top-0 z-[4] bg-header px-2 pb-2"
81 : "bg-header px-2 pb-2"
82 }
83 style={{ paddingTop: Math.max(insets.top, 12) }}
84 >
85 <View className="flex-row items-center gap-1" style={{ minHeight: toolbarHeight }}>
86 {searching ? (
87 <>
88 <AndroidHeaderIconButton
89 accessibilityLabel="Close search"
90 icon="arrow.left"
91 onPress={closeSearch}
92 />
93 {searchField}
94 </>
95 ) : (
96 <>
97 {/* Match the visible inset of the trailing 48dp icon button. */}
98 <View className="min-w-0 flex-1 pl-4">
99 <WorkspaceConnectionTitle
100 grow
101 onPress={props.onOpenEnvironments}
102 brand={<CompactBrandTitle allowFontScaling={false} />}
103 />
104 </View>
105 <AndroidHeaderIconButton
106 accessibilityLabel="Search threads"
107 icon="magnifyingglass"
108 onPress={openSearch}
109 />
110 <AndroidHeaderIconButton
111 accessibilityLabel="Open settings"
112 icon="gearshape"
113 onPress={props.onOpenSettings}
114 />
115 </>
116 )}
117 </View>
118 </View>
119 {/* Sit 8dp above the 56dp extended New thread FAB. */}
120 {state.hasConnections ? (
121 <View
122 className="absolute right-5 z-[5]"
123 style={{
124 bottom:
125 (props.sidebar ? Math.max(insets.bottom, 12) + 6 : Math.max(insets.bottom, 16) + 16) +
126 56 +
127 8,
128 }}
129 >
130 <AndroidAnchoredMenu actions={props.filterActions} onPressAction={props.onFilterAction}>
131 {(open) => (
132 <MaterialFloatingActionButton
133 label="Filter and sort threads"
134 icon={filterIcon}
135 onPress={open}
136 />
137 )}
138 </AndroidAnchoredMenu>
139 </View>
140 ) : null}
141 </>
142 );