|
| 1 | +import axios from 'axios'; |
| 2 | +import { useState } from 'react'; |
| 3 | +import { useLocation } from 'react-router-dom'; |
| 4 | + |
| 5 | +import { useMount } from '@react-devui/hooks'; |
| 6 | +import { DCustomIcon } from '@react-devui/icons'; |
| 7 | +import { DButton, DDropdown } from '@react-devui/ui'; |
| 8 | + |
| 9 | +export function AppVersions(): JSX.Element | null { |
| 10 | + const location = useLocation(); |
| 11 | + |
| 12 | + const version = (() => { |
| 13 | + const v = window.location.host.match(/^v[0-9]+/); |
| 14 | + return v ? v[0] : 'main'; |
| 15 | + })(); |
| 16 | + const [versions, setVersions] = useState<string[]>(() => { |
| 17 | + if (version === 'main') { |
| 18 | + return ['main']; |
| 19 | + } else { |
| 20 | + return ['main', version]; |
| 21 | + } |
| 22 | + }); |
| 23 | + useMount(() => { |
| 24 | + axios.get('/api/versions').then((response) => { |
| 25 | + setVersions(['main', ...(response.data as number[]).map((v) => `v${v}`)]); |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + return ( |
| 30 | + <DDropdown |
| 31 | + dList={versions.map((v) => ({ id: v, label: v, type: 'item' }))} |
| 32 | + onItemClick={(id) => { |
| 33 | + window.location = ((id === 'main' ? 'https://react-devui.com' : `https://${id}.react-devui.com`) + location.pathname) as any; |
| 34 | + }} |
| 35 | + > |
| 36 | + <DButton dType="text"> |
| 37 | + <div className="d-flex align-items-center"> |
| 38 | + {version} |
| 39 | + <DCustomIcon style={{ position: 'relative', top: 2, marginLeft: 2 }} viewBox="64 64 896 896" dSize={12}> |
| 40 | + <path d="M884 256h-75c-5.1 0-9.9 2.5-12.9 6.6L512 654.2 227.9 262.6c-3-4.1-7.8-6.6-12.9-6.6h-75c-6.5 0-10.3 7.4-6.5 12.7l352.6 486.1c12.8 17.6 39 17.6 51.7 0l352.6-486.1c3.9-5.3.1-12.7-6.4-12.7z"></path> |
| 41 | + </DCustomIcon> |
| 42 | + </div> |
| 43 | + </DButton> |
| 44 | + </DDropdown> |
| 45 | + ); |
| 46 | +} |
0 commit comments