import { Button, IconButton, List, ListItem, Typography } from "@mui/joy"; import React, { useRef, useState } from "react"; import { toast } from "react-hot-toast"; import { useResourceStore } from "@/store/v1"; import { Resource } from "@/types/proto/api/v1/resource_service"; import { useTranslate } from "@/utils/i18n"; import { generateDialog } from "./Dialog"; import Icon from "./Icon"; interface Props extends DialogProps { onCancel?: () => void; onConfirm?: (resourceList: Resource[]) => void; } interface State { uploadingFlag: boolean; } const CreateResourceDialog: React.FC = (props: Props) => { const t = useTranslate(); const { destroy, onCancel, onConfirm } = props; const resourceStore = useResourceStore(); const [state, setState] = useState({ uploadingFlag: false, }); const [fileList, setFileList] = useState([]); const fileInputRef = useRef(null); const handleReorderFileList = (fileName: string, direction: "up" | "down") => { const fileIndex = fileList.findIndex((file) => file.name === fileName); if (fileIndex === -1) { return; } const newFileList = [...fileList]; if (direction === "up") { if (fileIndex === 0) { return; } const temp = newFileList[fileIndex - 1]; newFileList[fileIndex - 1] = newFileList[fileIndex]; newFileList[fileIndex] = temp; } else if (direction === "down") { if (fileIndex === fileList.length - 1) { return; } const temp = newFileList[fileIndex + 1]; newFileList[fileIndex + 1] = newFileList[fileIndex]; newFileList[fileIndex] = temp; } setFileList(newFileList); }; const handleCloseDialog = () => { if (onCancel) { onCancel(); } destroy(); }; const handleFileInputChange = async () => { if (!fileInputRef.current || !fileInputRef.current.files) { return; } const files: File[] = []; for (const file of fileInputRef.current.files) { files.push(file); } setFileList(files); }; const allowConfirmAction = () => { if (!fileInputRef.current || !fileInputRef.current.files || fileInputRef.current.files.length === 0) { return false; } return true; }; const handleConfirmBtnClick = async () => { if (state.uploadingFlag) { return; } setState((state) => { return { ...state, uploadingFlag: true, }; }); const createdResourceList: Resource[] = []; try { if (!fileInputRef.current || !fileInputRef.current.files) { return; } const filesOnInput = Array.from(fileInputRef.current.files); for (const file of fileList) { const fileOnInput = filesOnInput.find((fileOnInput) => fileOnInput.name === file.name); if (!fileOnInput) { continue; } const { name: filename, size, type } = file; const buffer = new Uint8Array(await file.arrayBuffer()); const resource = await resourceStore.createResource({ resource: Resource.fromPartial({ filename, size, type, content: buffer, }), }); createdResourceList.push(resource); } } catch (error: any) { console.error(error); toast.error(error.details); } if (onConfirm) { onConfirm(createdResourceList); } destroy(); }; return ( <>

{t("resource.create-dialog.title")}

{fileList.map((file, index) => ( {file.name}
))}
); }; function showCreateResourceDialog(props: Omit) { generateDialog( { dialogName: "create-resource-dialog", }, CreateResourceDialog, props, ); } export default showCreateResourceDialog;