mirror of
https://github.com/binwiederhier/ntfy.git
synced 2025-06-17 18:13:21 +02:00
Run prettier
This commit is contained in:
parent
206ea312bf
commit
6f6a2d1f69
49 changed files with 22902 additions and 6633 deletions
web/src/components
|
@ -1,7 +1,7 @@
|
|||
import {useNavigate, useParams} from "react-router-dom";
|
||||
import {useEffect, useState} from "react";
|
||||
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 { disallowedTopic, expandSecureUrl, topicUrl } from "../app/utils";
|
||||
import notifier from "../app/Notifier";
|
||||
import routes from "./routes";
|
||||
import connectionManager from "../app/ConnectionManager";
|
||||
|
@ -9,7 +9,7 @@ import poller from "../app/Poller";
|
|||
import pruner from "../app/Pruner";
|
||||
import session from "../app/Session";
|
||||
import accountApi from "../app/AccountApi";
|
||||
import {UnauthorizedError} from "../app/errors";
|
||||
import { UnauthorizedError } from "../app/errors";
|
||||
|
||||
/**
|
||||
* Wire connectionManager and subscriptionManager so that subscriptions are updated when the connection
|
||||
|
@ -17,65 +17,82 @@ import {UnauthorizedError} from "../app/errors";
|
|||
* to the connection being re-established).
|
||||
*/
|
||||
export const useConnectionListeners = (account, subscriptions, users) => {
|
||||
const navigate = useNavigate();
|
||||
const navigate = useNavigate();
|
||||
|
||||
// Register listeners for incoming messages, and connection state changes
|
||||
useEffect(() => {
|
||||
const handleMessage = async (subscriptionId, message) => {
|
||||
const subscription = await subscriptionManager.get(subscriptionId);
|
||||
if (subscription.internal) {
|
||||
await handleInternalMessage(message);
|
||||
} else {
|
||||
await handleNotification(subscriptionId, message);
|
||||
}
|
||||
};
|
||||
|
||||
const handleInternalMessage = async (message) => {
|
||||
console.log(`[ConnectionListener] Received message on sync topic`, message.message);
|
||||
try {
|
||||
const data = JSON.parse(message.message);
|
||||
if (data.event === "sync") {
|
||||
console.log(`[ConnectionListener] Triggering account sync`);
|
||||
await accountApi.sync();
|
||||
} else {
|
||||
console.log(`[ConnectionListener] Unknown message type. Doing nothing.`);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(`[ConnectionListener] Error parsing sync topic message`, e);
|
||||
}
|
||||
};
|
||||
|
||||
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.registerMessageListener(handleMessage);
|
||||
return () => {
|
||||
connectionManager.resetStateListener();
|
||||
connectionManager.resetMessageListener();
|
||||
}
|
||||
},
|
||||
// We have to disable dep checking for "navigate". This is fine, it never changes.
|
||||
// eslint-disable-next-line
|
||||
[]
|
||||
);
|
||||
|
||||
// Sync topic listener: For accounts with sync_topic, subscribe to an internal topic
|
||||
useEffect(() => {
|
||||
if (!account || !account.sync_topic) {
|
||||
return;
|
||||
// Register listeners for incoming messages, and connection state changes
|
||||
useEffect(
|
||||
() => {
|
||||
const handleMessage = async (subscriptionId, message) => {
|
||||
const subscription = await subscriptionManager.get(subscriptionId);
|
||||
if (subscription.internal) {
|
||||
await handleInternalMessage(message);
|
||||
} else {
|
||||
await handleNotification(subscriptionId, message);
|
||||
}
|
||||
subscriptionManager.add(config.base_url, account.sync_topic, true); // Dangle!
|
||||
}, [account]);
|
||||
};
|
||||
|
||||
// When subscriptions or users change, refresh the connections
|
||||
useEffect(() => {
|
||||
connectionManager.refresh(subscriptions, users); // Dangle
|
||||
}, [subscriptions, users]);
|
||||
const handleInternalMessage = async (message) => {
|
||||
console.log(
|
||||
`[ConnectionListener] Received message on sync topic`,
|
||||
message.message
|
||||
);
|
||||
try {
|
||||
const data = JSON.parse(message.message);
|
||||
if (data.event === "sync") {
|
||||
console.log(`[ConnectionListener] Triggering account sync`);
|
||||
await accountApi.sync();
|
||||
} else {
|
||||
console.log(
|
||||
`[ConnectionListener] Unknown message type. Doing nothing.`
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(
|
||||
`[ConnectionListener] Error parsing sync topic message`,
|
||||
e
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
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.registerMessageListener(handleMessage);
|
||||
return () => {
|
||||
connectionManager.resetStateListener();
|
||||
connectionManager.resetMessageListener();
|
||||
};
|
||||
},
|
||||
// We have to disable dep checking for "navigate". This is fine, it never changes.
|
||||
// eslint-disable-next-line
|
||||
[]
|
||||
);
|
||||
|
||||
// Sync topic listener: For accounts with sync_topic, subscribe to an internal topic
|
||||
useEffect(() => {
|
||||
if (!account || !account.sync_topic) {
|
||||
return;
|
||||
}
|
||||
subscriptionManager.add(config.base_url, account.sync_topic, true); // Dangle!
|
||||
}, [account]);
|
||||
|
||||
// When subscriptions or users change, refresh the connections
|
||||
useEffect(() => {
|
||||
connectionManager.refresh(subscriptions, users); // Dangle
|
||||
}, [subscriptions, users]);
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -83,35 +100,43 @@ export const useConnectionListeners = (account, subscriptions, users) => {
|
|||
* This will only be run once after the initial page load.
|
||||
*/
|
||||
export const useAutoSubscribe = (subscriptions, selected) => {
|
||||
const [hasRun, setHasRun] = useState(false);
|
||||
const params = useParams();
|
||||
const [hasRun, setHasRun] = useState(false);
|
||||
const params = useParams();
|
||||
|
||||
useEffect(() => {
|
||||
const loaded = subscriptions !== null && subscriptions !== undefined;
|
||||
if (!loaded || hasRun) {
|
||||
return;
|
||||
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)
|
||||
: config.base_url;
|
||||
console.log(
|
||||
`[Hooks] Auto-subscribing to ${topicUrl(baseUrl, params.topic)}`
|
||||
);
|
||||
(async () => {
|
||||
const subscription = await subscriptionManager.add(
|
||||
baseUrl,
|
||||
params.topic
|
||||
);
|
||||
if (session.exists()) {
|
||||
try {
|
||||
await accountApi.addSubscription(baseUrl, params.topic);
|
||||
} catch (e) {
|
||||
console.log(`[Hooks] Auto-subscribing failed`, e);
|
||||
if (e instanceof UnauthorizedError) {
|
||||
session.resetAndRedirect(routes.login);
|
||||
}
|
||||
}
|
||||
}
|
||||
setHasRun(true);
|
||||
const eligible = params.topic && !selected && !disallowedTopic(params.topic);
|
||||
if (eligible) {
|
||||
const baseUrl = (params.baseUrl) ? expandSecureUrl(params.baseUrl) : config.base_url;
|
||||
console.log(`[Hooks] Auto-subscribing to ${topicUrl(baseUrl, params.topic)}`);
|
||||
(async () => {
|
||||
const subscription = await subscriptionManager.add(baseUrl, params.topic);
|
||||
if (session.exists()) {
|
||||
try {
|
||||
await accountApi.addSubscription(baseUrl, params.topic);
|
||||
} catch (e) {
|
||||
console.log(`[Hooks] Auto-subscribing failed`, e);
|
||||
if (e instanceof UnauthorizedError) {
|
||||
session.resetAndRedirect(routes.login);
|
||||
}
|
||||
}
|
||||
}
|
||||
poller.pollInBackground(subscription); // Dangle!
|
||||
})();
|
||||
}
|
||||
}, [params, subscriptions, selected, hasRun]);
|
||||
poller.pollInBackground(subscription); // Dangle!
|
||||
})();
|
||||
}
|
||||
}, [params, subscriptions, selected, hasRun]);
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -120,19 +145,19 @@ export const useAutoSubscribe = (subscriptions, selected) => {
|
|||
* up "unused" imports. See https://github.com/binwiederhier/ntfy/issues/186.
|
||||
*/
|
||||
export const useBackgroundProcesses = () => {
|
||||
useEffect(() => {
|
||||
poller.startWorker();
|
||||
pruner.startWorker();
|
||||
accountApi.startWorker();
|
||||
}, []);
|
||||
}
|
||||
useEffect(() => {
|
||||
poller.startWorker();
|
||||
pruner.startWorker();
|
||||
accountApi.startWorker();
|
||||
}, []);
|
||||
};
|
||||
|
||||
export const useAccountListener = (setAccount) => {
|
||||
useEffect(() => {
|
||||
accountApi.registerListener(setAccount);
|
||||
accountApi.sync(); // Dangle
|
||||
return () => {
|
||||
accountApi.resetListener();
|
||||
}
|
||||
}, []);
|
||||
}
|
||||
useEffect(() => {
|
||||
accountApi.registerListener(setAccount);
|
||||
accountApi.sync(); // Dangle
|
||||
return () => {
|
||||
accountApi.resetListener();
|
||||
};
|
||||
}, []);
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue