import { observer } from 'mobx-react-lite'; import { toDirective } from '@/components/utils'; import { AppState } from '@/ui_models/app_state'; import { WebApplication } from '@/ui_models/application'; import { useEffect, useState } from 'preact/hooks'; import { isSameDay } from '@/utils'; import { ApplicationEvent } from '@node_modules/@standardnotes/snjs'; import { ConfirmSignoutContainer } from '@/components/ConfirmSignoutModal'; import Authentication from '@/components/AccountMenu/Authentication'; import Footer from '@/components/AccountMenu/Footer'; import User from '@/components/AccountMenu/User'; import Encryption from '@/components/AccountMenu/Encryption'; import Protections from '@/components/AccountMenu/Protections'; import PasscodeLock from '@/components/AccountMenu/PasscodeLock'; import DataBackup from '@/components/AccountMenu/DataBackup'; import ErrorReporting from '@/components/AccountMenu/ErrorReporting'; import { useCallback } from '@node_modules/preact/hooks'; type Props = { appState: AppState; application: WebApplication; }; const AccountMenu = observer(({ application, appState }: Props) => { const getProtectionsDisabledUntil = useCallback((): string | null => { const protectionExpiry = application.getProtectionSessionExpiryDate(); const now = new Date(); if (protectionExpiry > now) { let f: Intl.DateTimeFormat; if (isSameDay(protectionExpiry, now)) { f = new Intl.DateTimeFormat(undefined, { hour: 'numeric', minute: 'numeric' }); } else { f = new Intl.DateTimeFormat(undefined, { weekday: 'long', day: 'numeric', month: 'short', hour: 'numeric', minute: 'numeric' }); } return f.format(protectionExpiry); } return null; }, [application]); const [showLogin, setShowLogin] = useState(false); const [showRegister, setShowRegister] = useState(false); const [encryptionStatusString, setEncryptionStatusString] = useState(undefined); const [isEncryptionEnabled, setIsEncryptionEnabled] = useState(false); const [server, setServer] = useState(application.getHost()); const [isBackupEncrypted, setIsBackupEncrypted] = useState(isEncryptionEnabled); const [protectionsDisabledUntil, setProtectionsDisabledUntil] = useState(getProtectionsDisabledUntil()); const [user, setUser] = useState(application.getUser()); const [hasProtections] = useState(application.hasProtectionSources()); const { notesAndTagsCount } = appState.accountMenu; const closeAccountMenu = () => { appState.accountMenu.closeAccountMenu(); }; // Add the required event observers useEffect(() => { const removeKeyStatusChangedObserver = application.addEventObserver( async () => { setUser(application.getUser()); }, ApplicationEvent.KeyStatusChanged ); const removeProtectionSessionExpiryDateChangedObserver = application.addEventObserver( async () => { setProtectionsDisabledUntil(getProtectionsDisabledUntil()); }, ApplicationEvent.ProtectionSessionExpiryDateChanged ); return () => { removeKeyStatusChangedObserver(); removeProtectionSessionExpiryDateChangedObserver(); }; }, [application, getProtectionsDisabledUntil]); return (
Account
Close
{!showLogin && !showRegister && (
{user && ( )} {hasProtections && ( )}
)}
); }); export const AccountMenuDirective = toDirective( AccountMenu );