hyper-tuner-cloud/src/components/TopBar.tsx

227 lines
6.0 KiB
TypeScript
Raw Normal View History

2021-03-22 14:29:03 -07:00
import {
matchPath,
useLocation,
useHistory,
} from 'react-router';
2021-12-25 14:32:07 -08:00
import { Link } from 'react-router-dom';
2021-03-22 14:29:03 -07:00
import {
Layout,
Space,
Button,
Input,
Row,
Col,
Tooltip,
Grid,
Menu,
Dropdown,
Typography,
Radio,
} from 'antd';
import {
UserOutlined,
ShareAltOutlined,
CloudUploadOutlined,
CloudDownloadOutlined,
SettingOutlined,
LoginOutlined,
LineChartOutlined,
SlidersOutlined,
GithubOutlined,
FileExcelOutlined,
FileTextOutlined,
FileZipOutlined,
SaveOutlined,
DesktopOutlined,
GlobalOutlined,
LinkOutlined,
DownOutlined,
SearchOutlined,
2021-03-22 15:24:39 -07:00
ToolOutlined,
FundOutlined,
2021-12-25 14:32:07 -08:00
UserAddOutlined,
LogoutOutlined,
2021-03-22 14:29:03 -07:00
} from '@ant-design/icons';
import {
useEffect,
useMemo,
useRef,
} from 'react';
import store from '../store';
import { isMac } from '../utils/env';
import {
isCommand,
isToggleSidebar,
} from '../utils/keyboard/shortcuts';
import { Routes } from '../routes';
2021-12-25 14:32:07 -08:00
import { useAuth } from '../contexts/AuthContext';
2021-03-22 14:29:03 -07:00
const { Header } = Layout;
const { useBreakpoint } = Grid;
const { SubMenu } = Menu;
const TopBar = () => {
const { sm } = useBreakpoint();
const { pathname } = useLocation();
2021-12-25 14:32:07 -08:00
const { currentUser, logout } = useAuth();
2021-03-22 14:29:03 -07:00
const history = useHistory();
const matchedTabPath = useMemo(() => matchPath(pathname, { path: Routes.TAB }), [pathname]);
const userMenu = (
<Menu>
2021-12-25 14:32:07 -08:00
{currentUser ? (
<Menu.Item key="logout" icon={<LogoutOutlined />} onClick={logout}>
Logout
</Menu.Item>
) : (
<>
<Menu.Item key="login" icon={<LoginOutlined />}>
<Link to={Routes.LOGIN}>Login</Link>
</Menu.Item>
<Menu.Item key="sign-up" icon={<UserAddOutlined />}>
<Link to={Routes.SIGN_UP}>Sign Up</Link>
</Menu.Item>
</>
)}
2021-09-26 04:28:19 -07:00
<Menu.Item key="gh" icon={<GithubOutlined />}>
2021-12-25 14:32:07 -08:00
<a
href="https://github.com/speedy-tuner/speedy-tuner-cloud"
target="__blank"
rel="noopener noreferrer"
>
2021-03-22 14:29:03 -07:00
GitHub
</a>
</Menu.Item>
<Menu.Divider />
2021-09-26 04:28:19 -07:00
<Menu.Item key="preferences" disabled icon={<SettingOutlined />}>
2021-03-22 14:29:03 -07:00
Preferences
</Menu.Item>
</Menu>
);
const shareMenu = (
<Menu>
2021-09-26 04:28:19 -07:00
<Menu.Item key="upload" disabled icon={<CloudUploadOutlined />}>
2021-03-22 14:29:03 -07:00
Upload
</Menu.Item>
2021-09-26 04:28:19 -07:00
<SubMenu key="download-sub" title="Download" icon={<CloudDownloadOutlined />}>
<SubMenu key="tune-sub" title="Tune" icon={<SlidersOutlined />}>
<Menu.Item key="download" icon={<SaveOutlined />}>
<a href="/tunes/202103.msq" target="__blank" rel="noopener noreferrer">
2021-03-22 14:29:03 -07:00
Download
</a>
</Menu.Item>
2021-09-26 04:28:19 -07:00
<Menu.Item key="open" disabled icon={<DesktopOutlined />}>
2021-03-22 14:29:03 -07:00
Open in app
</Menu.Item>
</SubMenu>
2021-09-26 04:28:19 -07:00
<SubMenu key="logs-sub" title="Logs" icon={<LineChartOutlined />}>
<Menu.Item key="mlg" disabled icon={<FileZipOutlined />}>
2021-03-22 14:29:03 -07:00
MLG
</Menu.Item>
2021-09-26 04:28:19 -07:00
<Menu.Item key="msl" disabled icon={<FileTextOutlined />}>
2021-03-22 14:29:03 -07:00
MSL
</Menu.Item>
2021-09-26 04:28:19 -07:00
<Menu.Item key="csv" disabled icon={<FileExcelOutlined />}>
2021-03-22 14:29:03 -07:00
CSV
</Menu.Item>
</SubMenu>
</SubMenu>
2021-09-26 04:28:19 -07:00
<Menu.Item key="link" disabled icon={<LinkOutlined />}>
2021-03-22 14:29:03 -07:00
Create link
</Menu.Item>
2021-09-26 04:28:19 -07:00
<Menu.Item key="publish" disabled icon={<GlobalOutlined />}>
2021-03-22 14:29:03 -07:00
Publish to Hub
</Menu.Item>
</Menu>
);
const searchInput = useRef<Input | null>(null);
const handleGlobalKeyboard = (e: KeyboardEvent) => {
if (isCommand(e)) {
if (searchInput) {
e.preventDefault();
searchInput.current!.focus();
}
}
if (isToggleSidebar(e)) {
e.preventDefault();
store.dispatch({ type: 'ui/toggleSidebar' });
}
};
useEffect(() => {
document.addEventListener('keydown', handleGlobalKeyboard);
return () => document.removeEventListener('keydown', handleGlobalKeyboard);
});
return (
<Header className="app-top-bar">
<Row>
2021-03-22 15:24:39 -07:00
<Col span={0} md={6} sm={0} />
<Col span={12} md={10} sm={16} style={{ textAlign: 'center' }}>
2021-03-22 14:29:03 -07:00
<Radio.Group
key={pathname}
defaultValue={matchedTabPath?.url}
optionType="button"
buttonStyle="solid"
onChange={(e) => history.push(e.target.value)}
>
2021-03-22 15:24:39 -07:00
<Radio.Button value={Routes.TUNE}>
<Space>
<ToolOutlined />
{sm && 'Tune'}
</Space>
</Radio.Button>
<Radio.Button value={Routes.LOG}>
<Space>
<FundOutlined />
{sm && 'Log'}
</Space>
</Radio.Button>
<Radio.Button value={Routes.DIAGNOSE}>
<Space>
<SettingOutlined />
{sm && 'Diagnose'}
</Space>
</Radio.Button>
2021-03-22 14:29:03 -07:00
</Radio.Group>
</Col>
<Col span={12} md={8} sm={8} style={{ textAlign: 'right' }}>
<Space>
2021-03-22 14:29:03 -07:00
<Tooltip title={
<>
<Typography.Text keyboard>{isMac ? '⌘' : 'CTRL'}</Typography.Text>
<Typography.Text keyboard>P</Typography.Text>
</>
}>
<Button icon={<SearchOutlined />} ref={searchInput as any} />
2021-03-22 14:29:03 -07:00
</Tooltip>
<Dropdown
overlay={shareMenu}
placement="bottomCenter"
2021-03-23 12:36:06 -07:00
trigger={['click']}
2021-03-22 14:29:03 -07:00
>
<Button icon={<ShareAltOutlined />}>
<DownOutlined />
</Button>
</Dropdown>
<Dropdown
overlay={userMenu}
placement="bottomCenter"
2021-03-23 12:36:06 -07:00
trigger={['click']}
2021-03-22 14:29:03 -07:00
>
<Button icon={<UserOutlined />}>
<DownOutlined />
</Button>
</Dropdown>
</Space>
</Col>
</Row>
</Header>
);
};
export default TopBar;