Skip to content

Commit

Permalink
chore(site): support multiple versions
Browse files Browse the repository at this point in the history
  • Loading branch information
xiejay97 committed Mar 1, 2023
1 parent 19e2c82 commit ccec72e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/site/src/app/routes/layout/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import { DCustomIcon, GithubOutlined } from '@react-devui/icons';
import { DDropdown, DMenu, DSeparator } from '@react-devui/ui';
import { getClassName } from '@react-devui/utils';

import { environment } from '../../../environments/environment';
import { AppVersions } from './Versions';

export interface AppHeaderProps {
menuOpen: boolean;
onMenuOpenChange: (open: boolean) => void;
Expand Down Expand Up @@ -46,6 +49,7 @@ export function AppHeader(props: AppHeaderProps): JSX.Element | null {
<div></div>
</div>
</button>
{environment.production && <AppVersions />}
<DMenu
className="d-none d-md-inline-block app-layout-header__menu"
dList={[
Expand Down
44 changes: 44 additions & 0 deletions packages/site/src/app/routes/layout/Versions.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import axios from 'axios';
import { useState } from 'react';
import { useLocation } from 'react-router-dom';

import { useMount } from '@react-devui/hooks';
import { DownOutlined } from '@react-devui/icons';
import { DButton, DDropdown } from '@react-devui/ui';

export function AppVersions(): JSX.Element | null {
const location = useLocation();

const version = (() => {
const v = window.location.host.match(/^v[0-9]+/);
return v ? v[0] : 'main';
})();
const [versions, setVersions] = useState<string[]>(() => {
if (version === 'main') {
return ['main'];
} else {
return ['main', version];
}
});
useMount(() => {
axios.get('/api/versions').then((response) => {
setVersions(['main', ...(response.data as number[]).map((v) => `v${v}`)]);
});
});

return (
<DDropdown
dList={versions.map((v) => ({ id: v, label: v, type: 'item' }))}
onItemClick={(id) => {
window.location = ((id === 'main' ? 'https://react-devui.com' : `https://${id}.react-devui.com`) + location.pathname) as any;
}}
>
<DButton dType="text">
<div className="d-flex align-items-center">
{version}
<DownOutlined style={{ position: 'relative', top: 2, marginLeft: 2 }} dSize={12} />
</div>
</DButton>
</DDropdown>
);
}

0 comments on commit ccec72e

Please sign in to comment.