@devries-cameron20 / t3code Mirror · read-only / commits / 55ca0bb65e
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 23, 05:30 UTC

refactor(web): pull request detail keeps its caching without "use memo" (#20811)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 17, 2026 at 00:30 UTC 55ca0bb65ee5b668f309e6f57f9ee7e936173df0
1 file changed +84 -71
apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx
+84 -71
@@ -14,10 +14,8 @@ import {
14 AuthSourceControlWriteScope,
15 type EnvironmentId,
16 type PullRequestAction,
17 - type PullRequestActivity,
17 type PullRequestDetail,
18 type PullRequestMergeMethod,
20 - type PullRequestSummary,
19 type PullRequestListEntry,
20 type PullRequestUpdateMethod,
21 type PullRequestRef,
@@ -400,21 +398,78 @@ function resolvePullRequestReference(
398 };
399 }
400
401 +function pullRequestRefKey(reference: PullRequestRef) {
402 + return `${reference.projectId}:${reference.host ?? ""}:${reference.repository}#${reference.number}`;
403 +}
404 +
405 function pullRequestDetailSummary(data: PullRequestDetail | null) {
406 return data === null ? null : { ...data, checksState: pullRequestChecksState(data.checks) };
407 }
408
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,
416 listEntry: PullRequestListEntry | null,
411 - activity: PullRequestActivity | null,
412 - canWriteSourceControl: boolean,
417 ) {
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.
473 const listSummary = listEntry === null ? null : pullRequestListEntryToSummary(listEntry);
474 // The list row is also published to the shared cache, but only after this commit's layout
475 // effects run, so it is compared directly rather than trusted to be there already.
@@ -480,7 +535,15 @@ function useDisplayedPullRequestDetail(
535 commits: activity?.commits ?? [],
536 reactions: activity?.reactions ?? [],
537 };
483 - return { sharedSummary, coreDetail, detail };
538 + return {
539 + canWriteSourceControl,
540 + detailQuery,
541 + activityQuery,
542 + activity,
543 + sharedSummary,
544 + coreDetail,
545 + detail,
546 + };
547 }
548
549 function pullRequestMarkdownContext(
@@ -556,7 +619,7 @@ export function PullRequestDetailPanel({
619 reference.repository,
620 reference.number,
621 );
559 - const pullRequestKey = `${reference.projectId}:${reference.host ?? ""}:${reference.repository}#${reference.number}`;
622 + const pullRequestKey = pullRequestRefKey(reference);
623 const matchingListEntry =
624 listEntry?.projectId === reference.projectId &&
625 listEntry.repository.toLowerCase() === reference.repository.toLowerCase() &&
@@ -666,66 +729,16 @@ export function PullRequestDetailPanel({
729 };
730 const inlineConfirm = useInlineActionConfirm(pullRequestKey);
731 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 - );
732 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 {
734 canWriteSourceControl,
728 - );
735 + detailQuery,
736 + activityQuery,
737 + activity,
738 + sharedSummary,
739 + coreDetail,
740 + detail,
741 + } = usePullRequestDetail(environmentId, reference, matchingListEntry);
742 const handoffSummary = detail ?? sharedSummary;
743 const keybindings = useAtomValue(primaryServerKeybindingsAtom);
744 const { copyToClipboard: copyReference } = useCopyToClipboard<string>({