2022-03-10 04:28:55 +00:00
|
|
|
import {useNavigate, useParams} from "react-router-dom";
|
|
|
|
import {useEffect, useState} from "react";
|
|
|
|
import subscriptionManager from "../app/SubscriptionManager";
|
|
|
|
import {disallowedTopic, expandSecureUrl, topicUrl} from "../app/utils";
|
|
|
|
import notifier from "../app/Notifier";
|
|
|
|
import routes from "./routes";
|
|
|
|
import connectionManager from "../app/ConnectionManager";
|
|
|
|
import poller from "../app/Poller";
|
2022-03-27 13:20:25 +00:00
|
|
|
import pruner from "../app/Pruner";
|
2022-12-09 01:50:48 +00:00
|
|
|
import session from "../app/Session";
|
|
|
|
import api from "../app/Api";
|
2022-03-10 04:28:55 +00:00
|
|
|
|
2022-03-11 19:43:54 +00:00
|
|
|
/**
|
|
|
|
* Wire connectionManager and subscriptionManager so that subscriptions are updated when the connection
|
|
|
|
* state changes. Conversely, when the subscription changes, the connection is refreshed (which may lead
|
|
|
|
* to the connection being re-established).
|
|
|
|
*/
|
|
|
|
export const useConnectionListeners = (subscriptions, users) => {
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const handleNotification = async (subscriptionId, notification) => {
|
|
|
|
const added = await subscriptionManager.addNotification(subscriptionId, notification);
|
|
|
|
if (added) {
|
|
|
|
const defaultClickAction = (subscription) => navigate(routes.forSubscription(subscription));
|
|
|
|
await notifier.notify(subscriptionId, notification, defaultClickAction)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
connectionManager.registerStateListener(subscriptionManager.updateState);
|
|
|
|
connectionManager.registerNotificationListener(handleNotification);
|
|
|
|
return () => {
|
|
|
|
connectionManager.resetStateListener();
|
|
|
|
connectionManager.resetNotificationListener();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// We have to disable dep checking for "navigate". This is fine, it never changes.
|
|
|
|
// eslint-disable-next-line
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
connectionManager.refresh(subscriptions, users); // Dangle
|
|
|
|
}, [subscriptions, users]);
|
2022-03-10 04:28:55 +00:00
|
|
|
};
|
|
|
|
|
2022-03-11 19:43:54 +00:00
|
|
|
/**
|
|
|
|
* Automatically adds a subscription if we navigate to a page that has not been subscribed to.
|
|
|
|
* This will only be run once after the initial page load.
|
|
|
|
*/
|
2022-03-10 04:28:55 +00:00
|
|
|
export const useAutoSubscribe = (subscriptions, selected) => {
|
2022-03-11 19:43:54 +00:00
|
|
|
const [hasRun, setHasRun] = useState(false);
|
|
|
|
const params = useParams();
|
2022-03-10 04:28:55 +00:00
|
|
|
|
2022-03-11 19:43:54 +00:00
|
|
|
useEffect(() => {
|
|
|
|
const loaded = subscriptions !== null && subscriptions !== undefined;
|
|
|
|
if (!loaded || hasRun) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setHasRun(true);
|
|
|
|
const eligible = params.topic && !selected && !disallowedTopic(params.topic);
|
|
|
|
if (eligible) {
|
|
|
|
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);
|
2022-12-09 01:50:48 +00:00
|
|
|
if (session.exists()) {
|
|
|
|
const remoteSubscription = await api.userSubscriptionAdd("http://localhost:2586", session.token(), {
|
|
|
|
base_url: baseUrl,
|
|
|
|
topic: params.topic
|
|
|
|
});
|
|
|
|
await subscriptionManager.setRemoteId(subscription.id, remoteSubscription.id);
|
|
|
|
}
|
2022-03-11 19:43:54 +00:00
|
|
|
poller.pollInBackground(subscription); // Dangle!
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
}, [params, subscriptions, selected, hasRun]);
|
2022-03-10 04:28:55 +00:00
|
|
|
};
|
2022-03-11 19:43:54 +00:00
|
|
|
|
2022-03-11 20:17:12 +00:00
|
|
|
/**
|
2022-03-27 13:20:25 +00:00
|
|
|
* Start the poller and the pruner. This is done in a side effect as opposed to just in Pruner.js
|
|
|
|
* and Poller.js, because side effect imports are not a thing in JS, and "Optimize imports" cleans
|
|
|
|
* up "unused" imports. See https://github.com/binwiederhier/ntfy/issues/186.
|
2022-03-11 20:17:12 +00:00
|
|
|
*/
|
2022-03-27 13:20:25 +00:00
|
|
|
export const useBackgroundProcesses = () => {
|
2022-03-11 19:43:54 +00:00
|
|
|
useEffect(() => {
|
2022-03-27 13:20:25 +00:00
|
|
|
poller.startWorker();
|
|
|
|
pruner.startWorker();
|
2022-03-11 19:43:54 +00:00
|
|
|
}, []);
|
|
|
|
}
|