The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(webapp): keep the notification permission row in step with Send test notification
Teknium664202cc2234
diff --git a/apps/desktop/src/app/settings/notifications-settings.browser-permission.test.tsx b/apps/desktop/src/app/settings/notifications-settings.browser-permission.test.tsxnew file mode 100644index 00000000000..b591f5d7364--- /dev/null+++ b/apps/desktop/src/app/settings/notifications-settings.browser-permission.test.tsx@@ -0,0 +1,67 @@+// Both Settings actions that can ask for browser notification permission must+// leave the permission row showing the answer, not the snapshot it mounted with.+import { act, cleanup, fireEvent, render, screen } from '@testing-library/react'+import { MemoryRouter } from 'react-router'+import { afterEach, beforeEach, expect, it, vi } from 'vitest'++import { NotificationsSettings } from './notifications-settings'++vi.mock('@/lib/host-capabilities', () => ({ hostHas: (name: string) => name === 'browserNotifications' }))+vi.mock('@/store/notifications', () => ({ notify: vi.fn() }))+vi.mock('@/store/native-notifications', async () => {+ const { atom } = await import('nanostores')++ return {+ $nativeNotifyPrefs: atom({ enabled: true, kinds: {} }),+ NATIVE_NOTIFICATION_KINDS: [],+ sendTestNativeNotification: vi.fn(async () => true),+ setNativeNotifyEnabled: vi.fn(),+ setNativeNotifyKind: vi.fn()+ }+})++class FakeNotification {+ static answer: NotificationPermission = 'granted'+ static permission: NotificationPermission = 'default'+ static requestPermission = vi.fn(async () => {+ FakeNotification.permission = FakeNotification.answer++ return FakeNotification.permission+ })+}++beforeEach(() => {+ FakeNotification.permission = 'default'+ vi.stubGlobal('Notification', FakeNotification)+ vi.stubGlobal('isSecureContext', true)+})++afterEach(() => {+ cleanup()+ vi.unstubAllGlobals()+})++function renderAlerts() {+ render(+ <MemoryRouter>+ <NotificationsSettings subpage="alerts" />+ </MemoryRouter>+ )+}++it.each([+ ['granted', /allowed/i],+ ['denied', /blocked/i]+] as const)('the permission row follows an answer given through Send test notification (%s)', async (answer, state) => {+ FakeNotification.answer = answer+ renderAlerts()+ expect(screen.getByRole('button', { name: 'Allow' })).toBeTruthy()++ await act(async () => {+ fireEvent.click(screen.getByRole('button', { name: 'Send test notification' }))+ })++ expect(FakeNotification.requestPermission).toHaveBeenCalled()+ expect(screen.queryByRole('button', { name: 'Allow' })).toBeNull()+ expect(screen.getByText(state)).toBeTruthy()+})diff --git a/apps/desktop/src/app/settings/notifications-settings.tsx b/apps/desktop/src/app/settings/notifications-settings.tsxindex ea709b718c4..966e10ebf9e 100644--- a/apps/desktop/src/app/settings/notifications-settings.tsx+++ b/apps/desktop/src/app/settings/notifications-settings.tsx@@ -1,15 +1,19 @@import { useStore } from '@nanostores/react'-import { type ReactNode, useState } from 'react'+import { type ReactNode, useEffect } from 'react'import { Button } from '@/components/ui/button'import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'import { useI18n } from '@/i18n'-import { browserNotificationPermission, requestBrowserNotificationPermission } from '@/lib/browser-notifications'import { COMPLETION_SOUND_VARIANTS, previewCompletionSound } from '@/lib/completion-sound'import { triggerHaptic } from '@/lib/haptics'import { hostHas } from '@/lib/host-capabilities'import { Bell, Play } from '@/lib/icons'import { cn } from '@/lib/utils'+import {+ $browserNotificationPermission,+ askBrowserNotificationPermission,+ refreshBrowserNotificationPermission+} from '@/store/browser-notification-permission'import { $completionSoundVariantId, setCompletionSoundVariantId } from '@/store/completion-sound'import { $hapticsMuted, setHapticsMuted } from '@/store/haptics'import {@@ -32,19 +36,22 @@ function Caption({ children, className }: { children: ReactNode; className?: strreturn <p className={cn(CAPTION, className)}>{children}</p>}-/** Webapp only: the browser's notification permission, asked from this click- * and never on page load. The tab title flashes whatever the answer. */+/** Webapp only: the browser's notification permission, asked from a click here+ * or on Send test notification, never on page load. The tab title flashes+ * whatever the answer. */function BrowserPermissionRow() {const { t } = useI18n()const copy = t.settings.notifications.browserPermission- const [permission, setPermission] = useState(browserNotificationPermission)+ const permission = useStore($browserNotificationPermission)++ useEffect(refreshBrowserNotificationPermission, [])return (<ListRowaction={permission === 'default' ? (<Button- onClick={() => void requestBrowserNotificationPermission().then(setPermission)}+ onClick={() => void askBrowserNotificationPermission()}size="sm"type="button"variant="outline"@@ -79,7 +86,7 @@ export function NotificationsSettings({ subpage }: NotificationsSettingsProps =triggerHaptic('open')if (hostHas('browserNotifications')) {- await requestBrowserNotificationPermission()+ await askBrowserNotificationPermission()}const ok = await sendTestNativeNotification(copy.testTitle, copy.testBody)diff --git a/apps/desktop/src/store/browser-notification-permission.ts b/apps/desktop/src/store/browser-notification-permission.tsnew file mode 100644index 00000000000..a24b99b5ef4--- /dev/null+++ b/apps/desktop/src/store/browser-notification-permission.ts@@ -0,0 +1,29 @@+/**+ * The Webapp's browser notification permission as one store, so every control+ * that can ask (the Allow button, Send test notification) and every row that+ * shows the answer agree. The user can also change it in the browser's site+ * settings, so a view refreshes it when it opens.+ */++import { atom } from 'nanostores'++import {+ browserNotificationPermission,+ type BrowserNotificationPermission,+ requestBrowserNotificationPermission+} from '@/lib/browser-notifications'++export const $browserNotificationPermission = atom<BrowserNotificationPermission>(browserNotificationPermission())++export function refreshBrowserNotificationPermission(): void {+ $browserNotificationPermission.set(browserNotificationPermission())+}++/** Call from a click handler only. */+export async function askBrowserNotificationPermission(): Promise<BrowserNotificationPermission> {+ const permission = await requestBrowserNotificationPermission()++ $browserNotificationPermission.set(permission)++ return permission+}