Skip to content

Commit

Permalink
ref(replay): Move some project code into useReplayProjectSlug (#82516)
Browse files Browse the repository at this point in the history
Extract some code that converts a projectId into a projectSlug.

I'm extracting it because it's something to be reused between
`components/replays/replayPlayer.tsx` and
`components/replays/player/replayPlayer.tsx`. Also, relying on the
ProjectStore as this does is something that'll need to be refactored in
the future because loading the ProjectStore is slow, so it's nice to
separate this out.
  • Loading branch information
ryan953 authored Dec 23, 2024
1 parent 1dcf3ac commit 819b6fb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
10 changes: 2 additions & 8 deletions static/app/utils/replays/hooks/useReplayData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import {DiscoverDatasets} from 'sentry/utils/discover/types';
import parseLinkHeader from 'sentry/utils/parseLinkHeader';
import type {ApiQueryKey} from 'sentry/utils/queryClient';
import {useApiQuery, useQueryClient} from 'sentry/utils/queryClient';
import {useReplayProjectSlug} from 'sentry/utils/replays/hooks/useReplayProjectSlug';
import {mapResponseToReplayRecord} from 'sentry/utils/replays/replayDataUtils';
import type RequestError from 'sentry/utils/requestError/requestError';
import useProjects from 'sentry/utils/useProjects';
import type {ReplayError, ReplayRecord} from 'sentry/views/replays/types';

type Options = {
Expand Down Expand Up @@ -77,7 +77,6 @@ function useReplayData({
segmentsPerPage = 100,
}: Options): Result {
const hasFetchedAttachments = useRef(false);
const projects = useProjects();
const queryClient = useQueryClient();

// Fetch every field of the replay. The TS type definition lists every field
Expand All @@ -98,12 +97,7 @@ function useReplayData({
[replayData?.data]
);

const projectSlug = useMemo(() => {
if (!replayRecord) {
return null;
}
return projects.projects.find(p => p.id === replayRecord.project_id)?.slug ?? null;
}, [replayRecord, projects.projects]);
const projectSlug = useReplayProjectSlug({replayRecord});

const getAttachmentsQueryKey = useCallback(
({cursor, per_page}): ApiQueryKey => {
Expand Down
18 changes: 18 additions & 0 deletions static/app/utils/replays/hooks/useReplayProjectSlug.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {useMemo} from 'react';

import useProjects from 'sentry/utils/useProjects';
import type {ReplayRecord} from 'sentry/views/replays/types';

interface Props {
replayRecord: ReplayRecord | undefined;
}

export function useReplayProjectSlug({replayRecord}: Props) {
const projects = useProjects();
return useMemo(() => {
if (!replayRecord) {
return null;
}
return projects.projects.find(p => p.id === replayRecord.project_id)?.slug ?? null;
}, [replayRecord, projects]);
}

0 comments on commit 819b6fb

Please sign in to comment.