1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-11-03 10:24:14 +01:00
ntfy/web/src/components/Navigation.js

364 lines
16 KiB
JavaScript
Raw Normal View History

2022-02-25 18:46:22 +01:00
import Drawer from "@mui/material/Drawer";
import * as React from "react";
2023-01-10 02:37:13 +01:00
import {useContext, useState} from "react";
2022-02-25 18:46:22 +01:00
import ListItemButton from "@mui/material/ListItemButton";
import ListItemIcon from "@mui/material/ListItemIcon";
import ChatBubbleOutlineIcon from "@mui/icons-material/ChatBubbleOutline";
2022-12-16 04:07:04 +01:00
import Person from "@mui/icons-material/Person";
2022-02-25 18:46:22 +01:00
import ListItemText from "@mui/material/ListItemText";
import Toolbar from "@mui/material/Toolbar";
import Divider from "@mui/material/Divider";
import List from "@mui/material/List";
import SettingsIcon from "@mui/icons-material/Settings";
import AddIcon from "@mui/icons-material/Add";
import SubscribeDialog from "./SubscribeDialog";
2023-02-11 03:19:44 +01:00
import {Alert, AlertTitle, Badge, CircularProgress, Link, ListSubheader, Portal, Tooltip} from "@mui/material";
2022-02-26 16:14:43 +01:00
import Button from "@mui/material/Button";
import Typography from "@mui/material/Typography";
2022-06-29 21:57:56 +02:00
import {openUrl, topicDisplayName, topicUrl} from "../app/utils";
import routes from "./routes";
import {ConnectionState} from "../app/Connection";
2023-01-05 04:47:12 +01:00
import {useLocation, useNavigate} from "react-router-dom";
2022-03-07 04:37:13 +01:00
import subscriptionManager from "../app/SubscriptionManager";
2023-02-11 03:19:44 +01:00
import {ChatBubble, MoreVert, NotificationsOffOutlined, Send} from "@mui/icons-material";
2022-03-08 17:33:17 +01:00
import Box from "@mui/material/Box";
import notifier from "../app/Notifier";
import config from "../app/config";
2022-03-11 04:58:24 +01:00
import ArticleIcon from '@mui/icons-material/Article';
import {Trans, useTranslation} from "react-i18next";
2022-12-16 04:07:04 +01:00
import session from "../app/Session";
2023-01-30 19:10:45 +01:00
import accountApi, {Permission, Role} from "../app/AccountApi";
2023-01-05 04:47:12 +01:00
import CelebrationIcon from '@mui/icons-material/Celebration';
2023-01-09 21:40:46 +01:00
import UpgradeDialog from "./UpgradeDialog";
2023-01-10 02:37:13 +01:00
import {AccountContext} from "./App";
2023-01-24 02:04:04 +01:00
import {PermissionDenyAll, PermissionRead, PermissionReadWrite, PermissionWrite} from "./ReserveIcons";
2023-02-01 03:39:30 +01:00
import IconButton from "@mui/material/IconButton";
2023-02-11 03:19:44 +01:00
import { SubscriptionPopup } from "./SubscriptionPopup";
2022-02-25 18:46:22 +01:00
2022-03-08 22:56:41 +01:00
const navWidth = 280;
2022-02-25 18:46:22 +01:00
const Navigation = (props) => {
2022-02-26 16:14:43 +01:00
const navigationList = <NavList {...props}/>;
2022-02-25 18:46:22 +01:00
return (
2022-05-03 01:30:29 +02:00
<Box
component="nav"
2022-05-03 21:09:20 +02:00
role="navigation"
2022-05-03 01:30:29 +02:00
sx={{width: {sm: Navigation.width}, flexShrink: {sm: 0}}}
>
2022-02-25 18:46:22 +01:00
{/* Mobile drawer; only shown if menu icon clicked (mobile open) and display is small */}
<Drawer
variant="temporary"
2022-05-03 21:09:20 +02:00
role="menubar"
2022-02-25 18:46:22 +01:00
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: navWidth },
}}
>
{navigationList}
</Drawer>
{/* Big screen drawer; persistent, shown if screen is big */}
<Drawer
open
variant="permanent"
2022-05-03 21:09:20 +02:00
role="menubar"
2022-02-25 18:46:22 +01:00
sx={{
display: { xs: 'none', sm: 'block' },
'& .MuiDrawer-paper': { boxSizing: 'border-box', width: navWidth },
}}
>
{navigationList}
</Drawer>
2022-03-08 17:33:17 +01:00
</Box>
2022-02-25 18:46:22 +01:00
);
};
Navigation.width = navWidth;
const NavList = (props) => {
2022-04-08 01:11:51 +02:00
const { t } = useTranslation();
2022-03-04 22:10:04 +01:00
const navigate = useNavigate();
const location = useLocation();
2023-01-10 02:37:13 +01:00
const { account } = useContext(AccountContext);
const [subscribeDialogKey, setSubscribeDialogKey] = useState(0);
2022-02-25 18:46:22 +01:00
const [subscribeDialogOpen, setSubscribeDialogOpen] = useState(false);
2022-03-06 04:33:34 +01:00
const handleSubscribeReset = () => {
2022-02-25 18:46:22 +01:00
setSubscribeDialogOpen(false);
setSubscribeDialogKey(prev => prev+1);
}
2022-03-06 04:33:34 +01:00
const handleSubscribeSubmit = (subscription) => {
2022-03-07 03:39:20 +01:00
console.log(`[Navigation] New subscription: ${subscription.id}`, subscription);
handleSubscribeReset();
navigate(routes.forSubscription(subscription));
2022-03-08 17:33:17 +01:00
handleRequestNotificationPermission();
2022-02-25 18:46:22 +01:00
}
2022-03-06 04:33:34 +01:00
2022-03-08 17:33:17 +01:00
const handleRequestNotificationPermission = () => {
notifier.maybeRequestPermission(granted => props.onNotificationGranted(granted))
};
2023-01-03 04:21:11 +01:00
const handleAccountClick = () => {
accountApi.sync(); // Dangle!
navigate(routes.account);
};
2023-01-30 19:10:45 +01:00
const isAdmin = account?.role === Role.ADMIN;
const isPaid = account?.billing?.subscription;
const showUpgradeBanner = config.enable_payments && !isAdmin && !isPaid;
2022-03-02 22:16:30 +01:00
const showSubscriptionsList = props.subscriptions?.length > 0;
const showNotificationBrowserNotSupportedBox = !notifier.browserSupported();
const showNotificationContextNotSupportedBox = notifier.browserSupported() && !notifier.contextSupported(); // Only show if notifications are generally supported in the browser
2022-03-11 00:11:12 +01:00
const showNotificationGrantBox = notifier.supported() && props.subscriptions?.length > 0 && !props.notificationsGranted;
const navListPadding = (showNotificationGrantBox || showNotificationBrowserNotSupportedBox || showNotificationContextNotSupportedBox) ? '0' : '';
2022-03-06 04:33:34 +01:00
2022-02-25 18:46:22 +01:00
return (
<>
2022-03-04 22:10:04 +01:00
<Toolbar sx={{ display: { xs: 'none', sm: 'block' } }}/>
<List component="nav" sx={{ paddingTop: navListPadding }}>
{showNotificationBrowserNotSupportedBox && <NotificationBrowserNotSupportedAlert/>}
{showNotificationContextNotSupportedBox && <NotificationContextNotSupportedAlert/>}
2022-03-11 00:11:12 +01:00
{showNotificationGrantBox && <NotificationGrantAlert onRequestPermissionClick={handleRequestNotificationPermission}/>}
2022-03-08 02:11:58 +01:00
{!showSubscriptionsList &&
2023-01-05 04:47:12 +01:00
<ListItemButton onClick={() => navigate(routes.app)} selected={location.pathname === config.app_root}>
2022-03-08 02:11:58 +01:00
<ListItemIcon><ChatBubble/></ListItemIcon>
2022-04-08 03:46:33 +02:00
<ListItemText primary={t("nav_button_all_notifications")}/>
2022-03-08 02:11:58 +01:00
</ListItemButton>}
2022-02-26 16:14:43 +01:00
{showSubscriptionsList &&
<>
2022-04-08 03:46:33 +02:00
<ListSubheader>{t("nav_topics_title")}</ListSubheader>
2023-01-05 04:47:12 +01:00
<ListItemButton onClick={() => navigate(routes.app)} selected={location.pathname === config.app_root}>
2022-03-08 02:11:58 +01:00
<ListItemIcon><ChatBubble/></ListItemIcon>
2022-04-08 03:46:33 +02:00
<ListItemText primary={t("nav_button_all_notifications")}/>
2022-03-08 02:11:58 +01:00
</ListItemButton>
2022-02-26 16:14:43 +01:00
<SubscriptionList
subscriptions={props.subscriptions}
selectedSubscription={props.selectedSubscription}
/>
2022-02-26 20:22:21 +01:00
<Divider sx={{my: 1}}/>
2022-02-26 16:14:43 +01:00
</>}
2022-12-16 04:07:04 +01:00
{session.exists() &&
2023-01-03 04:21:11 +01:00
<ListItemButton onClick={handleAccountClick} selected={location.pathname === routes.account}>
2022-12-16 04:07:04 +01:00
<ListItemIcon><Person/></ListItemIcon>
<ListItemText primary={t("nav_button_account")}/>
2022-12-29 08:32:05 +01:00
</ListItemButton>
}
<ListItemButton onClick={() => navigate(routes.settings)} selected={location.pathname === routes.settings}>
2022-03-04 22:10:04 +01:00
<ListItemIcon><SettingsIcon/></ListItemIcon>
2022-04-08 03:46:33 +02:00
<ListItemText primary={t("nav_button_settings")}/>
2022-02-25 18:46:22 +01:00
</ListItemButton>
2022-03-11 04:58:24 +01:00
<ListItemButton onClick={() => openUrl("/docs")}>
<ListItemIcon><ArticleIcon/></ListItemIcon>
2022-04-08 03:46:33 +02:00
<ListItemText primary={t("nav_button_documentation")}/>
2022-03-11 04:58:24 +01:00
</ListItemButton>
2022-04-04 04:58:44 +02:00
<ListItemButton onClick={() => props.onPublishMessageClick()}>
2022-04-04 04:11:26 +02:00
<ListItemIcon><Send/></ListItemIcon>
2022-04-08 03:46:33 +02:00
<ListItemText primary={t("nav_button_publish_message")}/>
2022-04-04 04:11:26 +02:00
</ListItemButton>
2022-02-25 18:46:22 +01:00
<ListItemButton onClick={() => setSubscribeDialogOpen(true)}>
2022-03-04 22:10:04 +01:00
<ListItemIcon><AddIcon/></ListItemIcon>
2022-04-08 01:11:51 +02:00
<ListItemText primary={t("nav_button_subscribe")}/>
2022-02-25 18:46:22 +01:00
</ListItemButton>
2023-01-05 04:47:12 +01:00
{showUpgradeBanner &&
2023-01-09 21:40:46 +01:00
<UpgradeBanner/>
2023-01-05 04:47:12 +01:00
}
2022-02-25 18:46:22 +01:00
</List>
<SubscribeDialog
2022-02-28 22:56:38 +01:00
key={`subscribeDialog${subscribeDialogKey}`} // Resets dialog when canceled/closed
2022-02-25 18:46:22 +01:00
open={subscribeDialogOpen}
subscriptions={props.subscriptions}
onCancel={handleSubscribeReset}
onSuccess={handleSubscribeSubmit}
2022-02-25 18:46:22 +01:00
/>
</>
);
};
2023-01-09 21:40:46 +01:00
const UpgradeBanner = () => {
const [dialogKey, setDialogKey] = useState(0);
2023-01-09 21:40:46 +01:00
const [dialogOpen, setDialogOpen] = useState(false);
const handleClick = () => {
setDialogKey(k => k + 1);
setDialogOpen(true);
};
2023-01-09 21:40:46 +01:00
return (
<Box sx={{
position: "fixed",
width: `${Navigation.width - 1}px`,
bottom: 0,
mt: 'auto',
background: "linear-gradient(150deg, rgba(196, 228, 221, 0.46) 0%, rgb(255, 255, 255) 100%)",
}}>
<Divider/>
<ListItemButton onClick={handleClick} sx={{pt: 2, pb: 2}}>
2023-01-09 21:40:46 +01:00
<ListItemIcon><CelebrationIcon sx={{ color: "#55b86e" }} fontSize="large"/></ListItemIcon>
<ListItemText
sx={{ ml: 1 }}
primary={"Upgrade to ntfy Pro"}
2023-01-09 23:56:51 +01:00
secondary={"Reserve topics, more messages & emails, and larger attachments"}
2023-01-09 21:40:46 +01:00
primaryTypographyProps={{
style: {
fontWeight: 500,
2023-01-09 23:56:51 +01:00
fontSize: "1.1rem",
2023-01-09 21:40:46 +01:00
background: "-webkit-linear-gradient(45deg, #09009f, #00ff95 80%)",
WebkitBackgroundClip: "text",
WebkitTextFillColor: "transparent"
}
}}
2023-01-09 23:56:51 +01:00
secondaryTypographyProps={{
style: {
fontSize: "1rem"
}
}}
2023-01-09 21:40:46 +01:00
/>
</ListItemButton>
<UpgradeDialog
key={`upgradeDialog${dialogKey}`}
2023-01-09 21:40:46 +01:00
open={dialogOpen}
onCancel={() => setDialogOpen(false)}
/>
</Box>
);
};
2022-02-26 16:14:43 +01:00
const SubscriptionList = (props) => {
2023-01-12 03:38:10 +01:00
const sortedSubscriptions = props.subscriptions
.filter(s => !s.internal)
.sort((a, b) => {
return (topicUrl(a.baseUrl, a.topic) < topicUrl(b.baseUrl, b.topic)) ? -1 : 1;
});
2022-02-25 18:46:22 +01:00
return (
<>
2022-03-05 14:52:52 +01:00
{sortedSubscriptions.map(subscription =>
<SubscriptionItem
2022-03-02 22:16:30 +01:00
key={subscription.id}
subscription={subscription}
2022-03-04 22:10:04 +01:00
selected={props.selectedSubscription && props.selectedSubscription.id === subscription.id}
/>)}
2022-02-25 18:46:22 +01:00
</>
);
}
const SubscriptionItem = (props) => {
2022-05-03 21:09:20 +02:00
const { t } = useTranslation();
2022-03-04 22:10:04 +01:00
const navigate = useNavigate();
2023-02-01 03:39:30 +01:00
const [menuAnchorEl, setMenuAnchorEl] = useState(null);
const subscription = props.subscription;
2022-03-07 22:36:49 +01:00
const iconBadge = (subscription.new <= 99) ? subscription.new : "99+";
2022-06-29 21:57:56 +02:00
const displayName = topicDisplayName(subscription);
2022-05-03 21:09:20 +02:00
const ariaLabel = (subscription.state === ConnectionState.Connecting)
2022-06-29 21:57:56 +02:00
? `${displayName} (${t("nav_button_connecting")})`
: displayName;
2023-02-01 03:39:30 +01:00
const icon = (subscription.state === ConnectionState.Connecting)
? <CircularProgress size="24px"/>
: <Badge badgeContent={iconBadge} invisible={subscription.new === 0} color="primary"><ChatBubbleOutlineIcon/></Badge>;
2022-03-07 04:37:13 +01:00
const handleClick = async () => {
navigate(routes.forSubscription(subscription));
2022-03-07 04:37:13 +01:00
await subscriptionManager.markNotificationsRead(subscription.id);
};
2023-02-01 03:39:30 +01:00
return (
2023-02-01 04:12:16 +01:00
<>
<ListItemButton onClick={handleClick} selected={props.selected} aria-label={ariaLabel} aria-live="polite">
<ListItemIcon>{icon}</ListItemIcon>
<ListItemText primary={displayName} primaryTypographyProps={{ style: { overflow: "hidden", textOverflow: "ellipsis" } }}/>
{subscription.reservation?.everyone &&
<ListItemIcon edge="end" sx={{ minWidth: "26px" }}>
{subscription.reservation?.everyone === Permission.READ_WRITE &&
<Tooltip title={t("prefs_reservations_table_everyone_read_write")}><PermissionReadWrite size="small"/></Tooltip>
}
{subscription.reservation?.everyone === Permission.READ_ONLY &&
<Tooltip title={t("prefs_reservations_table_everyone_read_only")}><PermissionRead size="small"/></Tooltip>
}
{subscription.reservation?.everyone === Permission.WRITE_ONLY &&
<Tooltip title={t("prefs_reservations_table_everyone_write_only")}><PermissionWrite size="small"/></Tooltip>
}
{subscription.reservation?.everyone === Permission.DENY_ALL &&
<Tooltip title={t("prefs_reservations_table_everyone_deny_all")}><PermissionDenyAll size="small"/></Tooltip>
}
</ListItemIcon>
}
{subscription.mutedUntil > 0 &&
<ListItemIcon edge="end" sx={{ minWidth: "26px" }} aria-label={t("nav_button_muted")}>
<Tooltip title={t("nav_button_muted")}><NotificationsOffOutlined /></Tooltip>
</ListItemIcon>
}
<ListItemIcon edge="end" sx={{minWidth: "26px"}}>
<IconButton size="small" onMouseDown={(e) => e.stopPropagation()} onClick={(e) => setMenuAnchorEl(e.currentTarget)}>
<MoreVert fontSize="small"/>
</IconButton>
2023-01-03 17:28:04 +01:00
</ListItemIcon>
2023-02-01 04:12:16 +01:00
</ListItemButton>
<Portal>
<SubscriptionPopup
subscription={subscription}
anchor={menuAnchorEl}
onClose={() => setMenuAnchorEl(null)}
/>
</Portal>
</>
);
};
2022-03-11 00:11:12 +01:00
const NotificationGrantAlert = (props) => {
2022-04-08 03:46:33 +02:00
const { t } = useTranslation();
return (
<>
<Alert severity="warning" sx={{paddingTop: 2}}>
2022-04-08 03:46:33 +02:00
<AlertTitle>{t("alert_grant_title")}</AlertTitle>
<Typography gutterBottom>{t("alert_grant_description")}</Typography>
<Button
sx={{float: 'right'}}
color="inherit"
size="small"
onClick={props.onRequestPermissionClick}
>
2022-04-08 03:46:33 +02:00
{t("alert_grant_button")}
</Button>
</Alert>
<Divider/>
</>
);
};
const NotificationBrowserNotSupportedAlert = () => {
2022-04-08 03:46:33 +02:00
const { t } = useTranslation();
2022-03-11 00:11:12 +01:00
return (
<>
<Alert severity="warning" sx={{paddingTop: 2}}>
2022-04-08 03:46:33 +02:00
<AlertTitle>{t("alert_not_supported_title")}</AlertTitle>
<Typography gutterBottom>{t("alert_not_supported_description")}</Typography>
2022-03-11 00:11:12 +01:00
</Alert>
<Divider/>
</>
);
};
const NotificationContextNotSupportedAlert = () => {
const { t } = useTranslation();
return (
<>
<Alert severity="warning" sx={{paddingTop: 2}}>
<AlertTitle>{t("alert_not_supported_title")}</AlertTitle>
<Typography gutterBottom>
<Trans
i18nKey="alert_not_supported_context_description"
components={{
mdnLink: <Link href="https://developer.mozilla.org/en-US/docs/Web/API/notification" target="_blank" rel="noopener"/>
}}
/>
</Typography>
</Alert>
<Divider/>
</>
);
};
2022-02-25 18:46:22 +01:00
export default Navigation;