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: VcsStatusResult | 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 useEffect(() => {
54 if (serverThreadRef === null) return;
55 const threadKey = scopedThreadKey(serverThreadRef);
56 if (replacement === null) {
57 relinkKeysRef.current.delete(threadKey);
58 return;
59 }
60 const relinkKey = `${replacement.projectId}:${replacement.repository}#${replacement.number}`;
61 if (relinkKeysRef.current.get(threadKey) === relinkKey) return;
62 relinkKeysRef.current.set(threadKey, relinkKey);
63 const openSurface = selectActiveRightPanelSurface(
64 useRightPanelStore.getState().byThreadKey,
65 serverThreadRef,
66 );
67 if (
68 openSurface?.kind === "pull-request" &&
69 persisted !== null &&
70 openSurface.projectId === persisted.projectId &&
71 openSurface.repository.toLowerCase() === persisted.repository.toLowerCase() &&
72 openSurface.number === persisted.number
73 ) {
74 useRightPanelStore.getState().openPullRequest(serverThreadRef, replacement);
75 }
76
77 relinkWriteRef.current = relinkWriteRef.current.then(async () => {
78 if (relinkKeysRef.current.get(threadKey) !== relinkKey) return;
79 const result = await updateThreadMetadata({
80 environmentId: serverThreadRef.environmentId,
81 input: { threadId: serverThreadRef.threadId, linkedPullRequest: replacement },
82 });
83 if (relinkKeysRef.current.get(threadKey) !== relinkKey) return;
84 if (result._tag !== "Failure") return;
85 relinkKeysRef.current.delete(threadKey);
86 if (isAtomCommandInterrupted(result)) return;
87 const error = squashAtomCommandFailure(result);
88 toastManager.add(
89 stackedThreadToast({
90 type: "error",
91 title: "Unable to update the thread pull request",
92 description: error instanceof Error ? error.message : "An error occurred.",
93 }),
94 );
95 });
96 }, [persisted, replacement, serverThreadRef, updateThreadMetadata]);
97
98 const linked = replacement ?? persisted;
99 return {
100 linked,
101 linkedKey: linked ? JSON.stringify([linked.projectId, linked.repository, linked.number]) : null,
102 hasDetail: persisted !== null,
103 };
104}