'use client';
import { useState, useMemo } from 'react';
import {
Dialog,
DialogContent,
DialogActions,
Button,
Box,
Typography,
TextField,
Card,
CardActionArea,
Chip,
IconButton,
Tooltip,
InputAdornment,
CircularProgress,
DialogTitle,
DialogContentText
} from '@mui/material';
import CloseIcon from '@mui/icons-material/Close';
import SearchIcon from '@mui/icons-material/Search';
import StorageIcon from '@mui/icons-material/Storage';
import { useTranslation } from 'react-i18next';
import { alpha, useTheme } from '@mui/material/styles';
import { StyledDialogTitle } from './ImportDialog.styles';
import { DATA_SETS } from '../constants';
export default function BuiltinDatasetDialog({ open, onClose, projectId, onSuccess }) {
const { t, i18n } = useTranslation();
const theme = useTheme();
const [keyword, setKeyword] = useState('');
const [selectedDataset, setSelectedDataset] = useState(null);
const [confirmOpen, setConfirmOpen] = useState(false);
const [downloading, setDownloading] = useState(false);
const isZh = i18n.language.startsWith('zh');
// 过滤数据集
const filteredDatasets = useMemo(() => {
if (!keyword) return DATA_SETS;
const lowerKeyword = keyword.toLowerCase();
return DATA_SETS.filter(
ds =>
ds.zh.toLowerCase().includes(lowerKeyword) ||
ds.en.toLowerCase().includes(lowerKeyword) ||
ds.type.toLowerCase().includes(lowerKeyword)
);
}, [keyword]);
const handleCardClick = dataset => {
setSelectedDataset(dataset);
setConfirmOpen(true);
};
const handleConfirmClose = () => {
setConfirmOpen(false);
setSelectedDataset(null);
};
const handleImport = async () => {
if (!selectedDataset) return;
setDownloading(true);
setConfirmOpen(false);
try {
const cdnUrl = `https://raw.githubusercontent.com/ConardLi/easy-dataset-eval/main/${selectedDataset.file}`;
const response = await fetch(cdnUrl);
if (!response.ok) {
throw new Error(`Failed to fetch dataset: ${response.statusText}`);
}
const jsonData = await response.blob();
const formData = new FormData();
const file = new File([jsonData], `${selectedDataset.en}.json`, { type: 'application/json' });
formData.append('file', file);
formData.append('questionType', selectedDataset.type);
const tags = `[${selectedDataset.level}] ${selectedDataset.en}`;
formData.append('tags', tags);
const importResponse = await fetch(`/api/projects/${projectId}/eval-datasets/import`, {
method: 'POST',
body: formData
});
const result = await importResponse.json();
if (result.code === 0) {
onSuccess?.(result.data);
handleClose();
} else {
console.error(result.error);
alert(result.error || t('evalDatasets.import.failed'));
}
} catch (error) {
console.error('Import failed:', error);
alert(error.message || t('evalDatasets.import.failed'));
} finally {
setDownloading(false);
setSelectedDataset(null);
}
};
const handleClose = () => {
if (downloading) return;
setKeyword('');
setSelectedDataset(null);
setConfirmOpen(false);
onClose();
};
return (
<>
>
);
}