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

HardwareKeyboardCommandProvider.tsx

163 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 pullRequests: activeThread?.pullRequests,
48 linkedPullRequestUrl:
49 (activeThread?.linkedPullRequest ?? activeThread?.branchPullRequest)?.url ?? null,
50 }),
51 [activeThread, activeThreadRef],
52 );
53 const [copyFeedback, setCopyFeedback] = useState<GitActionProgress>(EMPTY_COPY_FEEDBACK);
54 const copyRequestIdRef = useRef(0);
55 const copyFeedbackTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
56 const dismissCopyFeedback = useCallback(() => {
57 if (copyFeedbackTimerRef.current !== null) {
58 clearTimeout(copyFeedbackTimerRef.current);
59 copyFeedbackTimerRef.current = null;
60 }
61 setCopyFeedback(EMPTY_COPY_FEEDBACK);
62 }, []);
63 const showCopyFeedback = useCallback((feedback: GitActionProgress) => {
64 if (copyFeedbackTimerRef.current !== null) {
65 clearTimeout(copyFeedbackTimerRef.current);
66 }
67 setCopyFeedback(feedback);
68 copyFeedbackTimerRef.current = setTimeout(() => {
69 copyFeedbackTimerRef.current = null;
70 setCopyFeedback(EMPTY_COPY_FEEDBACK);
71 }, COPY_FEEDBACK_DISMISS_MS);
72 }, []);
73 useEffect(
74 () => () => {
75 if (copyFeedbackTimerRef.current !== null) {
76 clearTimeout(copyFeedbackTimerRef.current);
77 }
78 },
79 [],
80 );
81 const registrationVersion = useSyncExternalStore(
82 subscribeToHardwareKeyboardCommandRegistrations,
83 getHardwareKeyboardCommandRegistrationVersion,
84 getHardwareKeyboardCommandRegistrationVersion,
85 );
86 const enabledCommands = useMemo(() => {
87 const commands = new Set<HardwareKeyboardCommand>(getRegisteredHardwareKeyboardCommands());
88 commands.add("newTask");
89 if (pathname !== "/" || navigation.canGoBack()) commands.add("back");
90 if (activeThreadRef !== null) {
91 commands.add("files");
92 commands.add("terminal");
93 commands.add("review");
94 if (pathname.split("/")[4] !== "terminal") commands.add("copyThreadReference");
95 }
96 return [...commands];
97 }, [pathname, registrationVersion, navigation]);
98​
99 const onCommand = useCallback(
100 (command: HardwareKeyboardCommand) => {
101 if (dispatchHardwareKeyboardCommand(command)) return;
102​
103 if (command === "copyThreadReference") {
104 if (copyTarget === null) return;
105 const requestId = ++copyRequestIdRef.current;
106 void tryCopyTextWithHaptic(copyTarget.value, {
107 target: copyTarget.clipboardTarget,
108 }).then((didCopy) => {
109 if (requestId !== copyRequestIdRef.current) return;
110 showCopyFeedback(
111 didCopy
112 ? {
113 phase: "success",
114 label: copyTarget.successTitle,
115 description: copyTarget.value,
116 }
117 : {
118 phase: "error",
119 label: copyTarget.failureTitle,
120 description: "Try again.",
121 },
122 );
123 });
124 return;
125 }
126​
127 if (command === "newTask") {
128 navigation.navigate("NewTaskSheet", { screen: "NewTask" });
129 return;
130 }
131 if (command === "back") {
132 if (navigation.canGoBack()) {
133 navigation.goBack();
134 } else {
135 navigation.dispatch(StackActions.replace("Home"));
136 }
137 return;
138 }
139​
140 const thread = parseActiveThreadPath(pathname);
141 if (!thread) return;
142 if (command === "files" && !/\/files(?:\/|$)/.test(pathname)) {
143 navigation.navigate("ThreadFiles", thread);
144 }
145 if (command === "terminal" && !/\/terminal(?:\/|$)/.test(pathname)) {
146 navigation.navigate("ThreadTerminal", thread);
147 }
148 if (command === "review" && !/\/review(?:\/|$)/.test(pathname)) {
149 navigation.navigate("ThreadReview", thread);
150 }
151 },
152 [copyTarget, navigation, pathname, showCopyFeedback],
153 );
154​
155 return (
156 <>
157 <T3KeyboardCommands enabledCommands={enabledCommands} onCommand={onCommand}>
158 {children}
159 </T3KeyboardCommands>
160 <GitActionProgressOverlay progress={copyFeedback} onDismiss={dismissCopyFeedback} />
161 </>
162 );