The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { thrownErrorMessage } from "@t3tools/client-runtime/errors"; |
| 2 | import { |
| 3 | type EnvironmentId, |
| 4 | type ProjectId, |
| 5 | type ScopedThreadRef, |
| 6 | type ThreadLinkedPullRequest, |
| 7 | type VcsStatusResult, |
| 8 | } from "@t3tools/contracts"; |
| 9 | import { scopedThreadKey } from "@t3tools/client-runtime/environment"; |
| 10 | import { |
| 11 | isAtomCommandInterrupted, |
| 12 | squashAtomCommandFailure, |
| 13 | } from "@t3tools/client-runtime/state/runtime"; |
| 14 | import { msg } from "gt-react"; |
| 15 | import { useEffect, useRef } from "react"; |
| 16 | import { m } from "~/i18n/messages"; |
| 17 | import { selectActiveRightPanelSurface, useRightPanelStore } from "../../rightPanelStore"; |
| 18 | import { threadEnvironment } from "../../state/threads"; |
| 19 | import { useOrchestrationCommand } from "../../state/use-orchestration-command"; |
| 20 | import { useLinkedThreadPullRequest } from "../ThreadStatusIndicators"; |
| 21 | import { stackedThreadToast, toastManager } from "../ui/toast"; |
| 22 | |
| 23 | /** Relink replaced closed/merged requests to the newer open request on the same branch, moving its panel. */ |
| 24 | export function useThreadLinkedPullRequest(input: { |
| 25 | readonly serverThreadRef: ScopedThreadRef | null; |
| 26 | readonly environmentId: EnvironmentId | null; |
| 27 | readonly persisted: ThreadLinkedPullRequest | null; |
| 28 | readonly projectId: ProjectId | null; |
| 29 | readonly projectRepository: string | null; |
| 30 | readonly threadBranch: string | null; |
| 31 | readonly gitStatus: Pick<VcsStatusResult, "refName" | "pr"> | null; |
| 32 | }) { |
| 33 | const { serverThreadRef, persisted, projectId, projectRepository, threadBranch, gitStatus } = |
| 34 | input; |
| 35 | const updateThreadMetadata = useOrchestrationCommand(threadEnvironment.updateMetadata, { |
| 36 | reportFailure: false, |
| 37 | }); |
| 38 | const persistedStatus = useLinkedThreadPullRequest(input.environmentId, persisted); |
| 39 | const detected = gitStatus?.pr; |
| 40 | const replacement: ThreadLinkedPullRequest | null = |
| 41 | persisted === null || |
| 42 | (persistedStatus?.pr.state !== "merged" && persistedStatus?.pr.state !== "closed") || |
| 43 | gitStatus?.refName !== threadBranch || |
| 44 | detected?.state !== "open" || |
| 45 | detected.headRef !== threadBranch || |
| 46 | projectId === null || |
| 47 | projectRepository === null || |
| 48 | (persisted.projectId === projectId && |
| 49 | persisted.repository.toLowerCase() === projectRepository.toLowerCase() && |
| 50 | persisted.number === detected.number) |
| 51 | ? null |
| 52 | : { projectId, repository: projectRepository, number: detected.number, url: detected.url }; |
| 53 | |
| 54 | const relinkKeysRef = useRef(new Map<string, string>()); |
| 55 | const relinkWriteRef = useRef(Promise.resolve()); |
| 56 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- persists the detected PR to the server thread once per change and retargets the open right panel store |
| 57 | useEffect(() => { |
| 58 | if (serverThreadRef === null) return; |
| 59 | const threadKey = scopedThreadKey(serverThreadRef); |
| 60 | if (replacement === null) { |
| 61 | relinkKeysRef.current.delete(threadKey); |
| 62 | return; |
| 63 | } |
| 64 | const relinkKey = `${replacement.projectId}:${replacement.repository}#${replacement.number}`; |
| 65 | if (relinkKeysRef.current.get(threadKey) === relinkKey) return; |
| 66 | relinkKeysRef.current.set(threadKey, relinkKey); |
| 67 | const openSurface = selectActiveRightPanelSurface( |
| 68 | useRightPanelStore.getState().byThreadKey, |
| 69 | serverThreadRef, |
| 70 | ); |
| 71 | if ( |
| 72 | openSurface?.kind === "pull-request" && |
| 73 | persisted !== null && |
| 74 | openSurface.projectId === persisted.projectId && |
| 75 | openSurface.repository.toLowerCase() === persisted.repository.toLowerCase() && |
| 76 | openSurface.number === persisted.number |
| 77 | ) { |
| 78 | useRightPanelStore.getState().openPullRequest(serverThreadRef, replacement); |
| 79 | } |
| 80 | |
| 81 | relinkWriteRef.current = relinkWriteRef.current.then(async () => { |
| 82 | if (relinkKeysRef.current.get(threadKey) !== relinkKey) return; |
| 83 | const result = await updateThreadMetadata({ |
| 84 | environmentId: serverThreadRef.environmentId, |
| 85 | input: { threadId: serverThreadRef.threadId, linkedPullRequest: replacement }, |
| 86 | }); |
| 87 | if (relinkKeysRef.current.get(threadKey) !== relinkKey) return; |
| 88 | if (result._tag !== "Failure") return; |
| 89 | relinkKeysRef.current.delete(threadKey); |
| 90 | if (isAtomCommandInterrupted(result)) return; |
| 91 | const error = squashAtomCommandFailure(result); |
| 92 | toastManager.add( |
| 93 | stackedThreadToast({ |
| 94 | type: "error", |
| 95 | title: m(msg("Unable to update the thread pull request")), |
| 96 | description: thrownErrorMessage(error, m) ?? m(msg("An error occurred.")), |
| 97 | }), |
| 98 | ); |
| 99 | }); |
| 100 | }, [persisted, replacement, serverThreadRef, updateThreadMetadata]); |
| 101 | |
| 102 | const linked = replacement ?? persisted; |
| 103 | return { |
| 104 | linked, |
| 105 | linkedKey: linked ? JSON.stringify([linked.projectId, linked.repository, linked.number]) : null, |
| 106 | hasDetail: persisted !== null, |
| 107 | }; |
| 108 | } |