Repository navigation
Expand file tree
/
Copy pathusePullRequestStack.ts
More file actions
38 lines (35 loc) · 1.62 KB
/
Copy pathusePullRequestStack.ts
File metadata and controls
38 lines (35 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { useAtomValue } from "@effect/atom-react";
import { ThreadPullRequestLink, type EnvironmentId, type PullRequestRef } from "@t3tools/contracts";
import { Atom } from "effect/reactivity";
import * as Schema from "effect/Schema";
import {
savedPullRequestStack,
pullRequestStackView,
} from "../components/pullRequest/pullRequestStackSnapshot";
import { pullRequestStackAtom } from "./pullRequests";
import { useEnvironmentQuery } from "./query";
import { environmentThreadShells } from "./threads";
const sameLinks = Schema.toEquivalence(Schema.Array(ThreadPullRequestLink));
// Shell pushes recreate link arrays; reuse equal lists so streaming does not re-render the stack.
const environmentPullRequestLinksAtom = Atom.family((environmentId: EnvironmentId) => {
let previous: ReadonlyArray<ThreadPullRequestLink> = [];
return Atom.make((get) => {
const next = get(environmentThreadShells.environmentThreadsAtom(environmentId)).flatMap(
(thread) => thread.pullRequests ?? [],
);
if (sameLinks(previous, next)) return previous;
previous = next;
return next;
}).pipe(Atom.withLabel(`web-environment-pull-request-links:${environmentId}`));
});
export function usePullRequestStack(
environmentId: EnvironmentId,
reference: PullRequestRef | null,
) {
const links = useAtomValue(environmentPullRequestLinksAtom(environmentId));
const saved = reference === null ? null : savedPullRequestStack(links, reference);
const query = useEnvironmentQuery(
reference === null ? null : pullRequestStackAtom({ environmentId, input: reference }),
);
return { ...query, ...pullRequestStackView(query, saved) };
}