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 createLinkedPullRequestSummaryAtomFamily,
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";
15
16const pullRequestSummaryAtom = createLinkedPullRequestSummaryAtomFamily(connectionAtomRuntime);
17const MAX_THREAD_PR_SNAPSHOTS = 500;
18
19interface ThreadPrSnapshot {
20 readonly identity: string;
21 readonly presentation: ThreadPrPresentation;
22}
23
24// One bounded cache survives row virtualization without retaining one live
25// atom for every thread or pull request ever seen.
26const threadPrSnapshotsAtom = Atom.make<ReadonlyMap<string, ThreadPrSnapshot>>(new Map()).pipe(
27 Atom.keepAlive,
28 Atom.withLabel("mobile:thread-pr-snapshots"),
29);
30
31export {
32 presentThreadPr,
33 type ThreadPr,
34 type ThreadPrPresentation,
35} from "./thread-pr-presentation";
36
37/**
38 * Live status for a thread's server-provided PR. Visible rows share a summary
39 * request for the same PR in the same environment.
40 */
41export function useThreadPr(thread: EnvironmentThreadShell): ThreadPrPresentation | null {
42 const pullRequestRef = thread.linkedPullRequest ?? thread.branchPullRequest ?? null;
43 const threadKey = scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id));
44 const snapshotIdentity = JSON.stringify(pullRequestRef);
45 // Select this row's entry so writes for other rows do not re-render it.
46 const snapshotEntry = useAtomValue(
47 threadPrSnapshotsAtom,
48 useCallback(
49 (current: ReadonlyMap<string, ThreadPrSnapshot>) => current.get(threadKey),
50 [threadKey],
51 ),
52 );
53 const snapshot = snapshotEntry?.identity === snapshotIdentity ? snapshotEntry.presentation : null;
54 const pullRequestSummary = useEnvironmentQuery(
55 pullRequestRef === null
56 ? null
57 : pullRequestSummaryAtom({
58 environmentId: thread.environmentId,
59 input: {
60 projectId: pullRequestRef.projectId,
61 repository: pullRequestRef.repository,
62 number: pullRequestRef.number,
63 },
64 }),
65 );
66
67 const live = useMemo<ThreadPrPresentation | null | undefined>(() => {
68 if (pullRequestRef === null) return null;
69 const summary = pullRequestSummary.data;
70 return summary === null
71 ? undefined
72 : presentThreadPr(pullRequestDetailToVcsStatus(summary), {
73 kind: summary.provider,
74 name: summary.provider,
75 baseUrl: "",
76 });
77 }, [pullRequestRef, pullRequestSummary.data]);
78
79 useEffect(() => {
80 if (live === undefined) return;
81 appAtomRegistry.modify(threadPrSnapshotsAtom, (current) => {
82 const existing = current.get(threadKey);
83 if (live === null) {
84 if (existing === undefined) return [false, current];
85 const next = new Map(current);
86 next.delete(threadKey);
87 return [true, next];
88 }
89 if (existing?.identity === snapshotIdentity && existing.presentation === live) {
90 return [false, current];
91 }
92 const next = new Map(current);
93 next.delete(threadKey);
94 next.set(threadKey, { identity: snapshotIdentity, presentation: live });
95 while (next.size > MAX_THREAD_PR_SNAPSHOTS) {
96 const oldestKey = next.keys().next().value;
97 if (oldestKey === undefined) break;
98 next.delete(oldestKey);
99 }
100 return [true, next];
101 });
102 }, [live, snapshotIdentity, threadKey]);
103
104 return live === undefined ? snapshot : live;
105}