/* eslint-disable react/jsx-no-bind */ import type { FC } from '../../../lib/teact/teact'; import React, { memo, useCallback, useMemo, useState, } from '../../../lib/teact/teact'; import { getActions, withGlobal } from '../../../global'; import type { ApiSession } from '../../../api/types'; import { formatPastTimeShort } from '../../../util/dateFormat'; import useFlag from '../../../hooks/useFlag'; import useLang from '../../../hooks/useLang'; import useHistoryBack from '../../../hooks/useHistoryBack'; import getSessionIcon from './helpers/getSessionIcon'; import ListItem from '../../ui/ListItem'; import ConfirmDialog from '../../ui/ConfirmDialog'; import RadioGroup from '../../ui/RadioGroup'; import SettingsActiveSession from './SettingsActiveSession'; import './SettingsActiveSessions.scss'; type OwnProps = { isActive?: boolean; onReset: () => void; }; type StateProps = { byHash: Record; orderedHashes: string[]; ttlDays?: number; }; const SettingsActiveSessions: FC = ({ isActive, onReset, byHash, orderedHashes, ttlDays, }) => { const { terminateAuthorization, terminateAllAuthorizations, changeSessionTtl, } = getActions(); const lang = useLang(); const [isConfirmTerminateAllDialogOpen, openConfirmTerminateAllDialog, closeConfirmTerminateAllDialog] = useFlag(); const [openedSessionHash, setOpenedSessionHash] = useState(); const [isModalOpen, openModal, closeModal] = useFlag(); const autoTerminateValue = useMemo(() => { // eslint-disable-next-line max-len // https://github.com/DrKLO/Telegram/blob/96dce2c9aabc33b87db61d830aa087b6b03fe397/TMessagesProj/src/main/java/org/telegram/ui/SessionsActivity.java#L195 if (ttlDays === undefined) { return undefined; } if (ttlDays <= 7) { return '7'; } if (ttlDays <= 30) { return '30'; } if (ttlDays <= 93) { return '90'; } if (ttlDays <= 183) { return '183'; } if (ttlDays > 183) { return '365'; } return undefined; }, [ttlDays]); const AUTO_TERMINATE_OPTIONS = useMemo(() => { const options = [{ label: lang('Weeks', 1, 'i'), value: '7', }, { label: lang('Months', 1, 'i'), value: '30', }, { label: lang('Months', 3, 'i'), value: '90', }, { label: lang('Months', 6, 'i'), value: '183', }]; if (ttlDays && ttlDays >= 365) { options.push({ label: lang('Years', 1, 'i'), value: '365', }); } return options; }, [lang, ttlDays]); const handleTerminateSessionClick = useCallback((hash: string) => { terminateAuthorization({ hash }); }, [terminateAuthorization]); const handleTerminateAllSessions = useCallback(() => { closeConfirmTerminateAllDialog(); terminateAllAuthorizations(); }, [closeConfirmTerminateAllDialog, terminateAllAuthorizations]); const handleOpenSessionModal = useCallback((hash: string) => { setOpenedSessionHash(hash); openModal(); }, [openModal]); const handleCloseSessionModal = useCallback(() => { setOpenedSessionHash(undefined); closeModal(); }, [closeModal]); const handleChangeSessionTtl = useCallback((value: string) => { changeSessionTtl({ days: Number(value) }); }, [changeSessionTtl]); const currentSession = useMemo(() => { const currentSessionHash = orderedHashes.find((hash) => byHash[hash].isCurrent); return currentSessionHash ? byHash[currentSessionHash] : undefined; }, [byHash, orderedHashes]); const otherSessionHashes = useMemo(() => { return orderedHashes.filter((hash) => !byHash[hash].isCurrent); }, [byHash, orderedHashes]); const hasOtherSessions = Boolean(otherSessionHashes.length); useHistoryBack({ isActive, onBack: onReset, }); function renderCurrentSession(session: ApiSession) { return (

{lang('AuthSessions.CurrentSession')}

{session.deviceModel} {session.appName} {session.appVersion}, {session.platform} {session.systemVersion} {session.ip} - {getLocation(session)}
{hasOtherSessions && ( {lang('TerminateAllSessions')} )}
); } function renderOtherSessions(sessionHashes: string[]) { return (

{lang('OtherSessions')}

{sessionHashes.map(renderSession)}
); } function renderAutoTerminate() { return (

{lang('TerminateOldSessionHeader')}

{lang('IfInactiveFor')}

); } function renderSession(sessionHash: string) { const session = byHash[sessionHash]; return ( { handleTerminateSessionClick(session.hash); }, }]} icon={`device-${getSessionIcon(session)} icon-device`} onClick={() => { handleOpenSessionModal(session.hash); }} >
{formatPastTimeShort(lang, session.dateActive * 1000)} {session.deviceModel} {session.appName} {session.appVersion}, {session.platform} {session.systemVersion} {session.ip} {getLocation(session)}
); } return (
{currentSession && renderCurrentSession(currentSession)} {hasOtherSessions && renderOtherSessions(otherSessionHashes)} {renderAutoTerminate()} {hasOtherSessions && ( )}
); }; function getLocation(session: ApiSession) { return [session.region, session.country].filter(Boolean).join(', '); } export default memo(withGlobal( (global): StateProps => global.activeSessions, )(SettingsActiveSessions));