ionian/src/client/page/contentinfo.tsx

92 lines
3.1 KiB
TypeScript
Raw Normal View History

2021-01-08 17:26:38 +09:00
import React, { useState, useEffect, useContext } from 'react';
2021-01-08 10:46:19 +09:00
import { Redirect, Route, Switch, useHistory, useRouteMatch, match as MatchType, Link as RouterLink, useParams, useLocation } from 'react-router-dom';
2021-01-06 20:16:27 +09:00
import ContentAccessor, { Content } from '../accessor/contents';
import { LoadingCircle } from '../component/loading';
import { Link, Paper, makeStyles, Theme, Box, useTheme, Typography } from '@material-ui/core';
2021-01-08 10:46:19 +09:00
import {ArrowBack as ArrowBackIcon } from '@material-ui/icons';
2021-01-09 02:28:40 +09:00
import { getPresenter } from './reader/reader';
2021-01-11 20:47:42 +09:00
import { BackItem, CommonMenuList, ContentInfo, Headline, NavItem, NavList } from '../component/mod';
2021-01-06 20:16:27 +09:00
export const makeContentInfoUrl = (id: number) => `/doc/${id}`;
export const makeMangaReaderUrl = (id: number) => `/doc/${id}/reader`;
2021-01-06 22:09:53 +09:00
type ContentState = {
2021-01-06 20:16:27 +09:00
content: Content | undefined,
notfound: boolean,
}
2021-01-09 02:28:40 +09:00
const useStyles = makeStyles((theme:Theme)=>({
noPaddingContent:{
display:'flex',
flexDirection: 'column',
flexGrow: 1,
},
noPaddingToolbar:{
flex: '0 1 auto',
...theme.mixins.toolbar,
}
}));
2021-01-06 20:16:27 +09:00
export const ContentAbout = (prop: { match: MatchType }) => {
2021-01-08 10:46:19 +09:00
const match = useRouteMatch<{id:string}>("/doc/:id");
2021-01-06 20:16:27 +09:00
if (match == null) {
throw new Error("unreachable");
}
2021-01-08 10:46:19 +09:00
const id = Number.parseInt(match.params['id']);
2021-01-06 22:09:53 +09:00
const [info, setInfo] = useState<ContentState>({ content: undefined, notfound:false });
2021-01-11 20:47:42 +09:00
const menu_list = () => <CommonMenuList></CommonMenuList>;
2021-01-09 02:28:40 +09:00
2021-01-06 20:16:27 +09:00
useEffect(() => {
(async () => {
if (!isNaN(id)) {
const c = await ContentAccessor.findById(id);
setInfo({ content: c, notfound: c === undefined });
}
})()
}, []);
2021-01-09 02:28:40 +09:00
const classes = useStyles();
2021-01-06 20:16:27 +09:00
if (isNaN(id)) {
2021-01-08 10:46:19 +09:00
return (
2021-01-08 17:26:38 +09:00
<Headline menu={menu_list()}>
2021-01-08 10:46:19 +09:00
<Typography variant='h2'>Oops. Invalid ID</Typography>
</Headline>
);
2021-01-06 20:16:27 +09:00
}
else if(info.notfound){
2021-01-08 10:46:19 +09:00
return (
2021-01-08 17:26:38 +09:00
<Headline menu={menu_list()}>
2021-01-08 10:46:19 +09:00
<Typography variant='h2'>Content has been removed.</Typography>
</Headline>
)
2021-01-06 20:16:27 +09:00
}
else if (info.content === undefined) {
2021-01-08 17:26:38 +09:00
return (<Headline menu={menu_list()}>
2021-01-08 10:46:19 +09:00
<LoadingCircle />
</Headline>
);
2021-01-06 20:16:27 +09:00
}
2021-01-08 17:26:38 +09:00
else{
2021-01-09 02:28:40 +09:00
const ReaderPage = getPresenter(info.content);
2021-01-08 17:26:38 +09:00
return (<Switch>
2021-01-06 20:16:27 +09:00
<Route exact path={`${prop.match.path}/:id`}>
2021-01-08 17:26:38 +09:00
<Headline menu={menu_list()}>
2021-01-08 10:46:19 +09:00
<ContentInfo content={info.content}></ContentInfo>
</Headline>
2021-01-06 20:16:27 +09:00
</Route>
<Route exact path={`${prop.match.path}/:id/reader`}>
2021-01-09 02:28:40 +09:00
<Headline menu={menu_list(`${prop.match.path}/${id}`)} classes={{
content:classes.noPaddingContent,
toolbar:classes.noPaddingToolbar
}}>
<ReaderPage content={info.content}></ReaderPage>
2021-01-08 10:46:19 +09:00
</Headline>
2021-01-06 20:16:27 +09:00
</Route>
<Route>
2021-01-08 17:26:38 +09:00
<Headline menu={menu_list()}>
2021-01-08 10:46:19 +09:00
<div>404 Not Found invalid url : {prop.match.path}</div>
</Headline>
2021-01-06 20:16:27 +09:00
</Route>
</Switch>);
2021-01-08 17:26:38 +09:00
}
2021-01-06 20:16:27 +09:00
}