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

usePullRequestDiff.ts

153 lines5.0 KB

1import type { ChangeRequestTerminology } from "@t3tools/client-runtime/source-control-clients";
2import {
3 EnvironmentId,
4 ProjectId,
5 type PullRequestListEntry,
6 type PullRequestSummary,
7} from "@t3tools/contracts";
8import { useAtomRefresh } from "@effect/atom-react";
9import { useLayoutEffect, useMemo, useState } from "react";
10​
11import { composerPullRequests } from "../../state/pull-requests";
12import { useEnvironmentQuery } from "../../state/query";
13import { usePullToRefresh } from "../../state/use-pull-to-refresh";
14import {
15 getCachedPullRequestDiffPages,
16 retainPullRequestDiffPage,
17 type PullRequestDiffPage,
18} from "./pullRequestDiffPages";
19​
20export type PullRequestParams = {
21 readonly environmentId: string;
22 readonly projectId: string;
23 readonly repository: string;
24 readonly number: number;
25 readonly host?: string;
26 readonly initialSummary?: PullRequestListEntry | PullRequestSummary;
27};
28​
29export type PullRequestDiffParams = PullRequestParams & { readonly commit?: string };
30​
31export function pullRequestTarget(params: PullRequestParams) {
32 return {
33 environmentId: EnvironmentId.make(params.environmentId),
34 input: {
35 projectId: ProjectId.make(params.projectId),
36 repository: params.repository,
37 number: params.number,
38 ...(params.host ? { host: params.host } : {}),
39 },
40 };
42​
43const NO_PAGES: readonly PullRequestDiffPage[] = [];
44​
45export function usePullRequestDiffEntry(navigation: {
46 readonly addListener: (
47 type: "transitionStart" | "transitionEnd",
48 listener: (event: { readonly data: { readonly closing: boolean } }) => void,
49 ) => () => void;
50}) {
51 const [ready, setReady] = useState(false);
52 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to navigator transition events and frames to defer the diff until the opening transition ends
53 useLayoutEffect(() => {
54 if (ready) return;
55 let opening = false;
56 let closing = false;
57 const finish = () => {
58 if (!closing) setReady(true);
59 };
60 const removeStart = navigation.addListener("transitionStart", ({ data }) => {
61 closing = data.closing;
62 if (!closing) opening = true;
63 });
64 const removeEnd = navigation.addListener("transitionEnd", ({ data }) => {
65 closing = data.closing;
66 if (!closing) finish();
67 });
68 // Initial and unanimated presentations may have no transition events; let the shell paint first.
69 let frame = requestAnimationFrame(() => {
70 frame = requestAnimationFrame(() => {
71 if (!opening) finish();
72 });
73 });
74 // A missing native completion must not strand the route on its loading state.
75 const fallback = setTimeout(finish, 1_000);
76 return () => {
77 removeStart();
78 removeEnd();
79 cancelAnimationFrame(frame);
80 clearTimeout(fallback);
81 };
82 }, [navigation, ready]);
83 return ready;
85​
86export function usePullRequestDiff(
87 params: PullRequestDiffParams,
88 terminology: ChangeRequestTerminology,
89) {
90 const reference = pullRequestTarget(params);
91 const target = {
92 ...reference,
93 input: { ...reference.input, ...(params.commit ? { commit: params.commit } : {}) },
94 };
95 const key = JSON.stringify(target);
96 const firstPageAtom = composerPullRequests.diff(target);
97 const refreshFirstPage = useAtomRefresh(firstPageAtom);
98 const { refreshing, onRefresh } = usePullToRefresh(firstPageAtom);
99 const [state, setState] = useState<{
100 key: string;
101 cursor: string | null;
102 pages: readonly PullRequestDiffPage[];
103 }>({ key, cursor: null, pages: NO_PAGES });
104 const cursor = state.key === key ? state.cursor : null;
105 const query = useEnvironmentQuery(
106 composerPullRequests.diff({
107 ...target,
108 input: { ...target.input, ...(cursor ? { cursor } : {}) },
109 }),
110 );
111 const pages = useMemo(() => {
112 const loaded = state.key === key ? state.pages : NO_PAGES;
113 return query.data ? retainPullRequestDiffPage(loaded, cursor, query.data) : loaded;
114 }, [cursor, key, query.data, state.key, state.pages]);
115 if (state.key !== key || state.pages !== pages) setState({ key, cursor, pages });
116​
117 const parsed = useMemo(
118 () => getCachedPullRequestDiffPages(pages, key, terminology),
119 [key, pages, terminology],
120 );
121 const patch = useMemo(() => pages.map((page) => page.result.patch).join("\n"), [pages]);
122 const nextCursor = pages.at(-1)?.result.nextCursor ?? null;
123 const canLoadMore =
124 nextCursor !== null && nextCursor !== cursor && !query.isPending && query.error === null;
125 const loadMore = () => {
126 if (!canLoadMore) return;
127 setState({ key, cursor: nextCursor, pages });
128 };
129 const { error, refresh: refreshPage } = query;
130 const refresh = () => {
131 if (cursor !== null && error !== null) {
132 refreshPage();
133 return;
134 }
135 setState({ key, cursor: null, pages: NO_PAGES });
136 refreshFirstPage();
137 };
138 const onPullRefresh = () => {
139 setState({ key, cursor: null, pages: NO_PAGES });
140 onRefresh();
141 };
142 return {
143 ...query,
144 refresh,
145 parsed,
146 patch,
147 nextCursor,
148 canLoadMore,
149 loadMore,
150 refreshing,
151 onPullRefresh,
152 };