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