Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

use-thread-pr.ts

119 lines4.2 KB

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;
28​
29// Bound the cache across row virtualization instead of retaining live atoms for every past thread or PR.
30const threadPrSnapshotsAtom = Atom.make<ReadonlyMap<string, ThreadPrSnapshot>>(new Map()).pipe(
31 Atom.keepAlive,
32 Atom.withLabel("mobile:thread-pr-snapshots"),
33);
34​
35export {
36 presentThreadPr,
37 type ThreadPr,
38 type ThreadPrPresentation,
39} from "./thread-pr-presentation";
40​
41export function useThreadPr(thread: EnvironmentThreadShell): ThreadPrPresentation | null {
42 const supportsLinks = useAtomValue(
43 serverEnvironment.configValueAtom(thread.environmentId),
44 (config) => config?.environment.capabilities.threadPullRequests === true,
45 );
46 const { linkedPresentation, pullRequestRef } = useMemo(
47 () =>
48 resolveThreadPrSource(
49 {
50 pullRequests: thread.pullRequests,
51 linkedPullRequest: thread.linkedPullRequest,
52 branchPullRequest: thread.branchPullRequest,
53 },
54 { threadPullRequests: supportsLinks },
55 ),
56 [thread.pullRequests, thread.linkedPullRequest, thread.branchPullRequest, supportsLinks],
57 );
58 const threadKey = scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id));
59 const snapshotIdentity = JSON.stringify(pullRequestRef);
60 const snapshotEntry = useAtomValue(
61 threadPrSnapshotsAtom,
62 useCallback(
63 (current: ReadonlyMap<string, ThreadPrSnapshot>) => current.get(threadKey),
64 [threadKey],
65 ),
66 );
67 const snapshot = snapshotEntry?.identity === snapshotIdentity ? snapshotEntry.presentation : null;
68 const pullRequestSummary = useEnvironmentQuery(
69 pullRequestRef === null
70 ? null
71 : pullRequestSummaryAtom({
72 environmentId: thread.environmentId,
73 input: {
74 projectId: pullRequestRef.projectId,
75 repository: pullRequestRef.repository,
76 number: pullRequestRef.number,
77 },
78 }),
79 );
80​
81 const live = useMemo<ThreadPrPresentation | null | undefined>(() => {
82 if (pullRequestRef === null) return null;
83 const summary = pullRequestSummary.data;
84 return summary === null
85 ? undefined
86 : presentThreadPr(pullRequestDetailToVcsStatus(summary), {
87 kind: summary.provider,
88 name: summary.provider,
89 baseUrl: "",
90 });
91 }, [pullRequestRef, pullRequestSummary.data]);
92​
93 useEffect(() => {
94 if (live === undefined) return;
95 appAtomRegistry.modify(threadPrSnapshotsAtom, (current) => {
96 const existing = current.get(threadKey);
97 if (live === null) {
98 if (existing === undefined) return [false, current];
99 const next = new Map(current);
100 next.delete(threadKey);
101 return [true, next];
102 }
103 if (existing?.identity === snapshotIdentity && existing.presentation === live) {
104 return [false, current];
105 }
106 const next = new Map(current);
107 next.delete(threadKey);
108 next.set(threadKey, { identity: snapshotIdentity, presentation: live });
109 while (next.size > MAX_THREAD_PR_SNAPSHOTS) {
110 const oldestKey = next.keys().next().value;
111 if (oldestKey === undefined) break;
112 next.delete(oldestKey);
113 }
114 return [true, next];
115 });
116 }, [live, snapshotIdentity, threadKey]);
117​
118 return linkedPresentation ?? (live === undefined ? snapshot : live);