'use client'; import { useState, useEffect } from 'react'; import { Typography, Box, Button, TextField, Grid, Card, CardContent, Alert, Snackbar } from '@mui/material'; import SaveIcon from '@mui/icons-material/Save'; import { useTranslation } from 'react-i18next'; export default function BasicSettings({ projectId }) { const { t } = useTranslation(); const [projectInfo, setProjectInfo] = useState({ id: '', name: '', description: '' }); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [success, setSuccess] = useState(false); useEffect(() => { async function fetchProjectInfo() { try { setLoading(true); const response = await fetch(`/api/projects/${projectId}`); if (!response.ok) { throw new Error(t('projects.fetchFailed')); } const data = await response.json(); setProjectInfo(data); } catch (error) { console.error('获取项目信息出错:', error); setError(error.message); } finally { setLoading(false); } } fetchProjectInfo(); }, [projectId, t]); // 处理项目信息变更 const handleProjectInfoChange = e => { const { name, value } = e.target; setProjectInfo(prev => ({ ...prev, [name]: value })); }; // 保存项目信息 const handleSaveProjectInfo = async () => { try { const response = await fetch(`/api/projects/${projectId}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: projectInfo.name, description: projectInfo.description }) }); if (!response.ok) { throw new Error(t('projects.saveFailed')); } setSuccess(true); } catch (error) { console.error('保存项目信息出错:', error); setError(error.message); } }; const handleCloseSnackbar = () => { setSuccess(false); setError(null); }; if (loading) { return {t('common.loading')}; } return ( {t('settings.basicInfo')} {t('settings.saveSuccess')} {error} ); }