2026-10-16 10:50:29 -07:00
import { del , get , put } from "./api.js" ;
import { errorText , h , replace , showAlert , showConfirm } from "./dom.js" ;
2026-10-14 21:25:01 -07:00
import { on , state } from "./state.js" ;
const kinds = [
2026-10-16 08:35:49 -07:00
{ key : "questions" , label : "Questions" },
2026-10-14 21:25:01 -07:00
{ key : "reports" , label : "Turn summaries" },
{ key : "errors" , label : "Session errors" },
{ key : "rate_limits" , label : "Rate limits" },
];
2026-10-15 22:21:20 -07:00
export function mountSettings ( root ) {
2026-10-14 21:25:01 -07:00
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 ) {
2026-10-14 22:42:46 -07:00
void showAlert ( errorText ( err ));
2026-10-14 21:25:01 -07:00
}
renderPrefs ();
};
return h ( "label" , {}, box , " " , label );
}));
};
2026-10-15 18:34:53 -07:00
const renderDevice = () => {
if ( ! ( "Notification" in window )) {
replace ( device , h ( "p" , { class : "hint" }, "This browser doesn't support notifications." ));
2026-10-14 21:25:01 -07:00
return ;
}
2026-10-15 18:34:53 -07:00
if ( Notification . permission === "denied" ) {
replace ( device , h ( "p" , { class : "hint" }, "Notifications are blocked for this site in the browser's settings." ));
2026-10-14 21:25:01 -07:00
return ;
}
2026-10-15 18:34:53 -07:00
if ( Notification . permission === "granted" ) {
replace ( device , h ( "p" , {}, "This browser shows notifications while a byre page is open." ));
2026-10-14 21:25:01 -07:00
return ;
}
2026-10-15 18:34:53 -07:00
const button = h ( "button" , {}, "Turn on for this browser" );
2026-10-14 21:25:01 -07:00
button . onclick = async () => {
button . disabled = true ;
2026-10-15 18:34:53 -07:00
await Notification . requestPermission ();
renderDevice ();
2026-10-14 21:25:01 -07:00
};
2026-10-15 18:34:53 -07:00
replace ( device , h ( "p" , {}, "This browser doesn't show notifications." ), button );
2026-10-14 21:25:01 -07:00
};
2026-10-16 10:50:29 -07:00
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 ();
}
}),
];
2026-10-14 21:25:01 -07:00
renderPrefs ();
2026-10-15 18:34:53 -07:00
renderDevice ();
2026-10-16 10:50:29 -07:00
void loadNotes ();
return () => {
loads ++ ;
for ( const off of offs ) {
off ();
}
};
2026-10-14 21:25:01 -07:00
}
//# sourceMappingURL=settings.js.map