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 Typography from '@mui/material/Typography';
|
|
|
|
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';
|
2022-02-25 02:18:46 +01:00
|
|
|
import Drawer from '@mui/material/Drawer';
|
|
|
|
import AppBar from '@mui/material/AppBar';
|
2022-02-20 01:48:33 +01:00
|
|
|
import Toolbar from '@mui/material/Toolbar';
|
|
|
|
import ChatBubbleOutlineIcon from '@mui/icons-material/ChatBubbleOutline';
|
|
|
|
import List from '@mui/material/List';
|
|
|
|
import Divider from '@mui/material/Divider';
|
|
|
|
import IconButton from '@mui/material/IconButton';
|
|
|
|
import MenuIcon from '@mui/icons-material/Menu';
|
|
|
|
import ListItemIcon from "@mui/material/ListItemIcon";
|
|
|
|
import ListItemText from "@mui/material/ListItemText";
|
|
|
|
import ListItemButton from "@mui/material/ListItemButton";
|
|
|
|
import SettingsIcon from "@mui/icons-material/Settings";
|
|
|
|
import AddIcon from "@mui/icons-material/Add";
|
2022-02-25 02:18:46 +01:00
|
|
|
import SubscribeDialog from "./SubscribeDialog";
|
2022-02-21 22:24:13 +01:00
|
|
|
import NotificationList from "./NotificationList";
|
2022-02-25 02:18:46 +01:00
|
|
|
import IconSubscribeSettings from "./IconSubscribeSettings";
|
2022-02-20 04:26:58 +01:00
|
|
|
import theme from "./theme";
|
2022-02-24 02:30:12 +01:00
|
|
|
import api from "../app/Api";
|
|
|
|
import repository from "../app/Repository";
|
|
|
|
import connectionManager from "../app/ConnectionManager";
|
|
|
|
import Subscriptions from "../app/Subscriptions";
|
2022-02-20 01:48:33 +01:00
|
|
|
|
|
|
|
const drawerWidth = 240;
|
|
|
|
|
2022-02-25 02:18:46 +01:00
|
|
|
const NavSubscriptionList = (props) => {
|
2022-02-18 21:47:25 +01:00
|
|
|
const subscriptions = props.subscriptions;
|
2022-02-18 15:49:51 +01:00
|
|
|
return (
|
2022-02-20 04:26:58 +01:00
|
|
|
<>
|
2022-02-24 02:30:12 +01:00
|
|
|
{subscriptions.map((id, subscription) =>
|
2022-02-25 02:18:46 +01:00
|
|
|
<NavSubscriptionItem
|
2022-02-18 21:47:25 +01:00
|
|
|
key={id}
|
2022-02-24 02:30:12 +01:00
|
|
|
subscription={subscription}
|
|
|
|
selected={props.selectedSubscription && props.selectedSubscription.id === id}
|
2022-02-25 02:18:46 +01:00
|
|
|
onClick={() => props.onSubscriptionClick(id)}
|
2022-02-18 21:47:25 +01:00
|
|
|
/>)
|
|
|
|
}
|
2022-02-20 04:26:58 +01:00
|
|
|
</>
|
2022-02-18 15:49:51 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-25 02:18:46 +01:00
|
|
|
const NavSubscriptionItem = (props) => {
|
2022-02-18 20:41:01 +01:00
|
|
|
const subscription = props.subscription;
|
2022-02-18 15:49:51 +01:00
|
|
|
return (
|
2022-02-20 04:26:58 +01:00
|
|
|
<ListItemButton onClick={props.onClick} selected={props.selected}>
|
|
|
|
<ListItemIcon><ChatBubbleOutlineIcon /></ListItemIcon>
|
|
|
|
<ListItemText primary={subscription.shortUrl()}/>
|
2022-02-20 01:48:33 +01:00
|
|
|
</ListItemButton>
|
2022-02-18 15:49:51 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-25 02:18:46 +01:00
|
|
|
const NavList = (props) => {
|
|
|
|
const [subscribeDialogOpen, setSubscribeDialogOpen] = useState(false);
|
|
|
|
const handleSubscribeSubmit = (subscription) => {
|
|
|
|
setSubscribeDialogOpen(false);
|
|
|
|
props.onSubscribeSubmit(subscription);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Toolbar />
|
|
|
|
<Divider />
|
|
|
|
<List component="nav">
|
|
|
|
<NavSubscriptionList
|
|
|
|
subscriptions={props.subscriptions}
|
|
|
|
selectedSubscription={props.selectedSubscription}
|
|
|
|
onSubscriptionClick={props.onSubscriptionClick}
|
|
|
|
/>
|
|
|
|
<Divider sx={{ my: 1 }} />
|
|
|
|
<ListItemButton>
|
|
|
|
<ListItemIcon>
|
|
|
|
<SettingsIcon />
|
|
|
|
</ListItemIcon>
|
|
|
|
<ListItemText primary="Settings" />
|
|
|
|
</ListItemButton>
|
|
|
|
<ListItemButton onClick={() => setSubscribeDialogOpen(true)}>
|
|
|
|
<ListItemIcon>
|
|
|
|
<AddIcon />
|
|
|
|
</ListItemIcon>
|
|
|
|
<ListItemText primary="Add subscription" />
|
|
|
|
</ListItemButton>
|
|
|
|
</List>
|
|
|
|
<SubscribeDialog
|
|
|
|
open={subscribeDialogOpen}
|
|
|
|
onCancel={() => setSubscribeDialogOpen(false)}
|
|
|
|
onSubmit={handleSubscribeSubmit}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const ActionBar = (props) => {
|
|
|
|
const title = (props.selectedSubscription !== null)
|
|
|
|
? props.selectedSubscription.shortUrl()
|
|
|
|
: "ntfy";
|
|
|
|
return (
|
|
|
|
<AppBar position="fixed" sx={{ width: { sm: `calc(100% - ${drawerWidth}px)` }, ml: { sm: `${drawerWidth}px` } }}>
|
|
|
|
<Toolbar sx={{pr: '24px'}}>
|
|
|
|
<IconButton
|
|
|
|
color="inherit"
|
|
|
|
edge="start"
|
|
|
|
onClick={props.onMobileDrawerToggle}
|
|
|
|
sx={{ mr: 2, display: { sm: 'none' } }}
|
|
|
|
>
|
|
|
|
<MenuIcon />
|
|
|
|
</IconButton>
|
|
|
|
<Typography variant="h6" noWrap component="div" sx={{ flexGrow: 1 }}>{title}</Typography>
|
|
|
|
{props.selectedSubscription !== null && <IconSubscribeSettings
|
|
|
|
subscription={props.selectedSubscription}
|
|
|
|
onClearAll={props.onClearAll}
|
|
|
|
onUnsubscribe={props.onUnsubscribe}
|
|
|
|
/>}
|
|
|
|
</Toolbar>
|
|
|
|
</AppBar>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const Sidebar = (props) => {
|
|
|
|
const navigationList =
|
|
|
|
<NavList
|
|
|
|
subscriptions={props.subscriptions}
|
|
|
|
selectedSubscription={props.selectedSubscription}
|
|
|
|
onSubscriptionClick={props.onSubscriptionClick}
|
|
|
|
onSubscribeSubmit={props.onSubscribeSubmit}
|
|
|
|
/>;
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{/* Mobile drawer; only shown if menu icon clicked (mobile open) and display is small */}
|
|
|
|
<Drawer
|
|
|
|
variant="temporary"
|
|
|
|
open={props.mobileDrawerOpen}
|
|
|
|
onClose={props.onMobileDrawerToggle}
|
|
|
|
ModalProps={{ keepMounted: true }} // Better open performance on mobile.
|
|
|
|
sx={{
|
|
|
|
display: { xs: 'block', sm: 'none' },
|
|
|
|
'& .MuiDrawer-paper': { boxSizing: 'border-box', width: drawerWidth },
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{navigationList}
|
|
|
|
</Drawer>
|
|
|
|
{/* Big screen drawer; persistent, shown if screen is big */}
|
|
|
|
<Drawer
|
|
|
|
open
|
|
|
|
variant="permanent"
|
|
|
|
sx={{
|
|
|
|
display: { xs: 'none', sm: 'block' },
|
|
|
|
'& .MuiDrawer-paper': { boxSizing: 'border-box', width: drawerWidth },
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{navigationList}
|
|
|
|
</Drawer>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-02-18 20:41:01 +01:00
|
|
|
const App = () => {
|
2022-02-24 15:52:49 +01:00
|
|
|
console.log(`[App] Rendering main view`);
|
2022-02-21 02:04:03 +01:00
|
|
|
|
2022-02-25 02:18:46 +01:00
|
|
|
const [mobileDrawerOpen, setMobileDrawerOpen] = useState(false);
|
2022-02-24 02:30:12 +01:00
|
|
|
const [subscriptions, setSubscriptions] = useState(new Subscriptions());
|
2022-02-21 02:04:03 +01:00
|
|
|
const [selectedSubscription, setSelectedSubscription] = useState(null);
|
2022-02-24 02:30:12 +01:00
|
|
|
const handleNotification = (subscriptionId, notification) => {
|
|
|
|
setSubscriptions(prev => {
|
|
|
|
const newSubscription = prev.get(subscriptionId).addNotification(notification);
|
|
|
|
return prev.update(newSubscription).clone();
|
|
|
|
});
|
2022-02-18 21:47:25 +01:00
|
|
|
};
|
2022-02-23 04:10:50 +01:00
|
|
|
const handleSubscribeSubmit = (subscription) => {
|
2022-02-24 15:52:49 +01:00
|
|
|
console.log(`[App] New subscription: ${subscription.id}`);
|
2022-02-24 02:30:12 +01:00
|
|
|
setSubscriptions(prev => prev.add(subscription).clone());
|
2022-02-23 05:22:30 +01:00
|
|
|
setSelectedSubscription(subscription);
|
2022-02-24 02:30:12 +01:00
|
|
|
api.poll(subscription.baseUrl, subscription.topic)
|
2022-02-23 05:22:30 +01:00
|
|
|
.then(messages => {
|
2022-02-24 02:30:12 +01:00
|
|
|
setSubscriptions(prev => {
|
|
|
|
const newSubscription = prev.get(subscription.id).addNotifications(messages);
|
|
|
|
return prev.update(newSubscription).clone();
|
|
|
|
});
|
2022-02-23 05:22:30 +01:00
|
|
|
});
|
2022-02-18 21:47:25 +01:00
|
|
|
};
|
2022-02-25 16:23:04 +01:00
|
|
|
const handleDeleteNotification = (subscriptionId, notificationId) => {
|
|
|
|
console.log(`[App] Deleting notification ${notificationId} from ${subscriptionId}`);
|
|
|
|
setSubscriptions(prev => {
|
|
|
|
const newSubscription = prev.get(subscriptionId).deleteNotification(notificationId);
|
|
|
|
return prev.update(newSubscription).clone();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
const handleDeleteAllNotifications = (subscriptionId) => {
|
2022-02-24 18:26:07 +01:00
|
|
|
console.log(`[App] Deleting all notifications from ${subscriptionId}`);
|
|
|
|
setSubscriptions(prev => {
|
|
|
|
const newSubscription = prev.get(subscriptionId).deleteAllNotifications();
|
|
|
|
return prev.update(newSubscription).clone();
|
|
|
|
});
|
|
|
|
};
|
2022-02-24 02:30:12 +01:00
|
|
|
const handleUnsubscribe = (subscriptionId) => {
|
2022-02-24 15:52:49 +01:00
|
|
|
console.log(`[App] Unsubscribing from ${subscriptionId}`);
|
2022-02-23 04:10:50 +01:00
|
|
|
setSubscriptions(prev => {
|
2022-02-24 02:30:12 +01:00
|
|
|
const newSubscriptions = prev.remove(subscriptionId).clone();
|
|
|
|
setSelectedSubscription(newSubscriptions.firstOrNull());
|
2022-02-23 04:10:50 +01:00
|
|
|
return newSubscriptions;
|
|
|
|
});
|
|
|
|
};
|
2022-02-24 21:17:47 +01:00
|
|
|
useEffect(() => {
|
|
|
|
setSubscriptions(repository.loadSubscriptions());
|
|
|
|
}, [/* initial render only */]);
|
2022-02-21 02:04:03 +01:00
|
|
|
useEffect(() => {
|
2022-02-24 02:30:12 +01:00
|
|
|
connectionManager.refresh(subscriptions, handleNotification);
|
|
|
|
repository.saveSubscriptions(subscriptions);
|
2022-02-21 02:04:03 +01:00
|
|
|
}, [subscriptions]);
|
2022-02-25 02:18:46 +01:00
|
|
|
|
2022-02-18 15:49:51 +01:00
|
|
|
return (
|
2022-02-20 04:26:58 +01:00
|
|
|
<ThemeProvider theme={theme}>
|
2022-02-25 02:18:46 +01:00
|
|
|
<CssBaseline/>
|
|
|
|
<Box sx={{display: 'flex'}}>
|
|
|
|
<CssBaseline/>
|
|
|
|
<ActionBar
|
|
|
|
selectedSubscription={selectedSubscription}
|
2022-02-25 16:23:04 +01:00
|
|
|
onClearAll={handleDeleteAllNotifications}
|
2022-02-25 02:18:46 +01:00
|
|
|
onUnsubscribe={handleUnsubscribe}
|
|
|
|
onMobileDrawerToggle={() => setMobileDrawerOpen(!mobileDrawerOpen)}
|
|
|
|
/>
|
|
|
|
<Box component="nav" sx={{width: {sm: drawerWidth}, flexShrink: {sm: 0}}}>
|
|
|
|
<Sidebar
|
|
|
|
subscriptions={subscriptions}
|
|
|
|
selectedSubscription={selectedSubscription}
|
|
|
|
mobileDrawerOpen={mobileDrawerOpen}
|
|
|
|
onMobileDrawerToggle={() => setMobileDrawerOpen(!mobileDrawerOpen)}
|
|
|
|
onSubscriptionClick={(subscriptionId) => setSelectedSubscription(subscriptions.get(subscriptionId))}
|
|
|
|
onSubscribeSubmit={handleSubscribeSubmit}
|
|
|
|
/>
|
|
|
|
</Box>
|
2022-02-20 01:48:33 +01:00
|
|
|
<Box
|
|
|
|
component="main"
|
|
|
|
sx={{
|
|
|
|
flexGrow: 1,
|
2022-02-25 02:18:46 +01:00
|
|
|
p: 3,
|
|
|
|
width: {sm: `calc(100% - ${drawerWidth}px)`},
|
2022-02-20 01:48:33 +01:00
|
|
|
height: '100vh',
|
|
|
|
overflow: 'auto',
|
2022-02-25 02:18:46 +01:00
|
|
|
backgroundColor: (theme) => theme.palette.mode === 'light' ? theme.palette.grey[100] : theme.palette.grey[900]
|
|
|
|
}}>
|
|
|
|
<Toolbar/>
|
2022-02-25 16:23:04 +01:00
|
|
|
{selectedSubscription !== null &&
|
|
|
|
<NotificationList
|
|
|
|
subscription={selectedSubscription}
|
|
|
|
onDeleteNotification={handleDeleteNotification}
|
|
|
|
/>}
|
2022-02-20 01:48:33 +01:00
|
|
|
</Box>
|
2022-02-18 15:49:51 +01:00
|
|
|
</Box>
|
2022-02-20 01:48:33 +01:00
|
|
|
</ThemeProvider>
|
2022-02-18 15:49:51 +01:00
|
|
|
);
|
|
|
|
}
|
2022-02-18 20:41:01 +01:00
|
|
|
|
|
|
|
export default App;
|