diaREact/resources/js/components/pages/Page.tsx

88 lines
3.2 KiB
TypeScript
Raw Permalink Normal View History

2022-02-11 17:26:15 +01:00
import {
Alert,
AlertTitle,
Card, CardContent,
2022-02-11 17:26:15 +01:00
CardHeader, Collapse,
Grid, IconButton
2022-02-11 17:26:15 +01:00
} from '@mui/material';
import MoreVertIcon from '@mui/icons-material/MoreVert';
import * as React from 'react';
import {EncryptStorage} from 'storage-encryption';
import {Delete, Download} from "@mui/icons-material";
import MDEditor from "@uiw/react-md-editor";
import {PropPage} from "../../interfaces/PropPage";
2022-02-11 17:26:15 +01:00
const Page = function({page, url, passphrase, remove}: PropPage) {
const [more, setMore] = React.useState<boolean>(false);
2022-02-11 17:26:15 +01:00
const handleMoreClick = () => { setMore(!more); };
const encryptStorage = new EncryptStorage(passphrase);
2022-02-11 17:26:15 +01:00
try {
const [title, setTitle] = React.useState<string>(encryptStorage.decrypt(page.id + "title"));
const [content, setContent] = React.useState<string>(encryptStorage.decrypt(page.id + "text"));
const onLoad = async () => {
2022-02-11 17:26:15 +01:00
if (localStorage.getItem(page.id + "text") === null) {
const response = await fetch(url.replace("replace_me", page.id));
2022-02-11 17:26:15 +01:00
const json = await response.json();
2022-02-11 17:26:15 +01:00
localStorage.setItem(page.id + "title", json.metadata.title);
localStorage.setItem(page.id + "text", json.content);
}
setTitle(encryptStorage.decrypt(page.id + "title"));
setContent(encryptStorage.decrypt(page.id + "text"));
}
let icons;
if (content === null || content === "") {
icons = (<div>
<IconButton aria-label="load" onClick={onLoad} >
<Download />
</IconButton>
<IconButton aria-label="settings" onClick={handleMoreClick}>
<MoreVertIcon />
</IconButton>
</div>);
} else {
icons = (
<IconButton aria-label="settings" onClick={handleMoreClick}>
<MoreVertIcon />
</IconButton>
);
}
2022-02-11 17:26:15 +01:00
return (
<Grid item xs={12} sm={12} md={12} id={page.id}>
2022-02-11 17:26:15 +01:00
<Card>
<CardHeader
action={
icons
2022-02-11 17:26:15 +01:00
}
title={title}
subheader={page.date}
>
<Collapse in={more} timeout="auto" unmountOnExit>
<IconButton aria-label="remove" onClick={() => {remove(page.id)}} >
<Delete />
</IconButton>
</Collapse>
</CardHeader>
<CardContent>
<MDEditor.Markdown source={content} />
</CardContent>
2022-02-11 17:26:15 +01:00
</Card>
</Grid>
);
} catch (e) {
console.error(e);
return (
<Grid item xs={12} sm={12} md={12} id={page.id}>
<Alert severity="error">
<AlertTitle>Erreur</AlertTitle>
Cette page ne peut pas être décodée <strong>Réindiquez votre clef!</strong>
</Alert>
</Grid>
);
}
2022-02-11 17:26:15 +01:00
}
export default React.forwardRef(Page);