Files
cinny/src/app/hooks/useNotificationsQuiet.ts
T
Lotus CIandClaude Opus 5.5 c6419044cb
CI / Build & Quality Checks (pull_request) Successful in 4m7s
CI / Trigger Desktop Build (pull_request) Skipped
CI / Docker image build & smoke test (pull_request) Skipped
CI / Secret scan (gitleaks) (pull_request) Successful in 11s
CI / Playwright smoke (e2e) (pull_request) Successful in 13m45s
fix(notifications): quiet hours start and end while the app stays open (#325)
useNotificationsQuiet evaluated the quiet-hours window (and the
"Pause notifications" expiry) only at render time, and the long-lived
message/invite notifiers rarely re-render, so someone leaving Lotus open
overnight with quiet hours 22:00–07:00 got every sound all night (and a
30-minute pause could outlast itself). The hook now re-evaluates every
30 s.

Verified headless (notifications granted, window unfocused): quiet
hours set to start ~75 s after the app opened; a DM before the window
notified; one inside the window, with the app left alone: before it
still notified, now it's silent.

Fixes #325

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017PCb59uY3UKqDqt7B6hzd5
2026-10-16 22:19:24 -04:00

71 lines
2.5 KiB
TypeScript

import { useEffect, useState } from 'react';
import { useAtomValue } from 'jotai';
import { focusAssistActiveAtom } from '../state/focusAssist';
import { manualDndAtom } from '../state/manualDnd';
import { isSnoozeActive, notificationSnoozeUntilAtom } from '../state/notificationSnooze';
import { isWithinTimeWindow } from '../utils/timeWindow';
import { useSetting } from '../state/hooks/settings';
import { settingsAtom } from '../state/settings';
const QUIET_TICK_MS = 30_000;
export type NotificationsQuietInputs = {
focusAssistActive: boolean;
manualDnd: boolean;
snoozeUntil: number;
quietHoursEnabled: boolean;
quietHoursStart: string;
quietHoursEnd: string;
now?: Date;
};
// Gitea #28 — pure "should we make noise right now?" predicate, extracted from
// the gate that `ClientNonUIFeatures` already applies to message/invite sounds
// (Focus Assist, manual tray DND, the snooze, and the quiet-hours window) so the
// incoming-call ringtone (and anything else) can honour the same rules.
export function isNotificationsQuiet({
focusAssistActive,
manualDnd,
snoozeUntil,
quietHoursEnabled,
quietHoursStart,
quietHoursEnd,
now,
}: NotificationsQuietInputs): boolean {
return (
focusAssistActive ||
manualDnd ||
isSnoozeActive(snoozeUntil, now?.getTime()) ||
(quietHoursEnabled && isWithinTimeWindow(quietHoursStart, quietHoursEnd, now))
);
}
// Live-atoms/settings-backed version of `isNotificationsQuiet` for use in
// components.
export function useNotificationsQuiet(): boolean {
// [Gitea #325] The quiet-hours window and the snooze expiry depend on the
// clock, but the long-lived notifiers using this hook rarely re-render, so a
// window starting (or a 30-minute pause ending) while the app stayed open was
// never noticed. Re-evaluate on a tick, like the night-light overlay.
const [, setTick] = useState(0);
useEffect(() => {
const id = setInterval(() => setTick((t) => t + 1), QUIET_TICK_MS);
return () => clearInterval(id);
}, []);
const focusAssistActive = useAtomValue(focusAssistActiveAtom);
const manualDnd = useAtomValue(manualDndAtom);
const snoozeUntil = useAtomValue(notificationSnoozeUntilAtom);
const [quietHoursEnabled] = useSetting(settingsAtom, 'quietHoursEnabled');
const [quietHoursStart] = useSetting(settingsAtom, 'quietHoursStart');
const [quietHoursEnd] = useSetting(settingsAtom, 'quietHoursEnd');
return isNotificationsQuiet({
focusAssistActive,
manualDnd,
snoozeUntil,
quietHoursEnabled,
quietHoursStart,
quietHoursEnd,
});
}