ntfy/web/src/components/App.js

233 lines
8 KiB
JavaScript
Raw Normal View History

2022-02-19 03:49:51 +13:00
import * as React from 'react';
2022-02-21 14:04:03 +13:00
import {useEffect, useState} from 'react';
2022-02-19 03:49:51 +13:00
import Box from '@mui/material/Box';
2022-02-25 14:18:46 +13:00
import {ThemeProvider} from '@mui/material/styles';
2022-02-20 13:48:33 +13:00
import CssBaseline from '@mui/material/CssBaseline';
import Toolbar from '@mui/material/Toolbar';
import Notifications from "./Notifications";
2022-02-20 16:26:58 +13:00
import theme from "./theme";
2022-02-26 06:46:22 +13:00
import Navigation from "./Navigation";
import ActionBar from "./ActionBar";
2022-03-06 18:02:27 +13:00
import notifier from "../app/Notifier";
2022-03-01 10:56:38 +13:00
import Preferences from "./Preferences";
import {useLiveQuery} from "dexie-react-hooks";
import subscriptionManager from "../app/SubscriptionManager";
import userManager from "../app/UserManager";
import {BrowserRouter, Outlet, Route, Routes, useOutletContext, useParams} from "react-router-dom";
import {expandUrl, topicUrl} from "../app/utils";
import ErrorBoundary from "./ErrorBoundary";
import routes from "./routes";
import {useAutoSubscribe, useConnectionListeners, useLocalStorageMigration} from "./hooks";
2022-03-21 06:52:07 +13:00
import {Backdrop, ListItemIcon, ListItemText, Menu} from "@mui/material";
import Paper from "@mui/material/Paper";
import IconButton from "@mui/material/IconButton";
import {MoreVert} from "@mui/icons-material";
import MenuItem from "@mui/material/MenuItem";
import TextField from "@mui/material/TextField";
import SendIcon from "@mui/icons-material/Send";
import priority1 from "../img/priority-1.svg";
import priority2 from "../img/priority-2.svg";
import priority4 from "../img/priority-4.svg";
import priority5 from "../img/priority-5.svg";
import api from "../app/Api";
import SendDialog from "./SendDialog";
2022-02-25 14:18:46 +13:00
2022-03-12 04:43:18 +13:00
// TODO add drag and drop
2022-03-06 18:02:27 +13:00
// TODO races when two tabs are open
2022-03-07 04:42:05 +13:00
// TODO investigate service workers
2022-02-27 05:51:45 +13:00
2022-02-19 08:41:01 +13:00
const App = () => {
2022-03-05 10:10:04 +13:00
return (
2022-03-11 09:37:50 +13:00
<BrowserRouter>
<ThemeProvider theme={theme}>
<CssBaseline/>
<ErrorBoundary>
<Routes>
<Route element={<Layout/>}>
2022-03-11 09:37:50 +13:00
<Route path={routes.root} element={<AllSubscriptions/>}/>
<Route path={routes.settings} element={<Preferences/>}/>
<Route path={routes.subscription} element={<SingleSubscription/>}/>
<Route path={routes.subscriptionExternal} element={<SingleSubscription/>}/>
</Route>
</Routes>
2022-03-11 09:37:50 +13:00
</ErrorBoundary>
</ThemeProvider>
</BrowserRouter>
2022-03-05 10:10:04 +13:00
);
}
2022-03-09 08:29:03 +13:00
const AllSubscriptions = () => {
const { subscriptions } = useOutletContext();
return <Notifications mode="all" subscriptions={subscriptions}/>;
2022-03-09 09:19:15 +13:00
};
2022-03-09 08:29:03 +13:00
const SingleSubscription = () => {
2022-03-09 09:19:15 +13:00
const { subscriptions, selected } = useOutletContext();
2022-03-09 14:26:15 +13:00
useAutoSubscribe(subscriptions, selected);
2022-03-09 08:29:03 +13:00
return <Notifications mode="one" subscription={selected}/>;
2022-03-09 09:19:15 +13:00
};
2022-03-09 08:29:03 +13:00
const Layout = () => {
const params = useParams();
2022-02-25 14:18:46 +13:00
const [mobileDrawerOpen, setMobileDrawerOpen] = useState(false);
2022-03-06 18:02:27 +13:00
const [notificationsGranted, setNotificationsGranted] = useState(notifier.granted());
const users = useLiveQuery(() => userManager.all());
2022-03-09 08:29:03 +13:00
const subscriptions = useLiveQuery(() => subscriptionManager.all());
2022-03-07 16:37:13 +13:00
const newNotificationsCount = subscriptions?.reduce((prev, cur) => prev + cur.new, 0) || 0;
2022-03-09 08:29:03 +13:00
const [selected] = (subscriptions || []).filter(s => {
return (params.baseUrl && expandUrl(params.baseUrl).includes(s.baseUrl) && params.topic === s.topic)
|| (window.location.origin === s.baseUrl && params.topic === s.topic)
});
2022-03-05 10:10:04 +13:00
useConnectionListeners(subscriptions, users);
useLocalStorageMigration();
useEffect(() => updateTitle(newNotificationsCount), [newNotificationsCount]);
2022-03-07 16:37:13 +13:00
2022-02-19 03:49:51 +13:00
return (
2022-03-05 10:10:04 +13:00
<Box sx={{display: 'flex'}}>
2022-02-25 14:18:46 +13:00
<CssBaseline/>
2022-03-21 06:52:07 +13:00
<DropZone/>
2022-03-05 10:10:04 +13:00
<ActionBar
selected={selected}
2022-03-05 10:10:04 +13:00
onMobileDrawerToggle={() => setMobileDrawerOpen(!mobileDrawerOpen)}
/>
2022-03-09 05:33:17 +13:00
<Navigation
subscriptions={subscriptions}
selectedSubscription={selected}
2022-03-09 05:33:17 +13:00
notificationsGranted={notificationsGranted}
mobileDrawerOpen={mobileDrawerOpen}
onMobileDrawerToggle={() => setMobileDrawerOpen(!mobileDrawerOpen)}
onNotificationGranted={setNotificationsGranted}
/>
2022-03-05 10:10:04 +13:00
<Main>
<Toolbar/>
2022-03-09 08:29:03 +13:00
<Outlet context={{ subscriptions, selected }}/>
2022-03-05 10:10:04 +13:00
</Main>
<Sender selected={selected}/>
2022-03-05 10:10:04 +13:00
</Box>
2022-02-19 03:49:51 +13:00
);
}
2022-02-19 08:41:01 +13:00
2022-03-03 10:16:30 +13:00
const Main = (props) => {
return (
<Box
2022-03-08 17:07:07 +13:00
id="main"
2022-03-03 10:16:30 +13:00
component="main"
sx={{
display: 'flex',
flexGrow: 1,
flexDirection: 'column',
padding: 3,
width: {sm: `calc(100% - ${Navigation.width}px)`},
height: '100vh',
overflow: 'auto',
backgroundColor: (theme) => theme.palette.mode === 'light' ? theme.palette.grey[100] : theme.palette.grey[900]
}}
>
{props.children}
</Box>
);
};
2022-03-21 06:52:07 +13:00
const Sender = (props) => {
const [message, setMessage] = useState("");
const [sendDialogOpen, setSendDialogOpen] = useState(false);
const subscription = props.selected;
const handleSendClick = () => {
api.publish(subscription.baseUrl, subscription.topic, message);
setMessage("");
2022-03-21 06:52:07 +13:00
};
if (!props.selected) {
return null;
}
2022-03-21 06:52:07 +13:00
return (
<Paper
elevation={3}
sx={{
display: "flex",
position: 'fixed',
bottom: 0,
right: 0,
padding: 2,
width: `calc(100% - ${Navigation.width}px)`,
backgroundColor: (theme) => theme.palette.mode === 'light' ? theme.palette.grey[100] : theme.palette.grey[900]
}}
>
<IconButton color="inherit" size="large" edge="start" onClick={() => setSendDialogOpen(true)}>
2022-03-21 06:52:07 +13:00
<MoreVert/>
</IconButton>
2022-03-27 02:32:13 +13:00
<TextField
autoFocus
margin="dense"
placeholder="Message"
type="text"
fullWidth
variant="standard"
value={message}
onChange={ev => setMessage(ev.target.value)}
onKeyPress={(ev) => {
if (ev.key === 'Enter') {
ev.preventDefault();
handleSendClick();
}
}}
2022-03-27 02:32:13 +13:00
/>
<IconButton color="inherit" size="large" edge="end" onClick={handleSendClick}>
2022-03-21 06:52:07 +13:00
<SendIcon/>
</IconButton>
<SendDialog
open={sendDialogOpen}
onCancel={() => setSendDialogOpen(false)}
topicUrl={topicUrl(subscription.baseUrl, subscription.topic)}
message={message}
/>
2022-03-21 06:52:07 +13:00
</Paper>
);
};
const DropZone = (props) => {
const [showDropZone, setShowDropZone] = useState(false);
useEffect(() => {
window.addEventListener('dragenter', () => setShowDropZone(true));
}, []);
const allowSubmit = () => true;
const allowDrag = (e) => {
if (allowSubmit()) {
e.dataTransfer.dropEffect = 'copy';
e.preventDefault();
}
};
const handleDrop = (e) => {
e.preventDefault();
setShowDropZone(false);
console.log(e.dataTransfer.files[0]);
};
if (!showDropZone) {
return null;
}
return (
<Backdrop
sx={{ color: '#fff', zIndex: 3500 }}
open={showDropZone}
onClick={() => setShowDropZone(false)}
onDragEnter={allowDrag}
onDragOver={allowDrag}
onDragLeave={() => setShowDropZone(false)}
onDrop={handleDrop}
>
</Backdrop>
);
};
const updateTitle = (newNotificationsCount) => {
document.title = (newNotificationsCount > 0) ? `(${newNotificationsCount}) ntfy` : "ntfy";
}
2022-02-19 08:41:01 +13:00
export default App;