@@ -63,13 +63,7 @@ import { useEnvironments } from "~/state/environments";
6363import { useEnvironmentQuery } from "~/state/query" ;
6464import { readEnvironmentScope , useEnvironmentScope } from "~/state/session" ;
6565import { 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" ;
7367import { useAtomCommand } from "~/state/use-atom-command" ;
7468import { PullRequestStackMenu } from "./PullRequestStackMenu" ;
7569import { PullRequestChangeBreakdownStat } from "./PullRequestChangeBreakdownStat" ;
@@ -90,6 +84,7 @@ import {
9084} from "./PullRequestDetailLayout" ;
9185import { PullRequestDetailGhost , PullRequestTimelineGhost } from "./PullRequestGhosts" ;
9286import { PullRequestCopyableCode } from "./PullRequestCopyableCode" ;
87+ import { pullRequestRefKey , usePullRequestDetail } from "./usePullRequestDetail" ;
9388import { PullRequestActionsMenu } from "./PullRequestActionsMenu" ;
9489import {
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" ;
146138import { resolvePickableEnvironments } from "./pullRequestProjectAssignment.logic" ;
147139import { 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-
400236function pullRequestMarkdownContext (
401237 hostClient : ReturnType < typeof sourceControlClients . get > ,
402238 repositoryUrl : string | null ,
0 commit comments