ionian/src/client/page/contentinfo.tsx

91 lines
3.2 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-06 22:09:53 +09:00
import { MangaReader } from './reader/manga';
2021-01-08 10:46:19 +09:00
import { ContentInfo, Headline, NavItem, NavList } from '../component/mod';
2021-01-08 17:26:38 +09:00
import {BackLinkContext} from '../state';
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,
}
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-08 10:46:19 +09:00
const location = useLocation();
console.log("state : "+location.state);
2021-01-08 17:26:38 +09:00
const menu_list = (link?:string)=>(
2021-01-08 10:46:19 +09:00
<NavList>
2021-01-08 17:26:38 +09:00
<BackLinkContext.Consumer>
{
(ctx) => link === undefined ?
<NavItem name="Back" to={ctx.path} icon={<ArrowBackIcon/>}/>
: <NavItem name="Back" to={link} icon={<ArrowBackIcon/>}/>
}
</BackLinkContext.Consumer>
2021-01-08 10:46:19 +09:00
</NavList>
);
2021-01-06 20:16:27 +09:00
useEffect(() => {
(async () => {
2021-01-08 10:46:19 +09:00
console.log("mount content about");
2021-01-06 20:16:27 +09:00
if (!isNaN(id)) {
const c = await ContentAccessor.findById(id);
setInfo({ content: c, notfound: c === undefined });
}
})()
2021-01-08 10:46:19 +09:00
return ()=>{console.log("unmount content about")}
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{
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-08 17:26:38 +09:00
<Headline menu={menu_list(`${prop.match.path}/${id}`)}>
2021-01-08 10:46:19 +09:00
<MangaReader content={info.content}></MangaReader>
</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
}