-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(replay): Persist has-viewed state to the server when replays are…
… seen (#68743) Depends on #67951 Relates to getsentry/team-replay#19 Relates to #64924
- Loading branch information
Showing
4 changed files
with
97 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import {useCallback} from 'react'; | ||
|
||
import type {ApiResult} from 'sentry/api'; | ||
import {fetchMutation, useMutation, useQueryClient} from 'sentry/utils/queryClient'; | ||
import useApi from 'sentry/utils/useApi'; | ||
|
||
type TData = unknown; | ||
type TError = unknown; | ||
type TVariables = {projectSlug: string; replayId: string}; | ||
type TContext = unknown; | ||
|
||
import useOrganization from 'sentry/utils/useOrganization'; | ||
|
||
export default function useMarkReplayViewed() { | ||
const organization = useOrganization(); | ||
const api = useApi({ | ||
persistInFlight: false, | ||
}); | ||
const queryClient = useQueryClient(); | ||
|
||
const updateCache = useCallback( | ||
({replayId}: TVariables, hasViewed: boolean) => { | ||
const cache = queryClient.getQueryCache(); | ||
const cachedResponses = cache.findAll([ | ||
`/organizations/${organization.slug}/replays/${replayId}/`, | ||
]); | ||
cachedResponses.forEach(cached => { | ||
const [data, ...rest] = cached.state.data as ApiResult<{ | ||
data: Record<string, unknown>; | ||
}>; | ||
cached.setData([ | ||
{ | ||
data: { | ||
...data.data, | ||
has_viewed: hasViewed, | ||
}, | ||
}, | ||
...rest, | ||
]); | ||
}); | ||
}, | ||
[organization.slug, queryClient] | ||
); | ||
|
||
return useMutation<TData, TError, TVariables, TContext>({ | ||
onMutate: variables => { | ||
updateCache(variables, true); | ||
}, | ||
mutationFn: ({projectSlug, replayId}) => { | ||
const url = `/projects/${organization.slug}/${projectSlug}/replays/${replayId}/viewed-by/`; | ||
return fetchMutation(api)(['POST', url]); | ||
}, | ||
onError: (_error, variables) => { | ||
updateCache(variables, false); | ||
}, | ||
cacheTime: 0, | ||
retry: false, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters