Skip to content

Commit 995fbc4

Browse files
refactor(web): pull request detail keeps its caching without "use memo"
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 73af712 commit 995fbc4

1 file changed

Lines changed: 84 additions & 71 deletions

File tree

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

Lines changed: 84 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ import {
1414
AuthSourceControlWriteScope,
1515
type EnvironmentId,
1616
type PullRequestAction,
17-
type PullRequestActivity,
1817
type PullRequestDetail,
1918
type PullRequestMergeMethod,
20-
type PullRequestSummary,
2119
type PullRequestListEntry,
2220
type PullRequestUpdateMethod,
2321
type PullRequestRef,
@@ -400,21 +398,78 @@ function resolvePullRequestReference(
400398
};
401399
}
402400

401+
function pullRequestRefKey(reference: PullRequestRef) {
402+
return `${reference.projectId}:${reference.host ?? ""}:${reference.repository}#${reference.number}`;
403+
}
404+
403405
function pullRequestDetailSummary(data: PullRequestDetail | null) {
404406
return data === null ? null : { ...data, checksState: pullRequestChecksState(data.checks) };
405407
}
406408

407-
function useDisplayedPullRequestDetail(
408-
resolvedCoreDetail: PullRequestDetail | null,
409-
observedSummary: PullRequestSummary | null,
409+
/**
410+
* Loads a pull request's detail and activity and merges them, with the snapshot cache, the shared
411+
* summary and the list row, into the detail the panel shows.
412+
*/
413+
function usePullRequestDetail(
414+
environmentId: EnvironmentId,
415+
reference: PullRequestRef,
410416
listEntry: PullRequestListEntry | null,
411-
activity: PullRequestActivity | null,
412-
canWriteSourceControl: boolean,
413417
) {
414-
// Kept: hooks consume `detail` (the branch refs query keyed on `detail.workspaceRoot`, the stack
415-
// query, the guide writer) and plain calls read it afterwards (`isStackedPullRequestBase`), so a
416-
// body-level derivation gets no cache scope. A hook's return value is frozen and stays cached.
417-
"use memo";
418+
const scopeKey = `${environmentId}:${pullRequestRefKey(reference)}`;
419+
const canWriteSourceControl = useEnvironmentScope(environmentId, AuthSourceControlWriteScope);
420+
const detailQuery = useEnvironmentQuery(
421+
pullRequestEnvironment.detail({ environmentId, input: reference }),
422+
);
423+
const activityQuery = useEnvironmentQuery(
424+
pullRequestEnvironment.activity({ environmentId, input: reference }),
425+
);
426+
const [cachedDetail, setCachedDetail] = useState(() =>
427+
readPullRequestDetailSnapshot(
428+
typeof window === "undefined" ? undefined : window.localStorage,
429+
environmentId,
430+
reference,
431+
),
432+
);
433+
const [cachedDetailKey, setCachedDetailKey] = useState(scopeKey);
434+
if (cachedDetailKey !== scopeKey) {
435+
setCachedDetailKey(scopeKey);
436+
setCachedDetail(
437+
detailQuery.data ??
438+
readPullRequestDetailSnapshot(
439+
typeof window === "undefined" ? undefined : window.localStorage,
440+
environmentId,
441+
reference,
442+
),
443+
);
444+
} else if (detailQuery.data !== null && detailQuery.data !== cachedDetail) {
445+
setCachedDetail(detailQuery.data);
446+
}
447+
const saveDetailSnapshot = useEffectEvent((data: PullRequestDetail) => {
448+
writePullRequestDetailSnapshot(
449+
typeof window === "undefined" ? undefined : window.localStorage,
450+
environmentId,
451+
reference,
452+
data,
453+
);
454+
});
455+
useEffect(() => {
456+
if (detailQuery.data !== null) saveDetailSnapshot(detailQuery.data);
457+
}, [detailQuery.data]);
458+
const detailSummary = pullRequestDetailSummary(detailQuery.data);
459+
const observedSummary = useSharedPullRequestSummary(
460+
environmentId,
461+
reference,
462+
detailSummary,
463+
detailQuery.dataUpdatedAt,
464+
);
465+
const activity = activityQuery.data;
466+
const resolvedCoreDetail = resolveDisplayedPullRequestDetail({
467+
live: detailQuery.data,
468+
cached: cachedDetail,
469+
reference,
470+
});
471+
// Every hook runs above this line: a hook between the merge and the panel's plain readers of
472+
// `detail` would cost it its cache scope.
418473
const listSummary = listEntry === null ? null : pullRequestListEntryToSummary(listEntry);
419474
// The list row is also published to the shared cache, but only after this commit's layout
420475
// effects run, so it is compared directly rather than trusted to be there already.
@@ -480,7 +535,15 @@ function useDisplayedPullRequestDetail(
480535
commits: activity?.commits ?? [],
481536
reactions: activity?.reactions ?? [],
482537
};
483-
return { sharedSummary, coreDetail, detail };
538+
return {
539+
canWriteSourceControl,
540+
detailQuery,
541+
activityQuery,
542+
activity,
543+
sharedSummary,
544+
coreDetail,
545+
detail,
546+
};
484547
}
485548

486549
function pullRequestMarkdownContext(
@@ -556,7 +619,7 @@ export function PullRequestDetailPanel({
556619
reference.repository,
557620
reference.number,
558621
);
559-
const pullRequestKey = `${reference.projectId}:${reference.host ?? ""}:${reference.repository}#${reference.number}`;
622+
const pullRequestKey = pullRequestRefKey(reference);
560623
const matchingListEntry =
561624
listEntry?.projectId === reference.projectId &&
562625
listEntry.repository.toLowerCase() === reference.repository.toLowerCase() &&
@@ -666,66 +729,16 @@ export function PullRequestDetailPanel({
666729
};
667730
const inlineConfirm = useInlineActionConfirm(pullRequestKey);
668731
const [handoff, setHandoff] = useState<string | null>(null);
669-
const canWriteSourceControl = useEnvironmentScope(environmentId, AuthSourceControlWriteScope);
670-
const detailQuery = useEnvironmentQuery(
671-
pullRequestEnvironment.detail({ environmentId, input: reference }),
672-
);
673-
const activityQuery = useEnvironmentQuery(
674-
pullRequestEnvironment.activity({ environmentId, input: reference }),
675-
);
676732
const turnRefresh = usePullRequestTurnRefresh(environmentId);
677-
const [cachedDetail, setCachedDetail] = useState(() =>
678-
readPullRequestDetailSnapshot(
679-
typeof window === "undefined" ? undefined : window.localStorage,
680-
environmentId,
681-
reference,
682-
),
683-
);
684-
const [cachedDetailKey, setCachedDetailKey] = useState(tabScopeKey);
685-
if (cachedDetailKey !== tabScopeKey) {
686-
setCachedDetailKey(tabScopeKey);
687-
setCachedDetail(
688-
detailQuery.data ??
689-
readPullRequestDetailSnapshot(
690-
typeof window === "undefined" ? undefined : window.localStorage,
691-
environmentId,
692-
reference,
693-
),
694-
);
695-
} else if (detailQuery.data !== null && detailQuery.data !== cachedDetail) {
696-
setCachedDetail(detailQuery.data);
697-
}
698-
const saveDetailSnapshot = useEffectEvent((data: PullRequestDetail) => {
699-
writePullRequestDetailSnapshot(
700-
typeof window === "undefined" ? undefined : window.localStorage,
701-
environmentId,
702-
reference,
703-
data,
704-
);
705-
});
706-
useEffect(() => {
707-
if (detailQuery.data !== null) saveDetailSnapshot(detailQuery.data);
708-
}, [detailQuery.data]);
709-
const resolvedCoreDetail = resolveDisplayedPullRequestDetail({
710-
live: detailQuery.data,
711-
cached: cachedDetail,
712-
reference,
713-
});
714-
const detailSummary = pullRequestDetailSummary(detailQuery.data);
715-
const observedSummary = useSharedPullRequestSummary(
716-
environmentId,
717-
reference,
718-
detailSummary,
719-
detailQuery.dataUpdatedAt,
720-
);
721-
const activity = activityQuery.data;
722-
const { sharedSummary, coreDetail, detail } = useDisplayedPullRequestDetail(
723-
resolvedCoreDetail,
724-
observedSummary,
725-
matchingListEntry,
726-
activity,
733+
const {
727734
canWriteSourceControl,
728-
);
735+
detailQuery,
736+
activityQuery,
737+
activity,
738+
sharedSummary,
739+
coreDetail,
740+
detail,
741+
} = usePullRequestDetail(environmentId, reference, matchingListEntry);
729742
const handoffSummary = detail ?? sharedSummary;
730743
const keybindings = useAtomValue(primaryServerKeybindingsAtom);
731744
const { copyToClipboard: copyReference } = useCopyToClipboard<string>({

0 commit comments

Comments
 (0)