import type { FC } from '../../../lib/teact/teact'; import React, { memo, useCallback, useEffect, useState, } from '../../../lib/teact/teact'; import { getActions, getGlobal, withGlobal } from '../../../global'; import type { ApiWebSession } from '../../../api/types'; import buildClassName from '../../../util/buildClassName'; import { formatPastTimeShort } from '../../../util/dates/dateFormat'; import useFlag from '../../../hooks/useFlag'; import useHistoryBack from '../../../hooks/useHistoryBack'; import useOldLang from '../../../hooks/useOldLang'; import Avatar from '../../common/Avatar'; import FullNameTitle from '../../common/FullNameTitle'; import ConfirmDialog from '../../ui/ConfirmDialog'; import ListItem from '../../ui/ListItem'; import SettingsActiveWebsite from './SettingsActiveWebsite'; import styles from './SettingsActiveWebsites.module.scss'; type OwnProps = { isActive?: boolean; onReset: () => void; }; type StateProps = { byHash: Record; orderedHashes: string[]; }; const SettingsActiveWebsites: FC = ({ isActive, byHash, orderedHashes, onReset, }) => { const { terminateWebAuthorization, terminateAllWebAuthorizations, } = getActions(); const lang = useOldLang(); const [isConfirmTerminateAllDialogOpen, openConfirmTerminateAllDialog, closeConfirmTerminateAllDialog] = useFlag(); const [openedWebsiteHash, setOpenedWebsiteHash] = useState(); const [isModalOpen, openModal, closeModal] = useFlag(); const handleTerminateAuthClick = useCallback((hash: string) => { terminateWebAuthorization({ hash }); }, [terminateWebAuthorization]); const handleTerminateAllAuth = useCallback(() => { closeConfirmTerminateAllDialog(); terminateAllWebAuthorizations(); }, [closeConfirmTerminateAllDialog, terminateAllWebAuthorizations]); const handleOpenSessionModal = useCallback((hash: string) => { setOpenedWebsiteHash(hash); openModal(); }, [openModal]); const handleCloseWebsiteModal = useCallback(() => { setOpenedWebsiteHash(undefined); closeModal(); }, [closeModal]); // Close when empty useEffect(() => { if (!orderedHashes.length) { onReset(); } }, [onReset, orderedHashes]); useHistoryBack({ isActive, onBack: onReset, }); function renderSessions(sessionHashes: string[]) { return (

{lang('WebSessionsTitle')}

{sessionHashes.map(renderSession)}
); } function renderSession(sessionHash: string) { const session = byHash[sessionHash]; const bot = getGlobal().users.byId[session.botId]; return ( { handleTerminateAuthClick(session.hash); }, }]} // eslint-disable-next-line react/jsx-no-bind onClick={() => handleOpenSessionModal(session.hash)} >
{formatPastTimeShort(lang, session.dateActive * 1000)} {bot && } {session.domain}, {session.browser}, {session.platform} {session.ip} {session.region}
); } if (!orderedHashes.length) return undefined; return (
{lang('AuthSessions.LogOutApplications')}

{lang('ClearOtherWebSessionsHelp')}

{renderSessions(orderedHashes)}
); }; export default memo(withGlobal( (global): StateProps => { const { byHash, orderedHashes } = global.activeWebSessions; return { byHash, orderedHashes, }; }, )(SettingsActiveWebsites));