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