TypeScript
refactor(web): linked pull request status keeps its caching without "use memo" hooks (#20781)
Julius Marmingebcd3c12f4683
diff --git a/apps/web/src/components/ThreadStatusIndicators.tsx b/apps/web/src/components/ThreadStatusIndicators.tsxindex fa0584686a4..a697382b1de 100644--- a/apps/web/src/components/ThreadStatusIndicators.tsx+++ b/apps/web/src/components/ThreadStatusIndicators.tsx@@ -95,11 +95,14 @@ export function useLinkedThreadPullRequest(branchPullRequest?: ThreadLinkedPullRequest | null,): LinkedThreadPullRequestStatus | null {const supportsLinks = useSupportsMultiplePullRequests(environmentId);- const { current, currentStatus } = useCurrentPullRequestLink(supportsLinks, pullRequests);+ const current = supportsLinks ? resolveThreadCurrentPullRequestLink(pullRequests ?? []) : null;+ // Callers key on the returned status, so it stays cached across the query hooks.+ const currentStatus = current === null ? null : linkedPullRequestSnapshotStatus(current);const fallback =current === null ? ((!supportsLinks ? linkedPullRequest : null) ?? branchPullRequest) : null;- // A sidebar row must not touch the summary cache on every render.- const reference = useLinkedPullRequestReference(fallback);+ // The query atom and shared summary effect key on this object, and a sidebar row+ // must not touch the summary cache on every render.+ const reference = fallback == null ? null : linkedPullRequestReference(fallback);const queried = useEnvironmentQuery(!enabled || environmentId === null || reference === null? null@@ -121,24 +124,7 @@ export function useLinkedThreadPullRequest(};}-// Callers key on the returned status, so it stays stable across the query hooks.-function useCurrentPullRequestLink(- supportsLinks: boolean,- pullRequests: ReadonlyArray<ThreadPullRequestLink> | undefined,-) {- "use memo";- const current = supportsLinks ? resolveThreadCurrentPullRequestLink(pullRequests ?? []) : null;- return {- current,- currentStatus: current === null ? null : linkedPullRequestSnapshotStatus(current),- };-}--// The query atom and shared summary effect key on this object, so it stays-// stable per link across the hooks that read it.-function useLinkedPullRequestReference(fallback: ThreadLinkedPullRequest | null | undefined) {- "use memo";- if (fallback == null) return null;+function linkedPullRequestReference(fallback: ThreadLinkedPullRequest) {const host = parseChangeRequestUrl(fallback.url)?.host;return { ...fallback, ...(host === undefined ? {} : { host }) };}diff --git a/apps/web/src/state/pullRequests.ts b/apps/web/src/state/pullRequests.tsindex d111510d63b..64d761f4d53 100644--- a/apps/web/src/state/pullRequests.ts+++ b/apps/web/src/state/pullRequests.ts@@ -137,12 +137,11 @@ export function newestPullRequestObservation(}function observePullRequestSummary(- environmentId: EnvironmentId,- reference: PullRequestRef,+ key: string,summary: PullRequestSummary,observedAt: number,): void {- const atom = observedPullRequestSummaryAtom(pullRequestSummaryKey(environmentId, reference));+ const atom = observedPullRequestSummaryAtom(key);appAtomRegistry.modify(atom, (previous) => {const next = newestPullRequestObservation(previous, { summary, observedAt });return next === previous ? [false, previous] : [true, next];@@ -216,11 +215,11 @@ export function useSharedPullRequestSummary(: savedPullRequestKey(environmentId, reference),),);+ // Keyed structurally so a fresh reference object for the same pull request is a no-op.useLayoutEffect(() => {- if (environmentId === null || reference === null || current === null || observedAt === null)- return;- observePullRequestSummary(environmentId, reference, current, observedAt);- }, [current, environmentId, reference, observedAt]);+ if (key === "none" || current === null || observedAt === null) return;+ observePullRequestSummary(key, current, observedAt);+ }, [current, key, observedAt]);const summary =newestPullRequestObservation(observed,@@ -343,13 +342,12 @@ export function usePullRequestList(for (const [environmentId, answer, observedAt] of query.observations) {for (const entry of answer.entries) {observePullRequestSummary(- environmentId,- {+ pullRequestSummaryKey(environmentId, {projectId: entry.projectId,host: entry.host,repository: entry.repository,number: entry.number,- },+ }),pullRequestListEntryToSummary(entry),observedAt,);