import { useEffect, useState } from "react"; import { Storage } from "../network/models.ts"; import { network } from "../network/network.ts"; import showToast from "../components/toast.ts"; import Loading from "../components/loading.tsx"; import { MdAdd, MdMoreHoriz } from "react-icons/md"; import { ErrorAlert } from "../components/alert.tsx"; import { useTranslation } from "../utils/i18n"; import { app } from "../app.ts"; import showPopup, { PopupMenuItem } from "../components/popup.tsx"; import Badge from "../components/badge.tsx"; export default function StorageView() { const { t } = useTranslation(); const [storages, setStorages] = useState(null); const [loadingId, setLoadingId] = useState(null); useEffect(() => { if (app.user == null || !app.user.is_admin) { return; } network.listStorages().then((response) => { if (response.success) { setStorages(response.data!); } else { showToast({ message: response.message, type: "error", }); } }); }, []); if (!app.user) { return ( ); } if (!app.user?.is_admin) { return ( ); } if (storages == null) { return ; } const updateStorages = async () => { setStorages(null); const response = await network.listStorages(); if (response.success) { setStorages(response.data!); } else { showToast({ message: response.message, type: "error", }); } }; const handleDelete = async (id: number) => { if (loadingId != null) { return; } setLoadingId(id); const response = await network.deleteStorage(id); if (response.success) { showToast({ message: t("Storage deleted successfully"), }); updateStorages(); } else { showToast({ message: response.message, type: "error", }); } setLoadingId(null); }; const handleSetDefault = async (id: number) => { if (loadingId != null) { return; } setLoadingId(id); const response = await network.setDefaultStorage(id); if (response.success) { showToast({ message: t("Storage set as default successfully"), }); updateStorages(); } else { showToast({ message: response.message, type: "error", }); } setLoadingId(null); }; return ( <>
{t("No storage found. Please create a new storage.")}
{storages.map((s) => { return ( ); })}
{t("Name")} {t("Created At")} {t("Space")} {t("Action")}
{s.name} {s.isDefault && ( {t("Default")} )} {new Date(s.createdAt).toLocaleString()} {(s.currentSize / 1024 / 1024).toFixed(2)} /{" "} {s.maxSize / 1024 / 1024} MB

{t("Delete Storage")}

{t( "Are you sure you want to delete this storage? This action cannot be undone.", )}

); } enum StorageType { local, s3, } function NewStorageDialog({ onAdded }: { onAdded: () => void }) { const { t } = useTranslation(); const [storageType, setStorageType] = useState(null); const [params, setParams] = useState({ name: "", path: "", endPoint: "", accessKeyID: "", secretAccessKey: "", bucketName: "", maxSizeInMB: 0, domain: "", }); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(""); const handleSubmit = async () => { if (storageType == null) { return; } setIsLoading(true); let response; if (storageType === StorageType.local) { if (params.path === "" || params.name === "" || params.maxSizeInMB <= 0) { setError(t("All fields are required")); setIsLoading(false); return; } response = await network.createLocalStorage( params.name, params.path, params.maxSizeInMB, ); } else if (storageType === StorageType.s3) { if ( params.endPoint === "" || params.accessKeyID === "" || params.secretAccessKey === "" || params.bucketName === "" || params.name === "" || params.maxSizeInMB <= 0 ) { setError(t("All fields are required")); setIsLoading(false); return; } response = await network.createS3Storage( params.name, params.endPoint, params.accessKeyID, params.secretAccessKey, params.bucketName, params.maxSizeInMB, params.domain, ); } if (response!.success) { showToast({ message: t("Storage created successfully"), }); onAdded(); const dialog = document.getElementById( "new_storage_dialog", ) as HTMLDialogElement; dialog.close(); } else { setError(response!.message); } setIsLoading(false); }; return ( <>

{t("New Storage")}

{t("Type")}

{ setStorageType(null); }} /> { setStorageType(StorageType.local); }} /> { setStorageType(StorageType.s3); }} />
{storageType === StorageType.local && ( <> )} {storageType === StorageType.s3 && ( <> )} {error !== "" && }
); }