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

pull-requests.ts

69 lines2.9 KB

1import {
2 createPullRequestEnvironmentAtoms,
3 createPullRequestStackAtomFamily,
4} from "@t3tools/client-runtime/state/pull-requests";
5import type { EnvironmentId, PullRequestListInput } from "@t3tools/contracts";
6import * as Effect from "effect/Effect";
7import * as Option from "effect/Option";
8import { AsyncResult, Atom } from "effect/reactivity";
9import { connectionAtomRuntime } from "../connection/runtime";
10import {
11 EMPTY_PULL_REQUEST_STATS_CACHE,
12 pullRequestListHead,
13 withAnsweredPullRequestStats,
14 withListedPullRequests,
15 type PullRequestListPages,
16 type PullRequestStatsCache,
17} from "../features/pull-requests/pullRequestList";
18​
19export const pullRequestListPagesKey = (target: {
20 readonly environmentId: EnvironmentId;
21 readonly input: PullRequestListInput;
22}) => JSON.stringify([target.environmentId, target.input]);
23​
24/** The pages loaded past each listing's head, keyed by {@link pullRequestListPagesKey}. */
25export const pullRequestListPagesFamily = Atom.family((_key: string) =>
26 Atom.make<PullRequestListPages | null>(null).pipe(Atom.setIdleTTL(5 * 60_000)),
27);
28​
29/** Diff stats each environment has answered so far, so rows keep them when they scroll away. */
30export const pullRequestListStatsFamily = Atom.family((_environmentId: string) =>
31 Atom.make<PullRequestStatsCache>(EMPTY_PULL_REQUEST_STATS_CACHE).pipe(
32 Atom.setIdleTTL(5 * 60_000),
33 ),
34);
35​
36export const composerPullRequests = createPullRequestEnvironmentAtoms(connectionAtomRuntime, {
37 // A new head can change which files GitHub still counts as viewed, so the viewed-files read is
38 // refreshed where the new head lands. The detail atom still holds the previous answer here.
39 detail: (target, value, registry) =>
40 Effect.sync(() => {
41 const previous = AsyncResult.value(registry.get(composerPullRequests.detail(target)));
42 if (Option.isSome(previous) && previous.value.headSha !== value.headSha)
43 registry.refresh(composerPullRequests.filesViewed(target));
44 }),
45 list: (target, value, registry) =>
46 Effect.sync(() => {
47 const statsAtom = pullRequestListStatsFamily(target.environmentId);
48 const { cache, refresh } = withListedPullRequests(registry.get(statsAtom), value.entries);
49 registry.set(statsAtom, cache);
50 for (const input of refresh)
51 registry.refresh(
52 composerPullRequests.listStats({ environmentId: target.environmentId, input }),
53 );
54 if (target.input.cursors === undefined)
55 registry.update(pullRequestListPagesFamily(pullRequestListPagesKey(target)), (previous) =>
56 pullRequestListHead(previous, value),
57 );
58 }),
59 listStats: (target, value, registry) =>
60 Effect.sync(() =>
61 registry.update(pullRequestListStatsFamily(target.environmentId), (previous) =>
62 withAnsweredPullRequestStats(previous, target.input, value.stats),
63 ),
64 ),
65});
66export const pullRequestStackAtom = createPullRequestStackAtomFamily(
67 connectionAtomRuntime,
68 composerPullRequests.refreshes,
69);