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

pullRequests.ts

134 lines5.0 KB

1import { useAtomValue } from "@effect/atom-react";
2import {
3 createLinkedPullRequestDetailAtomFamily,
4 createPullRequestEnvironmentAtoms,
5} from "@t3tools/client-runtime/state/pull-requests";
6import type {
7 EnvironmentId,
8 PullRequestListInput,
9 PullRequestListStatsInput,
10} from "@t3tools/contracts";
11import * as Option from "effect/Option";
12import { AsyncResult, Atom } from "effect/unstable/reactivity";
13import { useCallback, useMemo } from "react";
14​
15import { connectionAtomRuntime } from "../connection/runtime";
16import { appAtomRegistry } from "../rpc/atomRegistry";
17import {
18 mergePullRequestLists,
19 type EnvironmentPullRequestStat,
20 type MergedPullRequestList,
21} from "../components/pullRequest/pullRequestList.logic";
22import { formatEnvironmentQueryError } from "./query";
23​
24export const pullRequestEnvironment = createPullRequestEnvironmentAtoms(connectionAtomRuntime);
25export const linkedPullRequestDetailAtom =
26 createLinkedPullRequestDetailAtomFamily(connectionAtomRuntime);
27​
28export interface EnvironmentQueryTarget<Input> {
29 readonly environmentId: EnvironmentId;
30 readonly input: Input;
32​
33interface MergedEnvironmentQueryView<A> {
34 /** One entry per environment that has answered, in the order the targets were given. */
35 readonly values: ReadonlyArray<readonly [EnvironmentId, A]>;
36 /** The first environment that failed. Others may still have answered — this is not fatal. */
37 readonly error: string | null;
38 readonly isPending: boolean;
40​
41/**
42 * The same per-environment query read across several environments at once. React cannot subscribe
43 * to a list of atoms whose length changes, so the fan-out happens inside one derived atom keyed by
44 * the targets — the same shape the cross-environment thread search uses.
45 *
46 * An environment that fails contributes nothing rather than blanking the page: the pull request
47 * list is a union, and one unreachable machine should not hide the others' rows.
48 */
49function createMergedEnvironmentQuery<Input, A>(
50 label: string,
51 atomFor: (
52 target: EnvironmentQueryTarget<Input>,
53 ) => Atom.Atom<AsyncResult.AsyncResult<A, unknown>>,
54) {
55 const family = Atom.family((key: string) =>
56 Atom.make((get): MergedEnvironmentQueryView<A> => {
57 const targets = JSON.parse(key) as ReadonlyArray<EnvironmentQueryTarget<Input>>;
58 const values: Array<readonly [EnvironmentId, A]> = [];
59 let error: string | null = null;
60 let isPending = false;
61 for (const target of targets) {
62 const result = get(atomFor(target));
63 isPending ||= result.waiting;
64 if (result._tag === "Failure" && error === null) {
65 error = formatEnvironmentQueryError(result.cause);
66 }
67 const value = Option.getOrNull(AsyncResult.value(result));
68 if (value !== null) values.push([target.environmentId, value]);
69 }
70 return { values, error, isPending };
71 }).pipe(Atom.withLabel(`${label}:${key}`)),
72 );
73 const empty = Atom.make<MergedEnvironmentQueryView<A>>({
74 values: [],
75 error: null,
76 isPending: false,
77 }).pipe(Atom.withLabel(`${label}:empty`));
78 return function useMergedQuery(targets: ReadonlyArray<EnvironmentQueryTarget<Input>>) {
79 const key = JSON.stringify(targets);
80 const view = useAtomValue(targets.length === 0 ? empty : family(key));
81 const refresh = useCallback(() => {
82 for (const target of JSON.parse(key) as ReadonlyArray<EnvironmentQueryTarget<Input>>) {
83 appAtomRegistry.refresh(atomFor(target));
84 }
85 }, [key]);
86 return { ...view, refresh };
87 };
89​
90const usePullRequestListsQuery = createMergedEnvironmentQuery(
91 "web-pull-requests:list",
92 pullRequestEnvironment.list,
93);
94​
95const usePullRequestStatsQuery = createMergedEnvironmentQuery(
96 "web-pull-requests:list-stats",
97 pullRequestEnvironment.listStats,
98);
99​
100export interface MergedPullRequestListView {
101 readonly data: MergedPullRequestList | null;
102 readonly error: string | null;
103 readonly isPending: boolean;
104 readonly refresh: () => void;
106​
107/** One listing per environment, merged into the single list the page renders. */
108export function usePullRequestList(
109 targets: ReadonlyArray<EnvironmentQueryTarget<PullRequestListInput>>,
110): MergedPullRequestListView {
111 const query = usePullRequestListsQuery(targets);
112 const data = useMemo(() => mergePullRequestLists(query.values), [query.values]);
113 return { data, error: query.error, isPending: query.isPending, refresh: query.refresh };
115​
116/** The line counts for the rows on screen, asked of each environment for its own rows. */
117export function usePullRequestListStats(
118 targets: ReadonlyArray<EnvironmentQueryTarget<PullRequestListStatsInput>>,
119): {
120 readonly stats: ReadonlyArray<EnvironmentPullRequestStat> | null;
121 readonly refresh: () => void;
122} {
123 const query = usePullRequestStatsQuery(targets);
124 const stats = useMemo(
125 () =>
126 query.values.length === 0
127 ? null
128 : query.values.flatMap(([environmentId, result]) =>
129 result.stats.map((stat) => ({ ...stat, environmentId })),
130 ),
131 [query.values],
132 );
133 return { stats, refresh: query.refresh };