354 lines
12 KiB
TypeScript
354 lines
12 KiB
TypeScript
/**
|
|
* User profile page for Prediqo web.
|
|
* Displays account information and self-service edits for personal data, password, and 2FA.
|
|
*/
|
|
import {
|
|
Alert,
|
|
Avatar,
|
|
Box,
|
|
Button,
|
|
Card,
|
|
CardContent,
|
|
Chip,
|
|
Divider,
|
|
Paper,
|
|
Skeleton,
|
|
Snackbar,
|
|
Stack,
|
|
Tab,
|
|
Tabs,
|
|
Typography,
|
|
} from '@mui/material';
|
|
import PersonRoundedIcon from '@mui/icons-material/PersonRounded';
|
|
import EditRoundedIcon from '@mui/icons-material/EditRounded';
|
|
import SecurityRoundedIcon from '@mui/icons-material/SecurityRounded';
|
|
import BadgeRoundedIcon from '@mui/icons-material/BadgeRounded';
|
|
import dayjs from 'dayjs';
|
|
import { useState } from 'react';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { useAuth } from '../../shared/auth/AuthContext';
|
|
import { PageHeader, PageSectionHeader, RefreshIconButton } from '../../shared/components/PageHeader';
|
|
import { ProfileEditForm } from './components/ProfileEditForm';
|
|
import { ChangePasswordForm } from './components/ChangePasswordForm';
|
|
import { TwoFactorPanel } from './components/TwoFactorPanel';
|
|
import { SecurityNotificationsPanel } from './components/SecurityNotificationsPanel';
|
|
import { EmailVerificationPanel } from './components/EmailVerificationPanel';
|
|
import { ReferralPanel } from './components/ReferralPanel';
|
|
import { displayName, useProfile } from './hooks/useProfile';
|
|
|
|
/** Profile route with personal info, account metadata, and security settings. */
|
|
export function ProfilePage() {
|
|
const { t } = useTranslation();
|
|
const { auth } = useAuth();
|
|
const profileQ = useProfile();
|
|
const [tab, setTab] = useState(0);
|
|
const [editing, setEditing] = useState(false);
|
|
const [toast, setToast] = useState<string | null>(null);
|
|
|
|
const profile = profileQ.data;
|
|
const name = profile ? displayName(profile) : '—';
|
|
|
|
const refresh = async () => {
|
|
await profileQ.refetch();
|
|
setToast(t('common.profileRefreshed'));
|
|
};
|
|
|
|
return (
|
|
<Box sx={{ minWidth: 0 }}>
|
|
<PageHeader
|
|
icon={<PersonRoundedIcon color="primary" sx={{ fontSize: 36 }} />}
|
|
title={t('profile.title')}
|
|
subtitle={t('profile.subtitle')}
|
|
mb={2.5}
|
|
actions={
|
|
<RefreshIconButton
|
|
label={t('profile.refreshProfile')}
|
|
onClick={() => void refresh()}
|
|
disabled={profileQ.isLoading}
|
|
/>
|
|
}
|
|
/>
|
|
|
|
{profileQ.isError && (
|
|
<Alert severity="error" sx={{ mb: 2, borderRadius: 2 }}>
|
|
{t('profile.loadError')}
|
|
</Alert>
|
|
)}
|
|
|
|
{/* Hero */}
|
|
<Paper
|
|
sx={{
|
|
p: { xs: 2.5, md: 3 },
|
|
mb: 2.5,
|
|
borderRadius: 4,
|
|
border: '1px solid rgba(214,178,94,0.25)',
|
|
background:
|
|
'linear-gradient(135deg, rgba(214,178,94,0.16) 0%, rgba(124,92,255,0.1) 60%, rgba(18,23,36,0.92) 100%)',
|
|
}}
|
|
>
|
|
{profileQ.isLoading ? (
|
|
<Stack direction="row" spacing={2} sx={{ alignItems: 'center' }}>
|
|
<Skeleton variant="circular" width={72} height={72} />
|
|
<Box sx={{ flex: 1 }}>
|
|
<Skeleton width={180} height={32} />
|
|
<Skeleton width={240} height={24} sx={{ mt: 1 }} />
|
|
</Box>
|
|
</Stack>
|
|
) : profile ? (
|
|
<Stack direction={{ xs: 'column', sm: 'row' }} spacing={2.5} sx={{ alignItems: 'center' }}>
|
|
<Avatar
|
|
sx={{
|
|
width: 72,
|
|
height: 72,
|
|
fontSize: 28,
|
|
fontWeight: 900,
|
|
background: 'linear-gradient(135deg, #D6B25E, #7C5CFF)',
|
|
}}
|
|
>
|
|
{name.charAt(0).toUpperCase()}
|
|
</Avatar>
|
|
<Box sx={{ flex: 1, minWidth: 0, textAlign: { xs: 'center', sm: 'left' } }}>
|
|
<Typography variant="h5" sx={{ fontWeight: 900 }}>
|
|
{name}
|
|
</Typography>
|
|
<Typography sx={{ opacity: 0.8, mt: 0.5 }}>{profile.email ?? '—'}</Typography>
|
|
<Stack
|
|
direction="row"
|
|
spacing={1}
|
|
useFlexGap
|
|
sx={{ mt: 1.5, flexWrap: 'wrap', justifyContent: { xs: 'center', sm: 'flex-start' } }}
|
|
>
|
|
<Chip size="small" label={`${t('common.role')}: ${profile.role}`} color="primary" variant="outlined" />
|
|
<Chip
|
|
size="small"
|
|
label={profile.emailVerified ? t('profile.emailVerified') : t('profile.emailNotVerified')}
|
|
color={profile.emailVerified ? 'success' : 'default'}
|
|
variant="outlined"
|
|
/>
|
|
<Chip
|
|
size="small"
|
|
label={profile.totpEnabled ? t('profile.twoFaEnabled') : t('profile.twoFaOff')}
|
|
color={profile.totpEnabled ? 'success' : 'warning'}
|
|
variant="outlined"
|
|
/>
|
|
</Stack>
|
|
</Box>
|
|
</Stack>
|
|
) : null}
|
|
</Paper>
|
|
|
|
<Tabs
|
|
value={tab}
|
|
onChange={(_, v) => {
|
|
setTab(v);
|
|
setEditing(false);
|
|
}}
|
|
variant="scrollable"
|
|
scrollButtons="auto"
|
|
allowScrollButtonsMobile
|
|
sx={{ mb: 2, borderBottom: '1px solid rgba(255,255,255,0.08)' }}
|
|
>
|
|
<Tab icon={<BadgeRoundedIcon />} iconPosition="start" label={t('profile.tabPersonal')} />
|
|
<Tab icon={<SecurityRoundedIcon />} iconPosition="start" label={t('profile.tabSecurity')} />
|
|
</Tabs>
|
|
|
|
{tab === 0 && (
|
|
<Box
|
|
sx={{
|
|
display: 'grid',
|
|
gap: 2.5,
|
|
gridTemplateColumns: { xs: '1fr', lg: '1.4fr 1fr' },
|
|
}}
|
|
>
|
|
<Card sx={{ borderRadius: 3 }}>
|
|
<CardContent sx={{ p: 2.5 }}>
|
|
<PageSectionHeader
|
|
title={t('profile.personalInfo')}
|
|
actions={
|
|
profile && !editing ? (
|
|
<Button
|
|
size="small"
|
|
startIcon={<EditRoundedIcon />}
|
|
onClick={() => setEditing(true)}
|
|
sx={{ flexShrink: 0 }}
|
|
>
|
|
{t('common.edit')}
|
|
</Button>
|
|
) : undefined
|
|
}
|
|
/>
|
|
{profileQ.isLoading ? (
|
|
<Stack spacing={1.5}>
|
|
{Array.from({ length: 5 }).map((_, i) => (
|
|
<Skeleton key={i} height={40} />
|
|
))}
|
|
</Stack>
|
|
) : profile ? (
|
|
<ProfileEditForm
|
|
profile={profile}
|
|
editing={editing}
|
|
onCancel={() => setEditing(false)}
|
|
onSaved={(msg) => {
|
|
setEditing(false);
|
|
setToast(msg);
|
|
}}
|
|
onError={setToast}
|
|
/>
|
|
) : null}
|
|
</CardContent>
|
|
</Card>
|
|
|
|
<Stack spacing={2.5}>
|
|
<Card sx={{ borderRadius: 3 }}>
|
|
<CardContent sx={{ p: 2.5 }}>
|
|
<Typography variant="h6" sx={{ fontWeight: 800, mb: 2 }}>
|
|
{t('profile.emailVerification')}
|
|
</Typography>
|
|
{profileQ.isLoading ? (
|
|
<Skeleton height={80} />
|
|
) : profile ? (
|
|
<EmailVerificationPanel
|
|
email={profile.email}
|
|
emailVerified={profile.emailVerified}
|
|
onNotify={setToast}
|
|
/>
|
|
) : null}
|
|
</CardContent>
|
|
</Card>
|
|
|
|
<Card sx={{ borderRadius: 3 }}>
|
|
<CardContent sx={{ p: 2.5 }}>
|
|
<Typography variant="h6" sx={{ fontWeight: 800, mb: 2 }}>
|
|
{t('profile.referralProgram')}
|
|
</Typography>
|
|
<ReferralPanel onNotify={setToast} />
|
|
</CardContent>
|
|
</Card>
|
|
|
|
<Card sx={{ borderRadius: 3 }}>
|
|
<CardContent sx={{ p: 2.5 }}>
|
|
<Typography variant="h6" sx={{ fontWeight: 800, mb: 2 }}>
|
|
{t('profile.accountDetails')}
|
|
</Typography>
|
|
{profileQ.isLoading ? (
|
|
<Stack spacing={1.5}>
|
|
<Skeleton height={36} />
|
|
<Skeleton height={36} />
|
|
<Skeleton height={36} />
|
|
</Stack>
|
|
) : profile ? (
|
|
<Stack spacing={2} divider={<Divider sx={{ opacity: 0.1 }} />}>
|
|
<MetaRow label={t('profile.userId')} value={profile.id} mono />
|
|
<MetaRow label={t('common.role')} value={profile.role} />
|
|
<MetaRow
|
|
label={t('profile.memberSince')}
|
|
value={dayjs(profile.createdAt).format('YYYY-MM-DD HH:mm')}
|
|
/>
|
|
<MetaRow
|
|
label={t('profile.emailVerification')}
|
|
value={profile.emailVerified ? t('common.yes') : t('common.no')}
|
|
/>
|
|
<MetaRow
|
|
label="2FA"
|
|
value={profile.totpEnabled ? t('profile.enabled') : t('profile.disabled')}
|
|
/>
|
|
</Stack>
|
|
) : (
|
|
<MetaRow label={t('profile.userId')} value={auth.userId ?? '—'} mono />
|
|
)}
|
|
</CardContent>
|
|
</Card>
|
|
</Stack>
|
|
</Box>
|
|
)}
|
|
|
|
{tab === 1 && (
|
|
<Box
|
|
sx={{
|
|
display: 'grid',
|
|
gap: 2.5,
|
|
gridTemplateColumns: { xs: '1fr', md: 'repeat(2, 1fr)' },
|
|
}}
|
|
>
|
|
<Card sx={{ borderRadius: 3 }}>
|
|
<CardContent sx={{ p: 2.5 }}>
|
|
<Typography variant="h6" sx={{ fontWeight: 800, mb: 0.5 }}>
|
|
{t('profile.changePassword')}
|
|
</Typography>
|
|
<Typography variant="body2" sx={{ opacity: 0.7, mb: 2 }}>
|
|
{t('profile.changePasswordHint')}
|
|
</Typography>
|
|
<ChangePasswordForm onSuccess={setToast} onError={setToast} />
|
|
</CardContent>
|
|
</Card>
|
|
|
|
<Card sx={{ borderRadius: 3 }}>
|
|
<CardContent sx={{ p: 2.5 }}>
|
|
<Typography variant="h6" sx={{ fontWeight: 800, mb: 0.5 }}>
|
|
{t('profile.twoFactorAuth')}
|
|
</Typography>
|
|
<Typography variant="body2" sx={{ opacity: 0.7, mb: 2 }}>
|
|
{t('profile.twoFactorHint')}
|
|
</Typography>
|
|
{profile ? (
|
|
<TwoFactorPanel
|
|
totpEnabled={profile.totpEnabled}
|
|
onSuccess={setToast}
|
|
onError={setToast}
|
|
/>
|
|
) : (
|
|
<Skeleton height={120} />
|
|
)}
|
|
</CardContent>
|
|
</Card>
|
|
</Box>
|
|
)}
|
|
|
|
{tab === 1 && (
|
|
<Card sx={{ borderRadius: 3, mt: 2.5 }}>
|
|
<CardContent sx={{ p: 2.5 }}>
|
|
<SecurityNotificationsPanel />
|
|
</CardContent>
|
|
</Card>
|
|
)}
|
|
|
|
<Snackbar
|
|
open={!!toast}
|
|
autoHideDuration={4000}
|
|
onClose={() => setToast(null)}
|
|
message={toast}
|
|
anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }}
|
|
/>
|
|
</Box>
|
|
);
|
|
}
|
|
|
|
function MetaRow({
|
|
label,
|
|
value,
|
|
mono,
|
|
}: {
|
|
label: string;
|
|
value: string;
|
|
mono?: boolean;
|
|
}) {
|
|
return (
|
|
<Box>
|
|
<Typography variant="caption" sx={{ opacity: 0.6 }}>
|
|
{label}
|
|
</Typography>
|
|
<Typography
|
|
sx={{
|
|
fontWeight: 600,
|
|
mt: 0.25,
|
|
fontFamily: mono ? 'monospace' : undefined,
|
|
fontSize: mono ? 12 : undefined,
|
|
wordBreak: 'break-all',
|
|
}}
|
|
>
|
|
{value}
|
|
</Typography>
|
|
</Box>
|
|
);
|
|
}
|