ntfy/web/src/components/App.js

147 lines
5.5 KiB
JavaScript
Raw Normal View History

2022-02-18 14:49:51 +00:00
import * as React from 'react';
2022-04-07 23:11:51 +00:00
import { Suspense } 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-02-25 17:46:22 +00:00
import Navigation from "./Navigation";
import ActionBar from "./ActionBar";
2022-03-06 05:02:27 +00:00
import notifier from "../app/Notifier";
2022-02-28 21:56:38 +00: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";
2022-04-04 14:04:01 +00:00
import {expandUrl} from "../app/utils";
import ErrorBoundary from "./ErrorBoundary";
import routes from "./routes";
import {useAutoSubscribe, useBackgroundProcesses, useConnectionListeners} from "./hooks";
2022-04-08 14:44:35 +00:00
import PublishDialog from "./PublishDialog";
2022-04-04 14:04:01 +00:00
import Messaging from "./Messaging";
2022-04-07 23:11:51 +00:00
import "./i18n"; // Translations!
2022-04-08 00:31:24 +00:00
import {Backdrop, CircularProgress} from "@mui/material";
2022-02-25 01:18:46 +00:00
2022-03-06 05:02:27 +00:00
// TODO races when two tabs are open
2022-03-06 15:42:05 +00:00
// TODO investigate service workers
2022-02-26 16:51:45 +00:00
2022-02-18 19:41:01 +00:00
const App = () => {
2022-03-04 21:10:04 +00:00
return (
2022-04-07 23:11:51 +00:00
<Suspense fallback={<Loader />}>
<BrowserRouter>
<ThemeProvider theme={theme}>
<CssBaseline/>
<ErrorBoundary>
<Routes>
<Route element={<Layout/>}>
<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>
</ErrorBoundary>
</ThemeProvider>
</BrowserRouter>
</Suspense>
2022-03-04 21:10:04 +00:00
);
}
2022-03-08 19:29:03 +00:00
const AllSubscriptions = () => {
const { subscriptions } = useOutletContext();
return <Notifications mode="all" subscriptions={subscriptions}/>;
2022-03-08 20:19:15 +00:00
};
2022-03-08 19:29:03 +00:00
const SingleSubscription = () => {
2022-03-08 20:19:15 +00:00
const { subscriptions, selected } = useOutletContext();
2022-03-09 01:26:15 +00:00
useAutoSubscribe(subscriptions, selected);
2022-03-08 19:29:03 +00:00
return <Notifications mode="one" subscription={selected}/>;
2022-03-08 20:19:15 +00:00
};
2022-03-08 19:29:03 +00:00
const Layout = () => {
const params = useParams();
2022-02-25 01:18:46 +00:00
const [mobileDrawerOpen, setMobileDrawerOpen] = useState(false);
2022-03-06 05:02:27 +00:00
const [notificationsGranted, setNotificationsGranted] = useState(notifier.granted());
2022-04-04 02:58:44 +00:00
const [sendDialogOpenMode, setSendDialogOpenMode] = useState("");
const users = useLiveQuery(() => userManager.all());
2022-03-08 19:29:03 +00:00
const subscriptions = useLiveQuery(() => subscriptionManager.all());
2022-03-07 03:37:13 +00:00
const newNotificationsCount = subscriptions?.reduce((prev, cur) => prev + cur.new, 0) || 0;
2022-03-08 19:29:03 +00: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-04 21:10:04 +00:00
useConnectionListeners(subscriptions, users);
useBackgroundProcesses();
useEffect(() => updateTitle(newNotificationsCount), [newNotificationsCount]);
2022-03-07 03:37:13 +00:00
2022-02-18 14:49:51 +00:00
return (
2022-03-04 21:10:04 +00:00
<Box sx={{display: 'flex'}}>
2022-02-25 01:18:46 +00:00
<CssBaseline/>
2022-03-04 21:10:04 +00:00
<ActionBar
selected={selected}
2022-03-04 21:10:04 +00:00
onMobileDrawerToggle={() => setMobileDrawerOpen(!mobileDrawerOpen)}
/>
2022-03-08 16:33:17 +00:00
<Navigation
subscriptions={subscriptions}
selectedSubscription={selected}
2022-03-08 16:33:17 +00:00
notificationsGranted={notificationsGranted}
mobileDrawerOpen={mobileDrawerOpen}
onMobileDrawerToggle={() => setMobileDrawerOpen(!mobileDrawerOpen)}
onNotificationGranted={setNotificationsGranted}
2022-04-08 14:44:35 +00:00
onPublishMessageClick={() => setSendDialogOpenMode(PublishDialog.OPEN_MODE_DEFAULT)}
2022-03-08 16:33:17 +00:00
/>
2022-03-04 21:10:04 +00:00
<Main>
<Toolbar/>
2022-03-08 19:29:03 +00:00
<Outlet context={{ subscriptions, selected }}/>
2022-03-04 21:10:04 +00:00
</Main>
2022-04-04 02:58:44 +00:00
<Messaging
selected={selected}
dialogOpenMode={sendDialogOpenMode}
onDialogOpenModeChange={setSendDialogOpenMode}
/>
2022-03-04 21:10:04 +00:00
</Box>
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
2022-03-08 04:07:07 +00:00
id="main"
2022-03-02 21:16:30 +00: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-04-08 00:31:24 +00:00
const Loader = () => (
<Backdrop
open={true}
sx={{
zIndex: 100000,
backgroundColor: (theme) => theme.palette.mode === 'light' ? theme.palette.grey[100] : theme.palette.grey[900]
}}
>
<CircularProgress color="success" disableShrink />
</Backdrop>
);
const updateTitle = (newNotificationsCount) => {
document.title = (newNotificationsCount > 0) ? `(${newNotificationsCount}) ntfy` : "ntfy";
}
2022-02-18 19:41:01 +00:00
export default App;