Open History to see the last commit
1import { settlePromise } from "@t3tools/client-runtime/state/runtime";
2import { useState } from "react";
3
4import {
5 hasDesktopNotifications,
6 hasNotificationSound,
7 NOTIFICATION_MODE_LABELS,
8 unlockNotificationAudio,
9} from "../../threadNotifications";
10import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select";
11import { SettingsRow } from "./settingsLayout";
12import { searchableSetting } from "./settingsSearch";
13import { useScopedSettings, useUpdateScopedSettings } from "./useScopedSettings";
14
15export function NotificationSettings() {
16 const mode = useScopedSettings((settings) => settings.notificationMode);
17 const updateSettings = useUpdateScopedSettings();
18 const [permissionMessage, setPermissionMessage] = useState<string | null>(null);
19 const [requesting, setRequesting] = useState(false);
20
21 return (
22 <SettingsRow
23 {...searchableSetting("thread-notifications")}
24 description={
25 permissionMessage ??
26 "System alerts when a thread finishes, fails, or needs input or approval. Applies to this device while T3 Code is open."
27 }
28 control={
29 <Select
30 value={mode}
31 disabled={requesting}
32 onValueChange={async (value) => {
33 if (
34 value !== "off" &&
35 value !== "notifications" &&
36 value !== "sound" &&
37 value !== "notifications-and-sound"
38 )
39 return;
40 setPermissionMessage(null);
41 if (hasNotificationSound(value)) unlockNotificationAudio();
42 if (hasDesktopNotifications(value)) {
43 if (typeof Notification === "undefined" || !window.isSecureContext) {
44 setPermissionMessage(
45 "Notifications need a supported browser over HTTPS, or the desktop app. Sound only is still available.",
46 );
47 return;
48 }
49 setRequesting(true);
50 const permission = await settlePromise(() => Notification.requestPermission());
51 setRequesting(false);
52 if (permission._tag === "Failure") {
53 setPermissionMessage(
54 "Notifications are unavailable in this browser. Sound only is still available.",
55 );
56 return;
57 }
58 if (permission.value !== "granted") {
59 setPermissionMessage(
60 "Allow notifications in your browser or system settings, then choose this option again. Sound only is still available.",
61 );
62 return;
63 }
64 }
65 updateSettings({ notificationMode: value });
66 }}
67 >
68 <SelectTrigger size="sm" className="w-full sm:w-56" aria-label="Thread notifications">
69 <SelectValue>{NOTIFICATION_MODE_LABELS[mode]}</SelectValue>
70 </SelectTrigger>
71 <SelectPopup align="end" alignItemWithTrigger={false}>
72 {Object.entries(NOTIFICATION_MODE_LABELS).map(([value, label]) => (
73 <SelectItem key={value} hideIndicator value={value}>
74 {label}
75 </SelectItem>
76 ))}
77 </SelectPopup>
78 </Select>
79 }
80 />
81 );
82}