Set PWA web push enabled on launch instead
parent
a8d3297c4e
commit
833293ad77
|
@ -2,7 +2,7 @@ import api from "./Api";
|
||||||
import notifier from "./Notifier";
|
import notifier from "./Notifier";
|
||||||
import prefs from "./Prefs";
|
import prefs from "./Prefs";
|
||||||
import db from "./db";
|
import db from "./db";
|
||||||
import { isLaunchedPWA, topicUrl } from "./utils";
|
import { topicUrl } from "./utils";
|
||||||
|
|
||||||
class SubscriptionManager {
|
class SubscriptionManager {
|
||||||
constructor(dbImpl) {
|
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
|
* 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.
|
* 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) {
|
if (!pushPossible) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
// the Promise.resolve wrapper is not superfluous, without it the live query breaks:
|
// 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
|
// 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 [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,7 @@ import accountApi from "../app/AccountApi";
|
||||||
import { UnauthorizedError } from "../app/errors";
|
import { UnauthorizedError } from "../app/errors";
|
||||||
import useWebPushListener from "../app/WebPush";
|
import useWebPushListener from "../app/WebPush";
|
||||||
import notifier from "../app/Notifier";
|
import notifier from "../app/Notifier";
|
||||||
|
import prefs from "../app/Prefs";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wire connectionManager and subscriptionManager so that subscriptions are updated when the connection
|
* 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]);
|
}, [params, subscriptions, selected, hasRun]);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useWebPushTopics = () => {
|
export const useStandaloneAutoWebPushSubscribe = () => {
|
||||||
const matchMedia = window.matchMedia("(display-mode: standalone)");
|
const matchMedia = window.matchMedia("(display-mode: standalone)");
|
||||||
|
|
||||||
const [isStandalone, setIsStandalone] = useState(isLaunchedPWA());
|
const [isStandalone, setIsStandalone] = useState(isLaunchedPWA());
|
||||||
const [pushPossible, setPushPossible] = useState(notifier.pushPossible());
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handler = (evt) => {
|
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);
|
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(() => {
|
useEffect(() => {
|
||||||
const handler = () => {
|
const handler = () => {
|
||||||
const newPushPossible = notifier.pushPossible();
|
const newPushPossible = notifier.pushPossible();
|
||||||
|
@ -173,9 +184,9 @@ export const useWebPushTopics = () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
const topics = useLiveQuery(
|
const topics = useLiveQuery(
|
||||||
async () => subscriptionManager.webPushTopics(isStandalone, pushPossible),
|
async () => subscriptionManager.webPushTopics(pushPossible),
|
||||||
// invalidate (reload) query when these values change
|
// invalidate (reload) query when these values change
|
||||||
[isStandalone, pushPossible]
|
[pushPossible]
|
||||||
);
|
);
|
||||||
|
|
||||||
useWebPushListener(topics);
|
useWebPushListener(topics);
|
||||||
|
@ -202,6 +213,8 @@ const stopWorkers = () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useBackgroundProcesses = () => {
|
export const useBackgroundProcesses = () => {
|
||||||
|
useStandaloneAutoWebPushSubscribe();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
console.log("[useBackgroundProcesses] mounting");
|
console.log("[useBackgroundProcesses] mounting");
|
||||||
startWorkers();
|
startWorkers();
|
||||||
|
|
Loading…
Reference in New Issue