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 {
60 presentThreadPr,
61 type ThreadPr,
62 type ThreadPrPresentation,
63} from "./thread-pr-presentation";
64
65export function useThreadPr(thread: EnvironmentThreadShell): ThreadPrPresentation | null {
66 const supportsLinks = useAtomValue(
67 serverEnvironment.configValueAtom(thread.environmentId),
68 (config) => config?.environment.capabilities.threadPullRequests === true,
69 );
70 const { linkedPresentation, pullRequestRef } = useMemo(
71 () =>
72 resolveThreadPrSource(
73 {
74 pullRequests: thread.pullRequests,
75 linkedPullRequest: thread.linkedPullRequest,
76 branchPullRequest: thread.branchPullRequest,
77 },
78 { threadPullRequests: supportsLinks },
79 ),
80 [thread.pullRequests, thread.linkedPullRequest, thread.branchPullRequest, supportsLinks],
81 );
82 const key = pullRequestRef === null ? null : snapshotKey(thread.environmentId, pullRequestRef);
83 // Select this row's entry so answers for other pull requests do not re-render it.
84 const snapshot = useAtomValue(pullRequestSnapshotsAtom, (current) =>
85 key === null ? undefined : current.get(key),
86 );
87 const pullRequestSummary = useEnvironmentQuery(
88 pullRequestRef === null
89 ? null
90 : pullRequestSummaryAtom({
91 environmentId: thread.environmentId,
92 input: {
93 projectId: pullRequestRef.projectId,
94 repository: pullRequestRef.repository,
95 number: pullRequestRef.number,
96 },
97 }),
98 );
99 const summary = pullRequestRef === null ? undefined : (pullRequestSummary.data ?? snapshot);
100 // Rows pass `pr` into callbacks; a fresh object per render would invalidate them.
101 const summaryPresentation = useMemo(
102 () =>
103 summary === undefined
104 ? null
105 : presentThreadPr(pullRequestDetailToVcsStatus(summary), {
106 kind: summary.provider,
107 name: summary.provider,
108 baseUrl: "",
109 }),
110 [summary],
111 );
112 return linkedPresentation ?? summaryPresentation;
113}