import React, { useState, useEffect } from 'react'; import { Box, Typography, CircularProgress, useTheme, useMediaQuery, } from '@mui/material'; import { useTranslation } from 'react-i18next'; import { CardContainer } from '@/components/CardContainer'; import { PageWrapper } from '../PageWrapper'; import { fetchProfile } from '../../api/settingsApi'; import type { LoginLog } from '../../types/settingsApiType'; import { useManualApi } from '@/hooks/useApi'; import { formatDate } from '@/utils/formatSessionDate'; export function RecentLogins() { const { t, i18n } = useTranslation('setting'); const [logs, setLogs] = useState([]); const theme = useTheme(); const isXsup = useMediaQuery(theme.breakpoints.up('xs')); const { data: profileData, loading: isLoading, error: fetchError, execute: executeFetchProfile, } = useManualApi(fetchProfile); useEffect(() => { executeFetchProfile(); }, [i18n.language]); useEffect(() => { if (profileData?.success && Array.isArray(profileData.loginLogs)) { setLogs(profileData.loginLogs); } }, [profileData]); const getErrorMessage = (error: unknown): string | null => { if (!error) return null; if (error instanceof Error) return error.message; return String(error); }; return ( {isLoading ? ( ) : fetchError ? ( {getErrorMessage(fetchError) || t('active.errorFetch')} ) : ( {logs.map((log, index) => ( {formatDate(log.loginDateTime, i18n.language, t)} {`${log.deviceOs} ${log.deviceName}`} {log.ipAddress} {isXsup && index < logs.length - 1 && ( )} ))} )} ); }