diff --git a/web/src/app/SubscriptionManager.js b/web/src/app/SubscriptionManager.js index 2d8e79cf..c68a198c 100644 --- a/web/src/app/SubscriptionManager.js +++ b/web/src/app/SubscriptionManager.js @@ -2,7 +2,7 @@ import api from "./Api"; import notifier from "./Notifier"; import prefs from "./Prefs"; import db from "./db"; -import { isLaunchedPWA, topicUrl } from "./utils"; +import { topicUrl } from "./utils"; class SubscriptionManager { constructor(dbImpl) { @@ -27,14 +27,15 @@ class SubscriptionManager { * It is important to note that "mutedUntil" must be part of the where() query, otherwise the Dexie live query * will not react to it, and the Web Push topics will not be updated when the user mutes a topic. */ - async webPushTopics(isStandalone = isLaunchedPWA(), pushPossible = notifier.pushPossible()) { + async webPushTopics(pushPossible = notifier.pushPossible()) { if (!pushPossible) { return []; } // the Promise.resolve wrapper is not superfluous, without it the live query breaks: // https://dexie.org/docs/dexie-react-hooks/useLiveQuery()#calling-non-dexie-apis-from-querier - if (!(isStandalone || (await Promise.resolve(prefs.webPushEnabled())))) { + const enabled = await Promise.resolve(prefs.webPushEnabled()); + if (!enabled) { return []; } diff --git a/web/src/components/hooks.js b/web/src/components/hooks.js index 5e9b2ed6..e065c2c2 100644 --- a/web/src/components/hooks.js +++ b/web/src/components/hooks.js @@ -12,6 +12,7 @@ import accountApi from "../app/AccountApi"; import { UnauthorizedError } from "../app/errors"; import useWebPushListener from "../app/WebPush"; import notifier from "../app/Notifier"; +import prefs from "../app/Prefs"; /** * Wire connectionManager and subscriptionManager so that subscriptions are updated when the connection @@ -135,15 +136,14 @@ export const useAutoSubscribe = (subscriptions, selected) => { }, [params, subscriptions, selected, hasRun]); }; -export const useWebPushTopics = () => { +export const useStandaloneAutoWebPushSubscribe = () => { const matchMedia = window.matchMedia("(display-mode: standalone)"); const [isStandalone, setIsStandalone] = useState(isLaunchedPWA()); - const [pushPossible, setPushPossible] = useState(notifier.pushPossible()); useEffect(() => { const handler = (evt) => { - console.log(`[useWebPushTopics] App is now running ${evt.matches ? "standalone" : "in the browser"}`); + console.log(`[useStandaloneAutoWebPushSubscribe] App is now running ${evt.matches ? "standalone" : "in the browser"}`); setIsStandalone(evt.matches); }; @@ -154,6 +154,17 @@ export const useWebPushTopics = () => { }; }); + useEffect(() => { + if (isStandalone) { + console.log(`[useStandaloneAutoWebPushSubscribe] Turning on web push automatically`); + prefs.setWebPushEnabled(true); + } + }, [isStandalone]); +}; + +export const useWebPushTopics = () => { + const [pushPossible, setPushPossible] = useState(notifier.pushPossible()); + useEffect(() => { const handler = () => { const newPushPossible = notifier.pushPossible(); @@ -173,9 +184,9 @@ export const useWebPushTopics = () => { }); const topics = useLiveQuery( - async () => subscriptionManager.webPushTopics(isStandalone, pushPossible), + async () => subscriptionManager.webPushTopics(pushPossible), // invalidate (reload) query when these values change - [isStandalone, pushPossible] + [pushPossible] ); useWebPushListener(topics); @@ -202,6 +213,8 @@ const stopWorkers = () => { }; export const useBackgroundProcesses = () => { + useStandaloneAutoWebPushSubscribe(); + useEffect(() => { console.log("[useBackgroundProcesses] mounting"); startWorkers();