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