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';
|
2022-02-28 17:52:50 +01:00
|
|
|
import Notifications from "./Notifications";
|
2022-02-20 04:26:58 +01:00
|
|
|
import theme from "./theme";
|
2022-02-25 18:46:22 +01:00
|
|
|
import Navigation from "./Navigation";
|
|
|
|
import ActionBar from "./ActionBar";
|
2022-03-06 06:02:27 +01:00
|
|
|
import notifier from "../app/Notifier";
|
2022-02-28 22:56:38 +01:00
|
|
|
import Preferences from "./Preferences";
|
2022-03-02 03:23:12 +01:00
|
|
|
import {useLiveQuery} from "dexie-react-hooks";
|
2022-03-03 22:52:07 +01:00
|
|
|
import subscriptionManager from "../app/SubscriptionManager";
|
|
|
|
import userManager from "../app/UserManager";
|
2022-03-10 05:28:55 +01:00
|
|
|
import {BrowserRouter, Outlet, Route, Routes, useOutletContext, useParams} from "react-router-dom";
|
2022-03-27 15:10:47 +02:00
|
|
|
import {expandUrl, topicUrl} from "../app/utils";
|
2022-03-10 05:28:55 +01:00
|
|
|
import ErrorBoundary from "./ErrorBoundary";
|
|
|
|
import routes from "./routes";
|
2022-03-28 20:14:20 +02:00
|
|
|
import {useAutoSubscribe, useBackgroundProcesses, useConnectionListeners} from "./hooks";
|
2022-03-20 18:52:07 +01:00
|
|
|
import Paper from "@mui/material/Paper";
|
|
|
|
import IconButton from "@mui/material/IconButton";
|
|
|
|
import TextField from "@mui/material/TextField";
|
|
|
|
import SendIcon from "@mui/icons-material/Send";
|
2022-03-27 15:10:47 +02:00
|
|
|
import api from "../app/Api";
|
|
|
|
import SendDialog from "./SendDialog";
|
2022-03-29 04:54:27 +02:00
|
|
|
import KeyboardArrowUpIcon from '@mui/icons-material/KeyboardArrowUp';
|
2022-02-25 02:18:46 +01:00
|
|
|
|
2022-03-11 16:43:18 +01:00
|
|
|
// TODO add drag and drop
|
2022-03-06 06:02:27 +01:00
|
|
|
// TODO races when two tabs are open
|
2022-03-06 16:42:05 +01:00
|
|
|
// TODO investigate service workers
|
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 (
|
2022-03-10 21:37:50 +01:00
|
|
|
<BrowserRouter>
|
|
|
|
<ThemeProvider theme={theme}>
|
|
|
|
<CssBaseline/>
|
|
|
|
<ErrorBoundary>
|
2022-03-10 05:28:55 +01:00
|
|
|
<Routes>
|
|
|
|
<Route element={<Layout/>}>
|
2022-03-10 21:37:50 +01:00
|
|
|
<Route path={routes.root} element={<AllSubscriptions/>}/>
|
|
|
|
<Route path={routes.settings} element={<Preferences/>}/>
|
|
|
|
<Route path={routes.subscription} element={<SingleSubscription/>}/>
|
|
|
|
<Route path={routes.subscriptionExternal} element={<SingleSubscription/>}/>
|
2022-03-10 05:28:55 +01:00
|
|
|
</Route>
|
|
|
|
</Routes>
|
2022-03-10 21:37:50 +01:00
|
|
|
</ErrorBoundary>
|
|
|
|
</ThemeProvider>
|
|
|
|
</BrowserRouter>
|
2022-03-04 22:10:04 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-08 20:29:03 +01:00
|
|
|
const AllSubscriptions = () => {
|
|
|
|
const { subscriptions } = useOutletContext();
|
|
|
|
return <Notifications mode="all" subscriptions={subscriptions}/>;
|
2022-03-08 21:19:15 +01:00
|
|
|
};
|
2022-03-08 20:13:32 +01:00
|
|
|
|
2022-03-08 20:29:03 +01:00
|
|
|
const SingleSubscription = () => {
|
2022-03-08 21:19:15 +01:00
|
|
|
const { subscriptions, selected } = useOutletContext();
|
2022-03-09 02:26:15 +01:00
|
|
|
useAutoSubscribe(subscriptions, selected);
|
2022-03-08 20:29:03 +01:00
|
|
|
return <Notifications mode="one" subscription={selected}/>;
|
2022-03-08 21:19:15 +01:00
|
|
|
};
|
2022-03-08 20:13:32 +01:00
|
|
|
|
2022-03-08 20:29:03 +01:00
|
|
|
const Layout = () => {
|
|
|
|
const params = useParams();
|
2022-02-25 02:18:46 +01:00
|
|
|
const [mobileDrawerOpen, setMobileDrawerOpen] = useState(false);
|
2022-03-06 06:02:27 +01:00
|
|
|
const [notificationsGranted, setNotificationsGranted] = useState(notifier.granted());
|
2022-03-03 22:52:07 +01:00
|
|
|
const users = useLiveQuery(() => userManager.all());
|
2022-03-08 20:29:03 +01:00
|
|
|
const subscriptions = useLiveQuery(() => subscriptionManager.all());
|
2022-03-07 04:37:13 +01:00
|
|
|
const newNotificationsCount = subscriptions?.reduce((prev, cur) => prev + cur.new, 0) || 0;
|
2022-03-08 20:29:03 +01: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 22:10:04 +01:00
|
|
|
|
2022-03-11 20:43:54 +01:00
|
|
|
useConnectionListeners(subscriptions, users);
|
2022-03-27 15:20:25 +02:00
|
|
|
useBackgroundProcesses();
|
2022-03-08 20:13:32 +01:00
|
|
|
useEffect(() => updateTitle(newNotificationsCount), [newNotificationsCount]);
|
2022-03-07 04:37:13 +01:00
|
|
|
|
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
|
2022-03-08 20:13:32 +01:00
|
|
|
selected={selected}
|
2022-03-04 22:10:04 +01:00
|
|
|
onMobileDrawerToggle={() => setMobileDrawerOpen(!mobileDrawerOpen)}
|
|
|
|
/>
|
2022-03-08 17:33:17 +01:00
|
|
|
<Navigation
|
|
|
|
subscriptions={subscriptions}
|
2022-03-08 20:13:32 +01:00
|
|
|
selectedSubscription={selected}
|
2022-03-08 17:33:17 +01:00
|
|
|
notificationsGranted={notificationsGranted}
|
|
|
|
mobileDrawerOpen={mobileDrawerOpen}
|
|
|
|
onMobileDrawerToggle={() => setMobileDrawerOpen(!mobileDrawerOpen)}
|
|
|
|
onNotificationGranted={setNotificationsGranted}
|
|
|
|
/>
|
2022-03-04 22:10:04 +01:00
|
|
|
<Main>
|
|
|
|
<Toolbar/>
|
2022-03-08 20:29:03 +01:00
|
|
|
<Outlet context={{ subscriptions, selected }}/>
|
2022-03-04 22:10:04 +01:00
|
|
|
</Main>
|
2022-04-02 23:06:26 +02:00
|
|
|
<Messaging selected={selected}/>
|
2022-03-04 22:10:04 +01:00
|
|
|
</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
|
2022-03-08 05:07:07 +01:00
|
|
|
id="main"
|
2022-03-02 22:16:30 +01: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-02 23:06:26 +02:00
|
|
|
const Messaging = (props) => {
|
2022-03-27 15:10:47 +02:00
|
|
|
const [message, setMessage] = useState("");
|
2022-04-02 23:06:26 +02:00
|
|
|
const [dialogKey, setDialogKey] = useState(0);
|
2022-04-04 01:51:32 +02:00
|
|
|
const [dialogOpenMode, setDialogOpenMode] = useState("");
|
2022-04-02 23:06:26 +02:00
|
|
|
const [showDialog, setShowDialog] = useState(false);
|
|
|
|
const [showDropZone, setShowDropZone] = useState(false);
|
|
|
|
|
2022-03-27 15:10:47 +02:00
|
|
|
const subscription = props.selected;
|
2022-04-02 23:06:26 +02:00
|
|
|
const selectedTopicUrl = (subscription) ? topicUrl(subscription.baseUrl, subscription.topic) : "";
|
2022-03-29 21:22:26 +02:00
|
|
|
|
2022-04-04 01:51:32 +02:00
|
|
|
const handleWindowDragEnter = () => {
|
|
|
|
setDialogOpenMode(prev => (prev) ? prev : SendDialog.OPEN_MODE_DRAG); // Only update if not already open
|
|
|
|
setShowDialog(true);
|
|
|
|
setShowDropZone(true);
|
|
|
|
};
|
|
|
|
|
2022-04-02 23:06:26 +02:00
|
|
|
useEffect(() => {
|
2022-04-04 01:51:32 +02:00
|
|
|
window.addEventListener('dragenter', handleWindowDragEnter);
|
2022-04-02 23:06:26 +02:00
|
|
|
}, []);
|
2022-03-29 21:22:26 +02:00
|
|
|
|
2022-04-04 01:51:32 +02:00
|
|
|
const handleOpenDialogClick = () => {
|
|
|
|
setDialogOpenMode(SendDialog.OPEN_MODE_DEFAULT);
|
|
|
|
setShowDialog(true);
|
|
|
|
setShowDropZone(false);
|
|
|
|
};
|
|
|
|
|
2022-03-29 21:22:26 +02:00
|
|
|
const handleSendDialogClose = () => {
|
2022-04-02 23:06:26 +02:00
|
|
|
setShowDialog(false);
|
|
|
|
setShowDropZone(false);
|
2022-04-04 01:51:32 +02:00
|
|
|
setDialogOpenMode("");
|
2022-04-02 23:06:26 +02:00
|
|
|
setDialogKey(prev => prev+1);
|
2022-03-29 21:22:26 +02:00
|
|
|
};
|
|
|
|
|
2022-04-02 23:06:26 +02:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{subscription && <MessageBar
|
|
|
|
subscription={subscription}
|
|
|
|
message={message}
|
|
|
|
onMessageChange={setMessage}
|
2022-04-04 01:51:32 +02:00
|
|
|
onOpenDialogClick={handleOpenDialogClick}
|
2022-04-02 23:06:26 +02:00
|
|
|
/>}
|
|
|
|
<SendDialog
|
|
|
|
key={`sendDialog${dialogKey}`} // Resets dialog when canceled/closed
|
2022-04-04 01:51:32 +02:00
|
|
|
topicUrl={selectedTopicUrl}
|
|
|
|
message={message}
|
2022-04-02 23:06:26 +02:00
|
|
|
open={showDialog}
|
2022-04-04 01:51:32 +02:00
|
|
|
openMode={dialogOpenMode}
|
2022-04-02 23:06:26 +02:00
|
|
|
dropZone={showDropZone}
|
|
|
|
onClose={handleSendDialogClose}
|
2022-04-04 01:51:32 +02:00
|
|
|
onHideDropZone={() => setShowDropZone(false)}
|
|
|
|
onResetOpenMode={() => setDialogOpenMode(SendDialog.OPEN_MODE_DEFAULT)}
|
2022-04-02 23:06:26 +02:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const MessageBar = (props) => {
|
|
|
|
const subscription = props.subscription;
|
|
|
|
const handleSendClick = () => {
|
|
|
|
api.publish(subscription.baseUrl, subscription.topic, props.message); // FIXME
|
|
|
|
props.onMessageChange("");
|
|
|
|
};
|
2022-03-20 18:52:07 +01:00
|
|
|
return (
|
|
|
|
<Paper
|
|
|
|
elevation={3}
|
|
|
|
sx={{
|
|
|
|
display: "flex",
|
|
|
|
position: 'fixed',
|
|
|
|
bottom: 0,
|
|
|
|
right: 0,
|
|
|
|
padding: 2,
|
|
|
|
width: `calc(100% - ${Navigation.width}px)`,
|
|
|
|
backgroundColor: (theme) => theme.palette.mode === 'light' ? theme.palette.grey[100] : theme.palette.grey[900]
|
|
|
|
}}
|
|
|
|
>
|
2022-04-02 23:06:26 +02:00
|
|
|
<IconButton color="inherit" size="large" edge="start" onClick={props.onOpenDialogClick}>
|
2022-03-29 04:54:27 +02:00
|
|
|
<KeyboardArrowUpIcon/>
|
2022-03-27 15:10:47 +02:00
|
|
|
</IconButton>
|
2022-03-26 14:32:13 +01:00
|
|
|
<TextField
|
|
|
|
autoFocus
|
|
|
|
margin="dense"
|
|
|
|
placeholder="Message"
|
|
|
|
type="text"
|
|
|
|
fullWidth
|
|
|
|
variant="standard"
|
2022-04-02 23:06:26 +02:00
|
|
|
value={props.message}
|
|
|
|
onChange={ev => props.onMessageChange(ev.target.value)}
|
2022-03-27 15:10:47 +02:00
|
|
|
onKeyPress={(ev) => {
|
|
|
|
if (ev.key === 'Enter') {
|
|
|
|
ev.preventDefault();
|
|
|
|
handleSendClick();
|
|
|
|
}
|
|
|
|
}}
|
2022-03-26 14:32:13 +01:00
|
|
|
/>
|
2022-03-27 15:10:47 +02:00
|
|
|
<IconButton color="inherit" size="large" edge="end" onClick={handleSendClick}>
|
2022-03-20 18:52:07 +01:00
|
|
|
<SendIcon/>
|
|
|
|
</IconButton>
|
|
|
|
</Paper>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-03-08 20:13:32 +01:00
|
|
|
const updateTitle = (newNotificationsCount) => {
|
2022-03-10 05:28:55 +01:00
|
|
|
document.title = (newNotificationsCount > 0) ? `(${newNotificationsCount}) ntfy` : "ntfy";
|
2022-03-08 20:13:32 +01:00
|
|
|
}
|
|
|
|
|
2022-02-18 20:41:01 +01:00
|
|
|
export default App;
|