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

use-thread-settings-sheet-presentation.ts

98 lines3.1 KB

1import { useCallback, useEffect, useRef, useState, type RefObject } from "react";
2import { KeyboardController } from "react-native-keyboard-controller";
3​
4import type { ComposerEditorHandle } from "../../components/ComposerEditor";
5​
6export type ThreadSettingsSheetCloseReason = "save" | "dismiss";
7​
8type PresentationPhase = "closed" | "opening" | "visible" | "closing";
9​
10/**
11 * Keeps the custom native composer and the settings modal from owning focus at
12 * the same time. Opening waits for the keyboard dismissal to finish, while
13 * focus restoration waits for the modal's dismissal callback.
14 */
15export function useThreadSettingsSheetPresentation(input: {
16 readonly editorRef: RefObject<ComposerEditorHandle | null>;
17 readonly isEditorFocused: boolean;
18}) {
19 const [phase, setPhase] = useState<PresentationPhase>("closed");
20 const isActiveRef = useRef(false);
21 const isMountedRef = useRef(true);
22 const openingIdRef = useRef(0);
23 const restoreFocusOnSaveRef = useRef(false);
24 const shouldRestoreAfterDismissRef = useRef(false);
25​
26 useEffect(
27 () => () => {
28 isMountedRef.current = false;
29 isActiveRef.current = false;
30 openingIdRef.current += 1;
31 },
32 [],
33 );
34​
35 const open = useCallback(() => {
36 if (isActiveRef.current) {
37 return;
38 }
39​
40 isActiveRef.current = true;
41 restoreFocusOnSaveRef.current = input.isEditorFocused || KeyboardController.isVisible();
42 shouldRestoreAfterDismissRef.current = false;
43 setPhase("opening");
44​
45 const openingId = openingIdRef.current + 1;
46 openingIdRef.current = openingId;
47​
48 // Keyboard.dismiss() only tracks React Native TextInputs. The composer is
49 // a custom native text view, so explicitly resign its first responder too.
50 input.editorRef.current?.blur();
51 void KeyboardController.dismiss().then(() => {
52 if (!isMountedRef.current || !isActiveRef.current || openingIdRef.current !== openingId) {
53 return;
54 }
55 setPhase("visible");
56 });
57 }, [input.editorRef, input.isEditorFocused]);
58​
59 const close = useCallback((reason: ThreadSettingsSheetCloseReason) => {
60 if (!isActiveRef.current) {
61 return;
62 }
63​
64 openingIdRef.current += 1;
65 shouldRestoreAfterDismissRef.current = reason === "save" && restoreFocusOnSaveRef.current;
66 setPhase("closing");
67 }, []);
68​
69 const onDismissed = useCallback(() => {
70 const shouldRestoreFocus = shouldRestoreAfterDismissRef.current;
71 shouldRestoreAfterDismissRef.current = false;
72 restoreFocusOnSaveRef.current = false;
73 isActiveRef.current = false;
74 setPhase("closed");
75​
76 if (shouldRestoreFocus) {
77 input.editorRef.current?.focus();
78 }
79 }, [input.editorRef]);
80​
81 // The new-task screen can have an autofocus queued before the sheet opens.
82 // Preserve that intent for Save without allowing it to focus under the modal.
83 const restoreFocusAfterSave = useCallback(() => {
84 if (isActiveRef.current) {
85 restoreFocusOnSaveRef.current = true;
86 }
87 }, []);
88​
89 return {
90 isActive: phase !== "closed",
91 isActiveRef,
92 isVisible: phase === "visible",
93 open,
94 close,
95 onDismissed,
96 restoreFocusAfterSave,
97 } as const;