| 1 |
import { |
| 2 |
Alert, |
| 3 |
AlertTitle, |
| 4 |
Badge, |
| 5 |
Box, |
| 6 |
Button, |
| 7 |
CircularProgress, |
| 8 |
Divider, |
| 9 |
Drawer, |
| 10 |
IconButton, |
| 11 |
Link, |
| 12 |
List, |
| 13 |
ListItemButton, |
| 14 |
ListItemIcon, |
| 15 |
ListItemText, |
| 16 |
ListSubheader, |
| 17 |
Portal, |
| 18 |
Toolbar, |
| 19 |
Tooltip, |
| 20 |
Typography, |
| 21 |
useTheme, |
| 22 |
} from "@mui/material"; |
| 23 |
import * as React from "react"; |
| 24 |
import { useContext, useState } from "react"; |
| 25 |
import ChatBubbleOutlineIcon from "@mui/icons-material/ChatBubbleOutlineOutlined"; |
| 26 |
import Person from "@mui/icons-material/Person"; |
| 27 |
import SettingsIcon from "@mui/icons-material/Settings"; |
| 28 |
import AddIcon from "@mui/icons-material/Add"; |
| 29 |
import { useLocation, useNavigate } from "react-router-dom"; |
| 30 |
import ChatBubble from "@mui/icons-material/ChatBubble"; |
| 31 |
import MoreVert from "@mui/icons-material/MoreVert"; |
| 32 |
import NotificationsOffOutlined from "@mui/icons-material/NotificationsOffOutlined"; |
| 33 |
import Send from "@mui/icons-material/Send"; |
| 34 |
import ArticleIcon from "@mui/icons-material/Article"; |
| 35 |
import { Trans, useTranslation } from "react-i18next"; |
| 36 |
import CelebrationIcon from "@mui/icons-material/Celebration"; |
| 37 |
import SubscribeDialog from "./SubscribeDialog"; |
| 38 |
import { openUrl, topicDisplayName, topicUrl } from "../app/utils"; |
| 39 |
import routes from "./routes"; |
| 40 |
import { ConnectionState } from "../app/Connection"; |
| 41 |
import subscriptionManager from "../app/SubscriptionManager"; |
| 42 |
import notifier from "../app/Notifier"; |
| 43 |
import config from "../app/config"; |
| 44 |
import session from "../app/Session"; |
| 45 |
import accountApi, { Permission, Role } from "../app/AccountApi"; |
| 46 |
import UpgradeDialog from "./UpgradeDialog"; |
| 47 |
import AccountContext from "./AccountContext"; |
| 48 |
import { PermissionDenyAll, PermissionRead, PermissionReadWrite, PermissionWrite } from "./ReserveIcons"; |
| 49 |
import { SubscriptionPopup } from "./SubscriptionPopup"; |
| 50 |
import { useNotificationPermissionListener, useVersionChangeListener } from "./hooks"; |
| 51 |
|
| 52 |
const navWidth = 280; |
| 53 |
|
| 54 |
const Navigation = (props) => { |
| 55 |
const navigationList = <NavList {...props} />; |
| 56 |
return ( |
| 57 |
<Box component="nav" role="navigation" sx={{ width: { sm: Navigation.width }, flexShrink: { sm: 0 } }}> |
| 58 |
{/* Mobile drawer; only shown if menu icon clicked (mobile open) and display is small */} |
| 59 |
<Drawer |
| 60 |
variant="temporary" |
| 61 |
role="menubar" |
| 62 |
open={props.mobileDrawerOpen} |
| 63 |
onClose={props.onMobileDrawerToggle} |
| 64 |
ModalProps={{ keepMounted: true }} // Better open performance on mobile. |
| 65 |
sx={{ |
| 66 |
display: { xs: "block", sm: "none" }, |
| 67 |
"& .MuiDrawer-paper": { boxSizing: "border-box", width: navWidth, backgroundImage: "none" }, |
| 68 |
}} |
| 69 |
> |
| 70 |
{navigationList} |
| 71 |
</Drawer> |
| 72 |
{/* Big screen drawer; persistent, shown if screen is big */} |
| 73 |
<Drawer |
| 74 |
open |
| 75 |
variant="permanent" |
| 76 |
role="menubar" |
| 77 |
sx={{ |
| 78 |
display: { xs: "none", sm: "block" }, |
| 79 |
"& .MuiDrawer-paper": { boxSizing: "border-box", width: navWidth }, |
| 80 |
}} |
| 81 |
> |
| 82 |
{navigationList} |
| 83 |
</Drawer> |
| 84 |
</Box> |
| 85 |
); |
| 86 |
}; |
| 87 |
Navigation.width = navWidth; |
| 88 |
|
| 89 |
const NavList = (props) => { |
| 90 |
const theme = useTheme(); |
| 91 |
const { t } = useTranslation(); |
| 92 |
const navigate = useNavigate(); |
| 93 |
const location = useLocation(); |
| 94 |
const { account } = useContext(AccountContext); |
| 95 |
const [subscribeDialogKey, setSubscribeDialogKey] = useState(0); |
| 96 |
const [subscribeDialogOpen, setSubscribeDialogOpen] = useState(false); |
| 97 |
const [versionChanged, setVersionChanged] = useState(false); |
| 98 |
|
| 99 |
const handleVersionChange = () => { |
| 100 |
setVersionChanged(true); |
| 101 |
}; |
| 102 |
|
| 103 |
useVersionChangeListener(handleVersionChange); |
| 104 |
|
| 105 |
const handleSubscribeReset = () => { |
| 106 |
setSubscribeDialogOpen(false); |
| 107 |
setSubscribeDialogKey((prev) => prev + 1); |
| 108 |
}; |
| 109 |
|
| 110 |
const handleSubscribeSubmit = (subscription) => { |
| 111 |
console.log(`[Navigation] New subscription: ${subscription.id}`, subscription); |
| 112 |
handleSubscribeReset(); |
| 113 |
navigate(routes.forSubscription(subscription)); |
| 114 |
}; |
| 115 |
|
| 116 |
const handleAccountClick = () => { |
| 117 |
accountApi.sync(); // Dangle! |
| 118 |
navigate(routes.account); |
| 119 |
}; |
| 120 |
|
| 121 |
const isAdmin = account?.role === Role.ADMIN; |
| 122 |
const isPaid = account?.billing?.subscription; |
| 123 |
const hasTier = !!account?.tier; |
| 124 |
const showUpgradeBanner = config.enable_payments && !isAdmin && !isPaid && !hasTier; |
| 125 |
const showSubscriptionsList = props.subscriptions?.length > 0; |
| 126 |
const showNotificationPermissionRequired = useNotificationPermissionListener(() => notifier.notRequested()); |
| 127 |
const showNotificationPermissionDenied = useNotificationPermissionListener(() => notifier.denied()); |
| 128 |
const showNotificationIOSInstallRequired = notifier.iosSupportedButInstallRequired(); |
| 129 |
const showNotificationBrowserNotSupportedBox = !showNotificationIOSInstallRequired && !notifier.browserSupported(); |
| 130 |
const showNotificationContextNotSupportedBox = notifier.browserSupported() && !notifier.contextSupported(); // Only show if notifications are generally supported in the browser |
| 131 |
|
| 132 |
const alertVisible = |
| 133 |
versionChanged || |
| 134 |
showNotificationPermissionRequired || |
| 135 |
showNotificationPermissionDenied || |
| 136 |
showNotificationIOSInstallRequired || |
| 137 |
showNotificationBrowserNotSupportedBox || |
| 138 |
showNotificationContextNotSupportedBox; |
| 139 |
|
| 140 |
return ( |
| 141 |
<> |
| 142 |
<Toolbar sx={{ display: { xs: "none", sm: "block" } }} /> |
| 143 |
<List component="nav" sx={{ paddingTop: { xs: 0, sm: alertVisible ? 0 : "" } }}> |
| 144 |
{versionChanged && <VersionUpdateBanner />} |
| 145 |
{showNotificationPermissionRequired && <NotificationPermissionRequired />} |
| 146 |
{showNotificationPermissionDenied && <NotificationPermissionDeniedAlert />} |
| 147 |
{showNotificationBrowserNotSupportedBox && <NotificationBrowserNotSupportedAlert />} |
| 148 |
{showNotificationContextNotSupportedBox && <NotificationContextNotSupportedAlert />} |
| 149 |
{showNotificationIOSInstallRequired && <NotificationIOSInstallRequiredAlert />} |
| 150 |
{alertVisible && <Divider />} |
| 151 |
{!showSubscriptionsList && ( |
| 152 |
<ListItemButton onClick={() => navigate(routes.app)} selected={location.pathname === config.app_root}> |
| 153 |
<ListItemIcon> |
| 154 |
<ChatBubble /> |
| 155 |
</ListItemIcon> |
| 156 |
<ListItemText primary={t("nav_button_all_notifications")} /> |
| 157 |
</ListItemButton> |
| 158 |
)} |
| 159 |
{showSubscriptionsList && ( |
| 160 |
<> |
| 161 |
<ListSubheader>{t("nav_topics_title")}</ListSubheader> |
| 162 |
<ListItemButton onClick={() => navigate(routes.app)} selected={location.pathname === config.app_root}> |
| 163 |
<ListItemIcon> |
| 164 |
<ChatBubble /> |
| 165 |
</ListItemIcon> |
| 166 |
<ListItemText primary={t("nav_button_all_notifications")} /> |
| 167 |
</ListItemButton> |
| 168 |
<SubscriptionList subscriptions={props.subscriptions} selectedSubscription={props.selectedSubscription} /> |
| 169 |
<Divider sx={{ my: 1 }} /> |
| 170 |
</> |
| 171 |
)} |
| 172 |
{session.exists() && ( |
| 173 |
<ListItemButton onClick={handleAccountClick} selected={location.pathname === routes.account}> |
| 174 |
<ListItemIcon> |
| 175 |
<Person /> |
| 176 |
</ListItemIcon> |
| 177 |
<ListItemText primary={t("nav_button_account")} /> |
| 178 |
</ListItemButton> |
| 179 |
)} |
| 180 |
<ListItemButton onClick={() => navigate(routes.settings)} selected={location.pathname === routes.settings}> |
| 181 |
<ListItemIcon> |
| 182 |
<SettingsIcon /> |
| 183 |
</ListItemIcon> |
| 184 |
<ListItemText primary={t("nav_button_settings")} /> |
| 185 |
</ListItemButton> |
| 186 |
<ListItemButton onClick={() => openUrl("/docs")}> |
| 187 |
<ListItemIcon> |
| 188 |
<ArticleIcon /> |
| 189 |
</ListItemIcon> |
| 190 |
<ListItemText primary={t("nav_button_documentation")} /> |
| 191 |
</ListItemButton> |
| 192 |
<ListItemButton onClick={() => props.onPublishMessageClick()}> |
| 193 |
<ListItemIcon> |
| 194 |
<Send /> |
| 195 |
</ListItemIcon> |
| 196 |
<ListItemText primary={t("nav_button_publish_message")} /> |
| 197 |
</ListItemButton> |
| 198 |
<ListItemButton onClick={() => setSubscribeDialogOpen(true)}> |
| 199 |
<ListItemIcon> |
| 200 |
<AddIcon /> |
| 201 |
</ListItemIcon> |
| 202 |
<ListItemText primary={t("nav_button_subscribe")} /> |
| 203 |
</ListItemButton> |
| 204 |
{showUpgradeBanner && ( |
| 205 |
// The text background gradient didn't seem to do well with switching between light/dark mode, |
| 206 |
// So adding a `key` forces React to replace the entire component when the theme changes |
| 207 |
<UpgradeBanner key={`upgrade-banner-${theme.palette.mode}`} mode={theme.palette.mode} /> |
| 208 |
)} |
| 209 |
</List> |
| 210 |
<SubscribeDialog |
| 211 |
key={`subscribeDialog${subscribeDialogKey}`} // Resets dialog when canceled/closed |
| 212 |
open={subscribeDialogOpen} |
| 213 |
subscriptions={props.subscriptions} |
| 214 |
onCancel={handleSubscribeReset} |
| 215 |
onSuccess={handleSubscribeSubmit} |
| 216 |
/> |
| 217 |
</> |
| 218 |
); |
| 219 |
}; |
| 220 |
|
| 221 |
const UpgradeBanner = ({ mode }) => { |
| 222 |
const { t } = useTranslation(); |
| 223 |
const [dialogKey, setDialogKey] = useState(0); |
| 224 |
const [dialogOpen, setDialogOpen] = useState(false); |
| 225 |
|
| 226 |
const handleClick = () => { |
| 227 |
setDialogKey((k) => k + 1); |
| 228 |
setDialogOpen(true); |
| 229 |
}; |
| 230 |
|
| 231 |
return ( |
| 232 |
<Box |
| 233 |
sx={{ |
| 234 |
position: "fixed", |
| 235 |
width: `${Navigation.width - 1}px`, |
| 236 |
bottom: 0, |
| 237 |
mt: "auto", |
| 238 |
background: |
| 239 |
mode === "light" |
| 240 |
? "linear-gradient(150deg, rgba(196, 228, 221, 0.46) 0%, rgb(255, 255, 255) 100%)" |
| 241 |
: "linear-gradient(150deg, #203631 0%, #2a6e60 100%)", |
| 242 |
}} |
| 243 |
> |
| 244 |
<Divider /> |
| 245 |
<ListItemButton onClick={handleClick} sx={{ pt: 2, pb: 2 }}> |
| 246 |
<ListItemIcon> |
| 247 |
<CelebrationIcon sx={{ color: mode === "light" ? "#55b86e" : "#00ff95" }} fontSize="large" /> |
| 248 |
</ListItemIcon> |
| 249 |
<ListItemText |
| 250 |
sx={{ ml: 1 }} |
| 251 |
primary={t("nav_upgrade_banner_label")} |
| 252 |
secondary={t("nav_upgrade_banner_description")} |
| 253 |
primaryTypographyProps={{ |
| 254 |
style: { |
| 255 |
fontWeight: 500, |
| 256 |
fontSize: "1.1rem", |
| 257 |
background: |
| 258 |
mode === "light" |
| 259 |
? "-webkit-linear-gradient(45deg, #09009f, #00ff95 80%)" |
| 260 |
: "-webkit-linear-gradient(45deg,rgb(255, 255, 255), #00ff95 80%)", |
| 261 |
WebkitBackgroundClip: "text", |
| 262 |
WebkitTextFillColor: "transparent", |
| 263 |
}, |
| 264 |
}} |
| 265 |
secondaryTypographyProps={{ |
| 266 |
style: { |
| 267 |
fontSize: "1rem", |
| 268 |
}, |
| 269 |
}} |
| 270 |
/> |
| 271 |
</ListItemButton> |
| 272 |
<UpgradeDialog key={`upgradeDialog${dialogKey}`} open={dialogOpen} onCancel={() => setDialogOpen(false)} /> |
| 273 |
</Box> |
| 274 |
); |
| 275 |
}; |
| 276 |
|
| 277 |
const SubscriptionList = (props) => { |
| 278 |
const sortedSubscriptions = props.subscriptions |
| 279 |
.filter((s) => !s.internal) |
| 280 |
.sort((a, b) => (topicUrl(a.baseUrl, a.topic) < topicUrl(b.baseUrl, b.topic) ? -1 : 1)); |
| 281 |
return ( |
| 282 |
<> |
| 283 |
{sortedSubscriptions.map((subscription) => ( |
| 284 |
<SubscriptionItem |
| 285 |
key={subscription.id} |
| 286 |
subscription={subscription} |
| 287 |
selected={props.selectedSubscription && props.selectedSubscription.id === subscription.id} |
| 288 |
/> |
| 289 |
))} |
| 290 |
</> |
| 291 |
); |
| 292 |
}; |
| 293 |
|
| 294 |
const SubscriptionItem = (props) => { |
| 295 |
const { t } = useTranslation(); |
| 296 |
const navigate = useNavigate(); |
| 297 |
const [menuAnchorEl, setMenuAnchorEl] = useState(null); |
| 298 |
|
| 299 |
const { subscription } = props; |
| 300 |
const iconBadge = subscription.new <= 99 ? subscription.new : "99+"; |
| 301 |
const displayName = topicDisplayName(subscription); |
| 302 |
const ariaLabel = subscription.state === ConnectionState.Connecting ? `${displayName} (${t("nav_button_connecting")})` : displayName; |
| 303 |
const icon = |
| 304 |
subscription.state === ConnectionState.Connecting ? ( |
| 305 |
<CircularProgress size="24px" /> |
| 306 |
) : ( |
| 307 |
<Badge badgeContent={iconBadge} invisible={subscription.new === 0} color="primary"> |
| 308 |
<ChatBubbleOutlineIcon /> |
| 309 |
</Badge> |
| 310 |
); |
| 311 |
|
| 312 |
const handleClick = async () => { |
| 313 |
navigate(routes.forSubscription(subscription)); |
| 314 |
await subscriptionManager.markNotificationsRead(subscription.id); |
| 315 |
}; |
| 316 |
|
| 317 |
return ( |
| 318 |
<> |
| 319 |
<ListItemButton onClick={handleClick} selected={props.selected} aria-label={ariaLabel} aria-live="polite"> |
| 320 |
<ListItemIcon>{icon}</ListItemIcon> |
| 321 |
<ListItemText |
| 322 |
primary={displayName} |
| 323 |
primaryTypographyProps={{ |
| 324 |
style: { overflow: "hidden", textOverflow: "ellipsis" }, |
| 325 |
}} |
| 326 |
/> |
| 327 |
{subscription.reservation?.everyone && ( |
| 328 |
<ListItemIcon edge="end" sx={{ minWidth: "26px" }}> |
| 329 |
{subscription.reservation?.everyone === Permission.READ_WRITE && ( |
| 330 |
<Tooltip title={t("prefs_reservations_table_everyone_read_write")}> |
| 331 |
<PermissionReadWrite size="small" /> |
| 332 |
</Tooltip> |
| 333 |
)} |
| 334 |
{subscription.reservation?.everyone === Permission.READ_ONLY && ( |
| 335 |
<Tooltip title={t("prefs_reservations_table_everyone_read_only")}> |
| 336 |
<PermissionRead size="small" /> |
| 337 |
</Tooltip> |
| 338 |
)} |
| 339 |
{subscription.reservation?.everyone === Permission.WRITE_ONLY && ( |
| 340 |
<Tooltip title={t("prefs_reservations_table_everyone_write_only")}> |
| 341 |
<PermissionWrite size="small" /> |
| 342 |
</Tooltip> |
| 343 |
)} |
| 344 |
{subscription.reservation?.everyone === Permission.DENY_ALL && ( |
| 345 |
<Tooltip title={t("prefs_reservations_table_everyone_deny_all")}> |
| 346 |
<PermissionDenyAll size="small" /> |
| 347 |
</Tooltip> |
| 348 |
)} |
| 349 |
</ListItemIcon> |
| 350 |
)} |
| 351 |
{subscription.mutedUntil > 0 && ( |
| 352 |
<ListItemIcon edge="end" sx={{ minWidth: "26px" }} aria-label={t("nav_button_muted")}> |
| 353 |
<Tooltip title={t("nav_button_muted")}> |
| 354 |
<NotificationsOffOutlined /> |
| 355 |
</Tooltip> |
| 356 |
</ListItemIcon> |
| 357 |
)} |
| 358 |
<ListItemIcon edge="end" sx={{ minWidth: "26px" }}> |
| 359 |
<IconButton |
| 360 |
size="small" |
| 361 |
onMouseDown={(e) => e.stopPropagation()} |
| 362 |
onClick={(e) => { |
| 363 |
e.stopPropagation(); |
| 364 |
setMenuAnchorEl(e.currentTarget); |
| 365 |
}} |
| 366 |
> |
| 367 |
<MoreVert fontSize="small" /> |
| 368 |
</IconButton> |
| 369 |
</ListItemIcon> |
| 370 |
</ListItemButton> |
| 371 |
<Portal> |
| 372 |
<SubscriptionPopup subscription={subscription} anchor={menuAnchorEl} onClose={() => setMenuAnchorEl(null)} /> |
| 373 |
</Portal> |
| 374 |
</> |
| 375 |
); |
| 376 |
}; |
| 377 |
|
| 378 |
const NotificationPermissionRequired = () => { |
| 379 |
const { t } = useTranslation(); |
| 380 |
const requestPermission = async () => { |
| 381 |
await notifier.maybeRequestPermission(); |
| 382 |
}; |
| 383 |
return ( |
| 384 |
<Alert severity="warning" sx={{ paddingTop: 2 }}> |
| 385 |
<AlertTitle>{t("alert_notification_permission_required_title")}</AlertTitle> |
| 386 |
<Typography gutterBottom>{t("alert_notification_permission_required_description")}</Typography> |
| 387 |
<Button sx={{ float: "right" }} color="inherit" size="small" onClick={requestPermission}> |
| 388 |
{t("alert_notification_permission_required_button")} |
| 389 |
</Button> |
| 390 |
</Alert> |
| 391 |
); |
| 392 |
}; |
| 393 |
|
| 394 |
const NotificationPermissionDeniedAlert = () => { |
| 395 |
const { t } = useTranslation(); |
| 396 |
return ( |
| 397 |
<Alert severity="warning" sx={{ paddingTop: 2 }}> |
| 398 |
<AlertTitle>{t("alert_notification_permission_denied_title")}</AlertTitle> |
| 399 |
<Typography gutterBottom>{t("alert_notification_permission_denied_description")}</Typography> |
| 400 |
</Alert> |
| 401 |
); |
| 402 |
}; |
| 403 |
|
| 404 |
const NotificationIOSInstallRequiredAlert = () => { |
| 405 |
const { t } = useTranslation(); |
| 406 |
return ( |
| 407 |
<Alert severity="warning" sx={{ paddingTop: 2 }}> |
| 408 |
<AlertTitle>{t("alert_notification_ios_install_required_title")}</AlertTitle> |
| 409 |
<Typography gutterBottom>{t("alert_notification_ios_install_required_description")}</Typography> |
| 410 |
</Alert> |
| 411 |
); |
| 412 |
}; |
| 413 |
|
| 414 |
const NotificationBrowserNotSupportedAlert = () => { |
| 415 |
const { t } = useTranslation(); |
| 416 |
return ( |
| 417 |
<Alert severity="warning" sx={{ paddingTop: 2 }}> |
| 418 |
<AlertTitle>{t("alert_not_supported_title")}</AlertTitle> |
| 419 |
<Typography gutterBottom>{t("alert_not_supported_description")}</Typography> |
| 420 |
</Alert> |
| 421 |
); |
| 422 |
}; |
| 423 |
|
| 424 |
const NotificationContextNotSupportedAlert = () => { |
| 425 |
const { t } = useTranslation(); |
| 426 |
return ( |
| 427 |
<Alert severity="warning" sx={{ paddingTop: 2 }}> |
| 428 |
<AlertTitle>{t("alert_not_supported_title")}</AlertTitle> |
| 429 |
<Typography gutterBottom> |
| 430 |
<Trans |
| 431 |
i18nKey="alert_not_supported_context_description" |
| 432 |
components={{ |
| 433 |
mdnLink: <Link href="https://developer.mozilla.org/en-US/docs/Web/API/notification" target="_blank" rel="noopener" />, |
| 434 |
}} |
| 435 |
/> |
| 436 |
</Typography> |
| 437 |
</Alert> |
| 438 |
); |
| 439 |
}; |
| 440 |
|
| 441 |
const VersionUpdateBanner = () => { |
| 442 |
const { t } = useTranslation(); |
| 443 |
const handleRefresh = () => { |
| 444 |
window.location.reload(); |
| 445 |
}; |
| 446 |
return ( |
| 447 |
<Alert severity="info" sx={{ paddingTop: 2 }}> |
| 448 |
<AlertTitle>{t("version_update_available_title")}</AlertTitle> |
| 449 |
<Typography gutterBottom>{t("version_update_available_description")}</Typography> |
| 450 |
<Button sx={{ float: "right" }} color="inherit" size="small" onClick={handleRefresh}> |
| 451 |
{t("common_refresh")} |
| 452 |
</Button> |
| 453 |
</Alert> |
| 454 |
); |
| 455 |
}; |
| 456 |
|
| 457 |
export default Navigation; |