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