1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-11-23 03:43:47 +01:00

Autosubscribe (WIP)

This commit is contained in:
Philipp Heckel 2022-03-08 20:26:15 -05:00
parent d9e5e08af5
commit 28e6f8a0f6

View file

@ -16,8 +16,8 @@ import subscriptionManager from "../app/SubscriptionManager";
import userManager from "../app/UserManager"; import userManager from "../app/UserManager";
import {BrowserRouter, Outlet, Route, Routes, useNavigate, useOutletContext, useParams} from "react-router-dom"; import {BrowserRouter, Outlet, Route, Routes, useNavigate, useOutletContext, useParams} from "react-router-dom";
import {expandSecureUrl, expandUrl, subscriptionRoute, topicUrl} from "../app/utils"; import {expandSecureUrl, expandUrl, subscriptionRoute, topicUrl} from "../app/utils";
import poller from "../app/Poller";
// TODO support unsubscribed routes
// TODO "copy url" toast // TODO "copy url" toast
// TODO "copy link url" button // TODO "copy link url" button
// TODO races when two tabs are open // TODO races when two tabs are open
@ -48,19 +48,7 @@ const AllSubscriptions = () => {
const SingleSubscription = () => { const SingleSubscription = () => {
const { subscriptions, selected } = useOutletContext(); const { subscriptions, selected } = useOutletContext();
const [missingAdded, setMissingAdded] = useState(false); useAutoSubscribe(subscriptions, selected);
const params = useParams();
useEffect(() => {
const loaded = subscriptions !== null && subscriptions !== undefined;
const missing = loaded && params.topic && !selected && !missingAdded;
if (missing) {
setMissingAdded(true);
const baseUrl = (params.baseUrl) ? expandSecureUrl(params.baseUrl) : window.location.origin;
console.log(`[App] Adding ephemeral subscription for ${topicUrl(baseUrl, params.topic)}`);
// subscriptionManager.add(baseUrl, params.topic, true); // Dangle!
}
}, [params, subscriptions, selected, missingAdded]);
return <Notifications mode="one" subscription={selected}/>; return <Notifications mode="one" subscription={selected}/>;
}; };
@ -147,6 +135,25 @@ const useConnectionListeners = () => {
[]); []);
}; };
const useAutoSubscribe = (subscriptions, selected) => {
const [autoSubscribed, setAutoSubscribed] = useState(false);
const params = useParams();
useEffect(() => {
const loaded = subscriptions !== null && subscriptions !== undefined;
const eligible = loaded && params.topic && !selected && !autoSubscribed;
if (eligible) {
setAutoSubscribed(true);
const baseUrl = (params.baseUrl) ? expandSecureUrl(params.baseUrl) : window.location.origin;
console.log(`[App] Auto-subscribing to ${topicUrl(baseUrl, params.topic)}`);
(async () => {
const subscription = await subscriptionManager.add(baseUrl, params.topic, true);
poller.pollInBackground(subscription); // Dangle!
})();
}
}, [params, subscriptions, selected, autoSubscribed]);
};
const updateTitle = (newNotificationsCount) => { const updateTitle = (newNotificationsCount) => {
document.title = (newNotificationsCount > 0) ? `(${newNotificationsCount}) ntfy web` : "ntfy web"; document.title = (newNotificationsCount > 0) ? `(${newNotificationsCount}) ntfy web` : "ntfy web";
} }