ntfy/web/src/App.js

99 lines
2.9 KiB
JavaScript
Raw Normal View History

2022-02-18 14:49:51 +00:00
import * as React from 'react';
import Container from '@mui/material/Container';
import Typography from '@mui/material/Typography';
import Box from '@mui/material/Box';
import Link from '@mui/material/Link';
2022-02-18 16:07:04 +00:00
import {useState} from "react";
2022-02-18 19:41:01 +00:00
import Subscription from './Subscription';
import WsConnection from './WsConnection';
2022-02-18 14:49:51 +00:00
function SubscriptionList(props) {
return (
<div className="subscriptionList">
2022-02-18 19:41:01 +00:00
{props.subscriptions.map(subscription =>
<SubscriptionItem key={subscription.url} subscription={subscription}/>)}
2022-02-18 14:49:51 +00:00
</div>
);
}
function SubscriptionItem(props) {
2022-02-18 19:41:01 +00:00
const subscription = props.subscription;
2022-02-18 14:49:51 +00:00
return (
<div>
2022-02-18 19:41:01 +00:00
<div>{subscription.shortUrl()}</div>
2022-02-18 14:49:51 +00:00
</div>
);
}
function NotificationList(props) {
return (
<div className="notificationList">
{props.notifications.map(notification => <NotificationItem key={notification.id} {...notification}/>)}
<div className="date">{props.timestamp}</div>
<div className="message">{props.message}</div>
</div>
);
}
2022-02-18 19:41:01 +00:00
const NotificationItem = (props) => {
2022-02-18 14:49:51 +00:00
return (
<div>
<div className="date">{props.time}</div>
<div className="message">{props.message}</div>
</div>
);
}
2022-02-18 19:41:01 +00:00
const defaultBaseUrl = "https://ntfy.sh"
const SubscriptionAddForm = (props) => {
2022-02-18 16:07:04 +00:00
const [topic, setTopic] = useState("");
const handleSubmit = (ev) => {
ev.preventDefault();
2022-02-18 19:41:01 +00:00
props.onSubmit(new Subscription(defaultBaseUrl, topic));
setTopic('');
2022-02-18 16:07:04 +00:00
}
return (
<form onSubmit={handleSubmit}>
<input
type="text"
value={topic}
onChange={ev => setTopic(ev.target.value)}
placeholder="Topic name, e.g. phil_alerts"
required
/>
</form>
);
}
2022-02-18 19:41:01 +00:00
const App = () => {
2022-02-18 16:07:04 +00:00
const [state, setState] = useState({
subscriptions: [],
});
2022-02-18 14:49:51 +00:00
const notifications = [
{id: "qGrfmhp3vK", times: 1645193395, message: "Message 1"},
{id: "m4YYjfxwyT", times: 1645193428, message: "Message 2"}
];
2022-02-18 16:07:04 +00:00
const addSubscription = (newSubscription) => {
2022-02-18 19:41:01 +00:00
const connection = new WsConnection(newSubscription.wsUrl());
connection.start();
2022-02-18 16:07:04 +00:00
setState(prevState => ({
subscriptions: [...prevState.subscriptions, newSubscription],
}));
}
2022-02-18 14:49:51 +00:00
return (
<Container maxWidth="sm">
<Box sx={{my: 4}}>
<Typography variant="h4" component="h1" gutterBottom>
ntfy
</Typography>
2022-02-18 16:07:04 +00:00
<SubscriptionAddForm onSubmit={addSubscription}/>
<SubscriptionList subscriptions={state.subscriptions}/>
2022-02-18 14:49:51 +00:00
<NotificationList notifications={notifications}/>
</Box>
</Container>
);
}
2022-02-18 19:41:01 +00:00
export default App;