|
| 1 | +import {Fragment} from 'react'; |
| 2 | + |
| 3 | +import {getCurrentPlatform} from 'sentry-docs/docTree'; |
| 4 | +import {serverContext} from 'sentry-docs/serverContext'; |
| 5 | +import {PlatformCategory} from 'sentry-docs/types'; |
| 6 | + |
| 7 | +import {Expandable} from './expandable'; |
| 8 | +import {codeToJsx} from './highlightCode'; |
| 9 | +import {SdkDefinition} from './sdkDefinition'; |
| 10 | +import {getPlatformHints} from './sdkOption'; |
| 11 | + |
| 12 | +export interface ParameterDef { |
| 13 | + name: string; |
| 14 | + type: string | ObjectParameterDef; |
| 15 | + defaultValue?: string; |
| 16 | + description?: string; |
| 17 | + required?: boolean; |
| 18 | +} |
| 19 | + |
| 20 | +type ObjectParameterDef = { |
| 21 | + properties: ParameterDef[]; |
| 22 | + name?: string; |
| 23 | +}; |
| 24 | + |
| 25 | +type Props = { |
| 26 | + name: string; |
| 27 | + parameters: ParameterDef[]; |
| 28 | + signature: string; |
| 29 | + categorySupported?: PlatformCategory[]; |
| 30 | + children?: React.ReactNode; |
| 31 | + language?: string; |
| 32 | +}; |
| 33 | + |
| 34 | +export function SdkApi({ |
| 35 | + name, |
| 36 | + children, |
| 37 | + signature, |
| 38 | + parameters = [], |
| 39 | + language, |
| 40 | + categorySupported = [], |
| 41 | +}: Props) { |
| 42 | + const {rootNode, path} = serverContext(); |
| 43 | + const platform = getCurrentPlatform(rootNode, path); |
| 44 | + const lang = language || platform?.language || 'typescript'; |
| 45 | + |
| 46 | + const {showBrowserOnly, showServerLikeOnly} = getPlatformHints(categorySupported); |
| 47 | + |
| 48 | + return ( |
| 49 | + <SdkDefinition name={name} categorySupported={categorySupported}> |
| 50 | + {showBrowserOnly && <div className="italic text-sm">Only available on Client</div>} |
| 51 | + {showServerLikeOnly && ( |
| 52 | + <div className="italic text-sm">Only available on Server</div> |
| 53 | + )} |
| 54 | + |
| 55 | + <pre className="mt-2 mb-2 text-sm">{codeToJsx(signature, lang)}</pre> |
| 56 | + |
| 57 | + {parameters.length ? ( |
| 58 | + <Expandable title="Parameters"> |
| 59 | + <div className="space-y-3"> |
| 60 | + {parameters.map(param => ( |
| 61 | + <ApiParameterDef key={param.name} language={lang} {...param} /> |
| 62 | + ))} |
| 63 | + </div> |
| 64 | + </Expandable> |
| 65 | + ) : null} |
| 66 | + |
| 67 | + {children} |
| 68 | + </SdkDefinition> |
| 69 | + ); |
| 70 | +} |
| 71 | + |
| 72 | +function ApiParameterDef({ |
| 73 | + name, |
| 74 | + type, |
| 75 | + description, |
| 76 | + required, |
| 77 | + language, |
| 78 | +}: ParameterDef & {language: string}) { |
| 79 | + return ( |
| 80 | + <div className="space-y-1"> |
| 81 | + <div className="font-bold m-0"> |
| 82 | + {name} |
| 83 | + {required ? <span className="text-red">*</span> : null} |
| 84 | + </div> |
| 85 | + <div className="space-y-1"> |
| 86 | + <div className="flex"> |
| 87 | + {typeof type === 'string' ? ( |
| 88 | + <pre className="m-0 pt-1 pb-1 text-sm"> |
| 89 | + <code>{codeToJsx(type, language)}</code> |
| 90 | + </pre> |
| 91 | + ) : ( |
| 92 | + <pre className="m-0 pt-1 pb-1 text-sm"> |
| 93 | + <NestedObject |
| 94 | + name={type.name} |
| 95 | + objProps={type.properties} |
| 96 | + language={language} |
| 97 | + /> |
| 98 | + </pre> |
| 99 | + )} |
| 100 | + </div> |
| 101 | + |
| 102 | + {description ? <p className="m-0">{description}</p> : null} |
| 103 | + </div> |
| 104 | + </div> |
| 105 | + ); |
| 106 | +} |
| 107 | + |
| 108 | +function NestedObject({ |
| 109 | + name, |
| 110 | + objProps, |
| 111 | + language, |
| 112 | +}: { |
| 113 | + language: string; |
| 114 | + objProps: ParameterDef[]; |
| 115 | + name?: string; |
| 116 | +}) { |
| 117 | + // NOTE: For now, we always render the nested object in typescript |
| 118 | + |
| 119 | + return ( |
| 120 | + <div> |
| 121 | + <div> |
| 122 | + <code> |
| 123 | + {name ? name : 'Object'} {'{'} |
| 124 | + </code> |
| 125 | + </div> |
| 126 | + |
| 127 | + <div className="flex flex-col gap-2 pl-4"> |
| 128 | + {objProps.map(prop => ( |
| 129 | + <div key={prop.name}> |
| 130 | + {prop.description && ( |
| 131 | + <div> |
| 132 | + <code>{codeToJsx(`// ${prop.description}`, 'typescript')}</code> |
| 133 | + </div> |
| 134 | + )} |
| 135 | + <div> |
| 136 | + {typeof prop.type === 'string' ? ( |
| 137 | + <Fragment> |
| 138 | + <code> |
| 139 | + {prop.name} |
| 140 | + {!prop.required ? '?' : ''}:{' '} |
| 141 | + </code> |
| 142 | + <code>{codeToJsx(prop.type, 'typescript')},</code> |
| 143 | + </Fragment> |
| 144 | + ) : ( |
| 145 | + <NestedObject |
| 146 | + name={`${prop.name}${!prop.required ? '?' : ''}: ${prop.type.name || 'Object'}`} |
| 147 | + objProps={prop.type.properties} |
| 148 | + language={language} |
| 149 | + /> |
| 150 | + )} |
| 151 | + </div> |
| 152 | + </div> |
| 153 | + ))} |
| 154 | + </div> |
| 155 | + <div>{'}'}</div> |
| 156 | + </div> |
| 157 | + ); |
| 158 | +} |
0 commit comments