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