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

ThreadUndoNotice.tsx

123 lines4.5 KB

1import { useAndroidFloatingTabs } from "../settings/appearance/AppearancePreferencesProvider";
2import { use, useState } from "react";
3import { useKeyboardState } from "react-native-keyboard-controller";
4import { Platform, View } from "react-native";
5import { useGT } from "gt-react-native";
6import { useSafeAreaInsets } from "react-native-safe-area-context";
7import { Toast } from "../../components/ui/Toast";
8import {
9 useDeviceType,
10 useDuoBottomBarInset,
11 useNativeLayoutMetrics,
12 useNativeColumnLayoutMetrics,
13} from "../../native/native-layout-metrics";
14import { workspaceSearchBottomClearance } from "../layout/workspace/workspace-tabs-state";
15import {
16 useWorkspaceTabControls,
17 useWorkspaceTabsSelector,
18} from "../layout/workspace/WorkspaceTabsContext";
19import { FabTopContext } from "./FabScrollContext";
20import { HostPressurePill, type OpenThread } from "./HostPressurePill";
21import {
22 dismissThreadUndoNotice,
23 readThreadUndoNotice,
24 useThreadUndoNotice,
25} from "./thread-undo-notice";
26​
27/** The bottom notice slot: an undo while one is offered, otherwise the machine load pill. */
28export function ThreadUndoNotice(props: {
29 readonly visible: boolean;
30 readonly onOpenThread: OpenThread;
31}) {
32 const gt = useGT();
33 const floatingTabsEnabled = useAndroidFloatingTabs();
34 const notice = useThreadUndoNotice();
35 const insets = useSafeAreaInsets();
36 const columnMetrics = useNativeColumnLayoutMetrics();
37 const rootMetrics = useNativeLayoutMetrics();
38 const workspace = useWorkspaceTabControls();
39 const threadsTab = useWorkspaceTabsSelector((state) => state.tabs.threads);
40 const deviceType = useDeviceType();
41 const keyboardHeight = useKeyboardState((keyboard) => keyboard.height);
42 const fabTop = use(FabTopContext);
43 const horizontalInsets =
44 Platform.OS === "ios"
45 ? deviceType === "phone"
46 ? { left: 29, right: 23 }
47 : { left: 20, right: 20 }
48 : { left: 12, right: 12 };
49 // The Duo's native tab pill extends above the 48-point toolbar reservation.
50 const workspacePadding =
51 useDuoBottomBarInset() === undefined
52 ? deviceType === "phone" && keyboardHeight === 0
53 ? 32
54 : 16
55 : 8;
56 // Replacing a notice must also cancel the native gesture tracking the old one.
57 const [instance, setInstance] = useState({ notice, key: 0 });
58 if (instance.notice !== notice) setInstance({ notice, key: instance.key + 1 });
59 if (!props.visible) return null;
60 const foldedResting =
61 rootMetrics?.hasHinge &&
62 rootMetrics.horizontalSizeClass === "compact" &&
63 threadsTab?.searchMode === "resting";
64 const bottom =
65 Platform.OS === "ios"
66 ? foldedResting
67 ? // Sit level with the rail's tab pill, which floats inside the bottom safe area.
68 Math.max(
69 rootMetrics.bottomBarInset ?? rootMetrics.safeArea.bottom + 8,
70 keyboardHeight + 8,
71 )
72 : rootMetrics?.hasHinge && rootMetrics.verticalBarEdge !== "none"
73 ? // The column's safe area already includes UIKit's search toolbar in this posture.
74 workspaceSearchBottomClearance({
75 bottomInset: rootMetrics.safeArea.bottom,
76 bottomBarInset: rootMetrics.bottomBarInset,
77 keyboardHeight,
78 }) -
79 16 +
80 8
81 : Math.max(
82 insets.bottom,
83 columnMetrics?.safeArea.bottom ?? 0,
84 (workspace?.bottomClearance ?? 0) - workspacePadding,
85 ) + 8
86 : floatingTabsEnabled && keyboardHeight === 0
87 ? Math.max(insets.bottom + 12, workspace?.bottomClearance ?? 0)
88 : Math.max(insets.bottom, fabTop ?? 0) + 12;
89 return (
90 <View
91 key={instance.key}
92 collapsable={false}
93 className="absolute z-[100]"
94 style={{ bottom, ...horizontalInsets }}
95 pointerEvents="box-none"
96 >
97 {notice === null ? (
98 <HostPressurePill onOpenThread={props.onOpenThread} />
99 ) : (
100 <Toast
101 message={
102 notice.action === "Settled"
103 ? gt`Settled thread`
104 : notice.action === "Snoozed"
105 ? gt`Snoozed thread`
106 : gt`Unpinned thread`
107 }
108 action={{
109 label: gt`Undo`,
110 onPress: () => {
111 void notice.undo();
112 },
113 }}
114 dismissMode="swipe"
115 onDismiss={() => {
116 // A newer action can replace the notice while the swipe is animating out.
117 if (readThreadUndoNotice() === notice) dismissThreadUndoNotice();
118 }}
119 />
120 )}
121 </View>
122 );