109 lines
4.1 KiB
JavaScript
109 lines
4.1 KiB
JavaScript
import { del, get, put } from "./api.js";
|
|
import { errorText, h, replace, showAlert, showConfirm } 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);
|
|
};
|
|
const notes = h("div", { class: "lead-notes" });
|
|
let loads = 0;
|
|
const loadNotes = async () => {
|
|
const n = ++loads;
|
|
let list;
|
|
try {
|
|
list = await get("/api/me/lead/notes");
|
|
}
|
|
catch (err) {
|
|
replace(notes, h("p", { class: "hint" }, errorText(err)));
|
|
return;
|
|
}
|
|
if (n !== loads) {
|
|
return;
|
|
}
|
|
if (list.length === 0) {
|
|
replace(notes, h("p", { class: "hint" }, "Your lead hasn't noted anything about how you work yet."));
|
|
return;
|
|
}
|
|
replace(notes, h("ul", {}, ...list.map((note) => {
|
|
const remove = h("button", {}, "delete");
|
|
remove.onclick = async () => {
|
|
if (!(await showConfirm([h("p", {}, "Delete this note? Your lead stops applying it."), h("p", {}, note.text)], "Delete", true, remove))) {
|
|
return;
|
|
}
|
|
try {
|
|
await del("/api/me/lead/notes/{noteId}", { path: { noteId: note.id } });
|
|
}
|
|
catch (err) {
|
|
void showAlert(errorText(err));
|
|
}
|
|
};
|
|
return h("li", {}, h("span", {}, note.text), remove);
|
|
})));
|
|
};
|
|
replace(root, h("div", { class: "settings-view" }, h("h2", {}, "Notifications"), device, prefs, h("h2", {}, "What your lead has learned"), h("p", { class: "hint" }, "Your lead notes your priorities and preferences as it works with you, and starts every session with them."), notes));
|
|
const offs = [
|
|
on("me", renderPrefs),
|
|
on("event", (detail) => {
|
|
const ev = detail;
|
|
if (ev.kind === "lead.note_saved" || ev.kind === "lead.note_deleted") {
|
|
void loadNotes();
|
|
}
|
|
}),
|
|
];
|
|
renderPrefs();
|
|
renderDevice();
|
|
void loadNotes();
|
|
return () => {
|
|
loads++;
|
|
for (const off of offs) {
|
|
off();
|
|
}
|
|
};
|
|
}
|
|
//# sourceMappingURL=settings.js.map
|