Skip to content
TypeScript

refactor(web): the pull request detail loader lives in its own hook (#22703)

Julius Marminge5db00458e522
diff --git a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx
index d5d0a4087a6..40e4a78746d 100644
--- a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx
+++ b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx
@@ -63,13 +63,7 @@ import { useEnvironments } from "~/state/environments";
import { useEnvironmentQuery } from "~/state/query";
import { readEnvironmentScope, useEnvironmentScope } from "~/state/session";
import { useLiveRefresh } from "~/hooks/useLiveRefresh";
-import {
- pullRequestEnvironment,
- pullRequestListEntryToSummary,
- newestPullRequestSummary,
- usePullRequestTurnRefresh,
- useSharedPullRequestSummary,
-} from "~/state/pullRequests";
+import { pullRequestEnvironment, usePullRequestTurnRefresh } from "~/state/pullRequests";
import { useAtomCommand } from "~/state/use-atom-command";
import { PullRequestStackMenu } from "./PullRequestStackMenu";
import { PullRequestChangeBreakdownStat } from "./PullRequestChangeBreakdownStat";
@@ -90,6 +84,7 @@ import {
} from "./PullRequestDetailLayout";
import { PullRequestDetailGhost, PullRequestTimelineGhost } from "./PullRequestGhosts";
import { PullRequestCopyableCode } from "./PullRequestCopyableCode";
+import { pullRequestRefKey, usePullRequestDetail } from "./usePullRequestDetail";
import { PullRequestActionsMenu } from "./PullRequestActionsMenu";
import {
PullRequestApproveWorkflowsButton,
@@ -130,9 +125,7 @@ import {
PULL_REQUEST_MERGE_METHOD_LABELS,
checkoutFailureDescription,
readableFailure,
- readPullRequestDetailSnapshot,
resolvePullRequestReferenceHost,
- resolveDisplayedPullRequestDetail,
resolvePullRequestPrimaryControl,
allowsSinglePullRequestMerge,
canDeletePullRequestBranch,
@@ -141,7 +134,6 @@ import {
type PullRequestFinding,
shouldRefreshPullRequestActivity,
stripPullRequestHandoffReferences,
- writePullRequestDetailSnapshot,
} from "./pullRequestDetail.logic";
import { resolvePickableEnvironments } from "./pullRequestProjectAssignment.logic";
import { PullRequestChecksPopover } from "./PullRequestChecksPopover";
@@ -241,162 +233,6 @@ function resolvePullRequestReference(
};
}
-function pullRequestRefKey(reference: PullRequestRef) {
- return `${reference.projectId}:${reference.host ?? ""}:${reference.repository}#${reference.number}`;
-}
-
-function pullRequestDetailSummary(data: PullRequestDetail | null) {
- return data === null ? null : { ...data, checksState: pullRequestChecksState(data.checks) };
-}
-
-/**
- * Loads a pull request's detail and activity and merges them, with the snapshot cache, the shared
- * summary and the list row, into the detail the panel shows.
- */
-function usePullRequestDetail(
- environmentId: EnvironmentId,
- reference: PullRequestRef,
- listEntry: PullRequestListEntry | null,
-) {
- const scopeKey = `${environmentId}:${pullRequestRefKey(reference)}`;
- const canWriteSourceControl = useEnvironmentScope(environmentId, AuthSourceControlWriteScope);
- const detailQuery = useEnvironmentQuery(
- pullRequestEnvironment.detail({ environmentId, input: reference }),
- );
- const activityQuery = useEnvironmentQuery(
- pullRequestEnvironment.activity({ environmentId, input: reference }),
- );
- const [cachedDetail, setCachedDetail] = useState(() =>
- readPullRequestDetailSnapshot(
- typeof window === "undefined" ? undefined : window.localStorage,
- environmentId,
- reference,
- ),
- );
- const [cachedDetailKey, setCachedDetailKey] = useState(scopeKey);
- if (cachedDetailKey !== scopeKey) {
- setCachedDetailKey(scopeKey);
- setCachedDetail(
- detailQuery.data ??
- readPullRequestDetailSnapshot(
- typeof window === "undefined" ? undefined : window.localStorage,
- environmentId,
- reference,
- ),
- );
- } else if (detailQuery.data !== null && detailQuery.data !== cachedDetail) {
- setCachedDetail(detailQuery.data);
- }
- const saveDetailSnapshot = useEffectEvent((data: PullRequestDetail) => {
- writePullRequestDetailSnapshot(
- typeof window === "undefined" ? undefined : window.localStorage,
- environmentId,
- reference,
- data,
- );
- });
- useEffect(() => {
- if (detailQuery.data !== null) saveDetailSnapshot(detailQuery.data);
- }, [detailQuery.data]);
- const detailSummary = pullRequestDetailSummary(detailQuery.data);
- const observedSummary = useSharedPullRequestSummary(
- environmentId,
- reference,
- detailSummary,
- detailQuery.dataUpdatedAt,
- );
- const activity = activityQuery.data;
- const resolvedCoreDetail = resolveDisplayedPullRequestDetail({
- live: detailQuery.data,
- cached: cachedDetail,
- reference,
- });
- // Every hook runs above this line: a hook between the merge and the panel's plain readers of
- // `detail` would cost it its cache scope.
- const listSummary = listEntry === null ? null : pullRequestListEntryToSummary(listEntry);
- // The list row is also published to the shared cache, but only after this commit's layout
- // effects run, so it is compared directly rather than trusted to be there already.
- const sharedSummary = newestPullRequestSummary(
- resolvedCoreDetail,
- newestPullRequestSummary(observedSummary, listSummary),
- );
- const coreDetail =
- resolvedCoreDetail === null || sharedSummary === null || sharedSummary === resolvedCoreDetail
- ? resolvedCoreDetail
- : {
- ...resolvedCoreDetail,
- title: sharedSummary.title,
- state: sharedSummary.state,
- headBranch: sharedSummary.headBranch,
- baseBranch: sharedSummary.baseBranch,
- updatedAt: sharedSummary.updatedAt,
- author: sharedSummary.author ?? resolvedCoreDetail.author,
- additions: sharedSummary.additions ?? resolvedCoreDetail.additions,
- deletions: sharedSummary.deletions ?? resolvedCoreDetail.deletions,
- changedFiles: sharedSummary.changedFiles ?? resolvedCoreDetail.changedFiles,
- mergeability: sharedSummary.mergeability ?? resolvedCoreDetail.mergeability,
- closedAt:
- sharedSummary.closedAt === undefined
- ? resolvedCoreDetail.closedAt
- : sharedSummary.closedAt,
- mergedAt:
- sharedSummary.mergedAt === undefined
- ? resolvedCoreDetail.mergedAt
- : sharedSummary.mergedAt,
- isDraft: sharedSummary.isDraft ?? resolvedCoreDetail.isDraft,
- };
- const detail =
- coreDetail === null
- ? null
- : {
- ...coreDetail,
- capabilities: canWriteSourceControl
- ? coreDetail.capabilities
- : {
- ...coreDetail.capabilities,
- reactions: false,
- edit: { changeRequest: false, comment: false },
- },
- viewerPermissions: canWriteSourceControl
- ? coreDetail.viewerPermissions
- : {
- ...coreDetail.viewerPermissions,
- actions: [],
- comment: false,
- resolve: false,
- verdicts: [],
- requestReviewers: false,
- updateMethods: [],
- labels: false,
- },
- author: activity?.author ?? coreDetail.author,
- reviewers: activity?.reviewers ?? coreDetail.reviewers,
- comments: activity?.comments ?? [],
- commentCount: Math.max(
- activity?.commentCount ?? 0,
- new Set([
- ...(activity?.comments.map((comment) => comment.id) ?? []),
- ...(activity?.reviewThreads.flatMap((thread) =>
- thread.comments.map((comment) => comment.id),
- ) ?? []),
- ]).size,
- ),
- commentsTruncated: activity?.commentsTruncated ?? false,
- reviewThreads: activity?.reviewThreads ?? [],
- commits: activity?.commits ?? [],
- reactions: activity?.reactions ?? [],
- };
- return {
- canWriteSourceControl,
- detailQuery,
- activityQuery,
- activity,
- sharedSummary,
- coreDetail,
- detail,
- };
-}
-
function pullRequestMarkdownContext(
hostClient: ReturnType<typeof sourceControlClients.get>,
repositoryUrl: string | null,
diff --git a/apps/web/src/components/pullRequest/usePullRequestDetail.ts b/apps/web/src/components/pullRequest/usePullRequestDetail.ts
new file mode 100644
index 00000000000..a3228f6b64f
--- /dev/null
+++ b/apps/web/src/components/pullRequest/usePullRequestDetail.ts
@@ -0,0 +1,179 @@
+import {
+ AuthSourceControlWriteScope,
+ type EnvironmentId,
+ type PullRequestDetail,
+ type PullRequestListEntry,
+ type PullRequestRef,
+} from "@t3tools/contracts";
+import { useEffect, useEffectEvent, useState } from "react";
+
+import { useEnvironmentQuery } from "~/state/query";
+import { useEnvironmentScope } from "~/state/session";
+import {
+ newestPullRequestSummary,
+ pullRequestEnvironment,
+ pullRequestListEntryToSummary,
+ useSharedPullRequestSummary,
+} from "~/state/pullRequests";
+import {
+ readPullRequestDetailSnapshot,
+ resolveDisplayedPullRequestDetail,
+ writePullRequestDetailSnapshot,
+} from "./pullRequestDetail.logic";
+import { pullRequestChecksState } from "./pullRequestPresentation";
+
+export function pullRequestRefKey(reference: PullRequestRef) {
+ return `${reference.projectId}:${reference.host ?? ""}:${reference.repository}#${reference.number}`;
+}
+
+function pullRequestDetailSummary(data: PullRequestDetail | null) {
+ return data === null ? null : { ...data, checksState: pullRequestChecksState(data.checks) };
+}
+
+/**
+ * Loads a pull request's detail and activity and merges them, with the snapshot cache, the shared
+ * summary and the list row, into the detail the panel shows.
+ */
+export function usePullRequestDetail(
+ environmentId: EnvironmentId,
+ reference: PullRequestRef,
+ listEntry: PullRequestListEntry | null,
+) {
+ const scopeKey = `${environmentId}:${pullRequestRefKey(reference)}`;
+ const canWriteSourceControl = useEnvironmentScope(environmentId, AuthSourceControlWriteScope);
+ const detailQuery = useEnvironmentQuery(
+ pullRequestEnvironment.detail({ environmentId, input: reference }),
+ );
+ const activityQuery = useEnvironmentQuery(
+ pullRequestEnvironment.activity({ environmentId, input: reference }),
+ );
+ const [cachedDetail, setCachedDetail] = useState(() =>
+ readPullRequestDetailSnapshot(
+ typeof window === "undefined" ? undefined : window.localStorage,
+ environmentId,
+ reference,
+ ),
+ );
+ const [cachedDetailKey, setCachedDetailKey] = useState(scopeKey);
+ if (cachedDetailKey !== scopeKey) {
+ setCachedDetailKey(scopeKey);
+ setCachedDetail(
+ detailQuery.data ??
+ readPullRequestDetailSnapshot(
+ typeof window === "undefined" ? undefined : window.localStorage,
+ environmentId,
+ reference,
+ ),
+ );
+ } else if (detailQuery.data !== null && detailQuery.data !== cachedDetail) {
+ setCachedDetail(detailQuery.data);
+ }
+ const saveDetailSnapshot = useEffectEvent((data: PullRequestDetail) => {
+ writePullRequestDetailSnapshot(
+ typeof window === "undefined" ? undefined : window.localStorage,
+ environmentId,
+ reference,
+ data,
+ );
+ });
+ useEffect(() => {
+ if (detailQuery.data !== null) saveDetailSnapshot(detailQuery.data);
+ }, [detailQuery.data]);
+ const detailSummary = pullRequestDetailSummary(detailQuery.data);
+ const observedSummary = useSharedPullRequestSummary(
+ environmentId,
+ reference,
+ detailSummary,
+ detailQuery.dataUpdatedAt,
+ );
+ const activity = activityQuery.data;
+ const resolvedCoreDetail = resolveDisplayedPullRequestDetail({
+ live: detailQuery.data,
+ cached: cachedDetail,
+ reference,
+ });
+ // Every hook runs above this line: a hook between the merge and the panel's plain readers of
+ // `detail` would cost it its cache scope.
+ const listSummary = listEntry === null ? null : pullRequestListEntryToSummary(listEntry);
+ // The list row is also published to the shared cache, but only after this commit's layout
+ // effects run, so it is compared directly rather than trusted to be there already.
+ const sharedSummary = newestPullRequestSummary(
+ resolvedCoreDetail,
+ newestPullRequestSummary(observedSummary, listSummary),
+ );
+ const coreDetail =
+ resolvedCoreDetail === null || sharedSummary === null || sharedSummary === resolvedCoreDetail
+ ? resolvedCoreDetail
+ : {
+ ...resolvedCoreDetail,
+ title: sharedSummary.title,
+ state: sharedSummary.state,
+ headBranch: sharedSummary.headBranch,
+ baseBranch: sharedSummary.baseBranch,
+ updatedAt: sharedSummary.updatedAt,
+ author: sharedSummary.author ?? resolvedCoreDetail.author,
+ additions: sharedSummary.additions ?? resolvedCoreDetail.additions,
+ deletions: sharedSummary.deletions ?? resolvedCoreDetail.deletions,
+ changedFiles: sharedSummary.changedFiles ?? resolvedCoreDetail.changedFiles,
+ mergeability: sharedSummary.mergeability ?? resolvedCoreDetail.mergeability,
+ closedAt:
+ sharedSummary.closedAt === undefined
+ ? resolvedCoreDetail.closedAt
+ : sharedSummary.closedAt,
+ mergedAt:
+ sharedSummary.mergedAt === undefined
+ ? resolvedCoreDetail.mergedAt
+ : sharedSummary.mergedAt,
+ isDraft: sharedSummary.isDraft ?? resolvedCoreDetail.isDraft,
+ };
+ const detail =
+ coreDetail === null
+ ? null
+ : {
+ ...coreDetail,
+ capabilities: canWriteSourceControl
+ ? coreDetail.capabilities
+ : {
+ ...coreDetail.capabilities,
+ reactions: false,
+ edit: { changeRequest: false, comment: false },
+ },
+ viewerPermissions: canWriteSourceControl
+ ? coreDetail.viewerPermissions
+ : {
+ ...coreDetail.viewerPermissions,
+ actions: [],
+ comment: false,
+ resolve: false,
+ verdicts: [],
+ requestReviewers: false,
+ updateMethods: [],
+ labels: false,
+ },
+ author: activity?.author ?? coreDetail.author,
+ reviewers: activity?.reviewers ?? coreDetail.reviewers,
+ comments: activity?.comments ?? [],
+ commentCount: Math.max(
+ activity?.commentCount ?? 0,
+ new Set([
+ ...(activity?.comments.map((comment) => comment.id) ?? []),
+ ...(activity?.reviewThreads.flatMap((thread) =>
+ thread.comments.map((comment) => comment.id),
+ ) ?? []),
+ ]).size,
+ ),
+ commentsTruncated: activity?.commentsTruncated ?? false,
+ reviewThreads: activity?.reviewThreads ?? [],
+ commits: activity?.commits ?? [],
+ reactions: activity?.reactions ?? [],
+ };
+ return {
+ canWriteSourceControl,
+ detailQuery,
+ activityQuery,
+ activity,
+ sharedSummary,
+ coreDetail,
+ detail,
+ };
+}