Skip to content

Commit 4f9e05b

Browse files
ref(platforms): Improve implementation of platformRegistry (getsentry#7505)
This change simplfies the PlatformRegistry
1 parent 4af6710 commit 4f9e05b

File tree

15 files changed

+447
-237
lines changed

15 files changed

+447
-237
lines changed

bin/lint-docs.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import fs from 'fs';
77

8-
import PlatformRegistry from '../src/shared/platformRegistry';
8+
import {buildPlatformRegistry} from '../src/shared/platformRegistry';
99

1010
enum Level {
1111
error,
@@ -45,11 +45,10 @@ const testConfig = config => {
4545
};
4646

4747
const main = async () => {
48-
const platformRegistry = new PlatformRegistry();
49-
await platformRegistry.init();
48+
const {platforms} = await buildPlatformRegistry();
5049

5150
const violations: Violation[] = [];
52-
platformRegistry.platforms.forEach(platform => {
51+
platforms.forEach(platform => {
5352
// test for wizard
5453
testConfig(platform).forEach(violation => {
5554
violations.push({...violation, context: platform.key});

src/components/__tests__/__snapshots__/configKey.test.js.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ exports[`ConfigKey renders correctly 1`] = `
1919
/>
2020
</svg>
2121
</a>
22-
<code />
22+
<code>
23+
my_option_name
24+
</code>
2325
</h3>
2426
`;

src/components/guideGrid.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import {PlatformIcon} from 'platformicons';
33

4-
import {Platform, usePlatform} from './hooks/usePlatform';
4+
import {usePlatform} from './hooks/usePlatform';
55
import {SmartLink} from './smartLink';
66

77
type Props = {
@@ -11,14 +11,14 @@ type Props = {
1111

1212
export function GuideGrid({platform, className}: Props) {
1313
const [currentPlatform] = usePlatform(platform);
14-
// platform might actually not be a platform, so lets handle that case gracefully
15-
if (!(currentPlatform as Platform).guides) {
14+
15+
if (currentPlatform.type === 'guide') {
1616
return null;
1717
}
1818

1919
return (
2020
<ul className={className}>
21-
{(currentPlatform as Platform).guides.map(guide => (
21+
{currentPlatform.guides.map(guide => (
2222
<li key={guide.key}>
2323
<SmartLink to={guide.url}>
2424
<PlatformIcon

src/components/hooks/usePlatform.tsx

Lines changed: 13 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {graphql, navigate, useStaticQuery} from 'gatsby';
44
import {parse} from 'query-string';
55

66
import {PageContext} from 'sentry-docs/components/pageContext';
7+
import {Platform, PlatformGuide} from 'sentry-docs/types';
78

89
import {useLocalStorage} from './useLocalStorage';
910

@@ -34,62 +35,6 @@ const query = graphql`
3435
}
3536
`;
3637

37-
export const formatCaseStyle = (style: string, value: string): string => {
38-
switch (style) {
39-
case 'snake_case':
40-
return value.replace(/-/g, '_');
41-
case 'camelCase':
42-
return value
43-
.split(/-/g)
44-
.map((val, idx) =>
45-
idx === 0 ? val : val.charAt(0).toUpperCase() + val.substring(1)
46-
)
47-
.join('');
48-
case 'PascalCase':
49-
return value
50-
.split(/-/g)
51-
.map(val => val.charAt(0).toUpperCase() + val.substring(1))
52-
.join('');
53-
default:
54-
return value;
55-
}
56-
};
57-
58-
// export enum CaseStyle {
59-
// canonical,
60-
// camelCase,
61-
// PascalCase,
62-
// snake_case,
63-
// }
64-
65-
// export enum SupportLevel {
66-
// production,
67-
// community,
68-
// }
69-
70-
export type Guide = {
71-
caseStyle: string;
72-
fallbackPlatform: string;
73-
key: string;
74-
name: string;
75-
sdk: string;
76-
supportLevel: string;
77-
title: string;
78-
url: string;
79-
};
80-
81-
export type Platform = {
82-
caseStyle: string;
83-
key: string;
84-
name: string;
85-
sdk: string;
86-
supportLevel: string;
87-
title: string;
88-
url: string;
89-
fallbackPlatform?: string;
90-
guides?: Guide[];
91-
};
92-
9338
export const DEFAULT_PLATFORM = 'javascript';
9439

9540
const normalizeSlug = (name: string): string => {
@@ -161,8 +106,9 @@ const rebuildPathForPlatform = (key: string, currentPath?: string): string => {
161106
export const usePlatformList = (): Platform[] => {
162107
const {
163108
allPlatform: {nodes: platformList},
164-
} = useStaticQuery(query);
165-
return platformList.sort((a: Platform, b: Platform) => {
109+
} = useStaticQuery<{allPlatform: {nodes: Platform[]}}>(query);
110+
111+
return platformList.sort((a, b) => {
166112
// Exclude leading non-alphanumeric characters to order .NET between Native and NodeJS instead of the beginning.
167113
const skippedPrefix = /^[^a-zA-Z]+/;
168114
return a.title
@@ -176,7 +122,7 @@ export const usePlatformList = (): Platform[] => {
176122
177123
* @param value platform key in format of `platformName[.guideName]`
178124
*/
179-
export const getPlatform = (key: string): Platform | Guide | null => {
125+
export const getPlatform = (key: string): Platform | PlatformGuide | null => {
180126
// XXX(epurkhiser): This is almost certinally a mistake, we should figure out
181127
// if `getPlatforms` should actually be something more like `useGetPlatforms`
182128
// or something
@@ -191,14 +137,13 @@ export const getPlatform = (key: string): Platform | Guide | null => {
191137
const [platformName, guideName] = key.split('.', 2);
192138
const activePlatform = platformList.find((p: Platform) => p.key === platformName);
193139
const activeGuide =
194-
activePlatform &&
195-
(activePlatform as Platform).guides.find((g: Guide) => g.name === guideName);
140+
activePlatform && activePlatform.guides.find(g => g.name === guideName);
196141

197142
return activeGuide ?? activePlatform ?? null;
198143
};
199144

200145
type UsePlatform = [
201-
Platform | Guide | null,
146+
Platform | PlatformGuide | null,
202147
(value: string, options?: SetPlatformOptions) => void,
203148
boolean
204149
];
@@ -207,7 +152,9 @@ type SetPlatformOptions = {
207152
noQueryString?: boolean;
208153
};
209154

210-
export const getPlatformsWithFallback = (platform: Platform | Guide): string[] => {
155+
export const getPlatformsWithFallback = (
156+
platform: Platform | PlatformGuide
157+
): string[] => {
211158
const result = [platform.key];
212159
let curPlatform = platform;
213160
while (curPlatform.fallbackPlatform) {
@@ -258,7 +205,7 @@ export function usePlatform(
258205

259206
const [stateValue, setStateValue] = useState(currentValue);
260207

261-
const setValue = (newValue: string, options: SetPlatformOptions = {}) => {
208+
const setPlatform = (newValue: string, options: SetPlatformOptions = {}) => {
262209
if (newValue === currentValue) {
263210
return;
264211
}
@@ -276,8 +223,8 @@ export function usePlatform(
276223
setStateValue(newValue);
277224
};
278225

279-
const activeValue: Platform | Guide | null =
226+
const activePlatform: Platform | PlatformGuide | null =
280227
getPlatform(stateValue) ?? (useDefault ? getPlatform(defaultValue) : null);
281228

282-
return [activeValue, setValue, isFixed];
229+
return [activePlatform, setPlatform, isFixed];
283230
}

src/components/platformContent.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import React, {Fragment, useState} from 'react';
22
import {graphql, useStaticQuery} from 'gatsby';
33

4-
import {
5-
getPlatform,
6-
getPlatformsWithFallback,
7-
Platform,
8-
usePlatform,
9-
} from './hooks/usePlatform';
4+
import {Platform, PlatformGuide} from 'sentry-docs/types';
5+
6+
import {getPlatform, getPlatformsWithFallback, usePlatform} from './hooks/usePlatform';
107
import {Content} from './content';
118
import {SmartLink} from './smartLink';
129

@@ -53,7 +50,7 @@ type Props = {
5350

5451
const getFileForPlatform = (
5552
fileList: FileNode[],
56-
platform: Platform
53+
platform: Platform | PlatformGuide
5754
): FileNode | null => {
5855
const platformsToSearch = getPlatformsWithFallback(platform);
5956
platformsToSearch.push('_default');

src/components/platformIdentifier.tsx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
11
import React from 'react';
22

3-
import {formatCaseStyle, usePlatform} from './hooks/usePlatform';
3+
import {PlatformCaseStyle} from 'sentry-docs/types';
4+
5+
import {usePlatform} from './hooks/usePlatform';
46

57
type Props = {
68
name: string;
79
platform?: string;
810
};
911

12+
function formatCaseStyle(style: PlatformCaseStyle | undefined, value: string) {
13+
switch (style) {
14+
case 'snake_case':
15+
return value.replace(/-/g, '_');
16+
case 'camelCase':
17+
return value
18+
.split(/-/g)
19+
.map((val, idx) =>
20+
idx === 0 ? val : val.charAt(0).toUpperCase() + val.substring(1)
21+
)
22+
.join('');
23+
case 'PascalCase':
24+
return value
25+
.split(/-/g)
26+
.map(val => val.charAt(0).toUpperCase() + val.substring(1))
27+
.join('');
28+
default:
29+
return value;
30+
}
31+
}
32+
1033
export function PlatformIdentifier({name, platform}: Props) {
1134
const [currentPlatform] = usePlatform(platform);
1235

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import {PlatformIcon} from 'platformicons';
33

4-
import {Platform, usePlatform} from './hooks/usePlatform';
4+
import {usePlatform} from './hooks/usePlatform';
55
import {SmartLink} from './smartLink';
66

77
type Props = {
@@ -12,17 +12,11 @@ type Props = {
1212

1313
export function PlatformLinkWithLogo({platform, label, url}: Props) {
1414
const [currentPlatform] = usePlatform(platform);
15-
let linkText = currentPlatform.title;
1615

17-
// platform might actually not be a platform, so lets handle that case gracefully
18-
if (!(currentPlatform as Platform).guides) {
16+
if (currentPlatform.type !== 'platform') {
1917
return null;
2018
}
2119

22-
if (label) {
23-
linkText = label;
24-
}
25-
2620
return (
2721
<SmartLink to={url}>
2822
<PlatformIcon
@@ -36,7 +30,7 @@ export function PlatformLinkWithLogo({platform, label, url}: Props) {
3630
}}
3731
format="sm"
3832
/>
39-
{linkText}
33+
{label ?? currentPlatform.title}
4034
</SmartLink>
4135
);
4236
}

src/components/platformSection.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, {Fragment} from 'react';
22

3-
import {getPlatformsWithFallback, Platform, usePlatform} from './hooks/usePlatform';
3+
import {getPlatformsWithFallback, usePlatform} from './hooks/usePlatform';
44

55
type Props = {
66
children?: React.ReactNode;
@@ -37,7 +37,7 @@ export function PlatformSection({
3737
return null;
3838
}
3939

40-
if (noGuides && !(currentPlatform as Platform).guides) {
40+
if (noGuides && currentPlatform.type !== 'platform') {
4141
return null;
4242
}
4343

src/gatsby/createPages/createPlatformPages.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import nodePath from 'path';
66
import {GatsbyNode, Node} from 'gatsby';
77
import {createFilePath} from 'gatsby-source-filesystem';
88

9-
import PlatformRegistry, {Guide, Platform} from '../../shared/platformRegistry';
9+
import {buildPlatformRegistry} from '../../shared/platformRegistry';
10+
import {Platform, PlatformGuide} from '../../types';
1011
import {getChild, getDataOrPanic} from '../helpers';
1112

1213
type CreatePageArgs = Parameters<GatsbyNode['createPages']>[0];
@@ -221,9 +222,6 @@ export const createPlatformPages = async ({
221222
reporter
222223
);
223224

224-
const platformRegistry = new PlatformRegistry();
225-
await platformRegistry.init();
226-
227225
// filter out nodes with no markdown content
228226
const {common, platforms} = buildPlatformPages(
229227
nodes.filter((n: FileNode) => getChild(n))
@@ -362,7 +360,7 @@ export const createPlatformPages = async ({
362360
const createPlatformGuidePages = (
363361
platform: Platform,
364362
platformData,
365-
guide: Guide,
363+
guide: PlatformGuide,
366364
guideData,
367365
sharedCommon: FileNode[],
368366
sharedContext: {[key: string]: any}
@@ -454,7 +452,9 @@ export const createPlatformPages = async ({
454452
});
455453
};
456454

457-
platformRegistry.platforms.forEach(platform => {
455+
const registry = await buildPlatformRegistry();
456+
457+
registry.platforms.forEach(platform => {
458458
makePlatformPage(platform, platforms[platform.name], common);
459459
});
460460

src/gatsby/onPostBuild.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import fs from 'fs';
66

77
import jsdom from 'jsdom';
88

9-
import PlatformRegistry from '../shared/platformRegistry';
9+
import {buildPlatformRegistry, PlatformRegistry} from '../shared/platformRegistry';
1010

1111
const rmDirSync = (dirPath: string) => {
1212
let files;
@@ -59,9 +59,6 @@ export default async function onPostBuild({graphql}) {
5959
}
6060
);
6161

62-
const platformRegistry = new PlatformRegistry();
63-
await platformRegistry.init();
64-
6562
const nodes = results.data.allFile.edges.map(e => e.node.childMarkdownRemark);
6663
if (!nodes.length) {
6764
const msg = 'No platform data found for wizard!';
@@ -73,6 +70,8 @@ export default async function onPostBuild({graphql}) {
7370
return;
7471
}
7572

73+
const platformRegistry = await buildPlatformRegistry();
74+
7675
await writeJson(output, nodes, platformRegistry);
7776
}
7877

@@ -160,7 +159,7 @@ const writeJson = (path: string, nodes, platformRegistry: PlatformRegistry) => {
160159
categories: [],
161160
};
162161

163-
const platform = platformRegistry.get(key);
162+
const platform = platformRegistry.platformGuideMapping[key];
164163
if (platform) {
165164
data.name = platform.title;
166165
data.aliases = platform.aliases || [];

0 commit comments

Comments
 (0)