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