|
| 1 | +import {getCurrentPlatformOrGuide} from 'sentry-docs/docTree'; |
| 2 | +import {serverContext} from 'sentry-docs/serverContext'; |
| 3 | +import {PlatformCategory} from 'sentry-docs/types'; |
| 4 | + |
| 5 | +import {PlatformCategorySection} from './platformCategorySection'; |
| 6 | +import {PlatformSection} from './platformSection'; |
| 7 | +import {SdkDefinition, SdkDefinitionTable, SdkDefinitionTableRow} from './sdkDefinition'; |
| 8 | + |
| 9 | +type Props = { |
| 10 | + name: string; |
| 11 | + type: string; |
| 12 | + categorySupported?: PlatformCategory[]; |
| 13 | + children?: React.ReactNode; |
| 14 | + defaultValue?: string; |
| 15 | + envVar?: string; |
| 16 | + group?: string; |
| 17 | +}; |
| 18 | + |
| 19 | +export function SdkOption({ |
| 20 | + name, |
| 21 | + children, |
| 22 | + type, |
| 23 | + defaultValue, |
| 24 | + envVar, |
| 25 | + categorySupported = [], |
| 26 | +}: Props) { |
| 27 | + const {showBrowserOnly, showServerLikeOnly} = getPlatformHints(categorySupported); |
| 28 | + |
| 29 | + return ( |
| 30 | + <SdkDefinition name={name} categorySupported={categorySupported}> |
| 31 | + <SdkDefinitionTable> |
| 32 | + {type && <SdkDefinitionTableRow label="Type">{type}</SdkDefinitionTableRow>} |
| 33 | + {defaultValue && ( |
| 34 | + <SdkDefinitionTableRow label="Default">{defaultValue}</SdkDefinitionTableRow> |
| 35 | + )} |
| 36 | + |
| 37 | + <PlatformCategorySection supported={['server', 'serverless']}> |
| 38 | + <PlatformSection notSupported={['javascript.nextjs']}> |
| 39 | + {envVar && ( |
| 40 | + <SdkDefinitionTableRow label="ENV Variable">{envVar}</SdkDefinitionTableRow> |
| 41 | + )} |
| 42 | + </PlatformSection> |
| 43 | + </PlatformCategorySection> |
| 44 | + |
| 45 | + {showBrowserOnly && ( |
| 46 | + <SdkDefinitionTableRow label="Only available on">Client</SdkDefinitionTableRow> |
| 47 | + )} |
| 48 | + |
| 49 | + {showServerLikeOnly && ( |
| 50 | + <SdkDefinitionTableRow label="Only available on">Server</SdkDefinitionTableRow> |
| 51 | + )} |
| 52 | + </SdkDefinitionTable> |
| 53 | + |
| 54 | + {children} |
| 55 | + </SdkDefinition> |
| 56 | + ); |
| 57 | +} |
| 58 | + |
| 59 | +function getPlatformHints(categorySupported: PlatformCategory[]) { |
| 60 | + const {rootNode, path} = serverContext(); |
| 61 | + const currentPlatformOrGuide = getCurrentPlatformOrGuide(rootNode, path); |
| 62 | + const currentCategories = currentPlatformOrGuide?.categories || []; |
| 63 | + |
| 64 | + // We only handle browser, server & serverless here for now |
| 65 | + const currentIsBrowser = currentCategories.includes('browser'); |
| 66 | + const currentIsServer = currentCategories.includes('server'); |
| 67 | + const currentIsServerless = currentCategories.includes('serverless'); |
| 68 | + const currentIsServerLike = currentIsServer || currentIsServerless; |
| 69 | + |
| 70 | + const hasCategorySupported = categorySupported.length > 0; |
| 71 | + const supportedBrowserOnly = |
| 72 | + categorySupported.includes('browser') && |
| 73 | + !categorySupported.includes('server') && |
| 74 | + !categorySupported.includes('serverless'); |
| 75 | + const supportedServerLikeOnly = |
| 76 | + !categorySupported.includes('browser') && |
| 77 | + (categorySupported.includes('server') || categorySupported.includes('serverless')); |
| 78 | + |
| 79 | + const showBrowserOnly = |
| 80 | + hasCategorySupported && supportedBrowserOnly && currentIsServerLike; |
| 81 | + const showServerLikeOnly = |
| 82 | + hasCategorySupported && supportedServerLikeOnly && currentIsBrowser; |
| 83 | + |
| 84 | + return {showBrowserOnly, showServerLikeOnly}; |
| 85 | +} |
0 commit comments