ntfy/web/src/components/App.js

247 lines
9.8 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 Typography from '@mui/material/Typography';
import Box from '@mui/material/Box';
2022-02-20 03:26:58 +00:00
import {styled, ThemeProvider} from '@mui/material/styles';
2022-02-20 00:48:33 +00:00
import CssBaseline from '@mui/material/CssBaseline';
import MuiDrawer from '@mui/material/Drawer';
import MuiAppBar from '@mui/material/AppBar';
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 ChevronLeftIcon from '@mui/icons-material/ChevronLeft';
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-20 03:26:58 +00:00
import AddDialog from "./AddDialog";
2022-02-21 21:24:13 +00:00
import NotificationList from "./NotificationList";
import DetailSettingsIcon from "./DetailSettingsIcon";
2022-02-20 03:26:58 +00:00
import theme from "./theme";
import api from "../app/Api";
import repository from "../app/Repository";
import connectionManager from "../app/ConnectionManager";
import Subscriptions from "../app/Subscriptions";
2022-02-20 00:48:33 +00:00
const drawerWidth = 240;
const AppBar = styled(MuiAppBar, {
shouldForwardProp: (prop) => prop !== 'open',
})(({ theme, open }) => ({
zIndex: theme.zIndex.drawer + 1,
transition: theme.transitions.create(['width', 'margin'], {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.leavingScreen,
}),
...(open && {
marginLeft: drawerWidth,
width: `calc(100% - ${drawerWidth}px)`,
transition: theme.transitions.create(['width', 'margin'], {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.enteringScreen,
}),
}),
}));
const Drawer = styled(MuiDrawer, { shouldForwardProp: (prop) => prop !== 'open' })(
({ theme, open }) => ({
'& .MuiDrawer-paper': {
position: 'relative',
whiteSpace: 'nowrap',
width: drawerWidth,
transition: theme.transitions.create('width', {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.enteringScreen,
}),
boxSizing: 'border-box',
...(!open && {
overflowX: 'hidden',
transition: theme.transitions.create('width', {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.leavingScreen,
}),
width: theme.spacing(7),
[theme.breakpoints.up('sm')]: {
width: theme.spacing(9),
},
}),
},
}),
);
const SubscriptionNav = (props) => {
const subscriptions = props.subscriptions;
2022-02-18 14:49:51 +00:00
return (
2022-02-20 03:26:58 +00:00
<>
{subscriptions.map((id, subscription) =>
2022-02-20 03:26:58 +00:00
<SubscriptionNavItem
key={id}
subscription={subscription}
selected={props.selectedSubscription && props.selectedSubscription.id === id}
onClick={() => props.handleSubscriptionClick(id)}
/>)
}
2022-02-20 03:26:58 +00:00
</>
2022-02-18 14:49:51 +00:00
);
}
2022-02-20 03:26:58 +00:00
const SubscriptionNavItem = (props) => {
2022-02-18 19:41:01 +00:00
const subscription = props.subscription;
2022-02-18 14:49:51 +00:00
return (
2022-02-20 03:26:58 +00:00
<ListItemButton onClick={props.onClick} selected={props.selected}>
<ListItemIcon><ChatBubbleOutlineIcon /></ListItemIcon>
<ListItemText primary={subscription.shortUrl()}/>
2022-02-20 00:48:33 +00:00
</ListItemButton>
2022-02-18 14:49:51 +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-20 03:26:58 +00:00
const [drawerOpen, setDrawerOpen] = useState(true);
const [subscriptions, setSubscriptions] = useState(new Subscriptions());
2022-02-21 01:04:03 +00:00
const [selectedSubscription, setSelectedSubscription] = useState(null);
2022-02-23 03:10:50 +00:00
const [subscribeDialogOpen, setSubscribeDialogOpen] = useState(false);
const handleNotification = (subscriptionId, notification) => {
setSubscriptions(prev => {
const newSubscription = prev.get(subscriptionId).addNotification(notification);
return prev.update(newSubscription).clone();
});
};
2022-02-23 03:10:50 +00:00
const handleSubscribeSubmit = (subscription) => {
console.log(`[App] New subscription: ${subscription.id}`);
2022-02-23 03:10:50 +00:00
setSubscribeDialogOpen(false);
setSubscriptions(prev => prev.add(subscription).clone());
2022-02-23 04:22:30 +00:00
setSelectedSubscription(subscription);
api.poll(subscription.baseUrl, subscription.topic)
2022-02-23 04:22:30 +00:00
.then(messages => {
setSubscriptions(prev => {
const newSubscription = prev.get(subscription.id).addNotifications(messages);
return prev.update(newSubscription).clone();
});
2022-02-23 04:22:30 +00:00
});
};
2022-02-23 03:10:50 +00:00
const handleSubscribeCancel = () => {
console.log(`[App] Cancel clicked`);
2022-02-23 03:10:50 +00:00
setSubscribeDialogOpen(false);
};
const handleUnsubscribe = (subscriptionId) => {
console.log(`[App] Unsubscribing from ${subscriptionId}`);
2022-02-23 03:10:50 +00:00
setSubscriptions(prev => {
const newSubscriptions = prev.remove(subscriptionId).clone();
setSelectedSubscription(newSubscriptions.firstOrNull());
2022-02-23 03:10:50 +00:00
return newSubscriptions;
});
};
const handleSubscriptionClick = (subscriptionId) => {
console.log(`[App] Selected ${subscriptionId}`);
setSelectedSubscription(subscriptions.get(subscriptionId));
};
const notifications = (selectedSubscription !== null) ? selectedSubscription.getNotifications() : [];
2022-02-20 00:48:33 +00:00
const toggleDrawer = () => {
2022-02-20 03:26:58 +00:00
setDrawerOpen(!drawerOpen);
2022-02-20 00:48:33 +00:00
};
2022-02-21 01:04:03 +00:00
useEffect(() => {
connectionManager.refresh(subscriptions, handleNotification);
repository.saveSubscriptions(subscriptions);
2022-02-21 01:04:03 +00:00
}, [subscriptions]);
2022-02-18 14:49:51 +00:00
return (
2022-02-20 03:26:58 +00:00
<ThemeProvider theme={theme}>
2022-02-21 21:24:13 +00:00
<CssBaseline />
2022-02-20 00:48:33 +00:00
<Box sx={{ display: 'flex' }}>
2022-02-20 03:26:58 +00:00
<AppBar position="absolute" open={drawerOpen}>
2022-02-21 21:24:13 +00:00
<Toolbar sx={{pr: '24px'}} color="primary">
2022-02-20 00:48:33 +00:00
<IconButton
edge="start"
color="inherit"
aria-label="open drawer"
onClick={toggleDrawer}
sx={{
marginRight: '36px',
2022-02-20 03:26:58 +00:00
...(drawerOpen && { display: 'none' }),
2022-02-20 00:48:33 +00:00
}}
>
<MenuIcon />
</IconButton>
<Typography
component="h1"
variant="h6"
color="inherit"
noWrap
sx={{ flexGrow: 1 }}
>
2022-02-23 04:22:30 +00:00
{(selectedSubscription !== null) ? selectedSubscription.shortUrl() : "ntfy"}
2022-02-20 00:48:33 +00:00
</Typography>
2022-02-23 04:22:30 +00:00
{selectedSubscription !== null && <DetailSettingsIcon
2022-02-23 03:10:50 +00:00
subscription={selectedSubscription}
onUnsubscribe={handleUnsubscribe}
2022-02-23 04:22:30 +00:00
/>}
2022-02-20 00:48:33 +00:00
</Toolbar>
</AppBar>
2022-02-20 03:26:58 +00:00
<Drawer variant="permanent" open={drawerOpen}>
2022-02-20 00:48:33 +00:00
<Toolbar
sx={{
display: 'flex',
alignItems: 'center',
justifyContent: 'flex-end',
px: [1],
}}
>
<IconButton onClick={toggleDrawer}>
<ChevronLeftIcon />
</IconButton>
</Toolbar>
<Divider />
<List component="nav">
<SubscriptionNav
subscriptions={subscriptions}
selectedSubscription={selectedSubscription}
handleSubscriptionClick={handleSubscriptionClick}
/>
<Divider sx={{ my: 1 }} />
<ListItemButton>
<ListItemIcon>
<SettingsIcon />
</ListItemIcon>
<ListItemText primary="Settings" />
</ListItemButton>
2022-02-23 03:10:50 +00:00
<ListItemButton onClick={() => setSubscribeDialogOpen(true)}>
2022-02-20 00:48:33 +00:00
<ListItemIcon>
<AddIcon />
</ListItemIcon>
<ListItemText primary="Add subscription" />
</ListItemButton>
</List>
</Drawer>
<Box
component="main"
sx={{
backgroundColor: (theme) =>
theme.palette.mode === 'light'
? theme.palette.grey[100]
: theme.palette.grey[900],
flexGrow: 1,
height: '100vh',
overflow: 'auto',
}}
>
<Toolbar />
2022-02-21 21:24:13 +00:00
<NotificationList notifications={notifications}/>
2022-02-20 00:48:33 +00:00
</Box>
2022-02-18 14:49:51 +00:00
</Box>
2022-02-20 03:26:58 +00:00
<AddDialog
2022-02-23 03:10:50 +00:00
open={subscribeDialogOpen}
onCancel={handleSubscribeCancel}
onSubmit={handleSubscribeSubmit}
2022-02-20 03:26:58 +00:00
/>
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
export default App;