misskey-tools/apps/frontend/src/GeneralLayout.tsx

91 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-06-09 12:20:13 +09:00
import React from 'react';
import { useTranslation } from 'react-i18next';
2022-06-09 12:34:31 +09:00
import { useDispatch } from 'react-redux';
2022-06-09 12:20:13 +09:00
import styled from 'styled-components';
2022-06-09 12:34:31 +09:00
import { NavigationMenu } from './components/NavigationMenu';
2022-06-19 10:42:00 +09:00
import { IsMobileProp } from './misc/is-mobile-prop';
2022-06-09 12:20:13 +09:00
import { useGetMetaQuery, useGetSessionQuery } from './services/session';
import { useSelector } from './store';
2022-06-09 12:34:31 +09:00
import { setDrawerShown } from './store/slices/screen';
2022-06-09 12:20:13 +09:00
const Container = styled.div<IsMobileProp>`
padding: var(--margin);
position: relative;
`;
const Sidebar = styled.nav`
width: 320px;
position: fixed;
top: var(--margin);
left: var(--margin);
`;
const Main = styled.main<IsMobileProp>`
flex: 1;
margin-top: 80px;
margin-left: ${p => !p.isMobile ? `${320 + 16}px` : 0};
min-width: 0;
`;
const MobileHeader = styled.header`
position: fixed;
top: 0;
left: 0;
right: 0;
height: 64px;
background: var(--panel);
2022-06-21 12:55:05 +09:00
z-index: 1000;
2022-06-09 12:20:13 +09:00
> h1 {
font-size: 1rem;
margin-bottom: 0;
}
`;
export const GeneralLayout: React.FC = ({children}) => {
2023-02-25 17:13:07 +09:00
const { data: session } = useGetSessionQuery(undefined);
const { data: meta } = useGetMetaQuery(undefined);
const { isMobile, title, isDrawerShown } = useSelector(state => state.screen);
const {t} = useTranslation();
2022-06-09 12:20:13 +09:00
2023-02-25 17:13:07 +09:00
const dispatch = useDispatch();
2022-06-09 12:20:13 +09:00
2023-02-25 17:13:07 +09:00
return (
<Container isMobile={isMobile}>
{isMobile && (
<MobileHeader className="navbar hstack f-middle shadow-2 pl-2">
<button className="btn flat" onClick={() => dispatch(setDrawerShown(!isDrawerShown))}>
<i className="fas fa-bars"></i>
</button>
<h1>{t(title ?? 'title')}</h1>
</MobileHeader>
)}
<div>
{!isMobile && (
<Sidebar className="pa-2">
<NavigationMenu />
</Sidebar>
)}
<Main isMobile={isMobile}>
{session && meta && meta.currentTokenVersion !== session.tokenVersion && (
<div className="alert bg-danger flex f-middle mb-2">
<i className="icon fas fa-circle-exclamation"></i>
{t('shouldUpdateToken')}
<a className="btn primary" href={`/login?host=${encodeURIComponent(session.host)}`}>
{t('update')}
</a>
</div>
)}
{children}
</Main>
</div>
<div className={`drawer-container ${isDrawerShown ? 'active' : ''}`}>
<div className="backdrop" onClick={() => dispatch(setDrawerShown(false))}></div>
<div className="drawer pa-2" onClick={e => e.stopPropagation()}>
<NavigationMenu />
</div>
</div>
</Container>
);
2022-06-09 12:20:13 +09:00
};