Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment";
3import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
4import {
5 createLinkedPullRequestDetailAtomFamily,
6 pullRequestDetailToVcsStatus,
7} from "@t3tools/client-runtime/state/pull-requests";
8import { Atom } from "effect/unstable/reactivity";
9import { useCallback, useEffect, useMemo } from "react";
10
11import { connectionAtomRuntime } from "../connection/runtime";
12import { appAtomRegistry } from "./atom-registry";
13import { useEnvironmentQuery } from "./query";
14import { presentThreadPr, type ThreadPrPresentation } from "./thread-pr-presentation";
15import { vcsEnvironment } from "./vcs";
16
17const linkedPullRequestDetailAtom = createLinkedPullRequestDetailAtomFamily(connectionAtomRuntime);
18const MAX_THREAD_PR_SNAPSHOTS = 500;
19
20interface ThreadPrSnapshot {
21 readonly identity: string;
22 readonly presentation: ThreadPrPresentation;
23}
24
25// One bounded cache survives row virtualization without retaining one live
26// atom for every thread, branch, directory, or linked pull request ever seen.
27const threadPrSnapshotsAtom = Atom.make<ReadonlyMap<string, ThreadPrSnapshot>>(new Map()).pipe(
28 Atom.keepAlive,
29 Atom.withLabel("mobile:thread-pr-snapshots"),
30);
31
32export {
33 presentThreadPr,
34 type ThreadPr,
35 type ThreadPrPresentation,
36} from "./thread-pr-presentation";
37
38/**
39 * Live PR status for a thread's branch. Subscriptions are deduplicated per
40 * (environmentId, cwd) by the atom family, so many rows on the same worktree
41 * or project root share one stream — and virtualization means only visible
42 * rows subscribe at all.
43 */
44export function useThreadPr(
45 thread: EnvironmentThreadShell,
46 projectCwd: string | null,
47): ThreadPrPresentation | null {
48 const cwd = thread.worktreePath ?? projectCwd;
49 const threadKey = scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id));
50 const snapshotIdentity = JSON.stringify(
51 thread.linkedPullRequest ?? { branch: thread.branch, cwd },
52 );
53 // Select this row's entry so writes for other rows do not re-render it.
54 const snapshotEntry = useAtomValue(
55 threadPrSnapshotsAtom,
56 useCallback(
57 (current: ReadonlyMap<string, ThreadPrSnapshot>) => current.get(threadKey),
58 [threadKey],
59 ),
60 );
61 const snapshot = snapshotEntry?.identity === snapshotIdentity ? snapshotEntry.presentation : null;
62 const gitStatus = useEnvironmentQuery(
63 thread.linkedPullRequest == null && thread.branch !== null && cwd !== null
64 ? vcsEnvironment.status({
65 environmentId: thread.environmentId,
66 input: { cwd },
67 })
68 : null,
69 );
70 const linkedPullRequest = useEnvironmentQuery(
71 thread.linkedPullRequest == null
72 ? null
73 : linkedPullRequestDetailAtom({
74 environmentId: thread.environmentId,
75 input: {
76 projectId: thread.linkedPullRequest.projectId,
77 repository: thread.linkedPullRequest.repository,
78 number: thread.linkedPullRequest.number,
79 },
80 }),
81 );
82
83 const live = useMemo<ThreadPrPresentation | null | undefined>(() => {
84 if (thread.linkedPullRequest != null) {
85 const detail = linkedPullRequest.data;
86 return detail === null
87 ? undefined
88 : presentThreadPr(pullRequestDetailToVcsStatus(detail), {
89 kind: detail.provider,
90 name: detail.provider,
91 baseUrl: "",
92 });
93 }
94
95 const status = gitStatus.data;
96 if (thread.branch === null) return null;
97 if (status === null) return undefined;
98 if (status.refName !== thread.branch || !status.pr) return null;
99 return presentThreadPr(status.pr, status.sourceControlProvider);
100 }, [gitStatus.data, linkedPullRequest.data, thread.branch, thread.linkedPullRequest]);
101
102 useEffect(() => {
103 if (live === undefined) return;
104 appAtomRegistry.modify(threadPrSnapshotsAtom, (current) => {
105 const existing = current.get(threadKey);
106 if (live === null) {
107 if (existing === undefined) return [false, current];
108 const next = new Map(current);
109 next.delete(threadKey);
110 return [true, next];
111 }
112 if (existing?.identity === snapshotIdentity && existing.presentation === live) {
113 return [false, current];
114 }
115 const next = new Map(current);
116 next.delete(threadKey);
117 next.set(threadKey, { identity: snapshotIdentity, presentation: live });
118 while (next.size > MAX_THREAD_PR_SNAPSHOTS) {
119 const oldestKey = next.keys().next().value;
120 if (oldestKey === undefined) break;
121 next.delete(oldestKey);
122 }
123 return [true, next];
124 });
125 }, [live, snapshotIdentity, threadKey]);
126
127 return live === undefined ? snapshot : live;
128}