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';
|
2022-03-01 05:52:50 +13:00
|
|
|
import Notifications from "./Notifications";
|
2022-02-20 16:26:58 +13:00
|
|
|
import theme from "./theme";
|
2022-02-24 14:30:12 +13:00
|
|
|
import connectionManager from "../app/ConnectionManager";
|
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 05:52:50 +13:00
|
|
|
import NoTopics from "./NoTopics";
|
2022-03-01 10:56:38 +13:00
|
|
|
import Preferences from "./Preferences";
|
2022-03-02 15:23:12 +13:00
|
|
|
import {useLiveQuery} from "dexie-react-hooks";
|
2022-03-03 10:16:30 +13:00
|
|
|
import poller from "../app/Poller";
|
|
|
|
import pruner from "../app/Pruner";
|
2022-03-04 10:52:07 +13:00
|
|
|
import subscriptionManager from "../app/SubscriptionManager";
|
|
|
|
import userManager from "../app/UserManager";
|
2022-03-05 10:10:04 +13:00
|
|
|
import {BrowserRouter, Route, Routes, useLocation, useNavigate} from "react-router-dom";
|
|
|
|
import {subscriptionRoute} from "../app/utils";
|
2022-02-25 14:18:46 +13:00
|
|
|
|
2022-03-06 02:52:52 +13:00
|
|
|
// TODO support unsubscribed routes
|
2022-03-06 16:11:32 +13:00
|
|
|
// TODO add "home" route that is selected when nothing else fits
|
2022-03-05 06:10:11 +13:00
|
|
|
// TODO new notification indicator
|
2022-03-06 16:11:32 +13:00
|
|
|
// TODO "copy url" toast
|
|
|
|
// TODO "copy link url" button
|
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 (
|
|
|
|
<BrowserRouter>
|
|
|
|
<ThemeProvider theme={theme}>
|
|
|
|
<CssBaseline/>
|
|
|
|
<Root/>
|
|
|
|
</ThemeProvider>
|
|
|
|
</BrowserRouter>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Root = () => {
|
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());
|
2022-03-05 10:10:04 +13:00
|
|
|
const location = useLocation();
|
2022-03-04 10:52:07 +13:00
|
|
|
const users = useLiveQuery(() => userManager.all());
|
2022-03-05 10:10:04 +13:00
|
|
|
const subscriptions = useLiveQuery(() => subscriptionManager.all());
|
2022-03-06 02:52:52 +13:00
|
|
|
const selectedSubscription = findSelected(location, subscriptions);
|
2022-03-05 10:10:04 +13:00
|
|
|
|
2022-03-07 15:39:20 +13:00
|
|
|
useWorkers();
|
|
|
|
useConnectionListeners();
|
2022-03-06 16:33:34 +13:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
connectionManager.refresh(subscriptions, users);
|
|
|
|
}, [subscriptions, users]); // Dangle!
|
|
|
|
|
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-05 10:10:04 +13: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 14:18:46 +13:00
|
|
|
selectedSubscription={selectedSubscription}
|
2022-03-05 10:10:04 +13:00
|
|
|
notificationsGranted={notificationsGranted}
|
2022-03-07 15:39:20 +13:00
|
|
|
requestNotificationPermission={() => notifier.maybeRequestPermission(granted => setNotificationsGranted(granted))}
|
|
|
|
mobileDrawerOpen={mobileDrawerOpen}
|
2022-02-25 14:18:46 +13:00
|
|
|
onMobileDrawerToggle={() => setMobileDrawerOpen(!mobileDrawerOpen)}
|
|
|
|
/>
|
2022-02-19 03:49:51 +13:00
|
|
|
</Box>
|
2022-03-05 10:10:04 +13:00
|
|
|
<Main>
|
|
|
|
<Toolbar/>
|
|
|
|
<Routes>
|
|
|
|
<Route path="/" element={<NoTopics />} />
|
|
|
|
<Route path="settings" element={<Preferences />} />
|
2022-03-06 02:52:52 +13:00
|
|
|
<Route path=":baseUrl/:topic" element={<Notifications subscription={selectedSubscription}/>} />
|
|
|
|
<Route path=":topic" element={<Notifications subscription={selectedSubscription}/>} />
|
2022-03-05 10:10:04 +13:00
|
|
|
</Routes>
|
|
|
|
</Main>
|
|
|
|
</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
|
|
|
|
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-06 02:52:52 +13:00
|
|
|
const findSelected = (location, subscriptions) => {
|
|
|
|
if (!subscriptions || !location) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-03-07 04:42:05 +13:00
|
|
|
const [subscription] = subscriptions.filter(s => location.pathname === subscriptionRoute(s));
|
2022-03-06 02:52:52 +13:00
|
|
|
return subscription;
|
2022-03-07 10:35:31 +13:00
|
|
|
|
|
|
|
/*
|
|
|
|
if (location.pathname === "/" || location.pathname === "/settings") {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!subscription) {
|
|
|
|
const [, topic] = location.pathname.split("/");
|
|
|
|
const subscription = {
|
|
|
|
id: topicUrl(window.location.origin, topic),
|
|
|
|
baseUrl: window.location.origin,
|
|
|
|
topic: topic,
|
|
|
|
last: ""
|
|
|
|
}
|
|
|
|
subscriptionManager.save(subscription);
|
|
|
|
return subscription;
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
2022-03-06 02:52:52 +13:00
|
|
|
};
|
|
|
|
|
2022-03-07 15:39:20 +13:00
|
|
|
|
|
|
|
const useWorkers = () => {
|
|
|
|
useEffect(() => {
|
|
|
|
poller.startWorker();
|
|
|
|
pruner.startWorker();
|
|
|
|
}, []);
|
|
|
|
};
|
|
|
|
|
|
|
|
const useConnectionListeners = () => {
|
|
|
|
const navigate = useNavigate();
|
|
|
|
useEffect(() => {
|
|
|
|
const handleNotification = async (subscriptionId, notification) => {
|
|
|
|
const added = await subscriptionManager.addNotification(subscriptionId, notification);
|
|
|
|
if (added) {
|
|
|
|
const defaultClickAction = (subscription) => navigate(subscriptionRoute(subscription));
|
|
|
|
await notifier.notify(subscriptionId, notification, defaultClickAction)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
connectionManager.registerStateListener(subscriptionManager.updateState);
|
|
|
|
connectionManager.registerNotificationListener(handleNotification);
|
|
|
|
return () => {
|
|
|
|
connectionManager.resetStateListener();
|
|
|
|
connectionManager.resetNotificationListener();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// We have to disable dep checking for "navigate". This is fine, it never changes.
|
|
|
|
// eslint-disable-next-line
|
|
|
|
[]);
|
|
|
|
};
|
|
|
|
|
2022-02-19 08:41:01 +13:00
|
|
|
export default App;
|