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