import { Dialog } from "@kobalte/core/dialog" import { Component, Show, createEffect, createResource } from "solid-js" import { showToastNotification } from "../lib/notifications" import { getOsNotificationCapability, requestOsNotificationPermission, type OsNotificationPermission, } from "../lib/os-notifications" import { useConfig } from "../stores/preferences" interface NotificationsSettingsModalProps { open: boolean onClose: () => void } function formatPermissionLabel(permission: OsNotificationPermission): string { switch (permission) { case "granted": return "Granted" case "denied": return "Denied" case "default": return "Not granted" case "unsupported": return "Unsupported" default: return String(permission) } } const NotificationsSettingsModal: Component = (props) => { const { preferences, updatePreferences } = useConfig() const [capability, { refetch }] = createResource(() => getOsNotificationCapability()) createEffect(() => { if (props.open) { void refetch() } }) const handleEnableToggle = async (enabled: boolean) => { if (!enabled) { updatePreferences({ osNotificationsEnabled: false }) return } const cap = capability() if (cap && !cap.supported) { showToastNotification({ title: "Notifications", message: cap.info ?? "OS notifications are not supported in this environment.", variant: "warning", }) updatePreferences({ osNotificationsEnabled: false }) return } const permission = await requestOsNotificationPermission() if (permission !== "granted") { showToastNotification({ title: "Notifications", message: permission === "denied" ? "Notification permission denied. Enable notifications in your system/browser settings." : "Notification permission not granted.", variant: "warning", }) updatePreferences({ osNotificationsEnabled: false }) return } updatePreferences({ osNotificationsEnabled: true }) void refetch() } const handleRequestPermission = async () => { const cap = capability() if (cap && !cap.supported) { showToastNotification({ title: "Notifications", message: cap.info ?? "Notifications are not supported in this environment.", variant: "warning", }) return } const permission = await requestOsNotificationPermission() if (permission === "granted") { showToastNotification({ title: "Notifications", message: "Permission granted. You can now enable notifications.", variant: "success", duration: 6000, }) void refetch() return } showToastNotification({ title: "Notifications", message: permission === "denied" ? "Permission denied. You may need to enable notifications in your system/browser settings." : "Permission not granted.", variant: "warning", }) void refetch() } const supported = () => capability()?.supported ?? false const permissionLabel = () => formatPermissionLabel(capability()?.permission ?? "unsupported") const infoMessage = () => capability()?.info return ( !open && props.onClose()}>
Notifications

Session Status Notifications

Enable
Permission: {permissionLabel()}
Request permission
Notify when app is focused
{infoMessage()}
Notifications are not supported in this environment. The bell icon stays disabled.
Notify me when
Session needs input
Session becomes idle
) } export default NotificationsSettingsModal