Files
byre/web/dist/settings.js
T

60 lines
2.4 KiB
JavaScript

import { put } from "./api.js";
import { errorText, h, replace, showAlert } from "./dom.js";
import { on, state } from "./state.js";
const kinds = [
{ key: "questions", label: "Questions" },
{ key: "reports", label: "Turn summaries" },
{ key: "errors", label: "Session errors" },
{ key: "rate_limits", label: "Rate limits" },
];
export function mountSettings(root) {
const device = h("div", { class: "push-device" });
const prefs = h("fieldset", { class: "push-prefs" });
const renderPrefs = () => {
const me = state.me;
if (!me) {
replace(prefs);
return;
}
replace(prefs, h("legend", {}, "Notify me about"), ...kinds.map(({ key, label }) => {
const box = h("input", { type: "checkbox", checked: me.notification_prefs[key] });
box.onchange = async () => {
try {
state.me = await put("/api/me/notifications", { body: { ...me.notification_prefs, [key]: box.checked } });
}
catch (err) {
void showAlert(errorText(err));
}
renderPrefs();
};
return h("label", {}, box, " ", label);
}));
};
const renderDevice = () => {
if (!("Notification" in window)) {
replace(device, h("p", { class: "hint" }, "This browser doesn't support notifications."));
return;
}
if (Notification.permission === "denied") {
replace(device, h("p", { class: "hint" }, "Notifications are blocked for this site in the browser's settings."));
return;
}
if (Notification.permission === "granted") {
replace(device, h("p", {}, "This browser shows notifications while a byre page is open."));
return;
}
const button = h("button", {}, "Turn on for this browser");
button.onclick = async () => {
button.disabled = true;
await Notification.requestPermission();
renderDevice();
};
replace(device, h("p", {}, "This browser doesn't show notifications."), button);
};
replace(root, h("div", { class: "settings-view" }, h("h2", {}, "Notifications"), device, prefs));
const off = on("me", renderPrefs);
renderPrefs();
renderDevice();
return off;
}
//# sourceMappingURL=settings.js.map