ntfy/web/src/components/App.js

168 lines
6.5 KiB
JavaScript
Raw Normal View History

2022-02-18 14:49:51 +00:00
import * as React from 'react';
2022-02-21 01:04:03 +00:00
import {useEffect, useState} from 'react';
2022-02-18 14:49:51 +00:00
import Box from '@mui/material/Box';
2022-02-25 01:18:46 +00:00
import {ThemeProvider} from '@mui/material/styles';
2022-02-20 00:48:33 +00:00
import CssBaseline from '@mui/material/CssBaseline';
import Toolbar from '@mui/material/Toolbar';
import Notifications from "./Notifications";
2022-02-20 03:26:58 +00:00
import theme from "./theme";
2022-03-02 21:16:30 +00:00
import prefs from "../app/Prefs";
import connectionManager from "../app/ConnectionManager";
2022-02-25 17:46:22 +00:00
import Navigation from "./Navigation";
import ActionBar from "./ActionBar";
2022-02-26 15:14:43 +00:00
import notificationManager from "../app/NotificationManager";
import NoTopics from "./NoTopics";
2022-02-28 21:56:38 +00:00
import Preferences from "./Preferences";
import {useLiveQuery} from "dexie-react-hooks";
2022-03-02 21:16:30 +00:00
import poller from "../app/Poller";
import pruner from "../app/Pruner";
import subscriptionManager from "../app/SubscriptionManager";
import userManager from "../app/UserManager";
2022-02-25 01:18:46 +00:00
2022-02-26 16:51:45 +00:00
// TODO subscribe dialog:
// - check/use existing user
// - add baseUrl
// TODO embed into ntfy server
// TODO make default server functional
// TODO business logic with callbacks
// TODO connection indicator in subscription list
2022-03-02 21:16:30 +00:00
// TODO connectionmanager should react on users changes
2022-02-26 16:51:45 +00:00
2022-02-18 19:41:01 +00:00
const App = () => {
console.log(`[App] Rendering main view`);
2022-02-21 01:04:03 +00:00
2022-02-25 01:18:46 +00:00
const [mobileDrawerOpen, setMobileDrawerOpen] = useState(false);
2022-02-28 21:56:38 +00:00
const [prefsOpen, setPrefsOpen] = useState(false);
2022-02-21 01:04:03 +00:00
const [selectedSubscription, setSelectedSubscription] = useState(null);
2022-02-26 15:14:43 +00:00
const [notificationsGranted, setNotificationsGranted] = useState(notificationManager.granted());
const subscriptions = useLiveQuery(() => subscriptionManager.all());
const users = useLiveQuery(() => userManager.all());
2022-03-02 21:16:30 +00:00
const handleSubscriptionClick = async (subscriptionId) => {
const subscription = await subscriptionManager.get(subscriptionId);
2022-03-02 21:16:30 +00:00
setSelectedSubscription(subscription);
2022-02-28 21:56:38 +00:00
setPrefsOpen(false);
}
2022-03-02 21:16:30 +00:00
const handleSubscribeSubmit = async (subscription) => {
console.log(`[App] New subscription: ${subscription.id}`, subscription);
2022-02-23 04:22:30 +00:00
setSelectedSubscription(subscription);
2022-02-26 15:14:43 +00:00
handleRequestPermission();
};
2022-03-02 21:16:30 +00:00
const handleUnsubscribe = async (subscriptionId) => {
console.log(`[App] Unsubscribing from ${subscriptionId}`);
const newSelected = await subscriptionManager.first(); // May be undefined
2022-03-02 21:16:30 +00:00
setSelectedSubscription(newSelected);
2022-02-23 03:10:50 +00:00
};
2022-02-26 15:14:43 +00:00
const handleRequestPermission = () => {
notificationManager.maybeRequestPermission((granted) => {
setNotificationsGranted(granted);
})
};
2022-02-28 21:56:38 +00:00
const handlePrefsClick = () => {
setPrefsOpen(true);
setSelectedSubscription(null);
};
// Define hooks: Note that the order of the hooks is important. The "loading" hooks
// must be before the "saving" hooks.
2022-02-24 20:17:47 +00:00
useEffect(() => {
2022-03-02 21:16:30 +00:00
poller.startWorker();
pruner.startWorker();
2022-03-02 03:01:51 +00:00
const load = async () => {
const subs = await subscriptionManager.all(); // FIXME this is broken
2022-03-02 21:16:30 +00:00
const selectedSubscriptionId = await prefs.selectedSubscriptionId();
2022-03-02 03:01:51 +00:00
// Set selected subscription
2022-03-02 21:16:30 +00:00
const maybeSelectedSubscription = subs?.filter(s => s.id = selectedSubscriptionId);
if (maybeSelectedSubscription.length > 0) {
setSelectedSubscription(maybeSelectedSubscription[0]);
2022-03-02 03:01:51 +00:00
}
2022-03-02 03:01:51 +00:00
};
2022-03-02 21:16:30 +00:00
setTimeout(() => load(), 5000);
}, [/* initial render */]);
2022-02-21 01:04:03 +00:00
useEffect(() => {
const notificationClickFallback = (subscription) => setSelectedSubscription(subscription);
2022-03-02 21:16:30 +00:00
const handleNotification = async (subscriptionId, notification) => {
try {
const added = await subscriptionManager.addNotification(subscriptionId, notification);
if (added) {
await notificationManager.notify(subscriptionId, notification, notificationClickFallback)
}
2022-03-02 21:16:30 +00:00
} catch (e) {
console.error(`[App] Error handling notification`, e);
}
};
connectionManager.refresh(subscriptions, users, handleNotification);
}, [subscriptions, users]);
useEffect(() => {
const subscriptionId = (selectedSubscription) ? selectedSubscription.id : "";
2022-03-02 21:16:30 +00:00
prefs.setSelectedSubscriptionId(subscriptionId)
}, [selectedSubscription]);
2022-02-18 14:49:51 +00:00
return (
2022-02-20 03:26:58 +00:00
<ThemeProvider theme={theme}>
2022-02-25 01:18:46 +00:00
<CssBaseline/>
<Box sx={{display: 'flex'}}>
<CssBaseline/>
<ActionBar
selectedSubscription={selectedSubscription}
onUnsubscribe={handleUnsubscribe}
onMobileDrawerToggle={() => setMobileDrawerOpen(!mobileDrawerOpen)}
/>
2022-02-25 17:46:22 +00:00
<Box component="nav" sx={{width: {sm: Navigation.width}, flexShrink: {sm: 0}}}>
<Navigation
2022-02-25 01:18:46 +00:00
subscriptions={subscriptions}
selectedSubscription={selectedSubscription}
mobileDrawerOpen={mobileDrawerOpen}
2022-02-26 15:14:43 +00:00
notificationsGranted={notificationsGranted}
2022-02-28 21:56:38 +00:00
prefsOpen={prefsOpen}
2022-02-25 01:18:46 +00:00
onMobileDrawerToggle={() => setMobileDrawerOpen(!mobileDrawerOpen)}
2022-02-28 21:56:38 +00:00
onSubscriptionClick={handleSubscriptionClick}
2022-02-25 01:18:46 +00:00
onSubscribeSubmit={handleSubscribeSubmit}
2022-02-28 21:56:38 +00:00
onPrefsClick={handlePrefsClick}
2022-02-26 15:14:43 +00:00
onRequestPermissionClick={handleRequestPermission}
2022-02-25 01:18:46 +00:00
/>
</Box>
2022-03-02 21:16:30 +00:00
<Main>
2022-02-25 01:18:46 +00:00
<Toolbar/>
2022-03-02 21:16:30 +00:00
<Content
2022-02-28 21:56:38 +00:00
subscription={selectedSubscription}
prefsOpen={prefsOpen}
/>
2022-03-02 21:16:30 +00:00
</Main>
2022-02-18 14:49:51 +00:00
</Box>
2022-02-20 00:48:33 +00:00
</ThemeProvider>
2022-02-18 14:49:51 +00:00
);
}
2022-02-18 19:41:01 +00:00
2022-03-02 21:16:30 +00: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>
);
};
const Content = (props) => {
2022-02-28 21:56:38 +00:00
if (props.prefsOpen) {
return <Preferences/>;
}
2022-03-02 21:16:30 +00:00
if (props.subscription) {
return <Notifications subscription={props.subscription}/>;
2022-02-28 21:56:38 +00:00
}
2022-03-02 21:16:30 +00:00
return <NoTopics/>;
2022-02-28 21:56:38 +00:00
};
2022-02-18 19:41:01 +00:00
export default App;