chore: formatting
This commit is contained in:
parent
fec6f48bce
commit
4e947dbaa6
@ -6,81 +6,82 @@ import { unwrap } from "@snort/shared";
|
||||
import { useEffect, useState } from "react";
|
||||
import { Icon } from "./icon";
|
||||
|
||||
export function NotificationsButton({ host, service }: { host: string, service: string }) {
|
||||
const login = useLogin();
|
||||
const publisher = login?.publisher();
|
||||
const [subscribed, setSubscribed] = useState(false);
|
||||
const api = new NostrStreamProvider("", service, publisher);
|
||||
export function NotificationsButton({ host, service }: { host: string; service: string }) {
|
||||
const login = useLogin();
|
||||
const publisher = login?.publisher();
|
||||
const [subscribed, setSubscribed] = useState(false);
|
||||
const api = new NostrStreamProvider("", service, publisher);
|
||||
|
||||
async function isSubscribed() {
|
||||
const reg = await navigator.serviceWorker.ready;
|
||||
if (reg) {
|
||||
const sub = await reg.pushManager.getSubscription();
|
||||
if (sub) {
|
||||
const auth = base64.encode(new Uint8Array(unwrap(sub.getKey("auth"))));
|
||||
const subs = await api.listStreamerSubscriptions(auth);
|
||||
setSubscribed(subs.includes(host));
|
||||
}
|
||||
}
|
||||
async function isSubscribed() {
|
||||
const reg = await navigator.serviceWorker.ready;
|
||||
if (reg) {
|
||||
const sub = await reg.pushManager.getSubscription();
|
||||
if (sub) {
|
||||
const auth = base64.encode(new Uint8Array(unwrap(sub.getKey("auth"))));
|
||||
const subs = await api.listStreamerSubscriptions(auth);
|
||||
setSubscribed(subs.includes(host));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function enableNotifications() {
|
||||
// request permissions to send notifications
|
||||
if ("Notification" in window) {
|
||||
try {
|
||||
if (Notification.permission !== "granted") {
|
||||
const res = await Notification.requestPermission();
|
||||
console.debug(res);
|
||||
}
|
||||
return Notification.permission === "granted";
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
async function enableNotifications() {
|
||||
// request permissions to send notifications
|
||||
if ("Notification" in window) {
|
||||
try {
|
||||
if (Notification.permission !== "granted") {
|
||||
const res = await Notification.requestPermission();
|
||||
console.debug(res);
|
||||
}
|
||||
return false;
|
||||
return Notification.permission === "granted";
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
async function subscribe() {
|
||||
if (await enableNotifications()) {
|
||||
try {
|
||||
if ("serviceWorker" in navigator) {
|
||||
const reg = await navigator.serviceWorker.ready;
|
||||
if (reg && publisher) {
|
||||
const sub = await reg.pushManager.subscribe({
|
||||
userVisibleOnly: true,
|
||||
applicationServerKey: (await api.getNotificationsInfo()).publicKey,
|
||||
});
|
||||
await api.subscribeNotifications({
|
||||
endpoint: sub.endpoint,
|
||||
key: base64.encode(new Uint8Array(unwrap(sub.getKey("p256dh")))),
|
||||
auth: base64.encode(new Uint8Array(unwrap(sub.getKey("auth")))),
|
||||
scope: `${location.protocol}//${location.hostname}`,
|
||||
});
|
||||
await api.addStreamerSubscription(host);
|
||||
setSubscribed(true);
|
||||
}
|
||||
} else {
|
||||
console.warn("No service worker")
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
async function subscribe() {
|
||||
if (await enableNotifications()) {
|
||||
try {
|
||||
if ("serviceWorker" in navigator) {
|
||||
const reg = await navigator.serviceWorker.ready;
|
||||
if (reg && publisher) {
|
||||
const sub = await reg.pushManager.subscribe({
|
||||
userVisibleOnly: true,
|
||||
applicationServerKey: (await api.getNotificationsInfo()).publicKey,
|
||||
});
|
||||
await api.subscribeNotifications({
|
||||
endpoint: sub.endpoint,
|
||||
key: base64.encode(new Uint8Array(unwrap(sub.getKey("p256dh")))),
|
||||
auth: base64.encode(new Uint8Array(unwrap(sub.getKey("auth")))),
|
||||
scope: `${location.protocol}//${location.hostname}`,
|
||||
});
|
||||
await api.addStreamerSubscription(host);
|
||||
setSubscribed(true);
|
||||
}
|
||||
} else {
|
||||
console.warn("No service worker");
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function unsubscribe() {
|
||||
if (publisher) {
|
||||
await api.removeStreamerSubscription(host);
|
||||
setSubscribed(false);
|
||||
}
|
||||
|
||||
async function unsubscribe() {
|
||||
if (publisher) {
|
||||
await api.removeStreamerSubscription(host);
|
||||
setSubscribed(false);
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
isSubscribed().catch(console.error);
|
||||
}, []);
|
||||
useEffect(() => {
|
||||
isSubscribed().catch(console.error);
|
||||
}, []);
|
||||
|
||||
return <AsyncButton onClick={subscribed ? unsubscribe : subscribe}>
|
||||
<Icon name={subscribed ? "bell-off" : "bell-ringing"} />
|
||||
return (
|
||||
<AsyncButton onClick={subscribed ? unsubscribe : subscribe}>
|
||||
<Icon name={subscribed ? "bell-off" : "bell-ringing"} />
|
||||
</AsyncButton>
|
||||
}
|
||||
);
|
||||
}
|
||||
|
@ -318,6 +318,9 @@
|
||||
"YagVIe": {
|
||||
"defaultMessage": "{n}p"
|
||||
},
|
||||
"YwzT/0": {
|
||||
"defaultMessage": "Clip title"
|
||||
},
|
||||
"Z8ZOEY": {
|
||||
"defaultMessage": "This method is insecure. We recommend using a {nostrlink}"
|
||||
},
|
||||
|
@ -105,6 +105,7 @@
|
||||
"XgWvGA": "Reactions",
|
||||
"YPh5Nq": "@ {rate}",
|
||||
"YagVIe": "{n}p",
|
||||
"YwzT/0": "Clip title",
|
||||
"Z8ZOEY": "This method is insecure. We recommend using a {nostrlink}",
|
||||
"ZmqxZs": "You can change this later",
|
||||
"Zse7yG": "Raid target",
|
||||
|
Loading…
x
Reference in New Issue
Block a user