2021-06-24 09:26:18 -04:00
|
|
|
import defaultsDeep from "lodash.defaultsdeep";
|
2021-07-05 06:23:23 -04:00
|
|
|
|
|
|
|
import styles from "./Panes.module.scss";
|
|
|
|
import { Text } from "preact-i18n";
|
2021-06-19 17:37:12 -04:00
|
|
|
import { useContext, useEffect, useState } from "preact/hooks";
|
2021-07-05 06:23:23 -04:00
|
|
|
|
2021-06-19 17:37:12 -04:00
|
|
|
import { urlBase64ToUint8Array } from "../../../lib/conversion";
|
2021-07-05 06:23:23 -04:00
|
|
|
|
|
|
|
import { dispatch } from "../../../redux";
|
|
|
|
import { connectState } from "../../../redux/connector";
|
|
|
|
import {
|
2021-07-05 06:25:20 -04:00
|
|
|
DEFAULT_SOUNDS,
|
|
|
|
NotificationOptions,
|
|
|
|
SoundOptions,
|
2021-07-05 06:23:23 -04:00
|
|
|
} from "../../../redux/reducers/settings";
|
|
|
|
|
2021-06-19 17:37:12 -04:00
|
|
|
import { useIntermediate } from "../../../context/intermediate/Intermediate";
|
2021-07-05 06:23:23 -04:00
|
|
|
import { AppContext } from "../../../context/revoltjs/RevoltClient";
|
|
|
|
|
|
|
|
import Checkbox from "../../../components/ui/Checkbox";
|
|
|
|
|
|
|
|
import { SOUNDS_ARRAY } from "../../../assets/sounds/Audio";
|
2021-06-19 17:37:12 -04:00
|
|
|
|
|
|
|
interface Props {
|
2021-07-05 06:25:20 -04:00
|
|
|
options?: NotificationOptions;
|
2021-06-19 17:37:12 -04:00
|
|
|
}
|
|
|
|
|
2021-07-05 05:59:48 -04:00
|
|
|
export function Component({ options }: Props) {
|
2021-07-05 06:25:20 -04:00
|
|
|
const client = useContext(AppContext);
|
|
|
|
const { openScreen } = useIntermediate();
|
|
|
|
const [pushEnabled, setPushEnabled] = useState<undefined | boolean>(
|
|
|
|
undefined,
|
|
|
|
);
|
2021-06-19 17:37:12 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
// Load current state of pushManager.
|
|
|
|
useEffect(() => {
|
|
|
|
navigator.serviceWorker
|
|
|
|
?.getRegistration()
|
|
|
|
.then(async (registration) => {
|
|
|
|
const sub = await registration?.pushManager?.getSubscription();
|
|
|
|
setPushEnabled(sub !== null && sub !== undefined);
|
|
|
|
});
|
|
|
|
}, []);
|
2021-06-19 17:37:12 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
const enabledSounds: SoundOptions = defaultsDeep(
|
|
|
|
options?.sounds ?? {},
|
|
|
|
DEFAULT_SOUNDS,
|
|
|
|
);
|
|
|
|
return (
|
|
|
|
<div className={styles.notifications}>
|
|
|
|
<h3>
|
|
|
|
<Text id="app.settings.pages.notifications.push_notifications" />
|
|
|
|
</h3>
|
|
|
|
<Checkbox
|
|
|
|
disabled={!("Notification" in window)}
|
|
|
|
checked={options?.desktopEnabled ?? false}
|
|
|
|
description={
|
|
|
|
<Text id="app.settings.pages.notifications.descriptions.enable_desktop" />
|
|
|
|
}
|
|
|
|
onChange={async (desktopEnabled) => {
|
|
|
|
if (desktopEnabled) {
|
2021-07-10 10:57:29 -04:00
|
|
|
const permission =
|
|
|
|
await Notification.requestPermission();
|
2021-07-05 06:25:20 -04:00
|
|
|
if (permission !== "granted") {
|
|
|
|
return openScreen({
|
|
|
|
id: "error",
|
|
|
|
error: "DeniedNotification",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2021-06-19 17:37:12 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
dispatch({
|
|
|
|
type: "SETTINGS_SET_NOTIFICATION_OPTIONS",
|
|
|
|
options: { desktopEnabled },
|
|
|
|
});
|
|
|
|
}}>
|
|
|
|
<Text id="app.settings.pages.notifications.enable_desktop" />
|
|
|
|
</Checkbox>
|
|
|
|
<Checkbox
|
|
|
|
disabled={typeof pushEnabled === "undefined"}
|
|
|
|
checked={pushEnabled ?? false}
|
|
|
|
description={
|
|
|
|
<Text id="app.settings.pages.notifications.descriptions.enable_push" />
|
|
|
|
}
|
|
|
|
onChange={async (pushEnabled) => {
|
|
|
|
try {
|
|
|
|
const reg =
|
|
|
|
await navigator.serviceWorker?.getRegistration();
|
|
|
|
if (reg) {
|
|
|
|
if (pushEnabled) {
|
|
|
|
const sub = await reg.pushManager.subscribe({
|
|
|
|
userVisibleOnly: true,
|
|
|
|
applicationServerKey: urlBase64ToUint8Array(
|
|
|
|
client.configuration!.vapid,
|
|
|
|
),
|
|
|
|
});
|
2021-06-19 17:37:12 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
// tell the server we just subscribed
|
|
|
|
const json = sub.toJSON();
|
|
|
|
if (json.keys) {
|
|
|
|
client.req("POST", "/push/subscribe", {
|
|
|
|
endpoint: sub.endpoint,
|
|
|
|
...(json.keys as {
|
|
|
|
p256dh: string;
|
|
|
|
auth: string;
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
setPushEnabled(true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const sub =
|
|
|
|
await reg.pushManager.getSubscription();
|
|
|
|
sub?.unsubscribe();
|
|
|
|
setPushEnabled(false);
|
2021-06-19 17:37:12 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
client.req("POST", "/push/unsubscribe");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Failed to enable push!", err);
|
|
|
|
}
|
|
|
|
}}>
|
|
|
|
<Text id="app.settings.pages.notifications.enable_push" />
|
|
|
|
</Checkbox>
|
|
|
|
<h3>
|
|
|
|
<Text id="app.settings.pages.notifications.sounds" />
|
|
|
|
</h3>
|
|
|
|
{SOUNDS_ARRAY.map((key) => (
|
|
|
|
<Checkbox
|
2021-07-10 10:57:29 -04:00
|
|
|
checked={!!enabledSounds[key]}
|
2021-07-05 06:25:20 -04:00
|
|
|
onChange={(enabled) =>
|
|
|
|
dispatch({
|
|
|
|
type: "SETTINGS_SET_NOTIFICATION_OPTIONS",
|
|
|
|
options: {
|
|
|
|
sounds: {
|
|
|
|
...options?.sounds,
|
|
|
|
[key]: enabled,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}>
|
|
|
|
<Text
|
|
|
|
id={`app.settings.pages.notifications.sound.${key}`}
|
|
|
|
/>
|
|
|
|
</Checkbox>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
);
|
2021-06-19 17:37:12 -04:00
|
|
|
}
|
|
|
|
|
2021-07-05 06:23:23 -04:00
|
|
|
export const Notifications = connectState(Component, (state) => {
|
2021-07-05 06:25:20 -04:00
|
|
|
return {
|
|
|
|
options: state.settings.notification,
|
|
|
|
};
|
2021-07-05 06:23:23 -04:00
|
|
|
});
|