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