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 { useMemo } from "react";
11
12import { connectionAtomRuntime } from "../connection/runtime";
13import { serverEnvironment } from "./server";
14import { useEnvironmentQuery } from "./query";
15import {
16 rememberBounded,
17 resolveThreadPrSource,
18 presentThreadPr,
19 type ThreadPrPresentation,
20} from "./thread-pr-presentation";
21
22const MAX_PULL_REQUEST_SNAPSHOTS = 500;
23
24// One bounded cache survives row virtualization (the summary query is dropped once idle) without
25// retaining one live atom for every pull request ever seen.
26const pullRequestSnapshotsAtom = Atom.make<ReadonlyMap<string, PullRequestSummary>>(new Map()).pipe(
27 Atom.keepAlive,
28 Atom.withLabel("mobile:thread-pr-snapshots"),
29);
30
31function snapshotKey(
32 environmentId: EnvironmentId,
33 reference: { readonly projectId: string; readonly repository: string; readonly number: number },
34): string {
35 return JSON.stringify([
36 environmentId,
37 reference.projectId,
38 reference.repository,
39 reference.number,
40 ]);
41}
42
43const pullRequestSummaryAtom = createLinkedPullRequestSummaryAtomFamily(
44 connectionAtomRuntime,
45 undefined,
46 ({ environmentId, input }, summary, registry) =>
47 Effect.sync(() =>
48 registry.update(pullRequestSnapshotsAtom, (current) =>
49 rememberBounded(
50 current,
51 snapshotKey(environmentId, input),
52 summary,
53 MAX_PULL_REQUEST_SNAPSHOTS,
54 ),
55 ),
56 ),
57);
58
59export { type ThreadPr, type ThreadPrPresentation } from "./thread-pr-presentation";
60
61export function useThreadPr(thread: EnvironmentThreadShell): ThreadPrPresentation | null {
62 const supportsLinks = useAtomValue(
63 serverEnvironment.configValueAtom(thread.environmentId),
64 (config) => config !== null,
65 );
66 const { linkedPresentation, pullRequestRef } = useMemo(
67 () =>
68 resolveThreadPrSource(
69 {
70 pullRequests: thread.pullRequests,
71 linkedPullRequest: thread.linkedPullRequest,
72 branchPullRequest: thread.branchPullRequest,
73 },
74 supportsLinks,
75 ),
76 [thread.pullRequests, thread.linkedPullRequest, thread.branchPullRequest, supportsLinks],
77 );
78 const key = pullRequestRef === null ? null : snapshotKey(thread.environmentId, pullRequestRef);
79 // Select this row's entry so answers for other pull requests do not re-render it.
80 const snapshot = useAtomValue(pullRequestSnapshotsAtom, (current) =>
81 key === null ? undefined : current.get(key),
82 );
83 const pullRequestSummary = useEnvironmentQuery(
84 pullRequestRef === null
85 ? null
86 : pullRequestSummaryAtom({
87 environmentId: thread.environmentId,
88 input: {
89 projectId: pullRequestRef.projectId,
90 repository: pullRequestRef.repository,
91 number: pullRequestRef.number,
92 },
93 }),
94 );
95 const summary = pullRequestRef === null ? undefined : (pullRequestSummary.data ?? snapshot);
96 // Rows pass `pr` into callbacks; a fresh object per render would invalidate them.
97 const summaryPresentation =
98 summary === undefined
99 ? null
100 : presentThreadPr(pullRequestDetailToVcsStatus(summary), {
101 kind: summary.provider,
102 name: summary.provider,
103 baseUrl: "",
104 });
105 return linkedPresentation ?? summaryPresentation;
106}