2022-02-21 21:24:13 +00:00
|
|
|
import Container from "@mui/material/Container";
|
2022-02-28 16:52:50 +00:00
|
|
|
import {CardContent, Link, Stack} from "@mui/material";
|
2022-02-21 21:24:13 +00:00
|
|
|
import Card from "@mui/material/Card";
|
|
|
|
import Typography from "@mui/material/Typography";
|
|
|
|
import * as React from "react";
|
2022-03-02 21:16:30 +00:00
|
|
|
import {formatMessage, formatTitle, topicShortUrl, unmatchedTags} from "../app/utils";
|
2022-02-25 15:23:04 +00:00
|
|
|
import IconButton from "@mui/material/IconButton";
|
|
|
|
import CloseIcon from '@mui/icons-material/Close';
|
2022-02-28 16:52:50 +00:00
|
|
|
import {Paragraph, VerticallyCenteredContainer} from "./styles";
|
2022-03-02 03:41:49 +00:00
|
|
|
import {useLiveQuery} from "dexie-react-hooks";
|
|
|
|
import db from "../app/db";
|
2022-02-21 21:24:13 +00:00
|
|
|
|
2022-02-28 16:52:50 +00:00
|
|
|
const Notifications = (props) => {
|
2022-02-25 15:23:04 +00:00
|
|
|
const subscription = props.subscription;
|
2022-03-02 03:41:49 +00:00
|
|
|
const notifications = useLiveQuery(() => {
|
|
|
|
return db.notifications
|
2022-03-02 21:16:30 +00:00
|
|
|
.where({ subscriptionId: subscription.id })
|
2022-03-02 03:41:49 +00:00
|
|
|
.toArray();
|
|
|
|
}, [subscription]);
|
|
|
|
if (!notifications || notifications.length === 0) {
|
2022-02-28 16:52:50 +00:00
|
|
|
return <NothingHereYet subscription={subscription}/>;
|
|
|
|
}
|
2022-03-02 21:16:30 +00:00
|
|
|
const sortedNotifications = Array.from(notifications)
|
|
|
|
.sort((a, b) => a.time < b.time ? 1 : -1);
|
2022-02-21 21:24:13 +00:00
|
|
|
return (
|
2022-02-28 16:52:50 +00:00
|
|
|
<Container maxWidth="lg" sx={{marginTop: 3, marginBottom: 3}}>
|
2022-02-24 17:26:07 +00:00
|
|
|
<Stack spacing={3}>
|
2022-02-21 21:24:13 +00:00
|
|
|
{sortedNotifications.map(notification =>
|
2022-02-25 15:23:04 +00:00
|
|
|
<NotificationItem
|
|
|
|
key={notification.id}
|
|
|
|
subscriptionId={subscription.id}
|
|
|
|
notification={notification}
|
|
|
|
/>)}
|
2022-02-21 21:24:13 +00:00
|
|
|
</Stack>
|
|
|
|
</Container>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const NotificationItem = (props) => {
|
2022-03-02 21:16:30 +00:00
|
|
|
const subscriptionId = props.subscriptionId;
|
2022-02-21 21:24:13 +00:00
|
|
|
const notification = props.notification;
|
|
|
|
const date = new Intl.DateTimeFormat('default', {dateStyle: 'short', timeStyle: 'short'})
|
|
|
|
.format(new Date(notification.time * 1000));
|
2022-02-24 17:26:07 +00:00
|
|
|
const otherTags = unmatchedTags(notification.tags);
|
|
|
|
const tags = (otherTags.length > 0) ? otherTags.join(', ') : null;
|
2022-03-02 21:16:30 +00:00
|
|
|
const handleDelete = async () => {
|
|
|
|
console.log(`[Notifications] Deleting notification ${notification.id} from ${subscriptionId}`);
|
|
|
|
await db.notifications.delete(notification.id); // FIXME
|
|
|
|
}
|
2022-02-21 21:24:13 +00:00
|
|
|
return (
|
|
|
|
<Card sx={{ minWidth: 275 }}>
|
|
|
|
<CardContent>
|
2022-03-02 21:16:30 +00:00
|
|
|
<IconButton onClick={handleDelete} sx={{ float: 'right', marginRight: -1, marginTop: -1 }}>
|
2022-02-25 15:23:04 +00:00
|
|
|
<CloseIcon />
|
|
|
|
</IconButton>
|
2022-02-24 17:26:07 +00:00
|
|
|
<Typography sx={{ fontSize: 14 }} color="text.secondary">
|
|
|
|
{date}
|
|
|
|
{[1,2,4,5].includes(notification.priority) &&
|
|
|
|
<img
|
|
|
|
src={`static/img/priority-${notification.priority}.svg`}
|
|
|
|
alt={`Priority ${notification.priority}`}
|
|
|
|
style={{ verticalAlign: 'bottom' }}
|
|
|
|
/>}
|
|
|
|
</Typography>
|
|
|
|
{notification.title && <Typography variant="h5" component="div">{formatTitle(notification)}</Typography>}
|
|
|
|
<Typography variant="body1" sx={{ whiteSpace: 'pre-line' }}>{formatMessage(notification)}</Typography>
|
2022-02-21 21:24:13 +00:00
|
|
|
{tags && <Typography sx={{ fontSize: 14 }} color="text.secondary">Tags: {tags}</Typography>}
|
|
|
|
</CardContent>
|
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-28 16:52:50 +00:00
|
|
|
const NothingHereYet = (props) => {
|
2022-03-02 21:16:30 +00:00
|
|
|
const shortUrl = topicShortUrl(props.subscription.baseUrl, props.subscription.topic);
|
2022-02-28 16:52:50 +00:00
|
|
|
return (
|
|
|
|
<VerticallyCenteredContainer maxWidth="xs">
|
|
|
|
<Typography variant="h5" align="center" sx={{ paddingBottom: 1 }}>
|
|
|
|
<img src="static/img/ntfy-outline.svg" height="64" width="64" alt="No notifications"/><br />
|
|
|
|
You haven't received any notifications for this topic yet.
|
|
|
|
</Typography>
|
|
|
|
<Paragraph>
|
|
|
|
To send notifications to this topic, simply PUT or POST to the topic URL.
|
|
|
|
</Paragraph>
|
|
|
|
<Paragraph>
|
|
|
|
Example:<br/>
|
|
|
|
<tt>
|
2022-03-02 21:16:30 +00:00
|
|
|
$ curl -d "Hi" {shortUrl}
|
2022-02-28 16:52:50 +00:00
|
|
|
</tt>
|
|
|
|
</Paragraph>
|
|
|
|
<Paragraph>
|
|
|
|
For more detailed instructions, check out the <Link href="https://ntfy.sh" target="_blank" rel="noopener">website</Link> or
|
|
|
|
{" "}<Link href="https://ntfy.sh/docs" target="_blank" rel="noopener">documentation</Link>.
|
|
|
|
</Paragraph>
|
|
|
|
</VerticallyCenteredContainer>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Notifications;
|