feat(knowledge): add knowledge base management with dialog system
- Implement knowledge base list, create, and detail pages - Add dialog provider and components for confirmation and alerts - Include knowledge card and grid view components - Enhance header with user menu and logout functionality - Implement knowledge operations hooks for CRUD operations
This commit is contained in:
162
src/components/Provider/DialogComponent.tsx
Normal file
162
src/components/Provider/DialogComponent.tsx
Normal file
@@ -0,0 +1,162 @@
|
||||
import React, { useState } from 'react';
|
||||
import {
|
||||
Dialog,
|
||||
DialogTitle,
|
||||
DialogContent,
|
||||
DialogActions,
|
||||
Button,
|
||||
Typography,
|
||||
Box,
|
||||
IconButton,
|
||||
} from '@mui/material';
|
||||
import {
|
||||
Close as CloseIcon,
|
||||
Info as InfoIcon,
|
||||
CheckCircle as SuccessIcon,
|
||||
Warning as WarningIcon,
|
||||
Error as ErrorIcon,
|
||||
Help as ConfirmIcon,
|
||||
} from '@mui/icons-material';
|
||||
import { type IDialogInstance } from '../../interfaces/common';
|
||||
|
||||
interface DialogComponentProps {
|
||||
dialog: IDialogInstance;
|
||||
onClose: (result: boolean) => void;
|
||||
}
|
||||
|
||||
const DialogComponent: React.FC<DialogComponentProps> = ({ dialog, onClose }) => {
|
||||
const [loading, setLoading] = useState(false);
|
||||
const { config } = dialog;
|
||||
|
||||
// 获取对话框图标
|
||||
const getDialogIcon = () => {
|
||||
const iconProps = { sx: { fontSize: 24, mr: 1 } };
|
||||
|
||||
switch (config.type) {
|
||||
case 'info':
|
||||
return <InfoIcon {...iconProps} color="info" />;
|
||||
case 'success':
|
||||
return <SuccessIcon {...iconProps} color="success" />;
|
||||
case 'warning':
|
||||
return <WarningIcon {...iconProps} color="warning" />;
|
||||
case 'error':
|
||||
return <ErrorIcon {...iconProps} color="error" />;
|
||||
case 'confirm':
|
||||
return <ConfirmIcon {...iconProps} color="warning" />;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
// 获取确认按钮颜色
|
||||
const getConfirmButtonColor = () => {
|
||||
switch (config.type) {
|
||||
case 'error':
|
||||
case 'warning':
|
||||
return 'error';
|
||||
case 'success':
|
||||
return 'success';
|
||||
case 'info':
|
||||
return 'info';
|
||||
default:
|
||||
return 'primary';
|
||||
}
|
||||
};
|
||||
|
||||
// 处理确认操作
|
||||
const handleConfirm = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
|
||||
if (config.onConfirm) {
|
||||
await config.onConfirm();
|
||||
}
|
||||
|
||||
onClose(true);
|
||||
} catch (error) {
|
||||
console.error('Dialog confirm error:', error);
|
||||
// 即使出错也关闭对话框,但返回false
|
||||
onClose(false);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
// 处理取消操作
|
||||
const handleCancel = () => {
|
||||
if (config.onCancel) {
|
||||
config.onCancel();
|
||||
}
|
||||
onClose(false);
|
||||
};
|
||||
|
||||
// 处理遮罩点击
|
||||
const handleBackdropClick = () => {
|
||||
if (config.maskClosable !== false) {
|
||||
handleCancel();
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog
|
||||
open={true}
|
||||
onClose={handleBackdropClick}
|
||||
maxWidth="sm"
|
||||
fullWidth
|
||||
PaperProps={{
|
||||
sx: {
|
||||
width: config.width || 'auto',
|
||||
maxWidth: config.width || '500px',
|
||||
},
|
||||
}}
|
||||
>
|
||||
{/* 标题栏 */}
|
||||
<DialogTitle sx={{ display: 'flex', alignItems: 'center', pr: 1 }}>
|
||||
<Box sx={{ display: 'flex', alignItems: 'center', flex: 1 }}>
|
||||
{getDialogIcon()}
|
||||
<Typography variant="h6" component="span">
|
||||
{config.title || '提示'}
|
||||
</Typography>
|
||||
</Box>
|
||||
<IconButton
|
||||
onClick={handleCancel}
|
||||
size="small"
|
||||
sx={{ ml: 1 }}
|
||||
>
|
||||
<CloseIcon />
|
||||
</IconButton>
|
||||
</DialogTitle>
|
||||
|
||||
{/* 内容区域 */}
|
||||
<DialogContent>
|
||||
<Typography variant="body1" sx={{ py: 1 }}>
|
||||
{config.content}
|
||||
</Typography>
|
||||
</DialogContent>
|
||||
|
||||
{/* 操作按钮 */}
|
||||
<DialogActions sx={{ px: 3, pb: 2 }}>
|
||||
{config.showCancel !== false && (
|
||||
<Button
|
||||
onClick={handleCancel}
|
||||
variant="outlined"
|
||||
disabled={loading}
|
||||
>
|
||||
{config.cancelText || '取消'}
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
onClick={handleConfirm}
|
||||
variant="contained"
|
||||
color={getConfirmButtonColor() as any}
|
||||
disabled={loading}
|
||||
sx={{ ml: 1 }}
|
||||
>
|
||||
{loading ? '处理中...' : (config.confirmText || '确定')}
|
||||
</Button>
|
||||
</DialogActions>
|
||||
</Dialog>
|
||||
);
|
||||
};
|
||||
|
||||
export default DialogComponent;
|
||||
Reference in New Issue
Block a user