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/**
21 * The change request a thread is linked to. When the linked one is merged or
22 * closed and the thread's branch has a newer open one, that one replaces it:
23 * the thread is relinked on the server, and an open panel showing the old one
24 * follows.
25 */
26export function useThreadLinkedPullRequest(input: {
27 // Null for drafts, which have nothing to relink on the server.
28 readonly serverThreadRef: ScopedThreadRef | null;
29 readonly environmentId: EnvironmentId | null;
30 readonly persisted: ThreadLinkedPullRequest | null;
31 readonly projectId: ProjectId | null;
32 readonly projectRepository: string | null;
33 readonly threadBranch: string | null;
34 readonly gitStatus: VcsStatusResult | null;
35}) {
36 const { serverThreadRef, persisted, projectId, projectRepository, threadBranch, gitStatus } =
37 input;
38 const updateThreadMetadata = useOrchestrationCommand(threadEnvironment.updateMetadata, {
39 reportFailure: false,
40 });
41 const persistedStatus = useLinkedThreadPullRequest(input.environmentId, persisted);
42 const detected = gitStatus?.pr;
43 const replacement: ThreadLinkedPullRequest | null =
44 persisted === null ||
45 (persistedStatus?.pr.state !== "merged" && persistedStatus?.pr.state !== "closed") ||
46 gitStatus?.refName !== threadBranch ||
47 detected?.state !== "open" ||
48 detected.headRef !== threadBranch ||
49 projectId === null ||
50 projectRepository === null ||
51 (persisted.projectId === projectId &&
52 persisted.repository.toLowerCase() === projectRepository.toLowerCase() &&
53 persisted.number === detected.number)
54 ? null
55 : { projectId, repository: projectRepository, number: detected.number, url: detected.url };
56
57 const relinkKeysRef = useRef(new Map<string, string>());
58 const relinkWriteRef = useRef(Promise.resolve());
59 useEffect(() => {
60 if (serverThreadRef === null) return;
61 const threadKey = scopedThreadKey(serverThreadRef);
62 if (replacement === null) {
63 relinkKeysRef.current.delete(threadKey);
64 return;
65 }
66 const relinkKey = `${replacement.projectId}:${replacement.repository}#${replacement.number}`;
67 if (relinkKeysRef.current.get(threadKey) === relinkKey) return;
68 relinkKeysRef.current.set(threadKey, relinkKey);
69 const openSurface = selectActiveRightPanelSurface(
70 useRightPanelStore.getState().byThreadKey,
71 serverThreadRef,
72 );
73 if (
74 openSurface?.kind === "pull-request" &&
75 persisted !== null &&
76 openSurface.projectId === persisted.projectId &&
77 openSurface.repository.toLowerCase() === persisted.repository.toLowerCase() &&
78 openSurface.number === persisted.number
79 ) {
80 useRightPanelStore.getState().openPullRequest(serverThreadRef, replacement);
81 }
82
83 relinkWriteRef.current = relinkWriteRef.current.then(async () => {
84 if (relinkKeysRef.current.get(threadKey) !== relinkKey) return;
85 const result = await updateThreadMetadata({
86 environmentId: serverThreadRef.environmentId,
87 input: { threadId: serverThreadRef.threadId, linkedPullRequest: replacement },
88 });
89 if (relinkKeysRef.current.get(threadKey) !== relinkKey) return;
90 if (result._tag !== "Failure") return;
91 relinkKeysRef.current.delete(threadKey);
92 if (isAtomCommandInterrupted(result)) return;
93 const error = squashAtomCommandFailure(result);
94 toastManager.add(
95 stackedThreadToast({
96 type: "error",
97 title: "Unable to update the thread pull request",
98 description: error instanceof Error ? error.message : "An error occurred.",
99 }),
100 );
101 });
102 }, [persisted, replacement, serverThreadRef, updateThreadMetadata]);
103
104 const linked = replacement ?? persisted;
105 return {
106 linked,
107 linkedKey: linked ? JSON.stringify([linked.projectId, linked.repository, linked.number]) : null,
108 // Detail is shown for the persisted link; a replacement only exists once it is merged.
109 hasDetail: persisted !== null,
110 };
111}