Skip to content

Commit b1d3feb

Browse files
committed
chore(site): support multiple versions
1 parent 19e2c82 commit b1d3feb

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

packages/site/src/app/routes/layout/Header.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import { DCustomIcon, GithubOutlined } from '@react-devui/icons';
1010
import { DDropdown, DMenu, DSeparator } from '@react-devui/ui';
1111
import { getClassName } from '@react-devui/utils';
1212

13+
import { environment } from '../../../environments/environment';
14+
import { AppVersions } from './Versions';
15+
1316
export interface AppHeaderProps {
1417
menuOpen: boolean;
1518
onMenuOpenChange: (open: boolean) => void;
@@ -46,6 +49,7 @@ export function AppHeader(props: AppHeaderProps): JSX.Element | null {
4649
<div></div>
4750
</div>
4851
</button>
52+
{environment.production && <AppVersions />}
4953
<DMenu
5054
className="d-none d-md-inline-block app-layout-header__menu"
5155
dList={[
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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

Comments
 (0)