Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
3import {
4 createLinkedPullRequestSummaryAtomFamily,
5 pullRequestDetailToVcsStatus,
6} from "@t3tools/client-runtime/state/pull-requests";
7import type { EnvironmentId, PullRequestSummary } from "@t3tools/contracts";
8import * as Effect from "effect/Effect";
9import { Atom } from "effect/reactivity";
10import { useMessages } from "gt-react-native";
11import { use, useMemo } from "react";
12
13import { connectionAtomRuntime } from "../connection/runtime";
14import { appAtomRegistry } from "./atom-registry";
15import {
16 observePullRequestSummary,
17 useReconciledThreadPullRequests,
18} from "./pull-request-observations";
19import { serverEnvironment } from "./server";
20import { environmentThreadShells } from "./threads";
21import { useEnvironmentQuery } from "./query";
22import { ThreadPrDemandContext } from "./thread-pr-demand";
23import {
24 knownPullRequestSummary,
25 rememberBounded,
26 resolveThreadPrSource,
27 presentThreadPr,
28 type ThreadPrPresentation,
29} from "./thread-pr-presentation";
30
31const MAX_PULL_REQUEST_SNAPSHOTS = 500;
32
33// One bounded cache survives row virtualization (the summary query is dropped once idle) without
34// retaining one live atom for every pull request ever seen.
35const pullRequestSnapshotsAtom = Atom.make<ReadonlyMap<string, PullRequestSummary>>(new Map()).pipe(
36 Atom.keepAlive,
37 Atom.withLabel("mobile:thread-pr-snapshots"),
38);
39
40function snapshotKey(
41 environmentId: EnvironmentId,
42 reference: { readonly projectId: string; readonly repository: string; readonly number: number },
43): string {
44 return JSON.stringify([
45 environmentId,
46 reference.projectId,
47 reference.repository,
48 reference.number,
49 ]);
50}
51
52const pullRequestSummaryAtom = createLinkedPullRequestSummaryAtomFamily(
53 connectionAtomRuntime,
54 undefined,
55 ({ environmentId, input }, summary, registry) =>
56 Effect.sync(() => {
57 observePullRequestSummary(registry, environmentId, summary);
58 registry.update(pullRequestSnapshotsAtom, (current) =>
59 rememberBounded(
60 current,
61 snapshotKey(environmentId, input),
62 summary,
63 MAX_PULL_REQUEST_SNAPSHOTS,
64 ),
65 );
66 }),
67);
68
69export { type ThreadPr, type ThreadPrPresentation } from "./thread-pr-presentation";
70
71/** Seeds a pull request page with a summary already in memory, never another pull request's. */
72export function withKnownPullRequestSummary<
73 Params extends { readonly host?: string; readonly repository: string; readonly number: number },
74>(params: Params) {
75 const initialSummary =
76 params.host === undefined
77 ? undefined
78 : knownPullRequestSummary(
79 { host: params.host, repository: params.repository, number: params.number },
80 appAtomRegistry.get(environmentThreadShells.threadShellsAtom),
81 appAtomRegistry.get(pullRequestSnapshotsAtom).values(),
82 );
83 return initialSummary === undefined ? params : { ...params, initialSummary };
84}
85
86export function useThreadPr(thread: EnvironmentThreadShell): ThreadPrPresentation | null {
87 const demand = use(ThreadPrDemandContext);
88 const resolve = useMessages();
89 const supportsLinks = useAtomValue(
90 serverEnvironment.configValueAtom(thread.environmentId),
91 (config) => config !== null,
92 );
93 const pullRequests = useReconciledThreadPullRequests(thread.environmentId, thread.pullRequests);
94 const { linkedPresentation, pullRequestRef } = useMemo(
95 () =>
96 resolveThreadPrSource(
97 {
98 pullRequests,
99 linkedPullRequest: thread.linkedPullRequest,
100 branchPullRequest: thread.branchPullRequest,
101 },
102 supportsLinks,
103 resolve,
104 ),
105 [pullRequests, thread.linkedPullRequest, thread.branchPullRequest, supportsLinks, resolve],
106 );
107 const key = pullRequestRef === null ? null : snapshotKey(thread.environmentId, pullRequestRef);
108 // Select this row's entry so answers for other pull requests do not re-render it.
109 const snapshot = useAtomValue(pullRequestSnapshotsAtom, (current) =>
110 key === null ? undefined : current.get(key),
111 );
112 const pullRequestSummary = useEnvironmentQuery(
113 pullRequestRef === null || !demand
114 ? null
115 : pullRequestSummaryAtom({
116 environmentId: thread.environmentId,
117 input: {
118 projectId: pullRequestRef.projectId,
119 repository: pullRequestRef.repository,
120 number: pullRequestRef.number,
121 },
122 }),
123 );
124 const summary = pullRequestRef === null ? undefined : (pullRequestSummary.data ?? snapshot);
125 // Rows pass `pr` into callbacks; a fresh object per render would invalidate them.
126 const summaryPresentation =
127 summary === undefined
128 ? null
129 : presentThreadPr(
130 pullRequestDetailToVcsStatus(summary),
131 {
132 kind: summary.provider,
133 name: summary.provider,
134 baseUrl: "",
135 },
136 resolve,
137 );
138 return linkedPresentation ?? summaryPresentation;
139}