ntfy/web/src/components/App.js

153 lines
5.6 KiB
JavaScript
Raw Normal View History

2022-02-18 15:49:51 +01:00
import * as React from 'react';
2022-02-21 02:04:03 +01:00
import {useEffect, useState} from 'react';
2022-02-18 15:49:51 +01:00
import Box from '@mui/material/Box';
2022-02-25 02:18:46 +01:00
import {ThemeProvider} from '@mui/material/styles';
2022-02-20 01:48:33 +01:00
import CssBaseline from '@mui/material/CssBaseline';
import Toolbar from '@mui/material/Toolbar';
import Notifications from "./Notifications";
2022-02-20 04:26:58 +01:00
import theme from "./theme";
import connectionManager from "../app/ConnectionManager";
2022-02-25 18:46:22 +01:00
import Navigation from "./Navigation";
import ActionBar from "./ActionBar";
2022-02-26 16:14:43 +01:00
import notificationManager from "../app/NotificationManager";
import NoTopics from "./NoTopics";
2022-02-28 22:56:38 +01:00
import Preferences from "./Preferences";
import {useLiveQuery} from "dexie-react-hooks";
2022-03-02 22:16:30 +01:00
import poller from "../app/Poller";
import pruner from "../app/Pruner";
import subscriptionManager from "../app/SubscriptionManager";
import userManager from "../app/UserManager";
2022-03-04 22:10:04 +01:00
import {BrowserRouter, Route, Routes, useLocation, useNavigate} from "react-router-dom";
import {subscriptionRoute} from "../app/utils";
2022-02-25 02:18:46 +01:00
2022-03-05 14:52:52 +01:00
// TODO support unsubscribed routes
// TODO add "home" route that is selected when nothing else fits
2022-03-04 18:10:11 +01:00
// TODO new notification indicator
2022-03-05 14:52:52 +01:00
// TODO sound
// TODO "copy url" toast
// TODO "copy link url" button
2022-02-26 17:51:45 +01:00
2022-02-18 20:41:01 +01:00
const App = () => {
2022-03-04 22:10:04 +01:00
return (
<BrowserRouter>
<ThemeProvider theme={theme}>
<CssBaseline/>
<Root/>
</ThemeProvider>
</BrowserRouter>
);
}
const Root = () => {
2022-02-25 02:18:46 +01:00
const [mobileDrawerOpen, setMobileDrawerOpen] = useState(false);
2022-02-26 16:14:43 +01:00
const [notificationsGranted, setNotificationsGranted] = useState(notificationManager.granted());
2022-03-04 22:10:04 +01:00
const navigate = useNavigate();
const location = useLocation();
const users = useLiveQuery(() => userManager.all());
2022-03-04 22:10:04 +01:00
const subscriptions = useLiveQuery(() => subscriptionManager.all());
2022-03-05 14:52:52 +01:00
const selectedSubscription = findSelected(location, subscriptions);
2022-03-04 22:10:04 +01:00
2022-03-02 22:16:30 +01:00
const handleSubscribeSubmit = async (subscription) => {
console.log(`[App] New subscription: ${subscription.id}`, subscription);
2022-03-04 22:10:04 +01:00
navigate(subscriptionRoute(subscription));
2022-02-26 16:14:43 +01:00
handleRequestPermission();
};
2022-03-06 04:33:34 +01:00
2022-02-26 16:14:43 +01:00
const handleRequestPermission = () => {
notificationManager.maybeRequestPermission(granted => setNotificationsGranted(granted));
2022-02-26 16:14:43 +01:00
};
2022-03-06 04:33:34 +01:00
2022-02-24 21:17:47 +01:00
useEffect(() => {
2022-03-02 22:16:30 +01:00
poller.startWorker();
pruner.startWorker();
}, [/* initial render */]);
2022-03-06 04:33:34 +01:00
2022-02-21 02:04:03 +01:00
useEffect(() => {
2022-03-02 22:16:30 +01:00
const handleNotification = async (subscriptionId, notification) => {
try {
const added = await subscriptionManager.addNotification(subscriptionId, notification);
if (added) {
2022-03-05 14:52:52 +01:00
const defaultClickAction = (subscription) => navigate(subscriptionRoute(subscription)); // FIXME
await notificationManager.notify(subscriptionId, notification, defaultClickAction)
}
2022-03-02 22:16:30 +01:00
} catch (e) {
console.error(`[App] Error handling notification`, e);
}
};
connectionManager.registerStateListener(subscriptionManager.updateState);
connectionManager.registerNotificationListener(handleNotification);
return () => {
connectionManager.resetStateListener();
connectionManager.resetNotificationListener();
}
// This is for the use of 'navigate' // FIXME
//eslint-disable-next-line
}, [/* initial render */]);
2022-03-06 04:33:34 +01:00
useEffect(() => {
connectionManager.refresh(subscriptions, users);
}, [subscriptions, users]); // Dangle!
2022-02-18 15:49:51 +01:00
return (
2022-03-04 22:10:04 +01:00
<Box sx={{display: 'flex'}}>
2022-02-25 02:18:46 +01:00
<CssBaseline/>
2022-03-04 22:10:04 +01:00
<ActionBar
subscriptions={subscriptions}
selectedSubscription={selectedSubscription}
onMobileDrawerToggle={() => setMobileDrawerOpen(!mobileDrawerOpen)}
/>
<Box component="nav" sx={{width: {sm: Navigation.width}, flexShrink: {sm: 0}}}>
<Navigation
subscriptions={subscriptions}
2022-02-25 02:18:46 +01:00
selectedSubscription={selectedSubscription}
2022-03-04 22:10:04 +01:00
mobileDrawerOpen={mobileDrawerOpen}
notificationsGranted={notificationsGranted}
2022-02-25 02:18:46 +01:00
onMobileDrawerToggle={() => setMobileDrawerOpen(!mobileDrawerOpen)}
2022-03-04 22:10:04 +01:00
onSubscribeSubmit={handleSubscribeSubmit}
onRequestPermissionClick={handleRequestPermission}
2022-02-25 02:18:46 +01:00
/>
2022-02-18 15:49:51 +01:00
</Box>
2022-03-04 22:10:04 +01:00
<Main>
<Toolbar/>
<Routes>
<Route path="/" element={<NoTopics />} />
<Route path="settings" element={<Preferences />} />
2022-03-05 14:52:52 +01:00
<Route path=":baseUrl/:topic" element={<Notifications subscription={selectedSubscription}/>} />
<Route path=":topic" element={<Notifications subscription={selectedSubscription}/>} />
2022-03-04 22:10:04 +01:00
</Routes>
</Main>
</Box>
2022-02-18 15:49:51 +01:00
);
}
2022-02-18 20:41:01 +01:00
2022-03-02 22:16:30 +01:00
const Main = (props) => {
return (
<Box
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-05 14:52:52 +01:00
const findSelected = (location, subscriptions) => {
if (!subscriptions || !location) {
return null;
}
const [subscription] = subscriptions
.filter(s => location.pathname === subscriptionRoute(s));
return subscription;
};
2022-02-18 20:41:01 +01:00
export default App;