|
| 1 | +import { useAtomValue } from "@effect/atom-react"; |
| 2 | +import { useNavigate } from "@tanstack/react-router"; |
| 3 | +import type { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 4 | +import * as Option from "effect/Option"; |
| 5 | +import { useEffect, useRef } from "react"; |
| 6 | + |
| 7 | +import { getClientSettings, useClientSettings } from "../hooks/useSettings"; |
| 8 | +import { useEnvironments } from "../state/environments"; |
| 9 | +import { environmentShell } from "../state/shell"; |
| 10 | +import { |
| 11 | + hasDesktopNotifications, |
| 12 | + hasNotificationSound, |
| 13 | + playNotificationSound, |
| 14 | + unlockNotificationAudio, |
| 15 | +} from "../threadNotifications"; |
| 16 | +import { resolveSidebarThreadStatus } from "./Sidebar.logic"; |
| 17 | + |
| 18 | +export function ThreadNotificationCoordinator() { |
| 19 | + const { environments } = useEnvironments(); |
| 20 | + const mode = useClientSettings((settings) => settings.notificationMode); |
| 21 | + |
| 22 | + useEffect(() => { |
| 23 | + if (!hasNotificationSound(mode)) return; |
| 24 | + document.addEventListener("pointerdown", unlockNotificationAudio); |
| 25 | + document.addEventListener("keydown", unlockNotificationAudio); |
| 26 | + return () => { |
| 27 | + document.removeEventListener("pointerdown", unlockNotificationAudio); |
| 28 | + document.removeEventListener("keydown", unlockNotificationAudio); |
| 29 | + }; |
| 30 | + }, [mode]); |
| 31 | + |
| 32 | + if (mode === "off") return null; |
| 33 | + |
| 34 | + return environments.map((environment) => ( |
| 35 | + <EnvironmentNotifications |
| 36 | + key={environment.environmentId} |
| 37 | + environmentId={environment.environmentId} |
| 38 | + /> |
| 39 | + )); |
| 40 | +} |
| 41 | + |
| 42 | +function EnvironmentNotifications({ environmentId }: { environmentId: EnvironmentId }) { |
| 43 | + const shell = useAtomValue(environmentShell.stateValueAtom(environmentId)); |
| 44 | + const mode = useClientSettings((settings) => settings.notificationMode); |
| 45 | + const navigate = useNavigate(); |
| 46 | + const previous = useRef(new Map<ThreadId, { input: string | null; completion: string | null }>()); |
| 47 | + |
| 48 | + useEffect(() => { |
| 49 | + if (shell.status !== "live" || Option.isNone(shell.snapshot)) { |
| 50 | + previous.current.clear(); |
| 51 | + return; |
| 52 | + } |
| 53 | + const next = new Map<ThreadId, { input: string | null; completion: string | null }>(); |
| 54 | + for (const thread of shell.snapshot.value.threads) { |
| 55 | + const status = resolveSidebarThreadStatus(thread); |
| 56 | + const prior = previous.current.get(thread.id); |
| 57 | + const input = |
| 58 | + status === "input" || status === "approval" |
| 59 | + ? `${thread.latestTurn?.turnId ?? ""}:${status}` |
| 60 | + : null; |
| 61 | + const completion = |
| 62 | + status === "ready" && thread.latestTurn?.state === "completed" |
| 63 | + ? thread.latestTurn.completedAt |
| 64 | + : (prior?.completion ?? null); |
| 65 | + next.set(thread.id, { input, completion }); |
| 66 | + if (!prior || mode === "off" || thread.archivedAt !== null) continue; |
| 67 | + const kind = |
| 68 | + input && input !== prior.input |
| 69 | + ? "input" |
| 70 | + : completion && completion !== prior.completion |
| 71 | + ? "completion" |
| 72 | + : null; |
| 73 | + if (!kind) continue; |
| 74 | + if (hasNotificationSound(mode)) { |
| 75 | + void playNotificationSound(kind, () => |
| 76 | + hasNotificationSound(getClientSettings().notificationMode), |
| 77 | + ); |
| 78 | + } |
| 79 | + if ( |
| 80 | + !hasDesktopNotifications(mode) || |
| 81 | + typeof Notification === "undefined" || |
| 82 | + Notification.permission !== "granted" |
| 83 | + ) |
| 84 | + continue; |
| 85 | + try { |
| 86 | + const notification = new Notification( |
| 87 | + kind === "completion" |
| 88 | + ? "Thread completed" |
| 89 | + : status === "approval" |
| 90 | + ? "Approval needed" |
| 91 | + : "Input needed", |
| 92 | + { body: thread.title, tag: `${environmentId}:${thread.id}`, silent: true }, |
| 93 | + ); |
| 94 | + notification.addEventListener("click", () => { |
| 95 | + notification.close(); |
| 96 | + window.focus(); |
| 97 | + void navigate({ |
| 98 | + to: "/$environmentId/$threadId", |
| 99 | + params: { environmentId, threadId: thread.id }, |
| 100 | + }); |
| 101 | + }); |
| 102 | + } catch { |
| 103 | + // Some browsers expose Notification but reject desktop presentation. |
| 104 | + } |
| 105 | + } |
| 106 | + previous.current = next; |
| 107 | + }, [environmentId, mode, navigate, shell]); |
| 108 | + |
| 109 | + return null; |
| 110 | +} |
0 commit comments