({ operation, slug }: Props)
| 41 | } |
| 42 | |
| 43 | export function RestCodeSamples({ operation, slug }: Props) { |
| 44 | const { t } = useTranslation('products') |
| 45 | |
| 46 | // Refs to track the request example, response example |
| 47 | // and the first render |
| 48 | const requestCodeExample = useRef<HTMLSpanElement>(null) |
| 49 | const responseCodeExample = useRef<HTMLSpanElement>(null) |
| 50 | const firstRender = useRef(true) |
| 51 | const scrollRef = useRef<HTMLDivElement>(null) |
| 52 | |
| 53 | // Get format examples for each language |
| 54 | const languageExamples = operation.codeExamples.map((sample) => ({ |
| 55 | description: sample.request.description, |
| 56 | curl: getShellExample(operation, sample), |
| 57 | javascript: getJSExample(operation, sample), |
| 58 | ghcli: getGHExample(operation, sample), |
| 59 | response: sample.response, |
| 60 | })) |
| 61 | |
| 62 | // Menu options for the language selector |
| 63 | const languageSelectOptions: LanguageOptionT[] = [{ key: CURLKEY, text: 'cURL' }] |
| 64 | |
| 65 | // Management Console operations are not supported by Octokit |
| 66 | if (operation.subcategory !== 'management-console') { |
| 67 | languageSelectOptions.push({ key: JSKEY, text: 'JavaScript' }) |
| 68 | |
| 69 | // Not all examples support the GH CLI language option. If any of |
| 70 | // the examples don't support it, we don't show GH CLI as an option. |
| 71 | if (!languageExamples.some((example) => example.ghcli === undefined)) { |
| 72 | languageSelectOptions.push({ key: GHCLIKEY, text: 'GitHub CLI' }) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // Menu options for the example selector |
| 77 | |
| 78 | // We show the media type in the examples menu items for each example if |
| 79 | // there's more than one example and if the media types aren't all the same |
| 80 | // for the examples (e.g. if all examples have content type `application/json`, |
| 81 | // we won't show that information in the menu items). |
| 82 | const showExampleOptionMediaType = |
| 83 | languageExamples.length > 1 && |
| 84 | !languageExamples.every( |
| 85 | (example) => example.response.contentType === languageExamples[0].response.contentType |
| 86 | ) |
| 87 | const exampleSelectOptions = languageExamples.map((example, index) => ({ |
| 88 | text: showExampleOptionMediaType |
| 89 | ? `${example.description} (${example.response.contentType})` |
| 90 | : example.description, |
| 91 | // maps to the index of the example in the languageExamples array |
| 92 | languageIndex: index, |
| 93 | })) |
| 94 | |
| 95 | const [selectedLanguage, setSelectedLanguage] = useState<keyof ExampleT>( |
| 96 | languageSelectOptions[0].key |
| 97 | ) |
| 98 | const [selectedExample, setSelectedExample] = useState(exampleSelectOptions[0]) |
| 99 | const [selectedResponse, setSelectedResponse] = useState(responseSelectOptions[0].key) |
| 100 | const [responseMaxHeight, setResponseMaxHeight] = useState(0) |
nothing calls this directly
no test coverage detected