11import { pipe } from "effect/Function" ;
22import { useAtomValue } from "@effect/atom-react" ;
3+ import { useNavigation , type StaticScreenProps } from "@react-navigation/native" ;
34import {
45 type LinkableMachine ,
56 linkRequestAnswerInput ,
@@ -20,6 +21,8 @@ import {
2021 type AuthMcpClientAccess ,
2122 type EnvironmentId ,
2223 type OrchestrationV2ProjectedTurnItem ,
24+ type ThreadId ,
25+ type TurnItemId ,
2326} from "@t3tools/contracts" ;
2427import * as Option from "effect/Option" ;
2528import * as Result from "effect/Result" ;
@@ -37,7 +40,10 @@ import { useAtomCommand } from "../../state/use-atom-command";
3740import { ListChoiceRow as SettingsChoiceRow } from "../../components/ui/ListChoiceRow" ;
3841import { SettingsSection } from "../settings/components/SettingsSection" ;
3942import { Button } from "../../components/ui/Button" ;
40- import { Sheet } from "../../components/ui/Sheet" ;
43+ import { ScreenContent } from "../../components/ui/ScreenContent" ;
44+ import { SheetHeader } from "../../components/ui/SheetHeader" ;
45+ import { useHardwareBack } from "../../lib/backHandler" ;
46+ import { useEnvironmentThread } from "../../state/threads" ;
4147import { RUNTIME_MODE_CHOICES } from "./thread-settings-options" ;
4248
4349/** The same five levels as web's access picker: read-only, then the runtime modes. */
@@ -104,7 +110,7 @@ function PendingLinkRequest(props: {
104110 readonly item : LinkRequestItem ;
105111} ) {
106112 const { item } = props ;
107- const [ open , setOpen ] = useState ( false ) ;
113+ const navigation = useNavigation ( ) ;
108114 const [ declining , setDeclining ] = useState ( false ) ;
109115 const [ error , setError ] = useState < string | null > ( null ) ;
110116 const answer = useAtomCommand ( peerLinkEnvironment . answerRequest , {
@@ -161,7 +167,13 @@ function PendingLinkRequest(props: {
161167 < Button
162168 label = { picksMachine ( item ) ? "Choose machine…" : "Review link…" }
163169 disabled = { declining }
164- onPress = { ( ) => setOpen ( true ) }
170+ onPress = { ( ) =>
171+ navigation . navigate ( "ThreadLinkRequest" , {
172+ environmentId : props . environmentId ,
173+ threadId : item . threadId ,
174+ itemId : item . id ,
175+ } )
176+ }
165177 tone = "primary"
166178 fullWidth
167179 />
@@ -183,13 +195,33 @@ function PendingLinkRequest(props: {
183195 Only a session that manages access here can answer this.
184196 </ Text >
185197 ) }
186- { open ? (
187- < LinkRequestSheet
188- environmentId = { props . environmentId }
189- item = { item }
190- onClose = { ( ) => setOpen ( false ) }
191- />
192- ) : null }
198+ </ View >
199+ ) ;
200+ }
201+
202+ export function LinkRequestRouteScreen ( {
203+ route,
204+ } : StaticScreenProps < {
205+ readonly environmentId : EnvironmentId ;
206+ readonly threadId : ThreadId ;
207+ readonly itemId : TurnItemId ;
208+ } > ) {
209+ const navigation = useNavigation ( ) ;
210+ const thread = useEnvironmentThread ( route . params . environmentId , route . params . threadId ) ;
211+ const item = Option . getOrNull ( thread . data ) ?. turnItems . find (
212+ ( item ) => item . id === route . params . itemId ,
213+ ) ;
214+ const close = ( ) => navigation . goBack ( ) ;
215+ return item ?. type === "link_request" && item . linkStatus === "pending" ? (
216+ < LinkRequestSheet environmentId = { route . params . environmentId } item = { item } onClose = { close } />
217+ ) : (
218+ < View className = "flex-1 bg-sheet" >
219+ < SheetHeader title = "Link a machine" onClose = { close } />
220+ < ScreenContent >
221+ < Text className = "p-5 text-sm text-foreground-muted" >
222+ This link request is no longer pending.
223+ </ Text >
224+ </ ScreenContent >
193225 </ View >
194226 ) ;
195227}
@@ -244,6 +276,7 @@ function LinkRequestSheet(props: {
244276 const [ mintFailed , setMintFailed ] = useState < ReadonlySet < EnvironmentId > > ( new Set ( ) ) ;
245277 // Submit then a tap can both run before a re-render; this guard is synchronous.
246278 const inFlight = useRef ( false ) ;
279+ useHardwareBack ( busy , ( ) => true ) ;
247280 const answer = useAtomCommand ( peerLinkEnvironment . answerRequest , {
248281 label : "answer link request" ,
249282 // The failure cause holds the request, and with it the code; keep it out of the console.
@@ -316,19 +349,16 @@ function LinkRequestSheet(props: {
316349 }
317350 } ;
318351
319- return (
320- < Sheet
321- title = { pickMachine ? "Link a machine" : `Link ${ targetName } ` }
322- onClose = { props . onClose }
323- // An answer that lands after a close would still link, and would close a reopened sheet.
324- dismissDisabled = { busy }
325- >
352+ const content = (
353+ < ScreenContent >
326354 < ScrollView
355+ nestedScrollEnabled
327356 className = "flex-1 bg-sheet-solid"
328357 contentInsetAdjustmentBehavior = "automatic"
329358 contentContainerClassName = "gap-5 p-5"
330359 contentContainerStyle = { { paddingBottom : insets . bottom + 20 } }
331360 keyboardShouldPersistTaps = "handled"
361+ automaticallyAdjustKeyboardInsets
332362 >
333363 < View className = "gap-1" >
334364 < Text className = "font-sans text-sm leading-5 text-foreground-muted" >
@@ -445,16 +475,26 @@ function LinkRequestSheet(props: {
445475 { error }
446476 </ Text >
447477 ) : null }
448- < Button
449- label = { busy ? "Linking…" : useExisting ? "Use this link" : "Link" }
450- disabled = { busy || ! ready }
451- onPress = { ( ) => void submit ( ) }
452- tone = "primary"
453- fullWidth
454- />
455- < Button label = "Cancel" tone = "secondary" disabled = { busy } onPress = { props . onClose } fullWidth />
456478 </ ScrollView >
457- </ Sheet >
479+ </ ScreenContent >
480+ ) ;
481+
482+ return (
483+ < View collapsable = { false } className = "flex-1 bg-sheet android:flex-col-reverse" >
484+ { content }
485+ < SheetHeader
486+ title = { pickMachine ? "Link a machine" : `Link ${ targetName } ` }
487+ onCancel = { props . onClose }
488+ // An answer that lands after a close would still link, and would close a reopened sheet.
489+ dismissDisabled = { busy }
490+ confirm = { {
491+ label : busy ? "Linking…" : useExisting ? "Use this link" : "Link" ,
492+ disabled : busy || ! ready ,
493+ loading : busy ,
494+ onPress : ( ) => void submit ( ) ,
495+ } }
496+ />
497+ </ View >
458498 ) ;
459499}
460500
0 commit comments