Use location.origin as default base URL

This commit is contained in:
Philipp Heckel 2022-03-05 22:11:32 -05:00
parent c5ce51f242
commit 420e35c33c
7 changed files with 16 additions and 19 deletions

View file

@ -1,5 +0,0 @@
//const config = window.config;
const config = {
defaultBaseUrl: "https://ntfy.sh"
};
export default config;

View file

@ -1,5 +1,4 @@
import {rawEmojis} from "./emojis";
import config from "./config";
export const topicUrl = (baseUrl, topic) => `${baseUrl}/${topic}`;
export const topicUrlWs = (baseUrl, topic) => `${topicUrl(baseUrl, topic)}/ws`
@ -116,7 +115,7 @@ export const openUrl = (url) => {
};
export const subscriptionRoute = (subscription) => {
if (subscription.baseUrl !== config.defaultBaseUrl) {
if (subscription.baseUrl !== window.location.origin) {
return `/${shortUrl(subscription.baseUrl)}/${subscription.topic}`;
}
return `/${subscription.topic}`;

View file

@ -21,8 +21,11 @@ import {BrowserRouter, Route, Routes, useLocation, useNavigate} from "react-rout
import {subscriptionRoute} from "../app/utils";
// TODO support unsubscribed routes
// TODO add "home" route that is selected when nothing else fits
// TODO new notification indicator
// TODO sound
// TODO "copy url" toast
// TODO "copy link url" button
const App = () => {
return (
@ -87,6 +90,8 @@ const Root = () => {
connectionManager.resetStateListener();
connectionManager.resetNotificationListener();
}
// This is for the use of 'navigate' // FIXME
//eslint-disable-next-line
}, [/* initial render */]);
useEffect(() => { connectionManager.refresh(subscriptions, users) }, [subscriptions, users]); // Dangle!
return (

View file

@ -17,7 +17,6 @@ import Typography from "@mui/material/Typography";
import {subscriptionRoute, topicShortUrl, topicUrl} from "../app/utils";
import {ConnectionState} from "../app/Connection";
import {useLocation, useNavigate} from "react-router-dom";
import config from "../app/config";
const navWidth = 240;
@ -125,7 +124,7 @@ const SubscriptionItem = (props) => {
const icon = (subscription.state === ConnectionState.Connecting)
? <CircularProgress size="24px"/>
: <ChatBubbleOutlineIcon/>;
const label = (subscription.baseUrl === config.defaultBaseUrl)
const label = (subscription.baseUrl === window.location.origin)
? subscription.topic
: topicShortUrl(subscription.baseUrl, subscription.topic);
return (

View file

@ -76,7 +76,7 @@ const NotificationItem = (props) => {
{date}
{[1,2,4,5].includes(notification.priority) &&
<img
src={`static/img/priority-${notification.priority}.svg`}
src={`/static/img/priority-${notification.priority}.svg`}
alt={`Priority ${notification.priority}`}
style={{ verticalAlign: 'bottom' }}
/>}

View file

@ -281,7 +281,7 @@ const UserDialog = (props) => {
setUsername(props.user.username);
setPassword(props.user.password);
}
}, []);
}, [editMode, props.user]);
return (
<Dialog open={props.open} onClose={props.onCancel} fullScreen={fullScreen}>
<DialogTitle>{editMode ? "Edit user" : "Add user"}</DialogTitle>

View file

@ -10,14 +10,13 @@ import DialogTitle from '@mui/material/DialogTitle';
import {Autocomplete, Checkbox, FormControlLabel, useMediaQuery} from "@mui/material";
import theme from "./theme";
import api from "../app/Api";
import config from "../app/config";
import {topicUrl, validTopic, validUrl} from "../app/utils";
import Box from "@mui/material/Box";
import userManager from "../app/UserManager";
import subscriptionManager from "../app/SubscriptionManager";
import poller from "../app/Poller";
const publicBaseUrl = "https://ntfy.sh"
const publicBaseUrl = "https://ntfy.sh";
const SubscribeDialog = (props) => {
const [baseUrl, setBaseUrl] = useState("");
@ -25,7 +24,7 @@ const SubscribeDialog = (props) => {
const [showLoginPage, setShowLoginPage] = useState(false);
const fullScreen = useMediaQuery(theme.breakpoints.down('sm'));
const handleSuccess = async () => {
const actualBaseUrl = (baseUrl) ? baseUrl : config.defaultBaseUrl;
const actualBaseUrl = (baseUrl) ? baseUrl : window.location.origin;
const subscription = {
id: topicUrl(actualBaseUrl, topic),
baseUrl: actualBaseUrl,
@ -61,11 +60,11 @@ const SubscribeDialog = (props) => {
const SubscribePage = (props) => {
const [anotherServerVisible, setAnotherServerVisible] = useState(false);
const [errorText, setErrorText] = useState("");
const baseUrl = (anotherServerVisible) ? props.baseUrl : config.defaultBaseUrl;
const baseUrl = (anotherServerVisible) ? props.baseUrl : window.location.origin;
const topic = props.topic;
const existingTopicUrls = props.subscriptions.map(s => topicUrl(s.baseUrl, s.topic));
const existingBaseUrls = Array.from(new Set([publicBaseUrl, ...props.subscriptions.map(s => s.baseUrl)]))
.filter(s => s !== config.defaultBaseUrl);
.filter(s => s !== window.location.origin);
const handleSubscribe = async () => {
const user = await userManager.get(baseUrl); // May be undefined
const username = (user) ? user.username : "anonymous";
@ -92,7 +91,7 @@ const SubscribePage = (props) => {
const isExistingTopicUrl = existingTopicUrls.includes(topicUrl(baseUrl, topic));
return validTopic(topic) && validUrl(baseUrl) && !isExistingTopicUrl;
} else {
const isExistingTopicUrl = existingTopicUrls.includes(topicUrl(config.defaultBaseUrl, topic)); // FIXME
const isExistingTopicUrl = existingTopicUrls.includes(topicUrl(window.location.origin, topic)); // FIXME
return validTopic(topic) && !isExistingTopicUrl;
}
})();
@ -127,7 +126,7 @@ const SubscribePage = (props) => {
inputValue={props.baseUrl}
onInputChange={(ev, newVal) => props.setBaseUrl(newVal)}
renderInput={ (params) =>
<TextField {...params} placeholder={config.defaultBaseUrl} variant="standard"/>
<TextField {...params} placeholder={window.location.origin} variant="standard"/>
}
/>}
</DialogContent>
@ -143,7 +142,7 @@ const LoginPage = (props) => {
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
const [errorText, setErrorText] = useState("");
const baseUrl = (props.baseUrl) ? props.baseUrl : config.defaultBaseUrl;
const baseUrl = (props.baseUrl) ? props.baseUrl : window.location.origin;
const topic = props.topic;
const handleLogin = async () => {
const user = {baseUrl, username, password};