Files
DataClaw/frontend/src/pages/Settings.tsx
T

150 lines
5.6 KiB
TypeScript
Raw Normal View History

2026-03-14 15:52:27 +08:00
import { useState, useEffect } from 'react';
2026-03-21 21:26:57 +08:00
import { useTranslation } from 'react-i18next';
2026-03-14 15:52:27 +08:00
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Card, CardContent, CardHeader, CardTitle, CardDescription, CardFooter } from "@/components/ui/card";
2026-03-29 14:44:32 +08:00
import { Save, Loader2 } from "lucide-react";
2026-03-14 15:52:27 +08:00
import { api } from "@/lib/api";
2026-03-14 19:56:34 +08:00
import { useAuthStore } from "@/store/authStore";
2026-03-14 15:52:27 +08:00
export function Settings() {
2026-03-21 21:26:57 +08:00
const { t } = useTranslation();
2026-03-14 19:56:34 +08:00
const { user, updateUser } = useAuthStore();
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [confirmPassword, setConfirmPassword] = useState('');
2026-03-14 15:52:27 +08:00
const [isSaving, setIsSaving] = useState(false);
2026-03-14 19:56:34 +08:00
const [error, setError] = useState('');
const [success, setSuccess] = useState('');
2026-03-14 15:52:27 +08:00
useEffect(() => {
2026-03-14 19:56:34 +08:00
if (user) {
setEmail(user.email || '');
2026-03-14 15:52:27 +08:00
}
2026-03-14 19:56:34 +08:00
}, [user]);
2026-03-29 00:20:53 +08:00
2026-03-14 19:56:34 +08:00
const isPasswordMismatch = password !== '' && confirmPassword !== '' && password !== confirmPassword;
2026-03-14 15:52:27 +08:00
const handleSave = async () => {
2026-03-14 19:56:34 +08:00
setError('');
setSuccess('');
if (isPasswordMismatch) {
2026-03-21 21:26:57 +08:00
setError(t('passwordsDoNotMatch'));
2026-03-14 19:56:34 +08:00
return;
}
2026-03-14 15:52:27 +08:00
setIsSaving(true);
try {
2026-03-14 19:56:34 +08:00
const updateData: any = {
email: email
2026-03-14 15:52:27 +08:00
};
2026-03-14 19:56:34 +08:00
if (password) {
updateData.password = password;
}
2026-03-14 15:52:27 +08:00
2026-03-14 19:56:34 +08:00
if (user && user.id) {
const response = await api.put<any>(`/api/v1/users/${user.id}`, updateData);
2026-03-21 21:26:57 +08:00
let successMsg = t('personalSettingsSaved');
2026-03-14 19:56:34 +08:00
if (password) {
2026-03-21 21:26:57 +08:00
successMsg = t('personalSettingsAndPasswordSaved');
2026-03-14 19:56:34 +08:00
}
setSuccess(successMsg);
setPassword('');
setConfirmPassword('');
// Update global state with new email
updateUser({ email: response.email });
2026-03-14 15:52:27 +08:00
}
2026-03-14 19:56:34 +08:00
} catch (error: any) {
2026-03-14 15:52:27 +08:00
console.error("Failed to save settings", error);
2026-03-21 21:26:57 +08:00
setError(error.message || t('failedToSaveSettings'));
2026-03-14 15:52:27 +08:00
} finally {
setIsSaving(false);
}
};
return (
2026-03-28 16:25:35 +08:00
<div className="flex-1 flex flex-col h-full bg-muted/50/30 overflow-hidden">
<div className="h-14 px-6 flex items-center justify-between border-b border-border bg-background">
<div className="flex items-center gap-2 text-foreground/80 font-medium">
2026-03-14 19:56:34 +08:00
<Save className="h-5 w-5 text-indigo-500" />
2026-03-29 00:20:53 +08:00
{t('personalSettings')}
2026-03-14 19:56:34 +08:00
</div>
2026-03-14 15:52:27 +08:00
</div>
2026-03-14 19:56:34 +08:00
<div className="flex-1 p-6 overflow-auto">
2026-03-29 00:20:53 +08:00
<div className="grid gap-6 max-w-4xl mx-auto">
2026-03-14 19:56:34 +08:00
{error && <div className="text-sm text-red-600 bg-red-50 border border-red-100 rounded-md p-3">{error}</div>}
{success && <div className="text-sm text-emerald-600 bg-emerald-50 border border-emerald-100 rounded-md p-3">{success}</div>}
2026-03-28 16:25:35 +08:00
<Card className="border-border shadow-sm">
2026-03-14 19:56:34 +08:00
<CardHeader>
2026-03-21 21:26:57 +08:00
<CardTitle className="text-xl">{t('accountInfo')}</CardTitle>
<CardDescription>{t('modifyLoginEmailAndPassword')}</CardDescription>
2026-03-14 19:56:34 +08:00
</CardHeader>
<CardContent className="space-y-4">
<div className="space-y-2">
2026-03-21 21:26:57 +08:00
<Label htmlFor="username">{t('username')}</Label>
2026-03-14 19:56:34 +08:00
<Input
id="username"
value={user?.username || ''}
disabled
2026-03-28 16:25:35 +08:00
className="bg-muted/50 text-muted-foreground"
2026-03-14 19:56:34 +08:00
/>
2026-03-28 16:25:35 +08:00
<p className="text-xs text-muted-foreground">{t('usernameCannotBeModified')}</p>
2026-03-14 19:56:34 +08:00
</div>
<div className="space-y-2">
2026-03-21 21:26:57 +08:00
<Label htmlFor="email">{t('emailAddress')}</Label>
2026-03-14 19:56:34 +08:00
<Input
id="email"
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
</div>
2026-03-14 15:52:27 +08:00
2026-03-28 16:25:35 +08:00
<div className="space-y-2 pt-4 border-t border-border">
2026-03-21 21:26:57 +08:00
<Label htmlFor="new-password">{t('newPassword')}</Label>
2026-03-14 19:56:34 +08:00
<Input
id="new-password"
type="password"
2026-03-21 21:26:57 +08:00
placeholder={t('leaveBlankIfNotModifying')}
2026-03-14 19:56:34 +08:00
value={password}
onChange={(e) => {
setPassword(e.target.value);
setError('');
}}
/>
2026-03-14 15:52:27 +08:00
</div>
2026-03-14 19:56:34 +08:00
<div className="space-y-2">
2026-03-21 21:26:57 +08:00
<Label htmlFor="confirm-password">{t('confirmNewPassword')}</Label>
2026-03-14 19:56:34 +08:00
<Input
id="confirm-password"
type="password"
2026-03-21 21:26:57 +08:00
placeholder={t('leaveBlankIfNotModifying')}
2026-03-14 19:56:34 +08:00
value={confirmPassword}
onChange={(e) => {
setConfirmPassword(e.target.value);
setError('');
}}
/>
2026-03-21 21:26:57 +08:00
{isPasswordMismatch && <p className="text-sm text-red-600">{t('passwordsDoNotMatch')}</p>}
2026-03-14 15:52:27 +08:00
</div>
2026-03-14 19:56:34 +08:00
</CardContent>
2026-03-28 16:25:35 +08:00
<CardFooter className="bg-muted/50/50 border-t border-border pt-6">
<Button onClick={handleSave} className="ml-auto bg-indigo-600 hover:bg-indigo-700 text-primary-foreground" disabled={isSaving || isPasswordMismatch}>
2026-03-14 19:56:34 +08:00
{isSaving ? <Loader2 className="h-4 w-4 mr-2 animate-spin" /> : <Save className="h-4 w-4 mr-2" />}
2026-03-29 00:20:53 +08:00
{t('saveSettings')}
</Button>
</CardFooter>
</Card>
2026-03-14 19:56:34 +08:00
</div>
2026-03-14 15:52:27 +08:00
</div>
</div>
);
2026-03-29 14:44:32 +08:00
}