Skip to content

Commit 9ef7939

Browse files
fix(replay): Fix error state quickly flashing when loading replay (#72823)
This fixes the error state that quickly flashes when loading a replay. The `useReplayData` hook makes ~5 requests, but they do not all happen in parallel. The fetching state ends up looking like the following: * fetch replay, fetch extra errors, fetch platform ("fetching" == true) * above requests finish ("fetching" == false) * fetch attachments, fetch extra errors, fetch platform errors ("fetching" == true) This intermittent fetching state means that we have [components that erroneously render](https://github.com/getsentry/sentry/blob/78334b95bc5ff69816fcaeafff8f67fadf0727c7/static/app/components/replays/replayView.tsx#L43) an error state because it believes API requests have completed and that the data is missing. To test: Loading a replay should no longer flicker and should no longer flicker with a red error screen. We use `useReplayReader` in quite a few places besides replay details, but I think all of them should benefit from this change. Closes #70077 --------- Co-authored-by: Michelle Zhang <[email protected]>
1 parent cd5e7e5 commit 9ef7939

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

Diff for: static/app/utils/replays/hooks/useReplayData.tsx

+27-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {useCallback, useMemo} from 'react';
1+
import {useCallback, useMemo, useRef} from 'react';
22

33
import {ALL_ACCESS_PROJECTS} from 'sentry/constants/pageFilters';
44
import useFetchParallelPages from 'sentry/utils/api/useFetchParallelPages';
@@ -76,8 +76,8 @@ function useReplayData({
7676
errorsPerPage = 50,
7777
segmentsPerPage = 100,
7878
}: Options): Result {
79+
const hasFetchedAttachments = useRef(false);
7980
const projects = useProjects();
80-
8181
const queryClient = useQueryClient();
8282

8383
// Fetch every field of the replay. The TS type definition lists every field
@@ -120,13 +120,16 @@ function useReplayData({
120120
[orgSlug, projectSlug, replayId]
121121
);
122122

123-
const {pages: attachmentPages, isFetching: isFetchingAttachments} =
124-
useFetchParallelPages({
125-
enabled: !fetchReplayError && Boolean(projectSlug) && Boolean(replayRecord),
126-
hits: replayRecord?.count_segments ?? 0,
127-
getQueryKey: getAttachmentsQueryKey,
128-
perPage: segmentsPerPage,
129-
});
123+
const {
124+
pages: attachmentPages,
125+
isFetching: isFetchingAttachments,
126+
error: fetchAttachmentsError,
127+
} = useFetchParallelPages({
128+
enabled: !fetchReplayError && Boolean(projectSlug) && Boolean(replayRecord),
129+
hits: replayRecord?.count_segments ?? 0,
130+
getQueryKey: getAttachmentsQueryKey,
131+
perPage: segmentsPerPage,
132+
});
130133

131134
const getErrorsQueryKey = useCallback(
132135
({cursor, per_page}): ApiQueryKey => {
@@ -230,12 +233,25 @@ function useReplayData({
230233
}, [orgSlug, replayId, projectSlug, queryClient]);
231234

232235
return useMemo(() => {
236+
// This hook can enter a state where `fetching` below is false
237+
// before it is entirely ready (i.e. it has not fetched
238+
// attachemnts yet). This can cause downstream components to
239+
// think it is no longer fetching and will display an error
240+
// because there are no attachments. The below will require
241+
// that we have attempted to fetch an attachment once (or it
242+
// errors) before we toggle fetching state to false.
243+
hasFetchedAttachments.current =
244+
hasFetchedAttachments.current || isFetchingAttachments;
245+
233246
const fetching =
234247
isFetchingReplay ||
235248
isFetchingAttachments ||
236249
isFetchingErrors ||
237250
isFetchingExtraErrors ||
238-
isFetchingPlatformErrors;
251+
isFetchingPlatformErrors ||
252+
(!hasFetchedAttachments.current &&
253+
!fetchAttachmentsError &&
254+
Boolean(replayRecord?.count_segments));
239255

240256
const allErrors = errorPages
241257
.concat(extraErrorPages)
@@ -256,6 +272,7 @@ function useReplayData({
256272
errorPages,
257273
extraErrorPages,
258274
fetchReplayError,
275+
fetchAttachmentsError,
259276
isFetchingAttachments,
260277
isFetchingErrors,
261278
isFetchingExtraErrors,

0 commit comments

Comments
 (0)