1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-11-03 18:34:15 +01:00
ntfy/web/src/components/Preferences.js

476 lines
19 KiB
JavaScript
Raw Normal View History

2022-02-28 22:56:38 +01:00
import * as React from 'react';
import {useEffect, useState} from 'react';
import {
CardActions,
CardContent,
FormControl,
Select,
Stack,
Table,
TableBody,
TableCell,
TableHead,
TableRow,
useMediaQuery
} from "@mui/material";
2022-02-28 22:56:38 +01:00
import Typography from "@mui/material/Typography";
2022-03-02 22:16:30 +01:00
import prefs from "../app/Prefs";
import {Paragraph} from "./styles";
import EditIcon from '@mui/icons-material/Edit';
import CloseIcon from "@mui/icons-material/Close";
import IconButton from "@mui/material/IconButton";
2022-03-06 06:02:27 +01:00
import PlayArrowIcon from '@mui/icons-material/PlayArrow';
import Container from "@mui/material/Container";
import TextField from "@mui/material/TextField";
import MenuItem from "@mui/material/MenuItem";
import Card from "@mui/material/Card";
import Button from "@mui/material/Button";
import {useLiveQuery} from "dexie-react-hooks";
import theme from "./theme";
import Dialog from "@mui/material/Dialog";
import DialogTitle from "@mui/material/DialogTitle";
import DialogContent from "@mui/material/DialogContent";
import DialogActions from "@mui/material/DialogActions";
import userManager from "../app/UserManager";
2022-12-02 21:37:48 +01:00
import {playSound, shuffle, sounds, validTopic, validUrl} from "../app/utils";
2022-04-08 02:31:24 +02:00
import {useTranslation} from "react-i18next";
2022-12-08 03:26:18 +01:00
import session from "../app/Session";
import routes from "./routes";
2022-12-25 17:59:44 +01:00
import accountApi, {UnauthorizedError} from "../app/AccountApi";
2022-12-29 14:20:53 +01:00
import {Pref, PrefGroup} from "./Pref";
2022-02-28 22:56:38 +01:00
2022-03-04 22:10:04 +01:00
const Preferences = () => {
return (
2022-03-03 20:51:56 +01:00
<Container maxWidth="md" sx={{marginTop: 3, marginBottom: 3}}>
<Stack spacing={3}>
<Notifications/>
2022-04-08 02:31:24 +02:00
<Appearance/>
2022-04-09 16:54:09 +02:00
<Users/>
</Stack>
</Container>
);
};
2022-03-02 04:01:51 +01:00
const Notifications = () => {
2022-04-08 16:44:35 +02:00
const { t } = useTranslation();
return (
2022-05-03 01:30:29 +02:00
<Card sx={{p: 3}} aria-label={t("prefs_notifications_title")}>
<Typography variant="h5" sx={{marginBottom: 2}}>
2022-04-08 16:44:35 +02:00
{t("prefs_notifications_title")}
</Typography>
<PrefGroup>
2022-03-06 06:02:27 +01:00
<Sound/>
<MinPriority/>
<DeleteAfter/>
</PrefGroup>
</Card>
);
};
2022-03-06 06:02:27 +01:00
const Sound = () => {
2022-04-08 16:44:35 +02:00
const { t } = useTranslation();
2022-05-03 01:30:29 +02:00
const labelId = "prefSound";
2022-03-06 06:02:27 +01:00
const sound = useLiveQuery(async () => prefs.sound());
const handleChange = async (ev) => {
await prefs.setSound(ev.target.value);
await maybeUpdateAccountSettings({
notification: {
sound: ev.target.value
}
});
2022-03-06 06:02:27 +01:00
}
if (!sound) {
return null; // While loading
}
let description;
if (sound === "none") {
description = t("prefs_notifications_sound_description_none");
} else {
description = t("prefs_notifications_sound_description_some", { sound: sounds[sound].label });
}
2022-03-06 06:02:27 +01:00
return (
2022-05-03 01:30:29 +02:00
<Pref labelId={labelId} title={t("prefs_notifications_sound_title")} description={description}>
2022-03-06 06:02:27 +01:00
<div style={{ display: 'flex', width: '100%' }}>
<FormControl fullWidth variant="standard" sx={{ margin: 1 }}>
2022-05-03 01:30:29 +02:00
<Select value={sound} onChange={handleChange} aria-labelledby={labelId}>
2022-04-08 16:44:35 +02:00
<MenuItem value={"none"}>{t("prefs_notifications_sound_no_sound")}</MenuItem>
{Object.entries(sounds).map(s => <MenuItem key={s[0]} value={s[0]}>{s[1].label}</MenuItem>)}
2022-03-06 06:02:27 +01:00
</Select>
</FormControl>
2022-05-03 01:30:29 +02:00
<IconButton onClick={() => playSound(sound)} disabled={sound === "none"} aria-label={t("prefs_notifications_sound_play")}>
2022-03-06 06:02:27 +01:00
<PlayArrowIcon />
</IconButton>
</div>
</Pref>
)
};
const MinPriority = () => {
2022-04-08 16:44:35 +02:00
const { t } = useTranslation();
2022-05-03 01:30:29 +02:00
const labelId = "prefMinPriority";
2022-03-06 06:02:27 +01:00
const minPriority = useLiveQuery(async () => prefs.minPriority());
2022-03-02 04:01:51 +01:00
const handleChange = async (ev) => {
2022-03-02 22:16:30 +01:00
await prefs.setMinPriority(ev.target.value);
await maybeUpdateAccountSettings({
notification: {
min_priority: ev.target.value
}
});
2022-03-02 04:01:51 +01:00
}
if (!minPriority) {
return null; // While loading
}
const priorities = {
1: t("priority_min"),
2: t("priority_low"),
3: t("priority_default"),
4: t("priority_high"),
5: t("priority_max")
}
let description;
if (minPriority === 1) {
description = t("prefs_notifications_min_priority_description_any");
} else if (minPriority === 5) {
description = t("prefs_notifications_min_priority_description_max");
} else {
description = t("prefs_notifications_min_priority_description_x_or_higher", {
number: minPriority,
name: priorities[minPriority]
});
}
return (
2022-05-03 01:30:29 +02:00
<Pref labelId={labelId} title={t("prefs_notifications_min_priority_title")} description={description}>
<FormControl fullWidth variant="standard" sx={{ m: 1 }}>
2022-05-03 01:30:29 +02:00
<Select value={minPriority} onChange={handleChange} aria-labelledby={labelId}>
2022-04-08 16:44:35 +02:00
<MenuItem value={1}>{t("prefs_notifications_min_priority_any")}</MenuItem>
<MenuItem value={2}>{t("prefs_notifications_min_priority_low_and_higher")}</MenuItem>
<MenuItem value={3}>{t("prefs_notifications_min_priority_default_and_higher")}</MenuItem>
<MenuItem value={4}>{t("prefs_notifications_min_priority_high_and_higher")}</MenuItem>
<MenuItem value={5}>{t("prefs_notifications_min_priority_max_only")}</MenuItem>
</Select>
</FormControl>
</Pref>
)
};
const DeleteAfter = () => {
2022-04-08 16:44:35 +02:00
const { t } = useTranslation();
2022-05-03 01:30:29 +02:00
const labelId = "prefDeleteAfter";
2022-03-02 22:16:30 +01:00
const deleteAfter = useLiveQuery(async () => prefs.deleteAfter());
2022-03-02 04:01:51 +01:00
const handleChange = async (ev) => {
2022-03-02 22:16:30 +01:00
await prefs.setDeleteAfter(ev.target.value);
await maybeUpdateAccountSettings({
notification: {
delete_after: ev.target.value
}
});
2022-03-02 04:01:51 +01:00
}
if (deleteAfter === null || deleteAfter === undefined) { // !deleteAfter will not work with "0"
2022-03-02 04:01:51 +01:00
return null; // While loading
}
const description = (() => {
switch (deleteAfter) {
case 0: return t("prefs_notifications_delete_after_never_description");
case 10800: return t("prefs_notifications_delete_after_three_hours_description");
case 86400: return t("prefs_notifications_delete_after_one_day_description");
case 604800: return t("prefs_notifications_delete_after_one_week_description");
case 2592000: return t("prefs_notifications_delete_after_one_month_description");
}
})();
return (
2022-05-03 01:30:29 +02:00
<Pref labelId={labelId} title={t("prefs_notifications_delete_after_title")} description={description}>
<FormControl fullWidth variant="standard" sx={{ m: 1 }}>
2022-05-03 01:30:29 +02:00
<Select value={deleteAfter} onChange={handleChange} aria-labelledby={labelId}>
2022-04-08 16:44:35 +02:00
<MenuItem value={0}>{t("prefs_notifications_delete_after_never")}</MenuItem>
<MenuItem value={10800}>{t("prefs_notifications_delete_after_three_hours")}</MenuItem>
<MenuItem value={86400}>{t("prefs_notifications_delete_after_one_day")}</MenuItem>
<MenuItem value={604800}>{t("prefs_notifications_delete_after_one_week")}</MenuItem>
<MenuItem value={2592000}>{t("prefs_notifications_delete_after_one_month")}</MenuItem>
</Select>
</FormControl>
</Pref>
)
};
2022-03-02 04:01:51 +01:00
const Users = () => {
2022-04-08 16:44:35 +02:00
const { t } = useTranslation();
const [dialogKey, setDialogKey] = useState(0);
const [dialogOpen, setDialogOpen] = useState(false);
const users = useLiveQuery(() => userManager.all());
const handleAddClick = () => {
setDialogKey(prev => prev+1);
setDialogOpen(true);
};
const handleDialogCancel = () => {
setDialogOpen(false);
};
const handleDialogSubmit = async (user) => {
setDialogOpen(false);
try {
await userManager.save(user);
console.debug(`[Preferences] User ${user.username} for ${user.baseUrl} added`);
} catch (e) {
console.log(`[Preferences] Error adding user.`, e);
}
};
return (
2022-05-03 01:30:29 +02:00
<Card sx={{ padding: 1 }} aria-label={t("prefs_users_title")}>
<CardContent sx={{ paddingBottom: 1 }}>
<Typography variant="h5" sx={{marginBottom: 2}}>
2022-04-08 16:44:35 +02:00
{t("prefs_users_title")}
</Typography>
<Paragraph>
2022-04-08 16:44:35 +02:00
{t("prefs_users_description")}
{session.exists() && <>{" " + t("prefs_users_description_no_sync")}</>}
</Paragraph>
{users?.length > 0 && <UserTable users={users}/>}
</CardContent>
<CardActions>
2022-04-08 16:44:35 +02:00
<Button onClick={handleAddClick}>{t("prefs_users_add_button")}</Button>
<UserDialog
key={`userAddDialog${dialogKey}`}
open={dialogOpen}
user={null}
users={users}
onCancel={handleDialogCancel}
onSubmit={handleDialogSubmit}
/>
</CardActions>
</Card>
2022-02-28 22:56:38 +01:00
);
};
const UserTable = (props) => {
2022-04-08 16:44:35 +02:00
const { t } = useTranslation();
const [dialogKey, setDialogKey] = useState(0);
const [dialogOpen, setDialogOpen] = useState(false);
const [dialogUser, setDialogUser] = useState(null);
const handleEditClick = (user) => {
setDialogKey(prev => prev+1);
setDialogUser(user);
setDialogOpen(true);
};
const handleDialogCancel = () => {
setDialogOpen(false);
};
const handleDialogSubmit = async (user) => {
setDialogOpen(false);
try {
await userManager.save(user);
console.debug(`[Preferences] User ${user.username} for ${user.baseUrl} updated`);
} catch (e) {
console.log(`[Preferences] Error updating user.`, e);
}
};
const handleDeleteClick = async (user) => {
try {
await userManager.delete(user.baseUrl);
console.debug(`[Preferences] User ${user.username} for ${user.baseUrl} deleted`);
} catch (e) {
console.error(`[Preferences] Error deleting user for ${user.baseUrl}`, e);
}
};
return (
<Table size="small" aria-label={t("prefs_users_table")}>
<TableHead>
<TableRow>
<TableCell sx={{paddingLeft: 0}}>{t("prefs_users_table_user_header")}</TableCell>
<TableCell>{t("prefs_users_table_base_url_header")}</TableCell>
<TableCell/>
</TableRow>
</TableHead>
<TableBody>
{props.users?.map(user => (
<TableRow
key={user.baseUrl}
sx={{'&:last-child td, &:last-child th': {border: 0}}}
>
<TableCell component="th" scope="row" sx={{paddingLeft: 0}}
aria-label={t("prefs_users_table_user_header")}>{user.username}</TableCell>
<TableCell aria-label={t("prefs_users_table_base_url_header")}>{user.baseUrl}</TableCell>
<TableCell align="right">
{user.baseUrl !== config.baseUrl &&
<>
<IconButton onClick={() => handleEditClick(user)}
aria-label={t("prefs_users_edit_button")}>
<EditIcon/>
</IconButton>
<IconButton onClick={() => handleDeleteClick(user)}
aria-label={t("prefs_users_delete_button")}>
<CloseIcon/>
</IconButton>
</>
}
</TableCell>
</TableRow>
))}
</TableBody>
<UserDialog
key={`userEditDialog${dialogKey}`}
open={dialogOpen}
user={dialogUser}
users={props.users}
onCancel={handleDialogCancel}
onSubmit={handleDialogSubmit}
/>
</Table>
);
};
const UserDialog = (props) => {
2022-04-08 16:44:35 +02:00
const { t } = useTranslation();
const [baseUrl, setBaseUrl] = useState("");
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
const fullScreen = useMediaQuery(theme.breakpoints.down('sm'));
const editMode = props.user !== null;
const addButtonEnabled = (() => {
if (editMode) {
return username.length > 0 && password.length > 0;
}
const baseUrlValid = validUrl(baseUrl);
const baseUrlExists = props.users?.map(user => user.baseUrl).includes(baseUrl);
return baseUrlValid
&& !baseUrlExists
&& username.length > 0
&& password.length > 0;
})();
const handleSubmit = async () => {
props.onSubmit({
baseUrl: baseUrl,
username: username,
password: password
})
};
useEffect(() => {
if (editMode) {
setBaseUrl(props.user.baseUrl);
setUsername(props.user.username);
setPassword(props.user.password);
}
}, [editMode, props.user]);
return (
<Dialog open={props.open} onClose={props.onCancel} fullScreen={fullScreen}>
2022-04-08 16:44:35 +02:00
<DialogTitle>{editMode ? t("prefs_users_dialog_title_edit") : t("prefs_users_dialog_title_add")}</DialogTitle>
<DialogContent>
{!editMode && <TextField
autoFocus
margin="dense"
id="baseUrl"
2022-04-08 16:44:35 +02:00
label={t("prefs_users_dialog_base_url_label")}
2022-05-03 01:30:29 +02:00
aria-label={t("prefs_users_dialog_base_url_label")}
value={baseUrl}
onChange={ev => setBaseUrl(ev.target.value)}
type="url"
fullWidth
variant="standard"
/>}
<TextField
autoFocus={editMode}
margin="dense"
id="username"
2022-04-08 16:44:35 +02:00
label={t("prefs_users_dialog_username_label")}
2022-05-03 01:30:29 +02:00
aria-label={t("prefs_users_dialog_username_label")}
value={username}
onChange={ev => setUsername(ev.target.value)}
type="text"
fullWidth
variant="standard"
/>
<TextField
margin="dense"
id="password"
2022-04-08 16:44:35 +02:00
label={t("prefs_users_dialog_password_label")}
2022-05-03 01:30:29 +02:00
aria-label={t("prefs_users_dialog_password_label")}
type="password"
value={password}
onChange={ev => setPassword(ev.target.value)}
fullWidth
variant="standard"
/>
</DialogContent>
<DialogActions>
2022-04-08 16:44:35 +02:00
<Button onClick={props.onCancel}>{t("prefs_users_dialog_button_cancel")}</Button>
<Button onClick={handleSubmit} disabled={!addButtonEnabled}>{editMode ? t("prefs_users_dialog_button_save") : t("prefs_users_dialog_button_add")}</Button>
</DialogActions>
</Dialog>
);
};
2022-04-08 02:31:24 +02:00
const Appearance = () => {
2022-04-08 16:44:35 +02:00
const { t } = useTranslation();
2022-04-08 02:31:24 +02:00
return (
2022-05-03 01:30:29 +02:00
<Card sx={{p: 3}} aria-label={t("prefs_appearance_title")}>
<Typography variant="h5" sx={{marginBottom: 2}}>
2022-04-08 16:44:35 +02:00
{t("prefs_appearance_title")}
2022-04-08 02:31:24 +02:00
</Typography>
<PrefGroup>
<Language/>
</PrefGroup>
</Card>
);
};
const Language = () => {
const { t, i18n } = useTranslation();
2022-05-03 01:30:29 +02:00
const labelId = "prefLanguage";
2022-09-27 13:44:00 +02:00
const randomFlags = shuffle(["🇬🇧", "🇺🇸", "🇪🇸", "🇫🇷", "🇧🇬", "🇨🇿", "🇩🇪", "🇵🇱", "🇺🇦", "🇨🇳", "🇮🇹", "🇭🇺", "🇧🇷", "🇳🇱", "🇮🇩", "🇯🇵", "🇷🇺", "🇹🇷"]).slice(0, 3);
2022-04-09 16:54:09 +02:00
const title = t("prefs_appearance_language_title") + " " + randomFlags.join(" ");
2022-04-30 02:12:12 +02:00
const lang = i18n.language ?? "en";
2022-04-09 16:54:09 +02:00
2022-12-08 02:44:20 +01:00
const handleChange = async (ev) => {
await i18n.changeLanguage(ev.target.value);
await maybeUpdateAccountSettings({
language: ev.target.value
});
2022-12-08 02:44:20 +01:00
};
2022-04-09 16:54:09 +02:00
// Remember: Flags are not languages. Don't put flags next to the language in the list.
// Languages names from: https://www.omniglot.com/language/names.htm
2022-04-12 02:18:18 +02:00
// Better: Sidebar in Wikipedia: https://en.wikipedia.org/wiki/Bokm%C3%A5l
2022-04-09 21:12:03 +02:00
2022-04-08 02:31:24 +02:00
return (
2022-05-03 01:30:29 +02:00
<Pref labelId={labelId} title={title}>
2022-04-08 02:31:24 +02:00
<FormControl fullWidth variant="standard" sx={{ m: 1 }}>
2022-12-08 02:44:20 +01:00
<Select value={lang} onChange={handleChange} aria-labelledby={labelId}>
2022-04-08 02:31:24 +02:00
<MenuItem value="en">English</MenuItem>
2022-05-26 22:38:09 +02:00
<MenuItem value="id">Bahasa Indonesia</MenuItem>
<MenuItem value="bg">Български</MenuItem>
2022-04-30 02:12:12 +02:00
<MenuItem value="cs">Čeština</MenuItem>
2022-06-01 06:03:56 +02:00
<MenuItem value="zh_Hans">中文</MenuItem>
2022-04-09 16:54:09 +02:00
<MenuItem value="de">Deutsch</MenuItem>
2022-05-01 02:16:17 +02:00
<MenuItem value="es">Español</MenuItem>
<MenuItem value="fr">Français</MenuItem>
2022-05-26 22:38:09 +02:00
<MenuItem value="it">Italiano</MenuItem>
2022-05-08 01:26:17 +02:00
<MenuItem value="hu">Magyar</MenuItem>
2022-10-01 20:54:16 +02:00
<MenuItem value="ko">한국어</MenuItem>
2022-04-09 16:54:09 +02:00
<MenuItem value="ja">日本語</MenuItem>
2022-06-02 20:45:36 +02:00
<MenuItem value="nl">Nederlands</MenuItem>
2022-04-12 02:18:18 +02:00
<MenuItem value="nb_NO">Norsk bokmål</MenuItem>
2022-09-23 18:55:40 +02:00
<MenuItem value="uk">Українська</MenuItem>
2022-05-13 20:46:30 +02:00
<MenuItem value="pt_BR">Português (Brasil)</MenuItem>
2022-09-27 13:44:00 +02:00
<MenuItem value="pl">Polski</MenuItem>
2022-04-20 01:31:50 +02:00
<MenuItem value="ru">Русский</MenuItem>
<MenuItem value="tr">Türkçe</MenuItem>
2022-04-08 02:31:24 +02:00
</Select>
</FormControl>
</Pref>
)
};
const maybeUpdateAccountSettings = async (payload) => {
if (!session.exists()) {
return;
}
try {
2022-12-25 17:59:44 +01:00
await accountApi.updateSettings(payload);
} catch (e) {
console.log(`[Preferences] Error updating account settings`, e);
if ((e instanceof UnauthorizedError)) {
2022-12-26 04:29:55 +01:00
session.resetAndRedirect(routes.login);
}
}
};
2022-02-28 22:56:38 +01:00
export default Preferences;