// Copyright 2025 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import React from 'react'; import type { LocalizerType } from '../types/Util.js'; import { getMidnight, type NotificationProfileIdString, type NotificationProfileOverride, type NotificationProfileType, } from '../types/NotificationProfile.js'; import { DAY, HOUR, SECOND } from '../util/durations/index.js'; import { formatTimestamp } from '../util/formatTimestamp.js'; import { AxoDropdownMenu } from '../axo/AxoDropdownMenu.js'; import { tw } from '../axo/tw.js'; import { ProfileAvatar } from './PreferencesNotificationProfiles.js'; import { AxoSymbol } from '../axo/AxoSymbol.js'; export type Props = Readonly<{ activeProfileId: NotificationProfileIdString | undefined; allProfiles: ReadonlyArray; currentOverride: NotificationProfileOverride | undefined; i18n: LocalizerType; isOpen: boolean; loading: boolean; onClose: () => void; onGoToSettings: () => void; trigger: React.ReactNode; setProfileOverride: ( id: NotificationProfileIdString, enabled: boolean, endsAtMs?: number ) => void; }>; function getSixPm() { const midnight = getMidnight(Date.now()); return midnight + 18 * HOUR; } function getEightAm() { const midnight = getMidnight(Date.now()); return midnight + 8 * HOUR; } function getEightAmTomorrow() { const midnight = getMidnight(Date.now() + DAY); return midnight + 8 * HOUR; } export function NotificationProfilesMenu({ activeProfileId, allProfiles, currentOverride, i18n, isOpen, loading, onClose, onGoToSettings, trigger, setProfileOverride, }: Props): JSX.Element { const enabledOverrideEndTime = currentOverride?.enabled?.endsAtMs; const [now, setNow] = React.useState(Date.now()); const [cachedProfiles, setCachedProfiles] = React.useState< ReadonlyArray >([]); React.useEffect(() => { if (!loading) { setCachedProfiles(allProfiles); } }, [loading, allProfiles]); let enabledLabel; if (activeProfileId && enabledOverrideEndTime) { enabledLabel = (
{i18n('icu:NotificationProfileMenu--on-with-end', { endTime: formatTimestamp(enabledOverrideEndTime, { timeStyle: 'short', }), })}
); } else if (activeProfileId) { enabledLabel =
{i18n('icu:NotificationProfileMenu--on')}
; } const profilesToRender = loading ? cachedProfiles : allProfiles; const sixPm = getSixPm(); const eightAm = getEightAm(); const eightAmTomorrow = getEightAmTomorrow(); let targetTime = sixPm; if (now < eightAm) { targetTime = eightAm; } else if (now > sixPm) { targetTime = eightAmTomorrow; } React.useEffect(() => { const interval = setInterval(() => { setNow(Date.now()); }, 30 * SECOND); return () => { clearInterval(interval); }; }, []); return ( { if (!open) { onClose(); } }} > {trigger}
{i18n('icu:NotificationProfileMenu--header')}
{enabledLabel}
{profilesToRender.length > 0 ? ( ) : undefined} {profilesToRender.map((profile, index) => { const isActive = activeProfileId && profile.id === activeProfileId; return ( {index > 0 && (
)} } onSelect={event => { event.preventDefault(); setProfileOverride(profile.id, !isActive); }} > {profile.name} {isActive ? ( { event.preventDefault(); setProfileOverride(profile.id, true, Date.now() + HOUR); }} > {i18n('icu:NotificationProfileMenu--for-one-hour')} ) : null} {isActive ? ( { event.preventDefault(); setProfileOverride(profile.id, true, targetTime); }} > {i18n('icu:NotificationProfileMenu--until-time', { time: formatTimestamp(targetTime, { timeStyle: 'short', }), })} ) : null} ); })} { event.preventDefault(); onGoToSettings(); }} customIcon={ } > {i18n('icu:NotificationProfileMenu--settings')} ); }