import { useState } from 'react' import { Badge, Box, Button, Center, Divider, Group, PasswordInput, SegmentedControl, Stack, Text, useMantineColorScheme, } from '@mantine/core' import { useForm } from '@mantine/form' import { notifications } from '@mantine/notifications' import { IconDeviceDesktop, IconMoon, IconSun } from '@tabler/icons-react' import dayjs from 'dayjs' import { authApi } from '@/api/auth' import { useAuth } from '@/hooks/useAuth' import { PageHeader } from '@/components/ui/PageHeader' import { Panel } from '@/components/ui/Panel' import { ConfirmModal } from '@/components/ui/ConfirmModal' const STRENGTH = [ { label: 'Слабый', color: 'red' }, { label: 'Средний', color: 'orange' }, { label: 'Хороший', color: 'lime' }, { label: 'Надёжный', color: 'green' }, ] // 0 — пусто, 1..4 — уровень function strengthOf(p: string) { if (!p) return 0 let s = 1 if (p.length >= 10) s++ if (/[A-ZА-ЯЁ]/.test(p) && /[a-zа-яё]/.test(p)) s++ if (/\d/.test(p) && /[^\p{L}\d\s]/u.test(p)) s++ return s } function PasswordStrength({ value }: { value: string }) { const level = strengthOf(value) if (level === 0) return null const meta = STRENGTH[level - 1] return ( {STRENGTH.map((_, i) => ( ))} {meta.label} ) } function Row({ label, children }: { label: string; children: React.ReactNode }) { return ( {label} {children} ) } export default function SettingsPage() { const { user, logout } = useAuth() const { colorScheme, setColorScheme } = useMantineColorScheme() const [loading, setLoading] = useState(false) const [pending, setPending] = useState(null) const role = user?.is_root ? 'Суперадмин' : user?.is_observer ? 'Наблюдатель' : 'Администратор' const form = useForm({ initialValues: { current_password: '', new_password: '', confirm_password: '' }, validate: { current_password: (v) => (v ? null : 'Введите текущий пароль'), new_password: (v, values) => { if (!v) return 'Введите новый пароль' if (v.length < 6) return 'Минимум 6 символов' if (v === values.current_password) return 'Новый пароль совпадает с текущим' return null }, confirm_password: (v, values) => (v !== values.new_password ? 'Пароли не совпадают' : null), }, }) const handleConfirm = async () => { setLoading(true) try { await authApi.updateProfile({ password: form.values.new_password, }) setPending(null) form.reset() notifications.show({ title: 'Пароль изменён', message: 'Сейчас вы будете перенаправлены на страницу входа', color: 'green', }) setTimeout(() => logout(), 1500) } catch (e: any) { setPending(null) notifications.show({ title: 'Ошибка', message: e.response?.data?.error_description || 'Не удалось изменить пароль', color: 'red', }) } finally { setLoading(false) } } return ( {role}}> {user?.login || '—'} {user?.last_login && ( <> {dayjs(user.last_login).format('D MMM YYYY, HH:mm')} )} {user?.last_ip_login && ( <> {user.last_ip_login} )} Тема интерфейса «Системная» следует настройкам вашей ОС setColorScheme(v as 'light' | 'dark' | 'auto')} data={[ { value: 'light', label:
Светлая
}, { value: 'dark', label:
Тёмная
}, { value: 'auto', label:
Системная
}, ]} />
setPending(v.new_password))}> После смены пароля потребуется войти заново
setPending(null)} onConfirm={handleConfirm} title="Изменить пароль?" message="Вы будете разлогинены на всех устройствах и должны будете войти заново." confirmLabel="Изменить" danger loading={loading} />
) }