2022-02-20 03:26:58 +00:00
|
|
|
import * as React from 'react';
|
2022-02-26 16:45:39 +00:00
|
|
|
import {useState} from 'react';
|
2022-02-20 03:26:58 +00:00
|
|
|
import Button from '@mui/material/Button';
|
|
|
|
import TextField from '@mui/material/TextField';
|
|
|
|
import Dialog from '@mui/material/Dialog';
|
|
|
|
import DialogActions from '@mui/material/DialogActions';
|
|
|
|
import DialogContent from '@mui/material/DialogContent';
|
|
|
|
import DialogContentText from '@mui/material/DialogContentText';
|
|
|
|
import DialogTitle from '@mui/material/DialogTitle';
|
2022-02-20 21:55:55 +00:00
|
|
|
import Subscription from "../app/Subscription";
|
2022-02-28 21:56:38 +00:00
|
|
|
import {Autocomplete, Checkbox, FormControlLabel, useMediaQuery} from "@mui/material";
|
2022-02-25 18:40:03 +00:00
|
|
|
import theme from "./theme";
|
|
|
|
import api from "../app/Api";
|
2022-02-28 21:56:38 +00:00
|
|
|
import {topicUrl, validTopic, validUrl} from "../app/utils";
|
2022-02-26 04:25:04 +00:00
|
|
|
import User from "../app/User";
|
2022-03-01 21:22:47 +00:00
|
|
|
import Box from "@mui/material/Box";
|
2022-02-20 03:26:58 +00:00
|
|
|
|
2022-02-24 14:52:49 +00:00
|
|
|
const defaultBaseUrl = "http://127.0.0.1"
|
|
|
|
//const defaultBaseUrl = "https://ntfy.sh"
|
2022-02-20 03:26:58 +00:00
|
|
|
|
2022-02-25 01:18:46 +00:00
|
|
|
const SubscribeDialog = (props) => {
|
2022-02-28 21:56:38 +00:00
|
|
|
const [baseUrl, setBaseUrl] = useState("");
|
2022-02-20 03:26:58 +00:00
|
|
|
const [topic, setTopic] = useState("");
|
2022-02-25 18:40:03 +00:00
|
|
|
const [showLoginPage, setShowLoginPage] = useState(false);
|
|
|
|
const fullScreen = useMediaQuery(theme.breakpoints.down('sm'));
|
2022-02-28 21:56:38 +00:00
|
|
|
const handleSuccess = (user) => {
|
|
|
|
const actualBaseUrl = (baseUrl) ? baseUrl : defaultBaseUrl; // FIXME
|
|
|
|
const subscription = new Subscription(actualBaseUrl, topic);
|
2022-02-26 04:25:04 +00:00
|
|
|
props.onSuccess(subscription, user);
|
2022-02-20 03:26:58 +00:00
|
|
|
}
|
2022-02-25 18:40:03 +00:00
|
|
|
return (
|
2022-02-28 21:56:38 +00:00
|
|
|
<Dialog open={props.open} onClose={props.onCancel} fullScreen={fullScreen}>
|
2022-02-25 18:40:03 +00:00
|
|
|
{!showLoginPage && <SubscribePage
|
2022-02-26 04:25:04 +00:00
|
|
|
baseUrl={baseUrl}
|
2022-02-28 21:56:38 +00:00
|
|
|
setBaseUrl={setBaseUrl}
|
2022-02-25 18:40:03 +00:00
|
|
|
topic={topic}
|
|
|
|
setTopic={setTopic}
|
2022-02-26 16:45:39 +00:00
|
|
|
subscriptions={props.subscriptions}
|
|
|
|
onCancel={props.onCancel}
|
2022-02-26 04:25:04 +00:00
|
|
|
onNeedsLogin={() => setShowLoginPage(true)}
|
|
|
|
onSuccess={handleSuccess}
|
2022-02-25 18:40:03 +00:00
|
|
|
/>}
|
|
|
|
{showLoginPage && <LoginPage
|
2022-02-25 21:07:25 +00:00
|
|
|
baseUrl={baseUrl}
|
2022-02-25 18:40:03 +00:00
|
|
|
topic={topic}
|
|
|
|
onBack={() => setShowLoginPage(false)}
|
2022-02-26 04:25:04 +00:00
|
|
|
onSuccess={handleSuccess}
|
2022-02-25 18:40:03 +00:00
|
|
|
/>}
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const SubscribePage = (props) => {
|
2022-02-28 21:56:38 +00:00
|
|
|
const [anotherServerVisible, setAnotherServerVisible] = useState(false);
|
|
|
|
const baseUrl = (anotherServerVisible) ? props.baseUrl : defaultBaseUrl;
|
2022-02-26 04:25:04 +00:00
|
|
|
const topic = props.topic;
|
2022-02-28 21:56:38 +00:00
|
|
|
const existingTopicUrls = props.subscriptions.map((id, s) => s.url());
|
|
|
|
const existingBaseUrls = Array.from(new Set(["https://ntfy.sh", ...props.subscriptions.map((id, s) => s.baseUrl)]))
|
|
|
|
.filter(s => s !== defaultBaseUrl);
|
2022-02-26 04:25:04 +00:00
|
|
|
const handleSubscribe = async () => {
|
|
|
|
const success = await api.auth(baseUrl, topic, null);
|
|
|
|
if (!success) {
|
|
|
|
console.log(`[SubscribeDialog] Login to ${topicUrl(baseUrl, topic)} failed for anonymous user`);
|
|
|
|
props.onNeedsLogin();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log(`[SubscribeDialog] Successful login to ${topicUrl(baseUrl, topic)} for anonymous user`);
|
2022-02-28 21:56:38 +00:00
|
|
|
props.onSuccess(null);
|
2022-02-26 04:25:04 +00:00
|
|
|
};
|
2022-02-28 21:56:38 +00: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 {
|
|
|
|
const isExistingTopicUrl = existingTopicUrls.includes(topicUrl(defaultBaseUrl, topic)); // FIXME
|
|
|
|
return validTopic(topic) && !isExistingTopicUrl;
|
|
|
|
}
|
|
|
|
})();
|
2022-02-25 18:40:03 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<DialogTitle>Subscribe to topic</DialogTitle>
|
|
|
|
<DialogContent>
|
|
|
|
<DialogContentText>
|
|
|
|
Topics may not be password-protected, so choose a name that's not easy to guess.
|
|
|
|
Once subscribed, you can PUT/POST notifications.
|
|
|
|
</DialogContentText>
|
|
|
|
<TextField
|
|
|
|
autoFocus
|
|
|
|
margin="dense"
|
|
|
|
id="topic"
|
2022-02-28 21:56:38 +00:00
|
|
|
placeholder="Topic name, e.g. phil_alerts"
|
2022-02-25 18:40:03 +00:00
|
|
|
value={props.topic}
|
|
|
|
onChange={ev => props.setTopic(ev.target.value)}
|
|
|
|
type="text"
|
|
|
|
fullWidth
|
|
|
|
variant="standard"
|
|
|
|
/>
|
2022-02-28 21:56:38 +00:00
|
|
|
<FormControlLabel
|
|
|
|
sx={{pt: 1}}
|
|
|
|
control={<Checkbox onChange={handleUseAnotherChanged}/>}
|
|
|
|
label="Use another server" />
|
|
|
|
{anotherServerVisible && <Autocomplete
|
|
|
|
freeSolo
|
|
|
|
options={existingBaseUrls}
|
|
|
|
sx={{ maxWidth: 400 }}
|
|
|
|
inputValue={props.baseUrl}
|
|
|
|
onInputChange={(ev, newVal) => props.setBaseUrl(newVal)}
|
|
|
|
renderInput={ (params) =>
|
|
|
|
<TextField {...params} placeholder={defaultBaseUrl} variant="standard"/>
|
|
|
|
}
|
|
|
|
/>}
|
2022-02-25 18:40:03 +00:00
|
|
|
</DialogContent>
|
|
|
|
<DialogActions>
|
|
|
|
<Button onClick={props.onCancel}>Cancel</Button>
|
2022-02-26 16:45:39 +00:00
|
|
|
<Button onClick={handleSubscribe} disabled={!subscribeButtonEnabled}>Subscribe</Button>
|
2022-02-25 18:40:03 +00:00
|
|
|
</DialogActions>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const LoginPage = (props) => {
|
2022-02-25 21:07:25 +00:00
|
|
|
const [username, setUsername] = useState("");
|
|
|
|
const [password, setPassword] = useState("");
|
|
|
|
const [errorText, setErrorText] = useState("");
|
2022-02-28 21:56:38 +00:00
|
|
|
const baseUrl = (props.baseUrl) ? props.baseUrl : defaultBaseUrl;
|
2022-02-25 21:07:25 +00:00
|
|
|
const topic = props.topic;
|
|
|
|
const handleLogin = async () => {
|
2022-02-26 04:25:04 +00:00
|
|
|
const user = new User(baseUrl, username, password);
|
2022-02-25 21:07:25 +00:00
|
|
|
const success = await api.auth(baseUrl, topic, user);
|
|
|
|
if (!success) {
|
|
|
|
console.log(`[SubscribeDialog] Login to ${topicUrl(baseUrl, topic)} failed for user ${username}`);
|
|
|
|
setErrorText(`User ${username} not authorized`);
|
|
|
|
return;
|
|
|
|
}
|
2022-02-26 04:25:04 +00:00
|
|
|
console.log(`[SubscribeDialog] Successful login to ${topicUrl(baseUrl, topic)} for user ${username}`);
|
2022-02-28 21:56:38 +00:00
|
|
|
props.onSuccess(user);
|
2022-02-25 21:07:25 +00:00
|
|
|
};
|
2022-02-20 03:26:58 +00:00
|
|
|
return (
|
|
|
|
<>
|
2022-02-25 18:40:03 +00:00
|
|
|
<DialogTitle>Login required</DialogTitle>
|
|
|
|
<DialogContent>
|
|
|
|
<DialogContentText>
|
|
|
|
This topic is password-protected. Please enter username and
|
|
|
|
password to subscribe.
|
|
|
|
</DialogContentText>
|
|
|
|
<TextField
|
|
|
|
autoFocus
|
|
|
|
margin="dense"
|
|
|
|
id="username"
|
|
|
|
label="Username, e.g. phil"
|
2022-02-25 21:07:25 +00:00
|
|
|
value={username}
|
|
|
|
onChange={ev => setUsername(ev.target.value)}
|
2022-02-25 18:40:03 +00:00
|
|
|
type="text"
|
|
|
|
fullWidth
|
|
|
|
variant="standard"
|
|
|
|
/>
|
|
|
|
<TextField
|
|
|
|
margin="dense"
|
|
|
|
id="password"
|
|
|
|
label="Password"
|
|
|
|
type="password"
|
2022-02-25 21:07:25 +00:00
|
|
|
value={password}
|
|
|
|
onChange={ev => setPassword(ev.target.value)}
|
2022-02-25 18:40:03 +00:00
|
|
|
fullWidth
|
|
|
|
variant="standard"
|
|
|
|
/>
|
|
|
|
</DialogContent>
|
2022-03-01 21:22:47 +00:00
|
|
|
<DialogFooter status={errorText}>
|
|
|
|
<Button onClick={props.onBack}>Back</Button>
|
|
|
|
<Button onClick={handleLogin}>Login</Button>
|
|
|
|
</DialogFooter>
|
2022-02-20 03:26:58 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-03-01 21:22:47 +00:00
|
|
|
const DialogFooter = (props) => {
|
|
|
|
return (
|
|
|
|
<Box sx={{
|
|
|
|
display: 'flex',
|
|
|
|
flexDirection: 'row',
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
paddingLeft: '24px',
|
|
|
|
paddingTop: '8px 24px',
|
|
|
|
paddingBottom: '8px 24px',
|
|
|
|
}}>
|
|
|
|
<DialogContentText sx={{
|
|
|
|
margin: '0px',
|
|
|
|
paddingTop: '8px',
|
|
|
|
}}>
|
|
|
|
{props.status}
|
|
|
|
</DialogContentText>
|
|
|
|
<DialogActions>
|
|
|
|
{props.children}
|
|
|
|
</DialogActions>
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-02-25 01:18:46 +00:00
|
|
|
export default SubscribeDialog;
|