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

use-thread-pr.ts

108 lines3.7 KB

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