Skip to content

Commit 310ea71

Browse files
authored
ref(onboarding): fix cache when platforms are the same (#60059)
The documentation needs to be re-fetched when the project changes so that they dsn in the response is updated. Fixes #59965
1 parent bb1cbec commit 310ea71

File tree

4 files changed

+60
-46
lines changed

4 files changed

+60
-46
lines changed

static/app/components/onboardingWizard/useOnboardingDocs.tsx

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {useEffect, useState} from 'react';
1+
import {useEffect, useRef, useState} from 'react';
22
import * as Sentry from '@sentry/react';
33

44
import {loadDocs} from 'sentry/actionCreators/projects';
@@ -17,44 +17,44 @@ type Options = {
1717
};
1818

1919
function useOnboardingDocs({docKeys, isPlatformSupported, project}: Options) {
20-
const organization = useOrganization();
2120
const api = useApi();
21+
const organization = useOrganization();
22+
23+
const loadingDocsRef = useRef<Record<string, boolean>>(INITIAL_LOADING_DOCS);
24+
const docContentsRef = useRef<Record<string, string>>(INITIAL_DOC_CONTENTS);
2225

23-
const [loadingDocs, setLoadingDocs] =
24-
useState<Record<string, boolean>>(INITIAL_LOADING_DOCS);
2526
const [docContents, setDocContents] =
2627
useState<Record<string, string>>(INITIAL_DOC_CONTENTS);
2728

28-
const currentPlatform = project.platform
29-
? platforms.find(p => p.id === project.platform)
30-
: undefined;
29+
docContentsRef.current = docContents;
3130

3231
useEffect(() => {
3332
if (!isPlatformSupported) {
34-
if (loadingDocs !== INITIAL_LOADING_DOCS) {
35-
setLoadingDocs(INITIAL_LOADING_DOCS);
33+
if (loadingDocsRef.current !== INITIAL_LOADING_DOCS) {
34+
loadingDocsRef.current = INITIAL_LOADING_DOCS;
3635
}
37-
if (docContents !== INITIAL_DOC_CONTENTS) {
36+
if (docContentsRef.current !== INITIAL_DOC_CONTENTS) {
3837
setDocContents(INITIAL_DOC_CONTENTS);
3938
}
40-
return;
39+
return undefined;
4140
}
4241

42+
let cancelRequest = false;
43+
4344
docKeys.forEach(docKey => {
44-
if (docKey in loadingDocs) {
45+
if (docKey in loadingDocsRef.current) {
4546
// If a documentation content is loading, we should not attempt to fetch it again.
4647
// otherwise, if it's not loading, we should only fetch at most once.
4748
// Any errors that occurred will be captured via Sentry.
4849
return;
4950
}
5051

51-
const setLoadingDoc = (loadingState: boolean) =>
52-
setLoadingDocs(prevState => {
53-
return {
54-
...prevState,
55-
[docKey]: loadingState,
56-
};
57-
});
52+
const setLoadingDoc = (loadingState: boolean) => {
53+
loadingDocsRef.current = {
54+
...loadingDocsRef.current,
55+
[docKey]: loadingState,
56+
};
57+
};
5858

5959
const setDocContent = (docContent: string | undefined) =>
6060
setDocContents(prevState => {
@@ -80,25 +80,33 @@ function useOnboardingDocs({docKeys, isPlatformSupported, project}: Options) {
8080
platform: docKey as any,
8181
})
8282
.then(({html}) => {
83-
setDocContent(html as string);
83+
if (cancelRequest) {
84+
return;
85+
}
8486
setLoadingDoc(false);
87+
setDocContent(html as string);
8588
})
8689
.catch(error => {
90+
if (cancelRequest) {
91+
return;
92+
}
8793
Sentry.captureException(error);
88-
setDocContent(undefined);
8994
setLoadingDoc(false);
95+
setDocContent(undefined);
9096
});
9197
});
92-
}, [
93-
currentPlatform,
94-
docKeys,
95-
isPlatformSupported,
96-
api,
97-
loadingDocs,
98-
organization.slug,
99-
project.slug,
100-
docContents,
101-
]);
98+
99+
return () => {
100+
cancelRequest = true;
101+
for (const key of docKeys) {
102+
delete loadingDocsRef.current[key];
103+
}
104+
};
105+
}, [docKeys, isPlatformSupported, api, organization.slug, project]);
106+
107+
const currentPlatform = project.platform
108+
? platforms.find(p => p.id === project.platform)
109+
: undefined;
102110

103111
if (!currentPlatform || !isPlatformSupported) {
104112
return {
@@ -108,23 +116,20 @@ function useOnboardingDocs({docKeys, isPlatformSupported, project}: Options) {
108116
};
109117
}
110118

111-
const isLoading = Boolean(
112-
docKeys?.some(key => {
113-
if (key in loadingDocs) {
114-
return !!loadingDocs[key];
119+
const isLoading =
120+
docKeys &&
121+
docKeys.some(key => {
122+
if (key in loadingDocsRef.current) {
123+
return !!loadingDocsRef.current[key];
115124
}
116125
return true;
117-
})
118-
);
119-
120-
const hasOnboardingContents = Boolean(
121-
docKeys?.every(key => typeof docContents[key] === 'string')
122-
);
126+
});
123127

124128
return {
125129
docKeys,
126130
isLoading,
127-
hasOnboardingContents,
131+
hasOnboardingContents:
132+
docKeys && docKeys.every(key => typeof docContents[key] === 'string'),
128133
docContents,
129134
};
130135
}

static/app/components/performanceOnboarding/sidebar.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Fragment, useEffect, useState} from 'react';
1+
import {Fragment, useEffect, useMemo, useState} from 'react';
22
import styled from '@emotion/styled';
33

44
import HighlightTopRightPattern from 'sentry-images/pattern/highlight-top-right.svg';
@@ -182,7 +182,10 @@ function OnboardingContent({currentProject}: {currentProject: Project}) {
182182
? platforms.find(p => p.id === currentProject.platform)
183183
: undefined;
184184

185-
const docKeys = currentPlatform ? generateDocKeys(currentPlatform.id) : [];
185+
const docKeys = useMemo(() => {
186+
return currentPlatform ? generateDocKeys(currentPlatform.id) : [];
187+
}, [currentPlatform]);
188+
186189
const {docContents, isLoading, hasOnboardingContents} = useOnboardingDocs({
187190
project: currentProject,
188191
docKeys,

static/app/components/profiling/ProfilingOnboarding/profilingOnboardingSidebar.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,13 @@ function OnboardingContent({
214214
}, [currentProject, previousProject]);
215215

216216
const docKeysMap = useMemo(() => makeDocKeyMap(currentPlatform?.id), [currentPlatform]);
217+
const docKeys = useMemo(
218+
() => (docKeysMap ? Object.values(docKeysMap) : []),
219+
[docKeysMap]
220+
);
217221

218222
const {docContents, isLoading, hasOnboardingContents} = useOnboardingDocs({
219-
docKeys: docKeysMap ? Object.values(docKeysMap) : [],
223+
docKeys,
220224
project: currentProject,
221225
isPlatformSupported: isSupported,
222226
});

static/app/components/replaysOnboarding/sidebar.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ function OnboardingContent({currentProject}: {currentProject: Project}) {
154154
? platforms.find(p => p.id === currentProject.platform)
155155
: undefined;
156156

157-
const docKeys = currentPlatform ? generateDocKeys(currentPlatform.id) : [];
157+
const docKeys = useMemo(() => {
158+
return currentPlatform ? generateDocKeys(currentPlatform.id) : [];
159+
}, [currentPlatform]);
158160

159161
const {docContents, isLoading, hasOnboardingContents} = useOnboardingDocs({
160162
project: currentProject,

0 commit comments

Comments
 (0)