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

usePullRequestDetail.ts

179 lines6.5 KB

1import {
2 AuthSourceControlWriteScope,
3 type EnvironmentId,
4 type PullRequestDetail,
5 type PullRequestListEntry,
6 type PullRequestRef,
7} from "@t3tools/contracts";
8import { useEffect, useEffectEvent, useState } from "react";
9​
10import { useEnvironmentQuery } from "~/state/query";
11import { useEnvironmentScope } from "~/state/session";
12import {
13 newestPullRequestSummary,
14 pullRequestEnvironment,
15 pullRequestListEntryToSummary,
16 useSharedPullRequestSummary,
17} from "~/state/pullRequests";
18import {
19 readPullRequestDetailSnapshot,
20 resolveDisplayedPullRequestDetail,
21 writePullRequestDetailSnapshot,
22} from "./pullRequestDetail.logic";
23import { pullRequestChecksState } from "./pullRequestPresentation";
24​
25export function pullRequestRefKey(reference: PullRequestRef) {
26 return `${reference.projectId}:${reference.host ?? ""}:${reference.repository}#${reference.number}`;
28​
29function pullRequestDetailSummary(data: PullRequestDetail | null) {
30 return data === null ? null : { ...data, checksState: pullRequestChecksState(data.checks) };
32​
33/**
34 * Loads a pull request's detail and activity and merges them, with the snapshot cache, the shared
35 * summary and the list row, into the detail the panel shows.
36 */
37export function usePullRequestDetail(
38 environmentId: EnvironmentId,
39 reference: PullRequestRef,
40 listEntry: PullRequestListEntry | null,
41) {
42 const scopeKey = `${environmentId}:${pullRequestRefKey(reference)}`;
43 const canWriteSourceControl = useEnvironmentScope(environmentId, AuthSourceControlWriteScope);
44 const detailQuery = useEnvironmentQuery(
45 pullRequestEnvironment.detail({ environmentId, input: reference }),
46 );
47 const activityQuery = useEnvironmentQuery(
48 pullRequestEnvironment.activity({ environmentId, input: reference }),
49 );
50 const [cachedDetail, setCachedDetail] = useState(() =>
51 readPullRequestDetailSnapshot(
52 typeof window === "undefined" ? undefined : window.localStorage,
53 environmentId,
54 reference,
55 ),
56 );
57 const [cachedDetailKey, setCachedDetailKey] = useState(scopeKey);
58 if (cachedDetailKey !== scopeKey) {
59 setCachedDetailKey(scopeKey);
60 setCachedDetail(
61 detailQuery.data ??
62 readPullRequestDetailSnapshot(
63 typeof window === "undefined" ? undefined : window.localStorage,
64 environmentId,
65 reference,
66 ),
67 );
68 } else if (detailQuery.data !== null && detailQuery.data !== cachedDetail) {
69 setCachedDetail(detailQuery.data);
70 }
71 const saveDetailSnapshot = useEffectEvent((data: PullRequestDetail) => {
72 writePullRequestDetailSnapshot(
73 typeof window === "undefined" ? undefined : window.localStorage,
74 environmentId,
75 reference,
76 data,
77 );
78 });
79 useEffect(() => {
80 if (detailQuery.data !== null) saveDetailSnapshot(detailQuery.data);
81 }, [detailQuery.data]);
82 const detailSummary = pullRequestDetailSummary(detailQuery.data);
83 const observedSummary = useSharedPullRequestSummary(
84 environmentId,
85 reference,
86 detailSummary,
87 detailQuery.dataUpdatedAt,
88 );
89 const activity = activityQuery.data;
90 const resolvedCoreDetail = resolveDisplayedPullRequestDetail({
91 live: detailQuery.data,
92 cached: cachedDetail,
93 reference,
94 });
95 // Every hook runs above this line: a hook between the merge and the panel's plain readers of
96 // `detail` would cost it its cache scope.
97 const listSummary = listEntry === null ? null : pullRequestListEntryToSummary(listEntry);
98 // The list row is also published to the shared cache, but only after this commit's layout
99 // effects run, so it is compared directly rather than trusted to be there already.
100 const sharedSummary = newestPullRequestSummary(
101 resolvedCoreDetail,
102 newestPullRequestSummary(observedSummary, listSummary),
103 );
104 const coreDetail =
105 resolvedCoreDetail === null || sharedSummary === null || sharedSummary === resolvedCoreDetail
106 ? resolvedCoreDetail
107 : {
108 ...resolvedCoreDetail,
109 title: sharedSummary.title,
110 state: sharedSummary.state,
111 headBranch: sharedSummary.headBranch,
112 baseBranch: sharedSummary.baseBranch,
113 updatedAt: sharedSummary.updatedAt,
114 author: sharedSummary.author ?? resolvedCoreDetail.author,
115 additions: sharedSummary.additions ?? resolvedCoreDetail.additions,
116 deletions: sharedSummary.deletions ?? resolvedCoreDetail.deletions,
117 changedFiles: sharedSummary.changedFiles ?? resolvedCoreDetail.changedFiles,
118 mergeability: sharedSummary.mergeability ?? resolvedCoreDetail.mergeability,
119 closedAt:
120 sharedSummary.closedAt === undefined
121 ? resolvedCoreDetail.closedAt
122 : sharedSummary.closedAt,
123 mergedAt:
124 sharedSummary.mergedAt === undefined
125 ? resolvedCoreDetail.mergedAt
126 : sharedSummary.mergedAt,
127 isDraft: sharedSummary.isDraft ?? resolvedCoreDetail.isDraft,
128 };
129 const detail =
130 coreDetail === null
131 ? null
132 : {
133 ...coreDetail,
134 capabilities: canWriteSourceControl
135 ? coreDetail.capabilities
136 : {
137 ...coreDetail.capabilities,
138 reactions: false,
139 edit: { changeRequest: false, comment: false },
140 },
141 viewerPermissions: canWriteSourceControl
142 ? coreDetail.viewerPermissions
143 : {
144 ...coreDetail.viewerPermissions,
145 actions: [],
146 comment: false,
147 resolve: false,
148 verdicts: [],
149 requestReviewers: false,
150 updateMethods: [],
151 labels: false,
152 },
153 author: activity?.author ?? coreDetail.author,
154 reviewers: activity?.reviewers ?? coreDetail.reviewers,
155 comments: activity?.comments ?? [],
156 commentCount: Math.max(
157 activity?.commentCount ?? 0,
158 new Set([
159 ...(activity?.comments.map((comment) => comment.id) ?? []),
160 ...(activity?.reviewThreads.flatMap((thread) =>
161 thread.comments.map((comment) => comment.id),
162 ) ?? []),
163 ]).size,
164 ),
165 commentsTruncated: activity?.commentsTruncated ?? false,
166 reviewThreads: activity?.reviewThreads ?? [],
167 commits: activity?.commits ?? [],
168 reactions: activity?.reactions ?? [],
169 };
170 return {
171 canWriteSourceControl,
172 detailQuery,
173 activityQuery,
174 activity,
175 sharedSummary,
176 coreDetail,
177 detail,
178 };