()
| 30 | } |
| 31 | |
| 32 | export const ApiVersionPicker = () => { |
| 33 | const router = useRouter() |
| 34 | const { currentVersion } = useVersion() |
| 35 | const { allVersions } = useMainContext() |
| 36 | const { t } = useTranslation(['products']) |
| 37 | const basePath = router.asPath.split('#')[0].split('?')[0] |
| 38 | // Get current date from cookie, query path, or lastly set it to latest rest version date |
| 39 | const isValidApiVersion = |
| 40 | (router.query.apiVersion && |
| 41 | typeof router.query.apiVersion === 'string' && |
| 42 | allVersions[currentVersion].apiVersions.includes(router.query.apiVersion)) || |
| 43 | false |
| 44 | |
| 45 | const currentDate = ( |
| 46 | isValidApiVersion ? router.query.apiVersion : allVersions[currentVersion].latestApiVersion |
| 47 | ) as string |
| 48 | |
| 49 | const currentDateDisplayText = |
| 50 | currentDate === allVersions[currentVersion].latestApiVersion |
| 51 | ? currentDate + API_VERSION_SUFFIX |
| 52 | : currentDate |
| 53 | |
| 54 | const apiVersionLinks = allVersions[currentVersion].apiVersions.map((date) => { |
| 55 | const itemLink = `/${router.locale}${basePath}?apiVersion=${date}` |
| 56 | const dateDisplayText = |
| 57 | date === allVersions[currentVersion].latestApiVersion ? date + API_VERSION_SUFFIX : date |
| 58 | |
| 59 | return { |
| 60 | text: dateDisplayText, |
| 61 | selected: router.query.apiVersion === date, |
| 62 | href: itemLink, |
| 63 | extra: { |
| 64 | info: false, |
| 65 | currentDate, |
| 66 | }, |
| 67 | } |
| 68 | }) |
| 69 | |
| 70 | apiVersionLinks.push({ |
| 71 | text: t('rest.versioning.about_versions'), |
| 72 | selected: false, |
| 73 | href: `/${router.locale}${ |
| 74 | currentVersion === DEFAULT_VERSION ? '' : `/${currentVersion}` |
| 75 | }/rest/overview/api-versions`, |
| 76 | extra: { |
| 77 | info: true, |
| 78 | currentDate, |
| 79 | }, |
| 80 | }) |
| 81 | |
| 82 | // This only shows the REST Version picker if it's calendar date versioned |
| 83 | return allVersions[currentVersion].apiVersions.length > 0 ? ( |
| 84 | <div className="mb-3"> |
| 85 | <div data-testid="api-version-picker" className="width-full"> |
| 86 | <Picker |
| 87 | defaultText={currentDateDisplayText} |
| 88 | items={apiVersionLinks} |
| 89 | pickerLabel="API Version" |
nothing calls this directly
no test coverage detected