This repository was archived by the owner on Aug 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathInboxReportDetailGate.tsx
More file actions
185 lines (173 loc) · 6.52 KB
/
Copy pathInboxReportDetailGate.tsx
File metadata and controls
185 lines (173 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import {
isDismissedReport,
isPullRequestReport,
isReportTabReport,
} from "@posthog/core/inbox/reportMembership";
import { Spinner } from "@posthog/quill";
import type { SignalReport } from "@posthog/shared/types";
import { DetailBackLink } from "@posthog/ui/features/inbox/components/DetailBackLink";
import { useInboxReportById } from "@posthog/ui/features/inbox/hooks/useInboxReports";
import {
type InboxDetailTab,
useReportOpenTracker,
} from "@posthog/ui/features/inbox/hooks/useReportOpenTracker";
import { Flex, Text } from "@radix-ui/themes";
import { useNavigate } from "@tanstack/react-router";
import { type ReactNode, useEffect } from "react";
interface InboxReportDetailGateProps {
reportId: string;
cachedReport?: SignalReport | null;
backTo:
| "/code/inbox/pulls"
| "/code/inbox/reports"
| "/code/inbox/runs"
| "/code/inbox/dismissed";
backLabel: string;
missingCopy: string;
children: (report: SignalReport) => ReactNode;
}
type InboxDetailRoute =
| "/code/inbox/pulls/$reportId"
| "/code/inbox/reports/$reportId"
| "/code/inbox/runs/$reportId"
| "/code/inbox/dismissed/$reportId";
/**
* Detail route a non-suppressed report belongs on, by the same tab-membership
* predicates the inbox tabs use: Pulls when a PR exists, Reports when it belongs
* to the Reports tab, otherwise Runs. `isReportTabReport` already excludes
* `failed` and in-flight runs, so failed/finished and live runs both fall
* through to Runs — the only tab that actually lists them.
*/
function nonSuppressedDetailRoute(report: SignalReport): InboxDetailRoute {
if (isPullRequestReport(report)) return "/code/inbox/pulls/$reportId";
if (isReportTabReport(report)) return "/code/inbox/reports/$reportId";
return "/code/inbox/runs/$reportId";
}
/**
* Shared loading + missing-report shell for inbox detail screens. The actual
* detail body is rendered by the `children` render prop once the report is
* resolved (either from the fresh query or from the cached/seeded report).
*/
export function InboxReportDetailGate({
reportId,
cachedReport = null,
backTo,
backLabel,
missingCopy,
children,
}: InboxReportDetailGateProps) {
const navigate = useNavigate();
const {
data: report,
isLoading,
isFetching,
isFetchedAfterMount,
} = useInboxReportById(reportId);
const resolvedReport = report ?? cachedReport;
// Keep the report on the route that matches its status. A status↔route mismatch
// happens when a URL goes stale — browser history, a bookmark, a copied deep
// link, or a status change in another session. An archived report (suppressed or
// resolved) reached via a /pulls, /reports, or /runs URL would otherwise render
// that tab's full triage actions (archive, discuss, create PR) on an
// out-of-pipeline report; a restored report reached via /dismissed would offer
// Restore and silently re-queue it (READY/RESOLVED → POTENTIAL is an allowed
// server-side transition). Redirect across that dismissed↔pipeline boundary,
// gated on a settled fetch so we act on the confirmed status rather than a
// pre-change cache snapshot (the detail query forces a fresh fetch on mount via
// `initialDataUpdatedAt: 0`). Both terminal states belong on the Archive route,
// so resolved cards keep their reference-only detail view instead of being
// bounced to Runs.
const onDismissedRoute = backTo === "/code/inbox/dismissed";
const isArchived =
resolvedReport != null && isDismissedReport(resolvedReport);
let redirectTo: InboxDetailRoute | null = null;
if (resolvedReport && !isFetching) {
if (isArchived && !onDismissedRoute) {
redirectTo = "/code/inbox/dismissed/$reportId";
} else if (!isArchived && onDismissedRoute) {
redirectTo = nonSuppressedDetailRoute(resolvedReport);
}
}
// The redirect above only fires once the fetch settles, so on a triage route we
// still hold an unconfirmed cached/seeded status during the forced post-mount
// fetch. Rendering the children then would briefly expose full triage actions
// (create PR, discuss, archive) for a report that another session has already
// suppressed, before the redirect kicks in. Hold the spinner until that same
// fetch settles. The Archive route stays render-from-cache (the PR's instant-open
// path): it's read-only and its one action, Restore, re-checks status server-side.
const statusUnconfirmed =
!onDismissedRoute && isFetching && !isFetchedAfterMount;
const redirectReportId = resolvedReport?.id;
useEffect(() => {
if (!redirectTo || !redirectReportId) return;
navigate({
to: redirectTo,
params: { reportId: redirectReportId },
replace: true,
});
}, [redirectTo, redirectReportId, navigate]);
if ((isLoading && !resolvedReport) || statusUnconfirmed) {
return (
<Flex align="center" justify="center" className="py-16">
<Spinner />
</Flex>
);
}
if (redirectTo) {
// Redirecting across the dismissed↔pipeline boundary; render nothing
// meaningful for the frame we're leaving.
return (
<Flex align="center" justify="center" className="py-16">
<Spinner />
</Flex>
);
}
if (!resolvedReport) {
return (
<Flex direction="column" className="h-full min-h-0">
<Flex
direction="column"
gap="3"
className="border-(--gray-5) border-b px-6 py-6"
>
<DetailBackLink to={backTo} label={backLabel} />
<Text className="text-[13px] text-gray-11">{missingCopy}</Text>
</Flex>
</Flex>
);
}
const trackTab = tabFromBackTo(backTo);
return (
<>
{trackTab && <ReportOpenTracker report={resolvedReport} tab={trackTab} />}
{children(resolvedReport)}
</>
);
}
/**
* The Dismissed tab isn't part of the triage funnel and isn't a tracked
* `InboxDetailTab` (its rank would be measured against the wrong list), so it
* returns `null` and the open/close engagement events are skipped for it.
*/
function tabFromBackTo(
backTo: InboxReportDetailGateProps["backTo"],
): InboxDetailTab | null {
if (backTo === "/code/inbox/pulls") return "pulls";
if (backTo === "/code/inbox/runs") return "runs";
if (backTo === "/code/inbox/dismissed") return null;
return "reports";
}
/**
* Mounts only once a report is resolved, so the OPENED/CLOSED engagement events
* bracket the time the detail body is actually on screen. Renders nothing.
*/
function ReportOpenTracker({
report,
tab,
}: {
report: SignalReport;
tab: InboxDetailTab;
}) {
useReportOpenTracker(report, tab);
return null;
}