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 { useCallback, 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 useLayoutEffect(() => {
48 if (ready) return;
49 let opening = false;
50 let closing = false;
51 const finish = () => {
52 if (!closing) setReady(true);
53 };
54 const removeStart = navigation.addListener("transitionStart", ({ data }) => {
55 closing = data.closing;
56 if (!closing) opening = true;
57 });
58 const removeEnd = navigation.addListener("transitionEnd", ({ data }) => {
59 closing = data.closing;
60 if (!closing) finish();
61 });
62 // Initial and unanimated presentations may have no transition events; let the shell paint first.
63 let frame = requestAnimationFrame(() => {
64 frame = requestAnimationFrame(() => {
65 if (!opening) finish();
66 });
67 });
68 // A missing native completion must not strand the route on its loading state.
69 const fallback = setTimeout(finish, 1_000);
70 return () => {
71 removeStart();
72 removeEnd();
73 cancelAnimationFrame(frame);
74 clearTimeout(fallback);
75 };
76 }, [navigation, ready]);
77 return ready;
78}
79
80export function usePullRequestDiff(
81 params: PullRequestDiffParams,
82 terminology: ChangeRequestTerminology,
83) {
84 const reference = pullRequestTarget(params);
85 const target = {
86 ...reference,
87 input: { ...reference.input, ...(params.commit ? { commit: params.commit } : {}) },
88 };
89 const key = JSON.stringify(target);
90 const firstPageAtom = composerPullRequests.diff(target);
91 const refreshFirstPage = useAtomRefresh(firstPageAtom);
92 const { refreshing, onRefresh } = usePullToRefresh(firstPageAtom);
93 const [state, setState] = useState<{
94 key: string;
95 cursor: string | null;
96 pages: readonly PullRequestDiffPage[];
97 }>({ key, cursor: null, pages: NO_PAGES });
98 const cursor = state.key === key ? state.cursor : null;
99 const query = useEnvironmentQuery(
100 composerPullRequests.diff({
101 ...target,
102 input: { ...target.input, ...(cursor ? { cursor } : {}) },
103 }),
104 );
105 const pages = useMemo(() => {
106 const loaded = state.key === key ? state.pages : NO_PAGES;
107 return query.data ? retainPullRequestDiffPage(loaded, cursor, query.data) : loaded;
108 }, [cursor, key, query.data, state.key, state.pages]);
109 if (state.key !== key || state.pages !== pages) setState({ key, cursor, pages });
110
111 const parsed = useMemo(
112 () => getCachedPullRequestDiffPages(pages, key, terminology),
113 [key, pages, terminology],
114 );
115 const patch = useMemo(() => pages.map((page) => page.result.patch).join("\n"), [pages]);
116 const nextCursor = pages.at(-1)?.result.nextCursor ?? null;
117 const canLoadMore =
118 nextCursor !== null && nextCursor !== cursor && !query.isPending && query.error === null;
119 const loadMore = useCallback(() => {
120 if (!canLoadMore) return;
121 setState({ key, cursor: nextCursor, pages });
122 }, [canLoadMore, key, nextCursor, pages, setState]);
123 const { error, refresh: refreshPage } = query;
124 const refresh = useCallback(() => {
125 if (cursor !== null && error !== null) {
126 refreshPage();
127 return;
128 }
129 setState({ key, cursor: null, pages: NO_PAGES });
130 refreshFirstPage();
131 }, [cursor, error, refreshPage, key, refreshFirstPage, setState]);
132 const onPullRefresh = useCallback(() => {
133 setState({ key, cursor: null, pages: NO_PAGES });
134 onRefresh();
135 }, [key, onRefresh, setState]);
136 return {
137 ...query,
138 refresh,
139 parsed,
140 patch,
141 nextCursor,
142 canLoadMore,
143 loadMore,
144 refreshing,
145 onPullRefresh,
146 };
147}