Skip to content

Commit a9ac4ea

Browse files
refactor(web): the pull request detail loader lives in its own hook
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4207420 commit a9ac4ea

2 files changed

Lines changed: 181 additions & 166 deletions

File tree

‎apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx‎

Lines changed: 2 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,7 @@ import { useEnvironments } from "~/state/environments";
6363
import { useEnvironmentQuery } from "~/state/query";
6464
import { readEnvironmentScope, useEnvironmentScope } from "~/state/session";
6565
import { useLiveRefresh } from "~/hooks/useLiveRefresh";
66-
import {
67-
pullRequestEnvironment,
68-
pullRequestListEntryToSummary,
69-
newestPullRequestSummary,
70-
usePullRequestTurnRefresh,
71-
useSharedPullRequestSummary,
72-
} from "~/state/pullRequests";
66+
import { pullRequestEnvironment, usePullRequestTurnRefresh } from "~/state/pullRequests";
7367
import { useAtomCommand } from "~/state/use-atom-command";
7468
import { PullRequestStackMenu } from "./PullRequestStackMenu";
7569
import { PullRequestChangeBreakdownStat } from "./PullRequestChangeBreakdownStat";
@@ -90,6 +84,7 @@ import {
9084
} from "./PullRequestDetailLayout";
9185
import { PullRequestDetailGhost, PullRequestTimelineGhost } from "./PullRequestGhosts";
9286
import { PullRequestCopyableCode } from "./PullRequestCopyableCode";
87+
import { pullRequestRefKey, usePullRequestDetail } from "./usePullRequestDetail";
9388
import { PullRequestActionsMenu } from "./PullRequestActionsMenu";
9489
import {
9590
PullRequestApproveWorkflowsButton,
@@ -130,9 +125,7 @@ import {
130125
PULL_REQUEST_MERGE_METHOD_LABELS,
131126
checkoutFailureDescription,
132127
readableFailure,
133-
readPullRequestDetailSnapshot,
134128
resolvePullRequestReferenceHost,
135-
resolveDisplayedPullRequestDetail,
136129
resolvePullRequestPrimaryControl,
137130
allowsSinglePullRequestMerge,
138131
canDeletePullRequestBranch,
@@ -141,7 +134,6 @@ import {
141134
type PullRequestFinding,
142135
shouldRefreshPullRequestActivity,
143136
stripPullRequestHandoffReferences,
144-
writePullRequestDetailSnapshot,
145137
} from "./pullRequestDetail.logic";
146138
import { resolvePickableEnvironments } from "./pullRequestProjectAssignment.logic";
147139
import { PullRequestChecksPopover } from "./PullRequestChecksPopover";
@@ -241,162 +233,6 @@ function resolvePullRequestReference(
241233
};
242234
}
243235

244-
function pullRequestRefKey(reference: PullRequestRef) {
245-
return `${reference.projectId}:${reference.host ?? ""}:${reference.repository}#${reference.number}`;
246-
}
247-
248-
function pullRequestDetailSummary(data: PullRequestDetail | null) {
249-
return data === null ? null : { ...data, checksState: pullRequestChecksState(data.checks) };
250-
}
251-
252-
/**
253-
* Loads a pull request's detail and activity and merges them, with the snapshot cache, the shared
254-
* summary and the list row, into the detail the panel shows.
255-
*/
256-
function usePullRequestDetail(
257-
environmentId: EnvironmentId,
258-
reference: PullRequestRef,
259-
listEntry: PullRequestListEntry | null,
260-
) {
261-
const scopeKey = `${environmentId}:${pullRequestRefKey(reference)}`;
262-
const canWriteSourceControl = useEnvironmentScope(environmentId, AuthSourceControlWriteScope);
263-
const detailQuery = useEnvironmentQuery(
264-
pullRequestEnvironment.detail({ environmentId, input: reference }),
265-
);
266-
const activityQuery = useEnvironmentQuery(
267-
pullRequestEnvironment.activity({ environmentId, input: reference }),
268-
);
269-
const [cachedDetail, setCachedDetail] = useState(() =>
270-
readPullRequestDetailSnapshot(
271-
typeof window === "undefined" ? undefined : window.localStorage,
272-
environmentId,
273-
reference,
274-
),
275-
);
276-
const [cachedDetailKey, setCachedDetailKey] = useState(scopeKey);
277-
if (cachedDetailKey !== scopeKey) {
278-
setCachedDetailKey(scopeKey);
279-
setCachedDetail(
280-
detailQuery.data ??
281-
readPullRequestDetailSnapshot(
282-
typeof window === "undefined" ? undefined : window.localStorage,
283-
environmentId,
284-
reference,
285-
),
286-
);
287-
} else if (detailQuery.data !== null && detailQuery.data !== cachedDetail) {
288-
setCachedDetail(detailQuery.data);
289-
}
290-
const saveDetailSnapshot = useEffectEvent((data: PullRequestDetail) => {
291-
writePullRequestDetailSnapshot(
292-
typeof window === "undefined" ? undefined : window.localStorage,
293-
environmentId,
294-
reference,
295-
data,
296-
);
297-
});
298-
useEffect(() => {
299-
if (detailQuery.data !== null) saveDetailSnapshot(detailQuery.data);
300-
}, [detailQuery.data]);
301-
const detailSummary = pullRequestDetailSummary(detailQuery.data);
302-
const observedSummary = useSharedPullRequestSummary(
303-
environmentId,
304-
reference,
305-
detailSummary,
306-
detailQuery.dataUpdatedAt,
307-
);
308-
const activity = activityQuery.data;
309-
const resolvedCoreDetail = resolveDisplayedPullRequestDetail({
310-
live: detailQuery.data,
311-
cached: cachedDetail,
312-
reference,
313-
});
314-
// Every hook runs above this line: a hook between the merge and the panel's plain readers of
315-
// `detail` would cost it its cache scope.
316-
const listSummary = listEntry === null ? null : pullRequestListEntryToSummary(listEntry);
317-
// The list row is also published to the shared cache, but only after this commit's layout
318-
// effects run, so it is compared directly rather than trusted to be there already.
319-
const sharedSummary = newestPullRequestSummary(
320-
resolvedCoreDetail,
321-
newestPullRequestSummary(observedSummary, listSummary),
322-
);
323-
const coreDetail =
324-
resolvedCoreDetail === null || sharedSummary === null || sharedSummary === resolvedCoreDetail
325-
? resolvedCoreDetail
326-
: {
327-
...resolvedCoreDetail,
328-
title: sharedSummary.title,
329-
state: sharedSummary.state,
330-
headBranch: sharedSummary.headBranch,
331-
baseBranch: sharedSummary.baseBranch,
332-
updatedAt: sharedSummary.updatedAt,
333-
author: sharedSummary.author ?? resolvedCoreDetail.author,
334-
additions: sharedSummary.additions ?? resolvedCoreDetail.additions,
335-
deletions: sharedSummary.deletions ?? resolvedCoreDetail.deletions,
336-
changedFiles: sharedSummary.changedFiles ?? resolvedCoreDetail.changedFiles,
337-
mergeability: sharedSummary.mergeability ?? resolvedCoreDetail.mergeability,
338-
closedAt:
339-
sharedSummary.closedAt === undefined
340-
? resolvedCoreDetail.closedAt
341-
: sharedSummary.closedAt,
342-
mergedAt:
343-
sharedSummary.mergedAt === undefined
344-
? resolvedCoreDetail.mergedAt
345-
: sharedSummary.mergedAt,
346-
isDraft: sharedSummary.isDraft ?? resolvedCoreDetail.isDraft,
347-
};
348-
const detail =
349-
coreDetail === null
350-
? null
351-
: {
352-
...coreDetail,
353-
capabilities: canWriteSourceControl
354-
? coreDetail.capabilities
355-
: {
356-
...coreDetail.capabilities,
357-
reactions: false,
358-
edit: { changeRequest: false, comment: false },
359-
},
360-
viewerPermissions: canWriteSourceControl
361-
? coreDetail.viewerPermissions
362-
: {
363-
...coreDetail.viewerPermissions,
364-
actions: [],
365-
comment: false,
366-
resolve: false,
367-
verdicts: [],
368-
requestReviewers: false,
369-
updateMethods: [],
370-
labels: false,
371-
},
372-
author: activity?.author ?? coreDetail.author,
373-
reviewers: activity?.reviewers ?? coreDetail.reviewers,
374-
comments: activity?.comments ?? [],
375-
commentCount: Math.max(
376-
activity?.commentCount ?? 0,
377-
new Set([
378-
...(activity?.comments.map((comment) => comment.id) ?? []),
379-
...(activity?.reviewThreads.flatMap((thread) =>
380-
thread.comments.map((comment) => comment.id),
381-
) ?? []),
382-
]).size,
383-
),
384-
commentsTruncated: activity?.commentsTruncated ?? false,
385-
reviewThreads: activity?.reviewThreads ?? [],
386-
commits: activity?.commits ?? [],
387-
reactions: activity?.reactions ?? [],
388-
};
389-
return {
390-
canWriteSourceControl,
391-
detailQuery,
392-
activityQuery,
393-
activity,
394-
sharedSummary,
395-
coreDetail,
396-
detail,
397-
};
398-
}
399-
400236
function pullRequestMarkdownContext(
401237
hostClient: ReturnType<typeof sourceControlClients.get>,
402238
repositoryUrl: string | null,
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
import {
2+
AuthSourceControlWriteScope,
3+
type EnvironmentId,
4+
type PullRequestDetail,
5+
type PullRequestListEntry,
6+
type PullRequestRef,
7+
} from "@t3tools/contracts";
8+
import { useEffect, useEffectEvent, useState } from "react";
9+
10+
import { useEnvironmentQuery } from "~/state/query";
11+
import { useEnvironmentScope } from "~/state/session";
12+
import {
13+
newestPullRequestSummary,
14+
pullRequestEnvironment,
15+
pullRequestListEntryToSummary,
16+
useSharedPullRequestSummary,
17+
} from "~/state/pullRequests";
18+
import {
19+
readPullRequestDetailSnapshot,
20+
resolveDisplayedPullRequestDetail,
21+
writePullRequestDetailSnapshot,
22+
} from "./pullRequestDetail.logic";
23+
import { pullRequestChecksState } from "./pullRequestPresentation";
24+
25+
export function pullRequestRefKey(reference: PullRequestRef) {
26+
return `${reference.projectId}:${reference.host ?? ""}:${reference.repository}#${reference.number}`;
27+
}
28+
29+
function pullRequestDetailSummary(data: PullRequestDetail | null) {
30+
return data === null ? null : { ...data, checksState: pullRequestChecksState(data.checks) };
31+
}
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+
*/
37+
export 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+
};
179+
}

0 commit comments

Comments
 (0)