Open History to see the last commit
1import {
2 createPullRequestEnvironmentAtoms,
3 createPullRequestStackAtomFamily,
4 patchPullRequestListEntries,
5} from "@t3tools/client-runtime/state/pull-requests";
6import type { EnvironmentId, PullRequestListInput, PullRequestRef } from "@t3tools/contracts";
7import * as Effect from "effect/Effect";
8import * as Option from "effect/Option";
9import { AsyncResult, Atom } from "effect/reactivity";
10import { connectionAtomRuntime } from "../connection/runtime";
11import {
12 EMPTY_PULL_REQUEST_STATS_CACHE,
13 pullRequestListHead,
14 withAnsweredPullRequestStats,
15 withListedPullRequests,
16 type PullRequestListPages,
17 type PullRequestStatsCache,
18} from "../features/pull-requests/pullRequestList";
19
20export const pullRequestListPagesKey = (target: {
21 readonly environmentId: EnvironmentId;
22 readonly input: PullRequestListInput;
23}) => JSON.stringify([target.environmentId, target.input]);
24
25// Which listing each pages atom holds, so an action's reply can reach the pages past its head.
26const pullRequestListPagesTargets = new WeakMap<
27 object,
28 readonly [EnvironmentId, PullRequestListInput]
29>();
30/** The pages loaded past each listing's head, keyed by {@link pullRequestListPagesKey}. */
31export const pullRequestListPagesFamily = Atom.family((key: string) => {
32 const atom = Atom.make<PullRequestListPages | null>(null).pipe(Atom.setIdleTTL(5 * 60_000));
33 pullRequestListPagesTargets.set(atom, JSON.parse(key));
34 return atom;
35});
36
37/** Diff stats each environment has answered so far, so rows keep them when they scroll away. */
38export const pullRequestListStatsFamily = Atom.family((_environmentId: string) =>
39 Atom.make<PullRequestStatsCache>(EMPTY_PULL_REQUEST_STATS_CACHE).pipe(
40 Atom.setIdleTTL(5 * 60_000),
41 ),
42);
43
44export const filesViewedReadsKey = (target: {
45 readonly environmentId: EnvironmentId;
46 readonly input: PullRequestRef;
47}) => JSON.stringify([target.environmentId, target.input]);
48
49/** Completed server reads of each viewed-files query; unchanged answers are structurally shared. */
50export const filesViewedReadsFamily = Atom.family((_key: string) =>
51 Atom.make(0).pipe(Atom.setIdleTTL(5 * 60_000)),
52);
53
54export const composerPullRequests = createPullRequestEnvironmentAtoms(connectionAtomRuntime, {
55 // Rows on pages past each listing's head; the head itself is patched where it is cached.
56 actionReply: (target, reply, registry) =>
57 Effect.sync(() => {
58 for (const atom of registry.getNodes().keys()) {
59 const listing =
60 typeof atom === "string" ? undefined : pullRequestListPagesTargets.get(atom);
61 if (listing === undefined || listing[0] !== target.environmentId) continue;
62 registry.update(atom as Atom.Writable<PullRequestListPages | null>, (pages) => {
63 if (pages === null) return pages;
64 const entries = patchPullRequestListEntries(
65 listing[1],
66 pages.entries,
67 target.input,
68 reply,
69 );
70 return entries === "refresh" || entries === pages.entries ? pages : { ...pages, entries };
71 });
72 }
73 }),
74 // A new head can change which files GitHub still counts as viewed, so the viewed-files read is
75 // refreshed where the new head lands. The detail atom still holds the previous answer here.
76 detail: (target, value, registry) =>
77 Effect.sync(() => {
78 const previous = AsyncResult.value(registry.get(composerPullRequests.detail(target)));
79 if (Option.isSome(previous) && previous.value.headSha !== value.headSha)
80 registry.refresh(composerPullRequests.filesViewed(target));
81 }),
82 filesViewed: (target, _value, registry) =>
83 Effect.sync(() =>
84 registry.update(filesViewedReadsFamily(filesViewedReadsKey(target)), (reads) => reads + 1),
85 ),
86 list: (target, value, registry) =>
87 Effect.sync(() => {
88 const statsAtom = pullRequestListStatsFamily(target.environmentId);
89 const { cache, refresh } = withListedPullRequests(registry.get(statsAtom), value.entries);
90 registry.set(statsAtom, cache);
91 for (const input of refresh)
92 registry.refresh(
93 composerPullRequests.listStats({ environmentId: target.environmentId, input }),
94 );
95 if (target.input.cursors === undefined)
96 registry.update(pullRequestListPagesFamily(pullRequestListPagesKey(target)), (previous) =>
97 pullRequestListHead(previous, value),
98 );
99 }),
100 listStats: (target, value, registry) =>
101 Effect.sync(() =>
102 registry.update(pullRequestListStatsFamily(target.environmentId), (previous) =>
103 withAnsweredPullRequestStats(previous, target.input, value.stats),
104 ),
105 ),
106});
107export const pullRequestStackAtom = createPullRequestStackAtomFamily(
108 connectionAtomRuntime,
109 composerPullRequests.refreshes,
110);