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

usePullRequestStack.ts

39 lines1.7 KB

1import { useAtomValue } from "@effect/atom-react";
2import { ThreadPullRequestLink, type EnvironmentId, type PullRequestRef } from "@t3tools/contracts";
3import { Atom } from "effect/reactivity";
4import * as Schema from "effect/Schema";
5import {
6 savedPullRequestStack,
7 pullRequestStackView,
8} from "../components/pullRequest/pullRequestStackSnapshot";
9import { pullRequestStackAtom } from "./pullRequests";
10import { useEnvironmentQuery } from "./query";
11import { environmentThreadShells } from "./threads";
12​
13const sameLinks = Schema.toEquivalence(Schema.Array(ThreadPullRequestLink));
14​
15// A shell update re-sends a thread's links as a new array, so equal links keep
16// the previous list and a streaming thread does not re-render the stack.
17const environmentPullRequestLinksAtom = Atom.family((environmentId: EnvironmentId) => {
18 let previous: ReadonlyArray<ThreadPullRequestLink> = [];
19 return Atom.make((get) => {
20 const next = get(environmentThreadShells.environmentThreadsAtom(environmentId)).flatMap(
21 (thread) => thread.pullRequests ?? [],
22 );
23 if (sameLinks(previous, next)) return previous;
24 previous = next;
25 return next;
26 }).pipe(Atom.withLabel(`web-environment-pull-request-links:${environmentId}`));
27});
28​
29export function usePullRequestStack(
30 environmentId: EnvironmentId,
31 reference: PullRequestRef | null,
32) {
33 const links = useAtomValue(environmentPullRequestLinksAtom(environmentId));
34 const saved = reference === null ? null : savedPullRequestStack(links, reference);
35 const query = useEnvironmentQuery(
36 reference === null ? null : pullRequestStackAtom({ environmentId, input: reference }),
37 );
38 return { ...query, ...pullRequestStackView(query, saved) };