import { useEffect, useState } from 'react'; import { Divider, Popover } from 'antd'; import clsx from 'clsx'; import { DiscIcon, FileBoxIcon, LoaderCircleIcon, PackageIcon, PackageSearchIcon, XIcon } from 'lucide-react'; import { useTranslation } from 'react-i18next'; import * as api from '@/api/storage'; import { client } from '@/lib/websocket.ts'; export const Storage = () => { const { t } = useTranslation(); const [isPopoverOpen, setIsPopoverOpen] = useState(false); const [isLoading, setIsLoading] = useState(false); const [files, setFiles] = useState([]); const [mountingFile, setMountingFile] = useState(''); const [mountedFile, setMountedFile] = useState(''); useEffect(() => { getFiles(); }, []); function handleOpenChange(open: boolean) { if (open) { getFiles(); } setIsPopoverOpen(open); } // 获取镜像列表 function getFiles() { if (isLoading) return; setIsLoading(true); api .getImages() .then((rsp) => { if (rsp.code !== 0) { return; } const images = rsp.data.files; if (!images?.length) { setFiles([]); return; } setFiles(images); // 获取已挂载的镜像 api.getMountedImage().then((rsp) => { if (rsp.code === 0 && rsp.data?.file && images.includes(rsp.data.file)) { setMountedFile(rsp.data.file); } }); }) .finally(() => { setIsLoading(false); }); } // 挂载/取消挂载 function mountFile(file: string) { if (mountingFile) return; setMountingFile(file); client.close(); const filename = mountedFile === file ? '' : file; api .mountImage(filename) .then((rsp) => { if (rsp.code !== 0) { console.log(rsp.msg); return; } setMountedFile(filename); }) .finally(() => { setMountingFile(''); client.connect(); }); } const content = (
{t('images')}
{isLoading ? (
{t('loading')}
) : files.length === 0 ? (
{t('empty')}
) : ( files.map((file) => (
mountFile(file)} > {mountedFile === file ? ( <>
) : mountingFile === file ? (
) : (
)} {file.replace(/^.*[\\/]/, '')}
)) )}
); return (
); };