Debian Codegraph @

web/src/components/ActionBar.jsx

raw
1 import { AppBar, Toolbar, IconButton, Typography, Box, MenuItem, Button, Divider, ListItemIcon, useTheme } from "@mui/material";
2 import MenuIcon from "@mui/icons-material/Menu";
3 import * as React from "react";
4 import { useState } from "react";
5 import { useLocation, useNavigate } from "react-router-dom";
6 import MoreVertIcon from "@mui/icons-material/MoreVert";
7 import NotificationsIcon from "@mui/icons-material/Notifications";
8 import NotificationsOffIcon from "@mui/icons-material/NotificationsOff";
9 import RefreshIcon from "@mui/icons-material/Refresh";
10 import { useTranslation } from "react-i18next";
11 import AccountCircleIcon from "@mui/icons-material/AccountCircle";
12 import Logout from "@mui/icons-material/Logout";
13 import Person from "@mui/icons-material/Person";
14 import Settings from "@mui/icons-material/Settings";
15 import session from "../app/Session";
16 import logo from "../img/ntfy.svg";
17 import subscriptionManager from "../app/SubscriptionManager";
18 import routes from "./routes";
19 import db from "../app/db";
20 import { topicDisplayName } from "../app/utils";
21 import { fadeNavigate } from "../app/transition";
22 import Navigation from "./Navigation";
23 import accountApi from "../app/AccountApi";
24 import PopupMenu from "./PopupMenu";
25 import { SubscriptionPopup } from "./SubscriptionPopup";
26 import { useIsLaunchedPWA } from "./hooks";
27
28 const ActionBar = (props) => {
29 const theme = useTheme();
30 const { t } = useTranslation();
31 const location = useLocation();
32 const isLaunchedPWA = useIsLaunchedPWA();
33
34 let title = "ntfy";
35 if (props.selected) {
36 title = topicDisplayName(props.selected);
37 } else if (location.pathname === routes.settings) {
38 title = t("action_bar_settings");
39 } else if (location.pathname === routes.account) {
40 title = t("action_bar_account");
41 }
42
43 const getActionBarBackground = () => {
44 if (isLaunchedPWA) {
45 return "#317f6f";
46 }
47
48 switch (theme.palette.mode) {
49 case "dark":
50 return "linear-gradient(150deg, #203631 0%, #2a6e60 100%)";
51
52 case "light":
53 default:
54 return "linear-gradient(150deg, #338574 0%, #56bda8 100%)";
55 }
56 };
57
58 return (
59 <AppBar
60 position="fixed"
61 sx={{
62 width: "100%",
63 zIndex: { sm: 1250 }, // > Navigation (1200), but < Dialog (1300)
64 ml: { sm: `${Navigation.width}px` },
65 }}
66 >
67 <Toolbar
68 sx={{
69 pr: "24px",
70 background: getActionBarBackground(),
71 }}
72 >
73 <IconButton
74 color="inherit"
75 edge="start"
76 aria-label={t("action_bar_show_menu")}
77 onClick={props.onMobileDrawerToggle}
78 sx={{ mr: 2, display: { sm: "none" } }}
79 >
80 <MenuIcon />
81 </IconButton>
82 <Box
83 component="img"
84 src={logo}
85 alt={t("action_bar_logo_alt")}
86 sx={{
87 display: { xs: "none", sm: "block" },
88 marginRight: "10px",
89 height: "28px",
90 }}
91 />
92 <Typography variant="h6" noWrap component="div" sx={{ flexGrow: 1 }}>
93 {title}
94 </Typography>
95 {isLaunchedPWA && <ReloadIcon />}
96 {props.selected && <SettingsIcons subscription={props.selected} onUnsubscribe={props.onUnsubscribe} />}
97 <ProfileIcon />
98 </Toolbar>
99 </AppBar>
100 );
101 };
102
103 const SettingsIcons = (props) => {
104 const { t } = useTranslation();
105 const [anchorEl, setAnchorEl] = useState(null);
106 const { subscription } = props;
107
108 const handleToggleMute = async () => {
109 const mutedUntil = subscription.mutedUntil ? 0 : 1; // Make this a timestamp in the future
110 await subscriptionManager.setMutedUntil(subscription.id, mutedUntil);
111 };
112
113 return (
114 <>
115 <IconButton color="inherit" size="large" edge="end" onClick={handleToggleMute} aria-label={t("action_bar_toggle_mute")}>
116 {subscription.mutedUntil ? <NotificationsOffIcon /> : <NotificationsIcon />}
117 </IconButton>
118 <IconButton
119 color="inherit"
120 size="large"
121 edge="end"
122 onClick={(ev) => setAnchorEl(ev.currentTarget)}
123 aria-label={t("action_bar_toggle_action_menu")}
124 >
125 <MoreVertIcon />
126 </IconButton>
127 <SubscriptionPopup subscription={subscription} anchor={anchorEl} placement="right" onClose={() => setAnchorEl(null)} />
128 </>
129 );
130 };
131
132 // ReloadIcon hard-refreshes the app. A plain reload would just serve the precached PWA shell,
133 // so we first purge the service worker caches to force fresh assets from the network.
134 const ReloadIcon = () => {
135 const { t } = useTranslation();
136
137 const handleReload = async () => {
138 try {
139 if ("caches" in window) {
140 const keys = await caches.keys();
141 await Promise.all(keys.map((key) => caches.delete(key)));
142 }
143 } catch (e) {
144 console.warn("[ActionBar] Error clearing caches during reload", e);
145 } finally {
146 window.location.reload();
147 }
148 };
149
150 return (
151 <IconButton color="inherit" size="large" edge="end" onClick={handleReload} aria-label={t("action_bar_reload")}>
152 <RefreshIcon />
153 </IconButton>
154 );
155 };
156
157 const ProfileIcon = () => {
158 const { t } = useTranslation();
159 const [anchorEl, setAnchorEl] = useState(null);
160 const open = Boolean(anchorEl);
161 const navigate = useNavigate();
162
163 const handleClick = (event) => {
164 setAnchorEl(event.currentTarget);
165 };
166
167 const handleClose = () => {
168 setAnchorEl(null);
169 };
170
171 const handleLogout = async () => {
172 try {
173 await accountApi.logout();
174 await db().delete();
175 } finally {
176 await session.resetAndRedirect(routes.app, { fade: true });
177 }
178 };
179
180 return (
181 <>
182 {session.exists() && (
183 <IconButton color="inherit" size="large" edge="end" onClick={handleClick} aria-label={t("action_bar_profile_title")}>
184 <AccountCircleIcon />
185 </IconButton>
186 )}
187 {!session.exists() && config.enable_login && (
188 <Button
189 color="inherit"
190 variant="text"
191 onClick={() => fadeNavigate(navigate, routes.login)}
192 sx={{ m: 1 }}
193 aria-label={t("action_bar_sign_in")}
194 >
195 {t("action_bar_sign_in")}
196 </Button>
197 )}
198 {!session.exists() && config.enable_signup && (
199 <Button
200 color="inherit"
201 variant="outlined"
202 onClick={() => fadeNavigate(navigate, routes.signup)}
203 aria-label={t("action_bar_sign_up")}
204 >
205 {t("action_bar_sign_up")}
206 </Button>
207 )}
208 <PopupMenu horizontal="right" anchorEl={anchorEl} open={open} onClose={handleClose}>
209 <MenuItem onClick={() => navigate(routes.account)}>
210 <ListItemIcon>
211 <Person />
212 </ListItemIcon>
213 <b>{session.username()}</b>
214 </MenuItem>
215 <Divider />
216 <MenuItem onClick={() => navigate(routes.settings)}>
217 <ListItemIcon>
218 <Settings fontSize="small" />
219 </ListItemIcon>
220 {t("action_bar_profile_settings")}
221 </MenuItem>
222 <MenuItem onClick={handleLogout}>
223 <ListItemIcon>
224 <Logout fontSize="small" />
225 </ListItemIcon>
226 {t("action_bar_profile_logout")}
227 </MenuItem>
228 </PopupMenu>
229 </>
230 );
231 };
232
233 export default ActionBar;