|
| 1 | +import { useAtomValue } from "@effect/atom-react"; |
| 2 | +import { useNavigation } from "@react-navigation/native"; |
| 3 | +import { threadHandoffNotice } from "@t3tools/client-runtime/state/threads"; |
| 4 | +import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/models"; |
| 5 | +import { |
| 6 | + isAtomCommandInterrupted, |
| 7 | + squashAtomCommandFailure, |
| 8 | +} from "@t3tools/client-runtime/state/runtime"; |
| 9 | +import { useEffect, useRef, useState } from "react"; |
| 10 | +import { View } from "react-native"; |
| 11 | +import { AppText as Text } from "../../components/ui/AppText"; |
| 12 | +import { Button } from "../../components/ui/Button"; |
| 13 | +import * as Dialog from "../../components/ui/Dialog"; |
| 14 | +import { appAtomRegistry } from "../../state/atom-registry"; |
| 15 | +import { environmentPresentations } from "../../state/presentation"; |
| 16 | +import { environmentThreadShells } from "../../state/threads"; |
| 17 | +import { threadHandoffEnvironment } from "../../state/threadHandoff"; |
| 18 | + |
| 19 | +export function ThreadHandoffCard({ thread }: { thread: EnvironmentThreadShell }) { |
| 20 | + return thread.handoff ? ( |
| 21 | + <Notice |
| 22 | + key={`${thread.environmentId}:${thread.id}:${thread.handoff.handoffId}`} |
| 23 | + thread={thread} |
| 24 | + /> |
| 25 | + ) : null; |
| 26 | +} |
| 27 | +function Notice({ thread }: { thread: EnvironmentThreadShell }) { |
| 28 | + const handoff = thread.handoff!; |
| 29 | + const ref = { environmentId: thread.environmentId, threadId: thread.id }; |
| 30 | + const notice = threadHandoffNotice(handoff)!; |
| 31 | + const canCancel = useAtomValue( |
| 32 | + threadHandoffEnvironment.cancel.permissionAtom(thread.environmentId), |
| 33 | + ); |
| 34 | + const canRetry = useAtomValue( |
| 35 | + threadHandoffEnvironment.start.permissionAtom(thread.environmentId), |
| 36 | + ); |
| 37 | + const presentations = useAtomValue(environmentPresentations.presentationsAtom); |
| 38 | + const sourceConnected = presentations.get(thread.environmentId)?.connection.phase === "connected"; |
| 39 | + const navigation = useNavigation(); |
| 40 | + const [busy, setBusy] = useState(false); |
| 41 | + const [error, setError] = useState<string | null>(null); |
| 42 | + const working = useRef(false); |
| 43 | + const alive = useRef(true); |
| 44 | + useEffect(() => { |
| 45 | + alive.current = true; |
| 46 | + return () => { |
| 47 | + alive.current = false; |
| 48 | + }; |
| 49 | + }, []); |
| 50 | + const current = () => { |
| 51 | + const live = appAtomRegistry.get(environmentThreadShells.threadShellAtom(ref))?.handoff; |
| 52 | + return alive.current && live?.handoffId === handoff.handoffId && live.state === handoff.state; |
| 53 | + }; |
| 54 | + const act = async (action: "cancel" | "retry" | "abandon") => { |
| 55 | + if (!current() || working.current) return; |
| 56 | + const command = |
| 57 | + action === "retry" ? threadHandoffEnvironment.start : threadHandoffEnvironment.cancel; |
| 58 | + if (!appAtomRegistry.get(command.permissionAtom(thread.environmentId))) return; |
| 59 | + working.current = true; |
| 60 | + setBusy(true); |
| 61 | + setError(null); |
| 62 | + try { |
| 63 | + const result = |
| 64 | + action === "retry" |
| 65 | + ? await threadHandoffEnvironment.start.run(appAtomRegistry, { |
| 66 | + environmentId: thread.environmentId, |
| 67 | + input: { |
| 68 | + threadId: thread.id, |
| 69 | + environmentId: handoff.environmentId, |
| 70 | + projectId: handoff.projectId, |
| 71 | + handoffId: handoff.handoffId, |
| 72 | + isCurrent: current, |
| 73 | + }, |
| 74 | + }) |
| 75 | + : await threadHandoffEnvironment.cancel.run(appAtomRegistry, { |
| 76 | + environmentId: thread.environmentId, |
| 77 | + input: { |
| 78 | + threadId: thread.id, |
| 79 | + handoffId: handoff.handoffId, |
| 80 | + isCurrent: current, |
| 81 | + ...(action === "abandon" ? { abandon: true } : {}), |
| 82 | + }, |
| 83 | + }); |
| 84 | + if (current() && result._tag === "Failure" && !isAtomCommandInterrupted(result)) { |
| 85 | + const failure = squashAtomCommandFailure(result); |
| 86 | + setError( |
| 87 | + failure instanceof Error ? failure.message : "Could not update the move. Try again.", |
| 88 | + ); |
| 89 | + } |
| 90 | + } finally { |
| 91 | + if (alive.current) { |
| 92 | + working.current = false; |
| 93 | + setBusy(false); |
| 94 | + } |
| 95 | + } |
| 96 | + }; |
| 97 | + const abandon = () => |
| 98 | + Dialog.confirm({ |
| 99 | + title: "Continue here?", |
| 100 | + message: |
| 101 | + "The move outcome is unknown. The destination may already have a copy or finish accepting it later. Continue working here anyway?", |
| 102 | + confirmText: "Continue here", |
| 103 | + destructive: true, |
| 104 | + onConfirm: () => { |
| 105 | + if (current()) void act("abandon"); |
| 106 | + }, |
| 107 | + }); |
| 108 | + return ( |
| 109 | + <View className="gap-3 rounded-2xl border border-border bg-card p-4"> |
| 110 | + <Text className="font-t3-medium">{notice.title}</Text> |
| 111 | + <Text className="text-sm text-foreground-muted">{notice.detail}</Text> |
| 112 | + {handoff.state === "pending" ? ( |
| 113 | + <Button |
| 114 | + label="Cancel move" |
| 115 | + disabled={!canCancel || !sourceConnected || busy} |
| 116 | + onPress={() => void act("cancel")} |
| 117 | + /> |
| 118 | + ) : null} |
| 119 | + {handoff.state === "departing" ? ( |
| 120 | + <> |
| 121 | + <Button |
| 122 | + label="Retry move" |
| 123 | + disabled={!canRetry || !sourceConnected || busy} |
| 124 | + onPress={() => void act("retry")} |
| 125 | + /> |
| 126 | + <Button |
| 127 | + label="Continue here…" |
| 128 | + tone="text" |
| 129 | + disabled={!canCancel || !sourceConnected || busy} |
| 130 | + onPress={abandon} |
| 131 | + /> |
| 132 | + </> |
| 133 | + ) : null} |
| 134 | + {handoff.state === "departed" && |
| 135 | + presentations.get(handoff.environmentId)?.connection.phase === "connected" ? ( |
| 136 | + <Button |
| 137 | + label={`Open on ${handoff.label}`} |
| 138 | + onPress={() => { |
| 139 | + if ( |
| 140 | + !current() || |
| 141 | + appAtomRegistry.get(environmentPresentations.presentationAtom(handoff.environmentId)) |
| 142 | + ?.connection.phase !== "connected" |
| 143 | + ) |
| 144 | + return; |
| 145 | + navigation.navigate("Thread", { |
| 146 | + environmentId: handoff.environmentId, |
| 147 | + threadId: handoff.threadId, |
| 148 | + }); |
| 149 | + }} |
| 150 | + /> |
| 151 | + ) : null} |
| 152 | + {error ? ( |
| 153 | + <Text accessibilityRole="alert" className="text-sm text-danger"> |
| 154 | + {error} |
| 155 | + </Text> |
| 156 | + ) : null} |
| 157 | + </View> |
| 158 | + ); |
| 159 | +} |
0 commit comments