11import { StackActions , useNavigation } from "@react-navigation/native" ;
2- import { useCallback , useMemo , useSyncExternalStore , type PropsWithChildren } from "react" ;
2+ import { resolveThreadReferenceCopyTarget } from "@t3tools/shared/threadReference" ;
3+ import {
4+ useCallback ,
5+ useEffect ,
6+ useMemo ,
7+ useRef ,
8+ useState ,
9+ useSyncExternalStore ,
10+ type PropsWithChildren ,
11+ } from "react" ;
312
13+ import { tryCopyTextWithHaptic } from "../../lib/copyTextWithHaptic" ;
414import { T3KeyboardCommands } from "../../native/T3KeyboardCommands" ;
15+ import { useProject , useThreadShell } from "../../state/entities" ;
16+ import { useEnvironmentQuery } from "../../state/query" ;
17+ import type { GitActionProgress } from "../../state/use-vcs-action-state" ;
18+ import { vcsEnvironment } from "../../state/vcs" ;
19+ import { GitActionProgressOverlay } from "../threads/GitActionProgressOverlay" ;
520import {
621 dispatchHardwareKeyboardCommand ,
722 getHardwareKeyboardCommandRegistrationVersion ,
@@ -11,11 +26,86 @@ import {
1126 type HardwareKeyboardCommand ,
1227} from "./hardwareKeyboardCommands" ;
1328
29+ const EMPTY_COPY_FEEDBACK : GitActionProgress = {
30+ phase : "idle" ,
31+ label : null ,
32+ description : null ,
33+ } ;
34+ const COPY_FEEDBACK_DISMISS_MS = 3_000 ;
35+
1436export function HardwareKeyboardCommandProvider ( {
1537 children,
1638 pathname,
1739} : PropsWithChildren < { readonly pathname : string } > ) {
1840 const navigation = useNavigation ( ) ;
41+ const activeThreadRef = useMemo ( ( ) => parseActiveThreadPath ( pathname ) , [ pathname ] ) ;
42+ const activeThread = useThreadShell ( activeThreadRef ) ;
43+ const activeProjectRef = useMemo (
44+ ( ) =>
45+ activeThread === null
46+ ? null
47+ : {
48+ environmentId : activeThread . environmentId ,
49+ projectId : activeThread . projectId ,
50+ } ,
51+ [ activeThread ] ,
52+ ) ;
53+ const activeProject = useProject ( activeProjectRef ) ;
54+ const activeThreadCwd = activeThread ?. worktreePath ?? activeProject ?. workspaceRoot ?? null ;
55+ const gitStatus = useEnvironmentQuery (
56+ activeThread !== null &&
57+ activeThread . linkedPullRequest == null &&
58+ activeThread . branch !== null &&
59+ activeThreadCwd !== null
60+ ? vcsEnvironment . status ( {
61+ environmentId : activeThread . environmentId ,
62+ input : { cwd : activeThreadCwd } ,
63+ } )
64+ : null ,
65+ ) . data ;
66+ const detectedPullRequestUrl =
67+ activeThread ?. branch != null && gitStatus ?. refName === activeThread . branch
68+ ? ( gitStatus . pr ?. url ?? null )
69+ : null ;
70+ const copyTarget = useMemo (
71+ ( ) =>
72+ activeThreadRef === null
73+ ? null
74+ : resolveThreadReferenceCopyTarget ( {
75+ threadId : activeThread ?. id ?? activeThreadRef . threadId ,
76+ linkedPullRequestUrl : activeThread ?. linkedPullRequest ?. url ?? null ,
77+ detectedPullRequestUrl,
78+ } ) ,
79+ [ activeThread , activeThreadRef , detectedPullRequestUrl ] ,
80+ ) ;
81+ const [ copyFeedback , setCopyFeedback ] = useState < GitActionProgress > ( EMPTY_COPY_FEEDBACK ) ;
82+ const copyRequestIdRef = useRef ( 0 ) ;
83+ const copyFeedbackTimerRef = useRef < ReturnType < typeof setTimeout > | null > ( null ) ;
84+ const dismissCopyFeedback = useCallback ( ( ) => {
85+ if ( copyFeedbackTimerRef . current !== null ) {
86+ clearTimeout ( copyFeedbackTimerRef . current ) ;
87+ copyFeedbackTimerRef . current = null ;
88+ }
89+ setCopyFeedback ( EMPTY_COPY_FEEDBACK ) ;
90+ } , [ ] ) ;
91+ const showCopyFeedback = useCallback ( ( feedback : GitActionProgress ) => {
92+ if ( copyFeedbackTimerRef . current !== null ) {
93+ clearTimeout ( copyFeedbackTimerRef . current ) ;
94+ }
95+ setCopyFeedback ( feedback ) ;
96+ copyFeedbackTimerRef . current = setTimeout ( ( ) => {
97+ copyFeedbackTimerRef . current = null ;
98+ setCopyFeedback ( EMPTY_COPY_FEEDBACK ) ;
99+ } , COPY_FEEDBACK_DISMISS_MS ) ;
100+ } , [ ] ) ;
101+ useEffect (
102+ ( ) => ( ) => {
103+ if ( copyFeedbackTimerRef . current !== null ) {
104+ clearTimeout ( copyFeedbackTimerRef . current ) ;
105+ }
106+ } ,
107+ [ ] ,
108+ ) ;
19109 const registrationVersion = useSyncExternalStore (
20110 subscribeToHardwareKeyboardCommandRegistrations ,
21111 getHardwareKeyboardCommandRegistrationVersion ,
@@ -25,10 +115,11 @@ export function HardwareKeyboardCommandProvider({
25115 const commands = new Set < HardwareKeyboardCommand > ( getRegisteredHardwareKeyboardCommands ( ) ) ;
26116 commands . add ( "newTask" ) ;
27117 if ( pathname !== "/" || navigation . canGoBack ( ) ) commands . add ( "back" ) ;
28- if ( parseActiveThreadPath ( pathname ) ) {
118+ if ( activeThreadRef !== null ) {
29119 commands . add ( "files" ) ;
30120 commands . add ( "terminal" ) ;
31121 commands . add ( "review" ) ;
122+ if ( pathname . split ( "/" ) [ 4 ] !== "terminal" ) commands . add ( "copyThreadReference" ) ;
32123 }
33124 return [ ...commands ] ;
34125 } , [ pathname , registrationVersion , navigation ] ) ;
@@ -37,6 +128,30 @@ export function HardwareKeyboardCommandProvider({
37128 ( command : HardwareKeyboardCommand ) => {
38129 if ( dispatchHardwareKeyboardCommand ( command ) ) return ;
39130
131+ if ( command === "copyThreadReference" ) {
132+ if ( copyTarget === null ) return ;
133+ const requestId = ++ copyRequestIdRef . current ;
134+ void tryCopyTextWithHaptic ( copyTarget . value , {
135+ target : copyTarget . clipboardTarget ,
136+ } ) . then ( ( didCopy ) => {
137+ if ( requestId !== copyRequestIdRef . current ) return ;
138+ showCopyFeedback (
139+ didCopy
140+ ? {
141+ phase : "success" ,
142+ label : copyTarget . successTitle ,
143+ description : copyTarget . value ,
144+ }
145+ : {
146+ phase : "error" ,
147+ label : copyTarget . failureTitle ,
148+ description : "Try again." ,
149+ } ,
150+ ) ;
151+ } ) ;
152+ return ;
153+ }
154+
40155 if ( command === "newTask" ) {
41156 navigation . navigate ( "NewTaskSheet" , { screen : "NewTask" } ) ;
42157 return ;
@@ -62,12 +177,15 @@ export function HardwareKeyboardCommandProvider({
62177 navigation . navigate ( "ThreadReview" , thread ) ;
63178 }
64179 } ,
65- [ pathname , navigation ] ,
180+ [ copyTarget , navigation , pathname , showCopyFeedback ] ,
66181 ) ;
67182
68183 return (
69- < T3KeyboardCommands enabledCommands = { enabledCommands } onCommand = { onCommand } >
70- { children }
71- </ T3KeyboardCommands >
184+ < >
185+ < T3KeyboardCommands enabledCommands = { enabledCommands } onCommand = { onCommand } >
186+ { children }
187+ </ T3KeyboardCommands >
188+ < GitActionProgressOverlay progress = { copyFeedback } onDismiss = { dismissCopyFeedback } />
189+ </ >
72190 ) ;
73191}
0 commit comments