-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathplatformSection.tsx
77 lines (69 loc) · 1.96 KB
/
platformSection.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import {DocNode, getCurrentPlatformOrGuide, getPlatform} from 'sentry-docs/docTree';
import {serverContext} from 'sentry-docs/serverContext';
import {Platform, PlatformGuide} from 'sentry-docs/types';
function getPlatformsWithFallback(
rootNode: DocNode,
platformOrGuide: Platform | PlatformGuide
) {
const result = [platformOrGuide.key];
let curPlatform: Platform | PlatformGuide | undefined = platformOrGuide;
while (curPlatform?.fallbackPlatform) {
result.push(curPlatform.fallbackPlatform);
curPlatform = getPlatform(rootNode, curPlatform.fallbackPlatform);
}
return result;
}
type Props = {
children?: React.ReactNode;
noGuides?: boolean;
notSupported?: string[];
platform?: string;
supported?: string[];
};
const isSupported = (
platformKey: string,
supported: string[],
notSupported: string[]
): boolean | null => {
if (supported.length && supported.find(p => p === platformKey)) {
return true;
}
if (notSupported.length && notSupported.find(p => p === platformKey)) {
return false;
}
return null;
};
export function PlatformSection({
supported = [],
notSupported = [],
noGuides,
children,
}: Props) {
const {rootNode, path} = serverContext();
const currentPlatformOrGuide = rootNode && getCurrentPlatformOrGuide(rootNode, path);
if (!currentPlatformOrGuide) {
return null;
}
if (noGuides && currentPlatformOrGuide.type !== 'platform') {
return null;
}
const platformsToSearch = getPlatformsWithFallback(rootNode, currentPlatformOrGuide);
let result: boolean | null = null;
// eslint-disable-next-line no-cond-assign
for (let platformKey: string, i = 0; (platformKey = platformsToSearch[i]); i++) {
if (!platformKey) {
continue;
}
result = isSupported(platformKey, supported, notSupported);
if (result === false) {
return null;
}
if (result === true) {
break;
}
}
if (result === null && supported.length) {
return null;
}
return children;
}