| 1 |
import * as React from "react"; |
| 2 |
import { useContext, useEffect, useState } from "react"; |
| 3 |
import { |
| 4 |
Alert, |
| 5 |
Box, |
| 6 |
CardActions, |
| 7 |
CardContent, |
| 8 |
Chip, |
| 9 |
FormControl, |
| 10 |
Select, |
| 11 |
Stack, |
| 12 |
Table, |
| 13 |
TableBody, |
| 14 |
TableCell, |
| 15 |
TableHead, |
| 16 |
TableRow, |
| 17 |
Tooltip, |
| 18 |
useMediaQuery, |
| 19 |
Typography, |
| 20 |
IconButton, |
| 21 |
Container, |
| 22 |
TextField, |
| 23 |
MenuItem, |
| 24 |
Card, |
| 25 |
Button, |
| 26 |
Dialog, |
| 27 |
DialogTitle, |
| 28 |
DialogContent, |
| 29 |
DialogActions, |
| 30 |
useTheme, |
| 31 |
} from "@mui/material"; |
| 32 |
import EditIcon from "@mui/icons-material/Edit"; |
| 33 |
import CloseIcon from "@mui/icons-material/Close"; |
| 34 |
import PlayArrowIcon from "@mui/icons-material/PlayArrow"; |
| 35 |
import { useLiveQuery } from "dexie-react-hooks"; |
| 36 |
import { useTranslation } from "react-i18next"; |
| 37 |
import Info from "@mui/icons-material/Info"; |
| 38 |
import { useOutletContext } from "react-router-dom"; |
| 39 |
import userManager from "../app/UserManager"; |
| 40 |
import { formatDate, formatTime, playSound, shortUrl, shuffle, sounds, validUrl } from "../app/utils"; |
| 41 |
import session from "../app/Session"; |
| 42 |
import routes from "./routes"; |
| 43 |
import accountApi, { Permission, Role } from "../app/AccountApi"; |
| 44 |
import { Pref, PrefGroup } from "./Pref"; |
| 45 |
import AccountContext from "./AccountContext"; |
| 46 |
import { Paragraph } from "./styles"; |
| 47 |
import prefs, { THEME, DATE_FORMAT, TIME_FORMAT } from "../app/Prefs"; |
| 48 |
import { PermissionDenyAll, PermissionRead, PermissionReadWrite, PermissionWrite } from "./ReserveIcons"; |
| 49 |
import { ReserveAddDialog, ReserveDeleteDialog, ReserveEditDialog } from "./ReserveDialogs"; |
| 50 |
import { UnauthorizedError } from "../app/errors"; |
| 51 |
import { subscribeTopic } from "./SubscribeDialog"; |
| 52 |
import notifier from "../app/Notifier"; |
| 53 |
import { useIsLaunchedPWA, useNotificationPermissionListener } from "./hooks"; |
| 54 |
import { usePrefCache } from "./PrefCache"; |
| 55 |
|
| 56 |
const maybeUpdateAccountSettings = async (payload) => { |
| 57 |
if (!session.exists()) { |
| 58 |
return; |
| 59 |
} |
| 60 |
try { |
| 61 |
await accountApi.updateSettings(payload); |
| 62 |
} catch (e) { |
| 63 |
console.log(`[Preferences] Error updating account settings`, e); |
| 64 |
if (e instanceof UnauthorizedError) { |
| 65 |
await session.resetAndRedirect(routes.login); |
| 66 |
} |
| 67 |
} |
| 68 |
}; |
| 69 |
|
| 70 |
const Preferences = () => ( |
| 71 |
<Container maxWidth="md" sx={{ marginTop: 3, marginBottom: 3 }}> |
| 72 |
<Stack spacing={3}> |
| 73 |
<Notifications /> |
| 74 |
<Reservations /> |
| 75 |
<Users /> |
| 76 |
<Appearance /> |
| 77 |
</Stack> |
| 78 |
</Container> |
| 79 |
); |
| 80 |
|
| 81 |
const Notifications = () => { |
| 82 |
const { t } = useTranslation(); |
| 83 |
const isLaunchedPWA = useIsLaunchedPWA(); |
| 84 |
const pushPossible = useNotificationPermissionListener(() => notifier.pushPossible()); |
| 85 |
|
| 86 |
return ( |
| 87 |
<Card sx={{ p: 3 }} aria-label={t("prefs_notifications_title")}> |
| 88 |
<Typography variant="h5" sx={{ marginBottom: 2 }}> |
| 89 |
{t("prefs_notifications_title")} |
| 90 |
</Typography> |
| 91 |
<PrefGroup> |
| 92 |
<Sound /> |
| 93 |
<MinPriority /> |
| 94 |
<DeleteAfter /> |
| 95 |
{!isLaunchedPWA && pushPossible && <WebPushEnabled />} |
| 96 |
</PrefGroup> |
| 97 |
</Card> |
| 98 |
); |
| 99 |
}; |
| 100 |
|
| 101 |
const Sound = () => { |
| 102 |
const { t } = useTranslation(); |
| 103 |
const labelId = "prefSound"; |
| 104 |
const { sound } = usePrefCache(); |
| 105 |
const handleChange = async (ev) => { |
| 106 |
await prefs.setSound(ev.target.value); |
| 107 |
await maybeUpdateAccountSettings({ |
| 108 |
notification: { |
| 109 |
sound: ev.target.value, |
| 110 |
}, |
| 111 |
}); |
| 112 |
}; |
| 113 |
let description; |
| 114 |
if (sound === "none") { |
| 115 |
description = t("prefs_notifications_sound_description_none"); |
| 116 |
} else { |
| 117 |
description = t("prefs_notifications_sound_description_some", { |
| 118 |
sound: sounds[sound].label, |
| 119 |
}); |
| 120 |
} |
| 121 |
return ( |
| 122 |
<Pref labelId={labelId} title={t("prefs_notifications_sound_title")} description={description}> |
| 123 |
<div style={{ display: "flex", width: "100%" }}> |
| 124 |
<FormControl fullWidth variant="standard" sx={{ margin: 1 }}> |
| 125 |
<Select value={sound} onChange={handleChange} aria-labelledby={labelId}> |
| 126 |
<MenuItem value="none">{t("prefs_notifications_sound_no_sound")}</MenuItem> |
| 127 |
{Object.entries(sounds).map((s) => ( |
| 128 |
<MenuItem key={s[0]} value={s[0]}> |
| 129 |
{s[1].label} |
| 130 |
</MenuItem> |
| 131 |
))} |
| 132 |
</Select> |
| 133 |
</FormControl> |
| 134 |
<IconButton onClick={() => playSound(sound)} disabled={sound === "none"} aria-label={t("prefs_notifications_sound_play")}> |
| 135 |
<PlayArrowIcon /> |
| 136 |
</IconButton> |
| 137 |
</div> |
| 138 |
</Pref> |
| 139 |
); |
| 140 |
}; |
| 141 |
|
| 142 |
const MinPriority = () => { |
| 143 |
const { t } = useTranslation(); |
| 144 |
const labelId = "prefMinPriority"; |
| 145 |
const { minPriority } = usePrefCache(); |
| 146 |
const handleChange = async (ev) => { |
| 147 |
await prefs.setMinPriority(ev.target.value); |
| 148 |
await maybeUpdateAccountSettings({ |
| 149 |
notification: { |
| 150 |
min_priority: ev.target.value, |
| 151 |
}, |
| 152 |
}); |
| 153 |
}; |
| 154 |
const priorities = { |
| 155 |
1: t("priority_min"), |
| 156 |
2: t("priority_low"), |
| 157 |
3: t("priority_default"), |
| 158 |
4: t("priority_high"), |
| 159 |
5: t("priority_max"), |
| 160 |
}; |
| 161 |
let description; |
| 162 |
if (minPriority === 1) { |
| 163 |
description = t("prefs_notifications_min_priority_description_any"); |
| 164 |
} else if (minPriority === 5) { |
| 165 |
description = t("prefs_notifications_min_priority_description_max"); |
| 166 |
} else { |
| 167 |
description = t("prefs_notifications_min_priority_description_x_or_higher", { |
| 168 |
number: minPriority, |
| 169 |
name: priorities[minPriority], |
| 170 |
}); |
| 171 |
} |
| 172 |
return ( |
| 173 |
<Pref labelId={labelId} title={t("prefs_notifications_min_priority_title")} description={description}> |
| 174 |
<FormControl fullWidth variant="standard" sx={{ m: 1 }}> |
| 175 |
<Select value={minPriority} onChange={handleChange} aria-labelledby={labelId}> |
| 176 |
<MenuItem value={1}>{t("prefs_notifications_min_priority_any")}</MenuItem> |
| 177 |
<MenuItem value={2}>{t("prefs_notifications_min_priority_low_and_higher")}</MenuItem> |
| 178 |
<MenuItem value={3}>{t("prefs_notifications_min_priority_default_and_higher")}</MenuItem> |
| 179 |
<MenuItem value={4}>{t("prefs_notifications_min_priority_high_and_higher")}</MenuItem> |
| 180 |
<MenuItem value={5}>{t("prefs_notifications_min_priority_max_only")}</MenuItem> |
| 181 |
</Select> |
| 182 |
</FormControl> |
| 183 |
</Pref> |
| 184 |
); |
| 185 |
}; |
| 186 |
|
| 187 |
const DeleteAfter = () => { |
| 188 |
const { t } = useTranslation(); |
| 189 |
const labelId = "prefDeleteAfter"; |
| 190 |
const { deleteAfter } = usePrefCache(); |
| 191 |
const handleChange = async (ev) => { |
| 192 |
await prefs.setDeleteAfter(ev.target.value); |
| 193 |
await maybeUpdateAccountSettings({ |
| 194 |
notification: { |
| 195 |
delete_after: ev.target.value, |
| 196 |
}, |
| 197 |
}); |
| 198 |
}; |
| 199 |
|
| 200 |
const description = (() => { |
| 201 |
switch (deleteAfter) { |
| 202 |
case 0: |
| 203 |
return t("prefs_notifications_delete_after_never_description"); |
| 204 |
case 10800: |
| 205 |
return t("prefs_notifications_delete_after_three_hours_description"); |
| 206 |
case 86400: |
| 207 |
return t("prefs_notifications_delete_after_one_day_description"); |
| 208 |
case 604800: |
| 209 |
return t("prefs_notifications_delete_after_one_week_description"); |
| 210 |
case 2592000: |
| 211 |
return t("prefs_notifications_delete_after_one_month_description"); |
| 212 |
default: |
| 213 |
return ""; |
| 214 |
} |
| 215 |
})(); |
| 216 |
|
| 217 |
return ( |
| 218 |
<Pref labelId={labelId} title={t("prefs_notifications_delete_after_title")} description={description}> |
| 219 |
<FormControl fullWidth variant="standard" sx={{ m: 1 }}> |
| 220 |
<Select value={deleteAfter} onChange={handleChange} aria-labelledby={labelId}> |
| 221 |
<MenuItem value={0}>{t("prefs_notifications_delete_after_never")}</MenuItem> |
| 222 |
<MenuItem value={10800}>{t("prefs_notifications_delete_after_three_hours")}</MenuItem> |
| 223 |
<MenuItem value={86400}>{t("prefs_notifications_delete_after_one_day")}</MenuItem> |
| 224 |
<MenuItem value={604800}>{t("prefs_notifications_delete_after_one_week")}</MenuItem> |
| 225 |
<MenuItem value={2592000}>{t("prefs_notifications_delete_after_one_month")}</MenuItem> |
| 226 |
</Select> |
| 227 |
</FormControl> |
| 228 |
</Pref> |
| 229 |
); |
| 230 |
}; |
| 231 |
|
| 232 |
// Shows the resolved/sample value of an option (the current system mode, or a date/time sample) in muted gray. |
| 233 |
const FormatExample = ({ children }) => ( |
| 234 |
<Box component="span" sx={{ color: "text.secondary", ml: 1 }}> |
| 235 |
{children} |
| 236 |
</Box> |
| 237 |
); |
| 238 |
|
| 239 |
const Theme = () => { |
| 240 |
const { t } = useTranslation(); |
| 241 |
const labelId = "prefTheme"; |
| 242 |
const { theme } = usePrefCache(); |
| 243 |
const prefersDarkMode = useMediaQuery("(prefers-color-scheme: dark)"); |
| 244 |
const handleChange = async (ev) => { |
| 245 |
await prefs.setTheme(ev.target.value); |
| 246 |
}; |
| 247 |
|
| 248 |
return ( |
| 249 |
<Pref labelId={labelId} title={t("prefs_appearance_theme_title")}> |
| 250 |
<FormControl fullWidth variant="standard" sx={{ m: 1 }}> |
| 251 |
<Select value={theme} onChange={handleChange} aria-labelledby={labelId}> |
| 252 |
<MenuItem value={THEME.SYSTEM}> |
| 253 |
{t("prefs_system_default")} |
| 254 |
<FormatExample>{prefersDarkMode ? t("prefs_appearance_theme_dark") : t("prefs_appearance_theme_light")}</FormatExample> |
| 255 |
</MenuItem> |
| 256 |
<MenuItem value={THEME.DARK}>{t("prefs_appearance_theme_dark")}</MenuItem> |
| 257 |
<MenuItem value={THEME.LIGHT}>{t("prefs_appearance_theme_light")}</MenuItem> |
| 258 |
</Select> |
| 259 |
</FormControl> |
| 260 |
</Pref> |
| 261 |
); |
| 262 |
}; |
| 263 |
|
| 264 |
// An unambiguous sample (April 26, 2:30 PM, current year) used for each option's example: the day |
| 265 |
// (26) is too large to be a month, so the day/month order is unmistakable, and 14:30 distinguishes |
| 266 |
// the 12h/24h clock. Using today's date could be ambiguous (e.g. when the day could pass for a month). |
| 267 |
const EXAMPLE_TIMESTAMP = Math.round(new Date(new Date().getFullYear(), 3, 26, 14, 30).getTime() / 1000); |
| 268 |
|
| 269 |
const DateFormat = () => { |
| 270 |
const { t } = useTranslation(); |
| 271 |
const labelId = "prefDateFormat"; |
| 272 |
const { dateFormat } = usePrefCache(); |
| 273 |
const handleChange = async (ev) => { |
| 274 |
await prefs.setDateFormat(ev.target.value); |
| 275 |
await maybeUpdateAccountSettings({ date_format: ev.target.value }); |
| 276 |
}; |
| 277 |
|
| 278 |
return ( |
| 279 |
<Pref labelId={labelId} title={t("prefs_appearance_date_format_title")}> |
| 280 |
<FormControl fullWidth variant="standard" sx={{ m: 1 }}> |
| 281 |
<Select value={dateFormat} onChange={handleChange} aria-labelledby={labelId}> |
| 282 |
<MenuItem value={DATE_FORMAT.SYSTEM}> |
| 283 |
{t("prefs_system_default")} |
| 284 |
<FormatExample>{formatDate(EXAMPLE_TIMESTAMP, DATE_FORMAT.SYSTEM)}</FormatExample> |
| 285 |
</MenuItem> |
| 286 |
<MenuItem value={DATE_FORMAT.ISO8601}> |
| 287 |
{t("prefs_appearance_date_format_iso8601")} |
| 288 |
<FormatExample>{formatDate(EXAMPLE_TIMESTAMP, DATE_FORMAT.ISO8601)}</FormatExample> |
| 289 |
</MenuItem> |
| 290 |
<MenuItem value={DATE_FORMAT.DMY}> |
| 291 |
{t("prefs_appearance_date_format_dmy")} |
| 292 |
<FormatExample>{formatDate(EXAMPLE_TIMESTAMP, DATE_FORMAT.DMY)}</FormatExample> |
| 293 |
</MenuItem> |
| 294 |
<MenuItem value={DATE_FORMAT.DMY_DOT}> |
| 295 |
{t("prefs_appearance_date_format_dmy_dot")} |
| 296 |
<FormatExample>{formatDate(EXAMPLE_TIMESTAMP, DATE_FORMAT.DMY_DOT)}</FormatExample> |
| 297 |
</MenuItem> |
| 298 |
<MenuItem value={DATE_FORMAT.MDY}> |
| 299 |
{t("prefs_appearance_date_format_mdy")} |
| 300 |
<FormatExample>{formatDate(EXAMPLE_TIMESTAMP, DATE_FORMAT.MDY)}</FormatExample> |
| 301 |
</MenuItem> |
| 302 |
</Select> |
| 303 |
</FormControl> |
| 304 |
</Pref> |
| 305 |
); |
| 306 |
}; |
| 307 |
|
| 308 |
const TimeFormat = () => { |
| 309 |
const { t } = useTranslation(); |
| 310 |
const labelId = "prefTimeFormat"; |
| 311 |
const { timeFormat, dateFormat } = usePrefCache(); |
| 312 |
// ISO 8601 dates are always 24-hour, so the clock choice is fixed while it's selected. Disable the |
| 313 |
// control and show 24-hour, but leave the stored pref untouched so it returns for other date formats. |
| 314 |
const fixedTo24h = dateFormat === DATE_FORMAT.ISO8601; |
| 315 |
const handleChange = async (ev) => { |
| 316 |
await prefs.setTimeFormat(ev.target.value); |
| 317 |
await maybeUpdateAccountSettings({ time_format: ev.target.value }); |
| 318 |
}; |
| 319 |
|
| 320 |
return ( |
| 321 |
<Pref labelId={labelId} title={t("prefs_appearance_time_format_title")}> |
| 322 |
<FormControl fullWidth variant="standard" sx={{ m: 1 }}> |
| 323 |
<Select value={fixedTo24h ? TIME_FORMAT.H24 : timeFormat} onChange={handleChange} disabled={fixedTo24h} aria-labelledby={labelId}> |
| 324 |
<MenuItem value={TIME_FORMAT.SYSTEM}> |
| 325 |
{t("prefs_system_default")} |
| 326 |
<FormatExample>{formatTime(EXAMPLE_TIMESTAMP, TIME_FORMAT.SYSTEM)}</FormatExample> |
| 327 |
</MenuItem> |
| 328 |
<MenuItem value={TIME_FORMAT.H12}> |
| 329 |
{t("prefs_appearance_time_format_12h")} |
| 330 |
<FormatExample>{formatTime(EXAMPLE_TIMESTAMP, TIME_FORMAT.H12)}</FormatExample> |
| 331 |
</MenuItem> |
| 332 |
<MenuItem value={TIME_FORMAT.H24}> |
| 333 |
{t("prefs_appearance_time_format_24h")} |
| 334 |
<FormatExample>{formatTime(EXAMPLE_TIMESTAMP, TIME_FORMAT.H24)}</FormatExample> |
| 335 |
</MenuItem> |
| 336 |
</Select> |
| 337 |
</FormControl> |
| 338 |
</Pref> |
| 339 |
); |
| 340 |
}; |
| 341 |
|
| 342 |
const WebPushEnabled = () => { |
| 343 |
const { t } = useTranslation(); |
| 344 |
const labelId = "prefWebPushEnabled"; |
| 345 |
const { webPushEnabled: enabled } = usePrefCache(); |
| 346 |
const handleChange = async (ev) => { |
| 347 |
await prefs.setWebPushEnabled(ev.target.value); |
| 348 |
}; |
| 349 |
|
| 350 |
return ( |
| 351 |
<Pref |
| 352 |
labelId={labelId} |
| 353 |
title={t("prefs_notifications_web_push_title")} |
| 354 |
description={enabled ? t("prefs_notifications_web_push_enabled_description") : t("prefs_notifications_web_push_disabled_description")} |
| 355 |
> |
| 356 |
<FormControl fullWidth variant="standard" sx={{ m: 1 }}> |
| 357 |
<Select value={enabled ?? false} onChange={handleChange} aria-labelledby={labelId}> |
| 358 |
<MenuItem value>{t("prefs_notifications_web_push_enabled", { server: shortUrl(config.base_url) })}</MenuItem> |
| 359 |
<MenuItem value={false}>{t("prefs_notifications_web_push_disabled")}</MenuItem> |
| 360 |
</Select> |
| 361 |
</FormControl> |
| 362 |
</Pref> |
| 363 |
); |
| 364 |
}; |
| 365 |
|
| 366 |
const Users = () => { |
| 367 |
const { t } = useTranslation(); |
| 368 |
const [dialogKey, setDialogKey] = useState(0); |
| 369 |
const [dialogOpen, setDialogOpen] = useState(false); |
| 370 |
const users = useLiveQuery(() => userManager.all()); |
| 371 |
const handleAddClick = () => { |
| 372 |
setDialogKey((prev) => prev + 1); |
| 373 |
setDialogOpen(true); |
| 374 |
}; |
| 375 |
const handleDialogCancel = () => { |
| 376 |
setDialogOpen(false); |
| 377 |
}; |
| 378 |
const handleDialogSubmit = async (user) => { |
| 379 |
setDialogOpen(false); |
| 380 |
try { |
| 381 |
await userManager.save(user); |
| 382 |
console.debug(`[Preferences] User ${user.username} for ${user.baseUrl} added`); |
| 383 |
} catch (e) { |
| 384 |
console.log(`[Preferences] Error adding user.`, e); |
| 385 |
} |
| 386 |
}; |
| 387 |
return ( |
| 388 |
<Card sx={{ padding: 1 }} aria-label={t("prefs_users_title")}> |
| 389 |
<CardContent sx={{ paddingBottom: 1 }}> |
| 390 |
<Typography variant="h5" sx={{ marginBottom: 2 }}> |
| 391 |
{t("prefs_users_title")} |
| 392 |
</Typography> |
| 393 |
<Paragraph> |
| 394 |
{t("prefs_users_description")} |
| 395 |
{session.exists() && <>{` ${t("prefs_users_description_no_sync")}`}</>} |
| 396 |
</Paragraph> |
| 397 |
{users?.length > 0 && <UserTable users={users} />} |
| 398 |
</CardContent> |
| 399 |
<CardActions> |
| 400 |
<Button onClick={handleAddClick}>{t("prefs_users_add_button")}</Button> |
| 401 |
<UserDialog |
| 402 |
key={`userAddDialog${dialogKey}`} |
| 403 |
open={dialogOpen} |
| 404 |
user={null} |
| 405 |
users={users} |
| 406 |
onCancel={handleDialogCancel} |
| 407 |
onSubmit={handleDialogSubmit} |
| 408 |
/> |
| 409 |
</CardActions> |
| 410 |
</Card> |
| 411 |
); |
| 412 |
}; |
| 413 |
|
| 414 |
const UserTable = (props) => { |
| 415 |
const { t } = useTranslation(); |
| 416 |
const [dialogKey, setDialogKey] = useState(0); |
| 417 |
const [dialogOpen, setDialogOpen] = useState(false); |
| 418 |
const [dialogUser, setDialogUser] = useState(null); |
| 419 |
|
| 420 |
const handleEditClick = (user) => { |
| 421 |
setDialogKey((prev) => prev + 1); |
| 422 |
setDialogUser(user); |
| 423 |
setDialogOpen(true); |
| 424 |
}; |
| 425 |
|
| 426 |
const handleDialogCancel = () => { |
| 427 |
setDialogOpen(false); |
| 428 |
}; |
| 429 |
|
| 430 |
const handleDialogSubmit = async (user) => { |
| 431 |
setDialogOpen(false); |
| 432 |
try { |
| 433 |
await userManager.save(user); |
| 434 |
console.debug(`[Preferences] User ${user.username} for ${user.baseUrl} updated`); |
| 435 |
} catch (e) { |
| 436 |
console.log(`[Preferences] Error updating user.`, e); |
| 437 |
} |
| 438 |
}; |
| 439 |
|
| 440 |
const handleDeleteClick = async (user) => { |
| 441 |
try { |
| 442 |
await userManager.delete(user.baseUrl); |
| 443 |
console.debug(`[Preferences] User ${user.username} for ${user.baseUrl} deleted`); |
| 444 |
} catch (e) { |
| 445 |
console.error(`[Preferences] Error deleting user for ${user.baseUrl}`, e); |
| 446 |
} |
| 447 |
}; |
| 448 |
|
| 449 |
return ( |
| 450 |
<Table size="small" aria-label={t("prefs_users_table")}> |
| 451 |
<TableHead> |
| 452 |
<TableRow> |
| 453 |
<TableCell sx={{ paddingLeft: 0 }}>{t("prefs_users_table_user_header")}</TableCell> |
| 454 |
<TableCell>{t("prefs_users_table_base_url_header")}</TableCell> |
| 455 |
<TableCell /> |
| 456 |
</TableRow> |
| 457 |
</TableHead> |
| 458 |
<TableBody> |
| 459 |
{props.users?.map((user) => ( |
| 460 |
<TableRow key={user.baseUrl} sx={{ "&:last-child td, &:last-child th": { border: 0 } }}> |
| 461 |
<TableCell component="th" scope="row" sx={{ paddingLeft: 0 }} aria-label={t("prefs_users_table_user_header")}> |
| 462 |
{user.username} |
| 463 |
</TableCell> |
| 464 |
<TableCell aria-label={t("prefs_users_table_base_url_header")}>{user.baseUrl}</TableCell> |
| 465 |
<TableCell align="right" sx={{ whiteSpace: "nowrap" }}> |
| 466 |
{(!session.exists() || user.baseUrl !== config.base_url) && ( |
| 467 |
<> |
| 468 |
<Tooltip title={t("prefs_users_edit_button")}> |
| 469 |
<IconButton onClick={() => handleEditClick(user)} aria-label={t("prefs_users_edit_button")}> |
| 470 |
<EditIcon /> |
| 471 |
</IconButton> |
| 472 |
</Tooltip> |
| 473 |
<Tooltip title={t("prefs_users_delete_button")}> |
| 474 |
<IconButton onClick={() => handleDeleteClick(user)} aria-label={t("prefs_users_delete_button")}> |
| 475 |
<CloseIcon /> |
| 476 |
</IconButton> |
| 477 |
</Tooltip> |
| 478 |
</> |
| 479 |
)} |
| 480 |
{session.exists() && user.baseUrl === config.base_url && ( |
| 481 |
<Tooltip title={t("prefs_users_table_cannot_delete_or_edit")}> |
| 482 |
<span> |
| 483 |
<IconButton disabled> |
| 484 |
<EditIcon /> |
| 485 |
</IconButton> |
| 486 |
<IconButton disabled> |
| 487 |
<CloseIcon /> |
| 488 |
</IconButton> |
| 489 |
</span> |
| 490 |
</Tooltip> |
| 491 |
)} |
| 492 |
</TableCell> |
| 493 |
</TableRow> |
| 494 |
))} |
| 495 |
</TableBody> |
| 496 |
<UserDialog |
| 497 |
key={`userEditDialog${dialogKey}`} |
| 498 |
open={dialogOpen} |
| 499 |
user={dialogUser} |
| 500 |
users={props.users} |
| 501 |
onCancel={handleDialogCancel} |
| 502 |
onSubmit={handleDialogSubmit} |
| 503 |
/> |
| 504 |
</Table> |
| 505 |
); |
| 506 |
}; |
| 507 |
|
| 508 |
const UserDialog = (props) => { |
| 509 |
const theme = useTheme(); |
| 510 |
const { t } = useTranslation(); |
| 511 |
const [baseUrl, setBaseUrl] = useState(""); |
| 512 |
const [username, setUsername] = useState(""); |
| 513 |
const [password, setPassword] = useState(""); |
| 514 |
const fullScreen = useMediaQuery(theme.breakpoints.down("sm")); |
| 515 |
const editMode = props.user !== null; |
| 516 |
const baseUrlValid = baseUrl.length === 0 || validUrl(baseUrl); |
| 517 |
const baseUrlExists = props.users?.map((user) => user.baseUrl).includes(baseUrl); |
| 518 |
const baseUrlError = baseUrl.length > 0 && (!baseUrlValid || baseUrlExists); |
| 519 |
const addButtonEnabled = (() => { |
| 520 |
if (editMode) { |
| 521 |
return username.length > 0 && password.length > 0; |
| 522 |
} |
| 523 |
return validUrl(baseUrl) && !baseUrlExists && username.length > 0 && password.length > 0; |
| 524 |
})(); |
| 525 |
const baseUrlHelperText = (() => { |
| 526 |
if (baseUrl.length > 0 && !baseUrlValid) { |
| 527 |
return t("prefs_users_dialog_base_url_invalid"); |
| 528 |
} |
| 529 |
if (baseUrlExists) { |
| 530 |
return t("prefs_users_dialog_base_url_exists"); |
| 531 |
} |
| 532 |
return ""; |
| 533 |
})(); |
| 534 |
const handleSubmit = async () => { |
| 535 |
props.onSubmit({ |
| 536 |
baseUrl, |
| 537 |
username, |
| 538 |
password, |
| 539 |
}); |
| 540 |
}; |
| 541 |
useEffect(() => { |
| 542 |
if (editMode) { |
| 543 |
setBaseUrl(props.user.baseUrl); |
| 544 |
setUsername(props.user.username); |
| 545 |
setPassword(props.user.password); |
| 546 |
} |
| 547 |
}, [editMode, props.user]); |
| 548 |
return ( |
| 549 |
<Dialog open={props.open} onClose={props.onCancel} fullScreen={fullScreen}> |
| 550 |
<DialogTitle>{editMode ? t("prefs_users_dialog_title_edit") : t("prefs_users_dialog_title_add")}</DialogTitle> |
| 551 |
<DialogContent> |
| 552 |
{!editMode && ( |
| 553 |
<TextField |
| 554 |
autoFocus |
| 555 |
margin="dense" |
| 556 |
id="baseUrl" |
| 557 |
label={t("prefs_users_dialog_base_url_label")} |
| 558 |
aria-label={t("prefs_users_dialog_base_url_label")} |
| 559 |
value={baseUrl} |
| 560 |
onChange={(ev) => setBaseUrl(ev.target.value)} |
| 561 |
type="url" |
| 562 |
fullWidth |
| 563 |
variant="standard" |
| 564 |
error={baseUrlError} |
| 565 |
helperText={baseUrlHelperText} |
| 566 |
/> |
| 567 |
)} |
| 568 |
<TextField |
| 569 |
autoFocus={editMode} |
| 570 |
margin="dense" |
| 571 |
id="username" |
| 572 |
label={t("prefs_users_dialog_username_label")} |
| 573 |
aria-label={t("prefs_users_dialog_username_label")} |
| 574 |
value={username} |
| 575 |
onChange={(ev) => setUsername(ev.target.value)} |
| 576 |
type="text" |
| 577 |
fullWidth |
| 578 |
variant="standard" |
| 579 |
/> |
| 580 |
<TextField |
| 581 |
margin="dense" |
| 582 |
id="password" |
| 583 |
label={t("prefs_users_dialog_password_label")} |
| 584 |
aria-label={t("prefs_users_dialog_password_label")} |
| 585 |
type="password" |
| 586 |
value={password} |
| 587 |
onChange={(ev) => setPassword(ev.target.value)} |
| 588 |
fullWidth |
| 589 |
variant="standard" |
| 590 |
/> |
| 591 |
</DialogContent> |
| 592 |
<DialogActions> |
| 593 |
<Button onClick={props.onCancel}>{t("common_cancel")}</Button> |
| 594 |
<Button onClick={handleSubmit} disabled={!addButtonEnabled}> |
| 595 |
{editMode ? t("common_save") : t("common_add")} |
| 596 |
</Button> |
| 597 |
</DialogActions> |
| 598 |
</Dialog> |
| 599 |
); |
| 600 |
}; |
| 601 |
|
| 602 |
const Appearance = () => { |
| 603 |
const { t } = useTranslation(); |
| 604 |
return ( |
| 605 |
<Card sx={{ p: 3 }} aria-label={t("prefs_appearance_title")}> |
| 606 |
<Typography variant="h5" sx={{ marginBottom: 2 }}> |
| 607 |
{t("prefs_appearance_title")} |
| 608 |
</Typography> |
| 609 |
<PrefGroup> |
| 610 |
<Theme /> |
| 611 |
<DateFormat /> |
| 612 |
<TimeFormat /> |
| 613 |
<Language /> |
| 614 |
</PrefGroup> |
| 615 |
</Card> |
| 616 |
); |
| 617 |
}; |
| 618 |
|
| 619 |
const Language = () => { |
| 620 |
const { t, i18n } = useTranslation(); |
| 621 |
const labelId = "prefLanguage"; |
| 622 |
const lang = i18n.resolvedLanguage ?? "en"; |
| 623 |
|
| 624 |
// Country flags are displayed using emoji. Emoji rendering is handled by platform fonts. |
| 625 |
// Windows in particular does not yet play nicely with flag emoji so for now, hide flags on Windows. |
| 626 |
const randomFlags = shuffle([ |
| 627 |
"🇬🇧", |
| 628 |
"🇺🇸", |
| 629 |
"🇪🇸", |
| 630 |
"🇫🇷", |
| 631 |
"🇧🇬", |
| 632 |
"🇨🇿", |
| 633 |
"🇩🇪", |
| 634 |
"🇵🇱", |
| 635 |
"🇺🇦", |
| 636 |
"🇨🇳", |
| 637 |
"🇮🇹", |
| 638 |
"🇭🇺", |
| 639 |
"🇧🇷", |
| 640 |
"🇳🇱", |
| 641 |
"🇮🇩", |
| 642 |
"🇯🇵", |
| 643 |
"🇷🇺", |
| 644 |
"🇹🇷", |
| 645 |
"🇫🇮", |
| 646 |
]).slice(0, 3); |
| 647 |
const showFlags = !navigator.userAgent.includes("Windows"); |
| 648 |
let title = t("prefs_appearance_language_title"); |
| 649 |
if (showFlags) { |
| 650 |
title += ` ${randomFlags.join(" ")}`; |
| 651 |
} |
| 652 |
|
| 653 |
const handleChange = async (ev) => { |
| 654 |
await i18n.changeLanguage(ev.target.value); |
| 655 |
await maybeUpdateAccountSettings({ |
| 656 |
language: ev.target.value, |
| 657 |
}); |
| 658 |
}; |
| 659 |
|
| 660 |
// Remember: Flags are not languages. Don't put flags next to the language in the list. |
| 661 |
// Languages names from: https://www.omniglot.com/language/names.htm |
| 662 |
// Better: Sidebar in Wikipedia: https://en.wikipedia.org/wiki/Bokm%C3%A5l |
| 663 |
|
| 664 |
return ( |
| 665 |
<Pref labelId={labelId} title={title}> |
| 666 |
<FormControl fullWidth variant="standard" sx={{ m: 1 }}> |
| 667 |
<Select value={lang} onChange={handleChange} aria-labelledby={labelId}> |
| 668 |
<MenuItem value="en">English</MenuItem> |
| 669 |
<MenuItem value="ar">العربية</MenuItem> |
| 670 |
<MenuItem value="id">Bahasa Indonesia</MenuItem> |
| 671 |
<MenuItem value="bg">Български</MenuItem> |
| 672 |
<MenuItem value="cs">Čeština</MenuItem> |
| 673 |
<MenuItem value="zh_Hant">繁體中文</MenuItem> |
| 674 |
<MenuItem value="zh_Hans">简体中文</MenuItem> |
| 675 |
<MenuItem value="da">Dansk</MenuItem> |
| 676 |
<MenuItem value="de">Deutsch</MenuItem> |
| 677 |
<MenuItem value="et">Eesti</MenuItem> |
| 678 |
<MenuItem value="es">Español</MenuItem> |
| 679 |
<MenuItem value="fr">Français</MenuItem> |
| 680 |
<MenuItem value="gl">Galego</MenuItem> |
| 681 |
<MenuItem value="it">Italiano</MenuItem> |
| 682 |
<MenuItem value="hu">Magyar</MenuItem> |
| 683 |
<MenuItem value="ko">한국어</MenuItem> |
| 684 |
<MenuItem value="ja">日本語</MenuItem> |
| 685 |
<MenuItem value="nl">Nederlands</MenuItem> |
| 686 |
<MenuItem value="nb_NO">Norsk bokmål</MenuItem> |
| 687 |
<MenuItem value="uk">Українська</MenuItem> |
| 688 |
<MenuItem value="pt">Português</MenuItem> |
| 689 |
<MenuItem value="pt_BR">Português (Brasil)</MenuItem> |
| 690 |
<MenuItem value="pl">Polski</MenuItem> |
| 691 |
<MenuItem value="ru">Русский</MenuItem> |
| 692 |
<MenuItem value="ro">Română</MenuItem> |
| 693 |
<MenuItem value="sk">Slovenčina</MenuItem> |
| 694 |
<MenuItem value="fi">Suomi</MenuItem> |
| 695 |
<MenuItem value="sv">Svenska</MenuItem> |
| 696 |
<MenuItem value="tr">Türkçe</MenuItem> |
| 697 |
<MenuItem value="ta">தமிழ்</MenuItem> |
| 698 |
</Select> |
| 699 |
</FormControl> |
| 700 |
</Pref> |
| 701 |
); |
| 702 |
}; |
| 703 |
|
| 704 |
const Reservations = () => { |
| 705 |
const { t } = useTranslation(); |
| 706 |
const { account } = useContext(AccountContext); |
| 707 |
const [dialogKey, setDialogKey] = useState(0); |
| 708 |
const [dialogOpen, setDialogOpen] = useState(false); |
| 709 |
|
| 710 |
if (!config.enable_reservations || !session.exists() || !account) { |
| 711 |
return <></>; |
| 712 |
} |
| 713 |
const reservations = account.reservations || []; |
| 714 |
const limitReached = account.role === Role.USER && account.stats.reservations_remaining === 0; |
| 715 |
|
| 716 |
const handleAddClick = () => { |
| 717 |
setDialogKey((prev) => prev + 1); |
| 718 |
setDialogOpen(true); |
| 719 |
}; |
| 720 |
|
| 721 |
return ( |
| 722 |
<Card sx={{ padding: 1 }} aria-label={t("prefs_reservations_title")}> |
| 723 |
<CardContent sx={{ paddingBottom: 1 }}> |
| 724 |
<Typography variant="h5" sx={{ marginBottom: 2 }}> |
| 725 |
{t("prefs_reservations_title")} |
| 726 |
</Typography> |
| 727 |
<Paragraph>{t("prefs_reservations_description")}</Paragraph> |
| 728 |
{reservations.length > 0 && <ReservationsTable reservations={reservations} />} |
| 729 |
{limitReached && <Alert severity="info">{t("prefs_reservations_limit_reached")}</Alert>} |
| 730 |
</CardContent> |
| 731 |
<CardActions> |
| 732 |
<Button onClick={handleAddClick} disabled={limitReached}> |
| 733 |
{t("prefs_reservations_add_button")} |
| 734 |
</Button> |
| 735 |
<ReserveAddDialog |
| 736 |
key={`reservationAddDialog${dialogKey}`} |
| 737 |
open={dialogOpen} |
| 738 |
reservations={reservations} |
| 739 |
onClose={() => setDialogOpen(false)} |
| 740 |
/> |
| 741 |
</CardActions> |
| 742 |
</Card> |
| 743 |
); |
| 744 |
}; |
| 745 |
|
| 746 |
const ReservationsTable = (props) => { |
| 747 |
const { t } = useTranslation(); |
| 748 |
const [dialogKey, setDialogKey] = useState(0); |
| 749 |
const [dialogReservation, setDialogReservation] = useState(null); |
| 750 |
const [editDialogOpen, setEditDialogOpen] = useState(false); |
| 751 |
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false); |
| 752 |
const { subscriptions } = useOutletContext(); |
| 753 |
const localSubscriptions = |
| 754 |
subscriptions?.length > 0 |
| 755 |
? Object.assign({}, ...subscriptions.filter((s) => s.baseUrl === config.base_url).map((s) => ({ [s.topic]: s }))) |
| 756 |
: {}; |
| 757 |
|
| 758 |
const handleEditClick = (reservation) => { |
| 759 |
setDialogKey((prev) => prev + 1); |
| 760 |
setDialogReservation(reservation); |
| 761 |
setEditDialogOpen(true); |
| 762 |
}; |
| 763 |
|
| 764 |
const handleDeleteClick = async (reservation) => { |
| 765 |
setDialogKey((prev) => prev + 1); |
| 766 |
setDialogReservation(reservation); |
| 767 |
setDeleteDialogOpen(true); |
| 768 |
}; |
| 769 |
|
| 770 |
const handleSubscribeClick = async (reservation) => { |
| 771 |
await subscribeTopic(config.base_url, reservation.topic, {}); |
| 772 |
}; |
| 773 |
|
| 774 |
return ( |
| 775 |
<Table size="small" aria-label={t("prefs_reservations_table")}> |
| 776 |
<TableHead> |
| 777 |
<TableRow> |
| 778 |
<TableCell sx={{ paddingLeft: 0 }}>{t("prefs_reservations_table_topic_header")}</TableCell> |
| 779 |
<TableCell>{t("prefs_reservations_table_access_header")}</TableCell> |
| 780 |
<TableCell /> |
| 781 |
</TableRow> |
| 782 |
</TableHead> |
| 783 |
<TableBody> |
| 784 |
{props.reservations.map((reservation) => ( |
| 785 |
<TableRow key={reservation.topic} sx={{ "&:last-child td, &:last-child th": { border: 0 } }}> |
| 786 |
<TableCell component="th" scope="row" sx={{ paddingLeft: 0 }} aria-label={t("prefs_reservations_table_topic_header")}> |
| 787 |
{reservation.topic} |
| 788 |
</TableCell> |
| 789 |
<TableCell aria-label={t("prefs_reservations_table_access_header")}> |
| 790 |
{reservation.everyone === Permission.READ_WRITE && ( |
| 791 |
<> |
| 792 |
<PermissionReadWrite size="small" sx={{ verticalAlign: "bottom", mr: 1.5 }} /> |
| 793 |
{t("prefs_reservations_table_everyone_read_write")} |
| 794 |
</> |
| 795 |
)} |
| 796 |
{reservation.everyone === Permission.READ_ONLY && ( |
| 797 |
<> |
| 798 |
<PermissionRead size="small" sx={{ verticalAlign: "bottom", mr: 1.5 }} /> |
| 799 |
{t("prefs_reservations_table_everyone_read_only")} |
| 800 |
</> |
| 801 |
)} |
| 802 |
{reservation.everyone === Permission.WRITE_ONLY && ( |
| 803 |
<> |
| 804 |
<PermissionWrite size="small" sx={{ verticalAlign: "bottom", mr: 1.5 }} /> |
| 805 |
{t("prefs_reservations_table_everyone_write_only")} |
| 806 |
</> |
| 807 |
)} |
| 808 |
{reservation.everyone === Permission.DENY_ALL && ( |
| 809 |
<> |
| 810 |
<PermissionDenyAll size="small" sx={{ verticalAlign: "bottom", mr: 1.5 }} /> |
| 811 |
{t("prefs_reservations_table_everyone_deny_all")} |
| 812 |
</> |
| 813 |
)} |
| 814 |
</TableCell> |
| 815 |
<TableCell align="right" sx={{ whiteSpace: "nowrap" }}> |
| 816 |
{!localSubscriptions[reservation.topic] && ( |
| 817 |
<Tooltip title={t("prefs_reservations_table_click_to_subscribe")}> |
| 818 |
<Chip |
| 819 |
icon={<Info />} |
| 820 |
onClick={() => handleSubscribeClick(reservation)} |
| 821 |
label={t("prefs_reservations_table_not_subscribed")} |
| 822 |
color="primary" |
| 823 |
variant="outlined" |
| 824 |
/> |
| 825 |
</Tooltip> |
| 826 |
)} |
| 827 |
<Tooltip title={t("prefs_reservations_edit_button")}> |
| 828 |
<IconButton onClick={() => handleEditClick(reservation)} aria-label={t("prefs_reservations_edit_button")}> |
| 829 |
<EditIcon /> |
| 830 |
</IconButton> |
| 831 |
</Tooltip> |
| 832 |
<Tooltip title={t("prefs_reservations_delete_button")}> |
| 833 |
<IconButton onClick={() => handleDeleteClick(reservation)} aria-label={t("prefs_reservations_delete_button")}> |
| 834 |
<CloseIcon /> |
| 835 |
</IconButton> |
| 836 |
</Tooltip> |
| 837 |
</TableCell> |
| 838 |
</TableRow> |
| 839 |
))} |
| 840 |
</TableBody> |
| 841 |
<ReserveEditDialog |
| 842 |
key={`reservationEditDialog${dialogKey}`} |
| 843 |
open={editDialogOpen} |
| 844 |
reservation={dialogReservation} |
| 845 |
reservations={props.reservations} |
| 846 |
onClose={() => setEditDialogOpen(false)} |
| 847 |
/> |
| 848 |
<ReserveDeleteDialog |
| 849 |
key={`reservationDeleteDialog${dialogKey}`} |
| 850 |
open={deleteDialogOpen} |
| 851 |
topic={dialogReservation?.topic} |
| 852 |
onClose={() => setDeleteDialogOpen(false)} |
| 853 |
/> |
| 854 |
</Table> |
| 855 |
); |
| 856 |
}; |
| 857 |
|
| 858 |
export default Preferences; |