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

HardwareKeyboardCommandProvider.tsx

162 lines5.5 KB

1import { StackActions, useNavigation } from "@react-navigation/native";
2import { resolveThreadReferenceCopyTarget } from "@t3tools/shared/threadReference";
3import {
4 useCallback,
5 useEffect,
6 useMemo,
7 useRef,
8 useState,
9 useSyncExternalStore,
10 type PropsWithChildren,
11} from "react";
12​
13import { tryCopyTextWithHaptic } from "../../lib/copyTextWithHaptic";
14import { T3KeyboardCommands } from "../../native/T3KeyboardCommands";
15import { useThreadShell } from "../../state/entities";
16import type { GitActionProgress } from "../../state/use-vcs-action-state";
17import { GitActionProgressOverlay } from "../threads/GitActionProgressOverlay";
18import {
19 dispatchHardwareKeyboardCommand,
20 getHardwareKeyboardCommandRegistrationVersion,
21 getRegisteredHardwareKeyboardCommands,
22 parseActiveThreadPath,
23 subscribeToHardwareKeyboardCommandRegistrations,
24 type HardwareKeyboardCommand,
25} from "./hardwareKeyboardCommands";
26​
27const EMPTY_COPY_FEEDBACK: GitActionProgress = {
28 phase: "idle",
29 label: null,
30 description: null,
31};
32const COPY_FEEDBACK_DISMISS_MS = 3_000;
33​
34export function HardwareKeyboardCommandProvider({
35 children,
36 pathname,
37}: PropsWithChildren<{ readonly pathname: string }>) {
38 const navigation = useNavigation();
39 const activeThreadRef = useMemo(() => parseActiveThreadPath(pathname), [pathname]);
40 const activeThread = useThreadShell(activeThreadRef);
41 const copyTarget = useMemo(
42 () =>
43 activeThreadRef === null
44 ? null
45 : resolveThreadReferenceCopyTarget({
46 threadId: activeThread?.id ?? activeThreadRef.threadId,
47 linkedPullRequestUrl:
48 (activeThread?.linkedPullRequest ?? activeThread?.branchPullRequest)?.url ?? null,
49 }),
50 [activeThread, activeThreadRef],
51 );
52 const [copyFeedback, setCopyFeedback] = useState<GitActionProgress>(EMPTY_COPY_FEEDBACK);
53 const copyRequestIdRef = useRef(0);
54 const copyFeedbackTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
55 const dismissCopyFeedback = useCallback(() => {
56 if (copyFeedbackTimerRef.current !== null) {
57 clearTimeout(copyFeedbackTimerRef.current);
58 copyFeedbackTimerRef.current = null;
59 }
60 setCopyFeedback(EMPTY_COPY_FEEDBACK);
61 }, []);
62 const showCopyFeedback = useCallback((feedback: GitActionProgress) => {
63 if (copyFeedbackTimerRef.current !== null) {
64 clearTimeout(copyFeedbackTimerRef.current);
65 }
66 setCopyFeedback(feedback);
67 copyFeedbackTimerRef.current = setTimeout(() => {
68 copyFeedbackTimerRef.current = null;
69 setCopyFeedback(EMPTY_COPY_FEEDBACK);
70 }, COPY_FEEDBACK_DISMISS_MS);
71 }, []);
72 useEffect(
73 () => () => {
74 if (copyFeedbackTimerRef.current !== null) {
75 clearTimeout(copyFeedbackTimerRef.current);
76 }
77 },
78 [],
79 );
80 const registrationVersion = useSyncExternalStore(
81 subscribeToHardwareKeyboardCommandRegistrations,
82 getHardwareKeyboardCommandRegistrationVersion,
83 getHardwareKeyboardCommandRegistrationVersion,
84 );
85 const enabledCommands = useMemo(() => {
86 const commands = new Set<HardwareKeyboardCommand>(getRegisteredHardwareKeyboardCommands());
87 commands.add("newTask");
88 if (pathname !== "/" || navigation.canGoBack()) commands.add("back");
89 if (activeThreadRef !== null) {
90 commands.add("files");
91 commands.add("terminal");
92 commands.add("review");
93 if (pathname.split("/")[4] !== "terminal") commands.add("copyThreadReference");
94 }
95 return [...commands];
96 }, [pathname, registrationVersion, navigation]);
97​
98 const onCommand = useCallback(
99 (command: HardwareKeyboardCommand) => {
100 if (dispatchHardwareKeyboardCommand(command)) return;
101​
102 if (command === "copyThreadReference") {
103 if (copyTarget === null) return;
104 const requestId = ++copyRequestIdRef.current;
105 void tryCopyTextWithHaptic(copyTarget.value, {
106 target: copyTarget.clipboardTarget,
107 }).then((didCopy) => {
108 if (requestId !== copyRequestIdRef.current) return;
109 showCopyFeedback(
110 didCopy
111 ? {
112 phase: "success",
113 label: copyTarget.successTitle,
114 description: copyTarget.value,
115 }
116 : {
117 phase: "error",
118 label: copyTarget.failureTitle,
119 description: "Try again.",
120 },
121 );
122 });
123 return;
124 }
125​
126 if (command === "newTask") {
127 navigation.navigate("NewTaskSheet", { screen: "NewTask" });
128 return;
129 }
130 if (command === "back") {
131 if (navigation.canGoBack()) {
132 navigation.goBack();
133 } else {
134 navigation.dispatch(StackActions.replace("Home"));
135 }
136 return;
137 }
138​
139 const thread = parseActiveThreadPath(pathname);
140 if (!thread) return;
141 if (command === "files" && !/\/files(?:\/|$)/.test(pathname)) {
142 navigation.navigate("ThreadFiles", thread);
143 }
144 if (command === "terminal" && !/\/terminal(?:\/|$)/.test(pathname)) {
145 navigation.navigate("ThreadTerminal", thread);
146 }
147 if (command === "review" && !/\/review(?:\/|$)/.test(pathname)) {
148 navigation.navigate("ThreadReview", thread);
149 }
150 },
151 [copyTarget, navigation, pathname, showCopyFeedback],
152 );
153​
154 return (
155 <>
156 <T3KeyboardCommands enabledCommands={enabledCommands} onCommand={onCommand}>
157 {children}
158 </T3KeyboardCommands>
159 <GitActionProgressOverlay progress={copyFeedback} onDismiss={dismissCopyFeedback} />
160 </>
161 );