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