2022-02-20 04:26:58 +01:00
|
|
|
import * as React from 'react';
|
2022-02-26 17:45:39 +01:00
|
|
|
import {useState} from 'react';
|
2022-02-20 04:26:58 +01:00
|
|
|
import Button from '@mui/material/Button';
|
|
|
|
import TextField from '@mui/material/TextField';
|
|
|
|
import Dialog from '@mui/material/Dialog';
|
|
|
|
import DialogContent from '@mui/material/DialogContent';
|
|
|
|
import DialogContentText from '@mui/material/DialogContentText';
|
|
|
|
import DialogTitle from '@mui/material/DialogTitle';
|
2022-02-28 22:56:38 +01:00
|
|
|
import {Autocomplete, Checkbox, FormControlLabel, useMediaQuery} from "@mui/material";
|
2022-02-25 19:40:03 +01:00
|
|
|
import theme from "./theme";
|
|
|
|
import api from "../app/Api";
|
2022-02-28 22:56:38 +01:00
|
|
|
import {topicUrl, validTopic, validUrl} from "../app/utils";
|
2022-03-03 22:52:07 +01:00
|
|
|
import userManager from "../app/UserManager";
|
|
|
|
import subscriptionManager from "../app/SubscriptionManager";
|
|
|
|
import poller from "../app/Poller";
|
2022-03-29 21:22:26 +02:00
|
|
|
import DialogFooter from "./DialogFooter";
|
2022-04-08 16:44:35 +02:00
|
|
|
import {useTranslation} from "react-i18next";
|
2022-02-20 04:26:58 +01:00
|
|
|
|
2022-03-06 04:11:32 +01:00
|
|
|
const publicBaseUrl = "https://ntfy.sh";
|
2022-02-20 04:26:58 +01:00
|
|
|
|
2022-02-25 02:18:46 +01:00
|
|
|
const SubscribeDialog = (props) => {
|
2022-02-28 22:56:38 +01:00
|
|
|
const [baseUrl, setBaseUrl] = useState("");
|
2022-02-20 04:26:58 +01:00
|
|
|
const [topic, setTopic] = useState("");
|
2022-02-25 19:40:03 +01:00
|
|
|
const [showLoginPage, setShowLoginPage] = useState(false);
|
|
|
|
const fullScreen = useMediaQuery(theme.breakpoints.down('sm'));
|
2022-03-03 22:52:07 +01:00
|
|
|
const handleSuccess = async () => {
|
2022-03-06 04:11:32 +01:00
|
|
|
const actualBaseUrl = (baseUrl) ? baseUrl : window.location.origin;
|
2022-03-10 05:28:55 +01:00
|
|
|
const subscription = await subscriptionManager.add(actualBaseUrl, topic);
|
2022-03-03 22:52:07 +01:00
|
|
|
poller.pollInBackground(subscription); // Dangle!
|
2022-03-02 03:23:12 +01:00
|
|
|
props.onSuccess(subscription);
|
2022-02-20 04:26:58 +01:00
|
|
|
}
|
2022-02-25 19:40:03 +01:00
|
|
|
return (
|
2022-02-28 22:56:38 +01:00
|
|
|
<Dialog open={props.open} onClose={props.onCancel} fullScreen={fullScreen}>
|
2022-02-25 19:40:03 +01:00
|
|
|
{!showLoginPage && <SubscribePage
|
2022-02-26 05:25:04 +01:00
|
|
|
baseUrl={baseUrl}
|
2022-02-28 22:56:38 +01:00
|
|
|
setBaseUrl={setBaseUrl}
|
2022-02-25 19:40:03 +01:00
|
|
|
topic={topic}
|
|
|
|
setTopic={setTopic}
|
2022-02-26 17:45:39 +01:00
|
|
|
subscriptions={props.subscriptions}
|
|
|
|
onCancel={props.onCancel}
|
2022-02-26 05:25:04 +01:00
|
|
|
onNeedsLogin={() => setShowLoginPage(true)}
|
|
|
|
onSuccess={handleSuccess}
|
2022-02-25 19:40:03 +01:00
|
|
|
/>}
|
|
|
|
{showLoginPage && <LoginPage
|
2022-02-25 22:07:25 +01:00
|
|
|
baseUrl={baseUrl}
|
2022-02-25 19:40:03 +01:00
|
|
|
topic={topic}
|
|
|
|
onBack={() => setShowLoginPage(false)}
|
2022-02-26 05:25:04 +01:00
|
|
|
onSuccess={handleSuccess}
|
2022-02-25 19:40:03 +01:00
|
|
|
/>}
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const SubscribePage = (props) => {
|
2022-04-08 16:44:35 +02:00
|
|
|
const { t } = useTranslation();
|
2022-02-28 22:56:38 +01:00
|
|
|
const [anotherServerVisible, setAnotherServerVisible] = useState(false);
|
2022-03-04 18:10:11 +01:00
|
|
|
const [errorText, setErrorText] = useState("");
|
2022-03-06 04:11:32 +01:00
|
|
|
const baseUrl = (anotherServerVisible) ? props.baseUrl : window.location.origin;
|
2022-02-26 05:25:04 +01:00
|
|
|
const topic = props.topic;
|
2022-03-02 22:16:30 +01:00
|
|
|
const existingTopicUrls = props.subscriptions.map(s => topicUrl(s.baseUrl, s.topic));
|
2022-03-04 18:10:11 +01:00
|
|
|
const existingBaseUrls = Array.from(new Set([publicBaseUrl, ...props.subscriptions.map(s => s.baseUrl)]))
|
2022-03-06 04:11:32 +01:00
|
|
|
.filter(s => s !== window.location.origin);
|
2022-02-26 05:25:04 +01:00
|
|
|
const handleSubscribe = async () => {
|
2022-03-04 18:10:11 +01:00
|
|
|
const user = await userManager.get(baseUrl); // May be undefined
|
2022-04-08 16:44:35 +02:00
|
|
|
const username = (user) ? user.username : t("subscribe_dialog_error_user_anonymous");
|
2022-03-04 18:10:11 +01:00
|
|
|
const success = await api.auth(baseUrl, topic, user);
|
2022-02-26 05:25:04 +01:00
|
|
|
if (!success) {
|
2022-03-04 18:10:11 +01:00
|
|
|
console.log(`[SubscribeDialog] Login to ${topicUrl(baseUrl, topic)} failed for user ${username}`);
|
|
|
|
if (user) {
|
2022-04-08 16:44:35 +02:00
|
|
|
setErrorText(t("subscribe_dialog_error_user_not_authorized", { username: username }));
|
2022-03-04 18:10:11 +01:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
props.onNeedsLogin();
|
|
|
|
return;
|
|
|
|
}
|
2022-02-26 05:25:04 +01:00
|
|
|
}
|
2022-03-04 18:10:11 +01:00
|
|
|
console.log(`[SubscribeDialog] Successful login to ${topicUrl(baseUrl, topic)} for user ${username}`);
|
2022-03-02 03:23:12 +01:00
|
|
|
props.onSuccess();
|
2022-02-26 05:25:04 +01:00
|
|
|
};
|
2022-02-28 22:56:38 +01:00
|
|
|
const handleUseAnotherChanged = (e) => {
|
|
|
|
props.setBaseUrl("");
|
|
|
|
setAnotherServerVisible(e.target.checked);
|
|
|
|
};
|
|
|
|
const subscribeButtonEnabled = (() => {
|
|
|
|
if (anotherServerVisible) {
|
|
|
|
const isExistingTopicUrl = existingTopicUrls.includes(topicUrl(baseUrl, topic));
|
|
|
|
return validTopic(topic) && validUrl(baseUrl) && !isExistingTopicUrl;
|
|
|
|
} else {
|
2022-03-11 21:17:12 +01:00
|
|
|
const isExistingTopicUrl = existingTopicUrls.includes(topicUrl(window.location.origin, topic));
|
2022-02-28 22:56:38 +01:00
|
|
|
return validTopic(topic) && !isExistingTopicUrl;
|
|
|
|
}
|
|
|
|
})();
|
2022-02-25 19:40:03 +01:00
|
|
|
return (
|
|
|
|
<>
|
2022-04-08 16:44:35 +02:00
|
|
|
<DialogTitle>{t("subscribe_dialog_subscribe_title")}</DialogTitle>
|
2022-02-25 19:40:03 +01:00
|
|
|
<DialogContent>
|
|
|
|
<DialogContentText>
|
2022-04-08 16:44:35 +02:00
|
|
|
{t("subscribe_dialog_subscribe_description")}
|
2022-02-25 19:40:03 +01:00
|
|
|
</DialogContentText>
|
|
|
|
<TextField
|
|
|
|
autoFocus
|
|
|
|
margin="dense"
|
|
|
|
id="topic"
|
2022-04-08 16:44:35 +02:00
|
|
|
placeholder={t("subscribe_dialog_subscribe_topic_placeholder")}
|
2022-02-25 19:40:03 +01:00
|
|
|
value={props.topic}
|
|
|
|
onChange={ev => props.setTopic(ev.target.value)}
|
|
|
|
type="text"
|
|
|
|
fullWidth
|
|
|
|
variant="standard"
|
2022-05-03 20:53:07 +02:00
|
|
|
inputProps={{
|
|
|
|
maxLength: 64,
|
|
|
|
"aria-label": t("subscribe_dialog_subscribe_topic_placeholder")
|
|
|
|
}}
|
2022-02-25 19:40:03 +01:00
|
|
|
/>
|
2022-02-28 22:56:38 +01:00
|
|
|
<FormControlLabel
|
|
|
|
sx={{pt: 1}}
|
2022-05-03 20:53:07 +02:00
|
|
|
control={
|
|
|
|
<Checkbox
|
|
|
|
onChange={handleUseAnotherChanged}
|
|
|
|
inputProps={{
|
|
|
|
"aria-label": t("subscribe_dialog_subscribe_use_another_label")
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
}
|
2022-04-08 16:44:35 +02:00
|
|
|
label={t("subscribe_dialog_subscribe_use_another_label")} />
|
2022-02-28 22:56:38 +01:00
|
|
|
{anotherServerVisible && <Autocomplete
|
|
|
|
freeSolo
|
|
|
|
options={existingBaseUrls}
|
|
|
|
sx={{ maxWidth: 400 }}
|
|
|
|
inputValue={props.baseUrl}
|
|
|
|
onInputChange={(ev, newVal) => props.setBaseUrl(newVal)}
|
|
|
|
renderInput={ (params) =>
|
2022-05-03 20:53:07 +02:00
|
|
|
<TextField
|
|
|
|
{...params}
|
|
|
|
placeholder={window.location.origin}
|
|
|
|
variant="standard"
|
|
|
|
aria-label={t("subscribe_dialog_subscribe_base_url_label")}
|
|
|
|
/>
|
2022-02-28 22:56:38 +01:00
|
|
|
}
|
|
|
|
/>}
|
2022-02-25 19:40:03 +01:00
|
|
|
</DialogContent>
|
2022-03-04 18:10:11 +01:00
|
|
|
<DialogFooter status={errorText}>
|
2022-04-08 16:44:35 +02:00
|
|
|
<Button onClick={props.onCancel}>{t("subscribe_dialog_subscribe_button_cancel")}</Button>
|
|
|
|
<Button onClick={handleSubscribe} disabled={!subscribeButtonEnabled}>{t("subscribe_dialog_subscribe_button_subscribe")}</Button>
|
2022-03-04 18:10:11 +01:00
|
|
|
</DialogFooter>
|
2022-02-25 19:40:03 +01:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const LoginPage = (props) => {
|
2022-04-08 16:44:35 +02:00
|
|
|
const { t } = useTranslation();
|
2022-02-25 22:07:25 +01:00
|
|
|
const [username, setUsername] = useState("");
|
|
|
|
const [password, setPassword] = useState("");
|
|
|
|
const [errorText, setErrorText] = useState("");
|
2022-03-06 04:11:32 +01:00
|
|
|
const baseUrl = (props.baseUrl) ? props.baseUrl : window.location.origin;
|
2022-02-25 22:07:25 +01:00
|
|
|
const topic = props.topic;
|
|
|
|
const handleLogin = async () => {
|
2022-03-02 03:23:12 +01:00
|
|
|
const user = {baseUrl, username, password};
|
2022-02-25 22:07:25 +01:00
|
|
|
const success = await api.auth(baseUrl, topic, user);
|
|
|
|
if (!success) {
|
|
|
|
console.log(`[SubscribeDialog] Login to ${topicUrl(baseUrl, topic)} failed for user ${username}`);
|
2022-04-08 16:44:35 +02:00
|
|
|
setErrorText(t("subscribe_dialog_error_user_not_authorized", { username: username }));
|
2022-02-25 22:07:25 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-02-26 05:25:04 +01:00
|
|
|
console.log(`[SubscribeDialog] Successful login to ${topicUrl(baseUrl, topic)} for user ${username}`);
|
2022-03-03 22:52:07 +01:00
|
|
|
await userManager.save(user);
|
2022-03-02 03:23:12 +01:00
|
|
|
props.onSuccess();
|
2022-02-25 22:07:25 +01:00
|
|
|
};
|
2022-02-20 04:26:58 +01:00
|
|
|
return (
|
|
|
|
<>
|
2022-04-08 16:44:35 +02:00
|
|
|
<DialogTitle>{t("subscribe_dialog_login_title")}</DialogTitle>
|
2022-02-25 19:40:03 +01:00
|
|
|
<DialogContent>
|
|
|
|
<DialogContentText>
|
2022-04-08 16:44:35 +02:00
|
|
|
{t("subscribe_dialog_login_description")}
|
2022-02-25 19:40:03 +01:00
|
|
|
</DialogContentText>
|
|
|
|
<TextField
|
|
|
|
autoFocus
|
|
|
|
margin="dense"
|
|
|
|
id="username"
|
2022-04-08 16:44:35 +02:00
|
|
|
label={t("subscribe_dialog_login_username_label")}
|
2022-02-25 22:07:25 +01:00
|
|
|
value={username}
|
|
|
|
onChange={ev => setUsername(ev.target.value)}
|
2022-02-25 19:40:03 +01:00
|
|
|
type="text"
|
|
|
|
fullWidth
|
|
|
|
variant="standard"
|
2022-05-03 20:53:07 +02:00
|
|
|
inputProps={{
|
|
|
|
"aria-label": t("subscribe_dialog_login_username_label")
|
|
|
|
}}
|
2022-02-25 19:40:03 +01:00
|
|
|
/>
|
|
|
|
<TextField
|
|
|
|
margin="dense"
|
|
|
|
id="password"
|
2022-04-08 16:44:35 +02:00
|
|
|
label={t("subscribe_dialog_login_password_label")}
|
2022-02-25 19:40:03 +01:00
|
|
|
type="password"
|
2022-02-25 22:07:25 +01:00
|
|
|
value={password}
|
|
|
|
onChange={ev => setPassword(ev.target.value)}
|
2022-02-25 19:40:03 +01:00
|
|
|
fullWidth
|
|
|
|
variant="standard"
|
2022-05-03 20:53:07 +02:00
|
|
|
inputProps={{
|
|
|
|
"aria-label": t("subscribe_dialog_login_password_label")
|
|
|
|
}}
|
2022-02-25 19:40:03 +01:00
|
|
|
/>
|
|
|
|
</DialogContent>
|
2022-03-01 22:22:47 +01:00
|
|
|
<DialogFooter status={errorText}>
|
2022-04-08 16:44:35 +02:00
|
|
|
<Button onClick={props.onBack}>{t("subscribe_dialog_login_button_back")}</Button>
|
|
|
|
<Button onClick={handleLogin}>{t("subscribe_dialog_login_button_login")}</Button>
|
2022-03-01 22:22:47 +01:00
|
|
|
</DialogFooter>
|
2022-02-20 04:26:58 +01:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-02-25 02:18:46 +01:00
|
|
|
export default SubscribeDialog;
|