ionian/src/client/page/contentinfo.tsx

80 lines
2.8 KiB
TypeScript
Raw Normal View History

2021-01-06 20:16:27 +09:00
import React, { useState, useEffect } 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-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);
const menu_list = (
<NavList>
<NavItem name="Back" to="/" icon={<ArrowBackIcon/>}></NavItem>
</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 (
<Headline menu={menu_list}>
<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 (
<Headline menu={menu_list}>
<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 10:46:19 +09:00
return (<Headline menu={menu_list}>
<LoadingCircle />
</Headline>
);
2021-01-06 20:16:27 +09:00
}
else return (<Switch>
<Route exact path={`${prop.match.path}/:id`}>
2021-01-08 10:46:19 +09:00
<Headline menu={menu_list}>
<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 10:46:19 +09:00
<Headline menu={menu_list}>
<MangaReader content={info.content}></MangaReader>
</Headline>
2021-01-06 20:16:27 +09:00
</Route>
<Route>
2021-01-08 10:46:19 +09:00
<Headline menu={menu_list}>
<div>404 Not Found invalid url : {prop.match.path}</div>
</Headline>
2021-01-06 20:16:27 +09:00
</Route>
</Switch>);
}