2022-02-18 09:49:51 -05:00
|
|
|
import * as React from 'react';
|
2023-01-09 20:37:13 -05:00
|
|
|
import {createContext, Suspense, useContext, useEffect, useState} from 'react';
|
2022-02-18 09:49:51 -05:00
|
|
|
import Box from '@mui/material/Box';
|
2022-02-24 20:18:46 -05:00
|
|
|
import {ThemeProvider} from '@mui/material/styles';
|
2022-02-19 19:48:33 -05:00
|
|
|
import CssBaseline from '@mui/material/CssBaseline';
|
|
|
|
import Toolbar from '@mui/material/Toolbar';
|
2023-01-09 20:37:13 -05:00
|
|
|
import {AllSubscriptions, SingleSubscription} from "./Notifications";
|
2022-02-19 22:26:58 -05:00
|
|
|
import theme from "./theme";
|
2022-02-25 12:46:22 -05:00
|
|
|
import Navigation from "./Navigation";
|
|
|
|
import ActionBar from "./ActionBar";
|
2022-03-06 00:02:27 -05:00
|
|
|
import notifier from "../app/Notifier";
|
2022-02-28 16:56:38 -05:00
|
|
|
import Preferences from "./Preferences";
|
2022-03-01 21:23:12 -05:00
|
|
|
import {useLiveQuery} from "dexie-react-hooks";
|
2022-03-03 16:52:07 -05:00
|
|
|
import subscriptionManager from "../app/SubscriptionManager";
|
|
|
|
import userManager from "../app/UserManager";
|
2023-01-09 20:37:13 -05:00
|
|
|
import {BrowserRouter, Outlet, Route, Routes, useParams} from "react-router-dom";
|
2022-04-04 10:04:01 -04:00
|
|
|
import {expandUrl} from "../app/utils";
|
2022-03-09 23:28:55 -05:00
|
|
|
import ErrorBoundary from "./ErrorBoundary";
|
|
|
|
import routes from "./routes";
|
2023-01-09 20:37:13 -05:00
|
|
|
import {useAccountListener, useBackgroundProcesses, useConnectionListeners} from "./hooks";
|
2022-04-08 10:44:35 -04:00
|
|
|
import PublishDialog from "./PublishDialog";
|
2022-04-04 10:04:01 -04:00
|
|
|
import Messaging from "./Messaging";
|
2022-04-07 19:11:51 -04:00
|
|
|
import "./i18n"; // Translations!
|
2022-04-07 20:31:24 -04:00
|
|
|
import {Backdrop, CircularProgress} from "@mui/material";
|
2022-12-02 15:37:48 -05:00
|
|
|
import Login from "./Login";
|
2022-12-14 05:36:53 -05:00
|
|
|
import Signup from "./Signup";
|
2022-12-15 22:07:04 -05:00
|
|
|
import Account from "./Account";
|
2023-01-09 20:37:13 -05:00
|
|
|
|
|
|
|
export const AccountContext = createContext(null);
|
2022-02-24 20:18:46 -05:00
|
|
|
|
2022-02-18 14:41:01 -05:00
|
|
|
const App = () => {
|
2023-01-09 20:37:13 -05:00
|
|
|
const [account, setAccount] = useState(null);
|
2022-03-04 16:10:04 -05:00
|
|
|
return (
|
2022-04-07 19:11:51 -04:00
|
|
|
<Suspense fallback={<Loader />}>
|
|
|
|
<BrowserRouter>
|
|
|
|
<ThemeProvider theme={theme}>
|
2023-01-09 20:37:13 -05:00
|
|
|
<AccountContext.Provider value={{ account, setAccount }}>
|
|
|
|
<CssBaseline/>
|
|
|
|
<ErrorBoundary>
|
|
|
|
<Routes>
|
|
|
|
<Route path={routes.login} element={<Login/>}/>
|
|
|
|
<Route path={routes.signup} element={<Signup/>}/>
|
|
|
|
<Route element={<Layout/>}>
|
|
|
|
<Route path={routes.app} element={<AllSubscriptions/>}/>
|
|
|
|
<Route path={routes.account} element={<Account/>}/>
|
|
|
|
<Route path={routes.settings} element={<Preferences/>}/>
|
|
|
|
<Route path={routes.subscription} element={<SingleSubscription/>}/>
|
|
|
|
<Route path={routes.subscriptionExternal} element={<SingleSubscription/>}/>
|
|
|
|
</Route>
|
|
|
|
</Routes>
|
|
|
|
</ErrorBoundary>
|
|
|
|
</AccountContext.Provider>
|
2022-04-07 19:11:51 -04:00
|
|
|
</ThemeProvider>
|
|
|
|
</BrowserRouter>
|
|
|
|
</Suspense>
|
2022-03-04 16:10:04 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-08 14:29:03 -05:00
|
|
|
const Layout = () => {
|
|
|
|
const params = useParams();
|
2023-01-09 20:37:13 -05:00
|
|
|
const { account, setAccount } = useContext(AccountContext);
|
2022-02-24 20:18:46 -05:00
|
|
|
const [mobileDrawerOpen, setMobileDrawerOpen] = useState(false);
|
2022-03-06 00:02:27 -05:00
|
|
|
const [notificationsGranted, setNotificationsGranted] = useState(notifier.granted());
|
2022-04-03 22:58:44 -04:00
|
|
|
const [sendDialogOpenMode, setSendDialogOpenMode] = useState("");
|
2022-03-03 16:52:07 -05:00
|
|
|
const users = useLiveQuery(() => userManager.all());
|
2022-03-08 14:29:03 -05:00
|
|
|
const subscriptions = useLiveQuery(() => subscriptionManager.all());
|
2023-01-17 10:09:37 -05:00
|
|
|
const subscriptionsWithoutInternal = subscriptions?.filter(s => !s.internal);
|
|
|
|
const newNotificationsCount = subscriptionsWithoutInternal?.reduce((prev, cur) => prev + cur.new, 0) || 0;
|
|
|
|
const [selected] = (subscriptionsWithoutInternal || []).filter(s => {
|
2022-03-08 14:29:03 -05:00
|
|
|
return (params.baseUrl && expandUrl(params.baseUrl).includes(s.baseUrl) && params.topic === s.topic)
|
2023-01-04 22:47:12 -05:00
|
|
|
|| (config.base_url === s.baseUrl && params.topic === s.topic)
|
2022-03-08 14:29:03 -05:00
|
|
|
});
|
2022-03-04 16:10:04 -05:00
|
|
|
|
2023-01-24 15:31:39 -05:00
|
|
|
useConnectionListeners(account, subscriptions, users);
|
2023-01-02 22:21:11 -05:00
|
|
|
useAccountListener(setAccount)
|
2022-03-27 09:20:25 -04:00
|
|
|
useBackgroundProcesses();
|
2022-03-08 14:13:32 -05:00
|
|
|
useEffect(() => updateTitle(newNotificationsCount), [newNotificationsCount]);
|
2022-03-06 22:37:13 -05:00
|
|
|
|
2022-02-18 09:49:51 -05:00
|
|
|
return (
|
2022-03-04 16:10:04 -05:00
|
|
|
<Box sx={{display: 'flex'}}>
|
|
|
|
<ActionBar
|
2022-03-08 14:13:32 -05:00
|
|
|
selected={selected}
|
2022-03-04 16:10:04 -05:00
|
|
|
onMobileDrawerToggle={() => setMobileDrawerOpen(!mobileDrawerOpen)}
|
|
|
|
/>
|
2022-03-08 11:33:17 -05:00
|
|
|
<Navigation
|
2023-01-17 10:09:37 -05:00
|
|
|
subscriptions={subscriptionsWithoutInternal}
|
2022-03-08 14:13:32 -05:00
|
|
|
selectedSubscription={selected}
|
2022-03-08 11:33:17 -05:00
|
|
|
notificationsGranted={notificationsGranted}
|
|
|
|
mobileDrawerOpen={mobileDrawerOpen}
|
|
|
|
onMobileDrawerToggle={() => setMobileDrawerOpen(!mobileDrawerOpen)}
|
|
|
|
onNotificationGranted={setNotificationsGranted}
|
2022-04-08 10:44:35 -04:00
|
|
|
onPublishMessageClick={() => setSendDialogOpenMode(PublishDialog.OPEN_MODE_DEFAULT)}
|
2022-03-08 11:33:17 -05:00
|
|
|
/>
|
2022-03-04 16:10:04 -05:00
|
|
|
<Main>
|
|
|
|
<Toolbar/>
|
2023-01-17 10:09:37 -05:00
|
|
|
<Outlet context={{
|
|
|
|
subscriptions: subscriptionsWithoutInternal,
|
|
|
|
selected: selected
|
|
|
|
}}/>
|
2022-03-04 16:10:04 -05:00
|
|
|
</Main>
|
2022-04-03 22:58:44 -04:00
|
|
|
<Messaging
|
|
|
|
selected={selected}
|
|
|
|
dialogOpenMode={sendDialogOpenMode}
|
|
|
|
onDialogOpenModeChange={setSendDialogOpenMode}
|
|
|
|
/>
|
2022-03-04 16:10:04 -05:00
|
|
|
</Box>
|
2022-02-18 09:49:51 -05:00
|
|
|
);
|
|
|
|
}
|
2022-02-18 14:41:01 -05:00
|
|
|
|
2022-03-02 16:16:30 -05:00
|
|
|
const Main = (props) => {
|
|
|
|
return (
|
|
|
|
<Box
|
2022-03-07 23:07:07 -05:00
|
|
|
id="main"
|
2022-03-02 16:16:30 -05: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-07 20:31:24 -04: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>
|
|
|
|
);
|
|
|
|
|
2022-03-08 14:13:32 -05:00
|
|
|
const updateTitle = (newNotificationsCount) => {
|
2022-03-09 23:28:55 -05:00
|
|
|
document.title = (newNotificationsCount > 0) ? `(${newNotificationsCount}) ntfy` : "ntfy";
|
2022-03-08 14:13:32 -05:00
|
|
|
}
|
|
|
|
|
2022-02-18 14:41:01 -05:00
|
|
|
export default App;
|