import { Button } from "@mui/joy"; import copy from "copy-to-clipboard"; import { useEffect } from "react"; import { useTranslation } from "react-i18next"; import useLoading from "../hooks/useLoading"; import { useResourceStore } from "../store/module"; import { getResourceUrl } from "../utils/resource"; import Icon from "./Icon"; import toastHelper from "./Toast"; import Dropdown from "./common/Dropdown"; import { generateDialog } from "./Dialog"; import { showCommonDialog } from "./Dialog/CommonDialog"; import showPreviewImageDialog from "./PreviewImageDialog"; import showCreateResourceDialog from "./CreateResourceDialog"; import showChangeResourceFilenameDialog from "./ChangeResourceFilenameDialog"; import "../less/resources-dialog.less"; type Props = DialogProps; const ResourcesDialog: React.FC = (props: Props) => { const { destroy } = props; const { t } = useTranslation(); const loadingState = useLoading(); const resourceStore = useResourceStore(); const resources = resourceStore.state.resources; useEffect(() => { resourceStore .fetchResourceList() .catch((error) => { console.error(error); toastHelper.error(error.response.data.message); }) .finally(() => { loadingState.setFinish(); }); }, []); const handlePreviewBtnClick = (resource: Resource) => { const resourceUrl = getResourceUrl(resource); if (resource.type.startsWith("image")) { showPreviewImageDialog( resources.filter((r) => r.type.startsWith("image")).map((r) => getResourceUrl(r)), resources.findIndex((r) => r.id === resource.id) ); } else { window.open(resourceUrl); } }; const handleRenameBtnClick = (resource: Resource) => { showChangeResourceFilenameDialog(resource.id, resource.filename); }; const handleCopyResourceLinkBtnClick = (resource: Resource) => { const url = getResourceUrl(resource); copy(url); toastHelper.success(t("message.succeed-copy-resource-link")); }; const handleDeleteUnusedResourcesBtnClick = () => { let warningText = t("resources.warning-text-unused"); const unusedResources = resources.filter((resource) => { if (resource.linkedMemoAmount === 0) { warningText = warningText + `\n- ${resource.filename}`; return true; } return false; }); if (unusedResources.length === 0) { toastHelper.success(t("resources.no-unused-resources")); return; } showCommonDialog({ title: t("resources.delete-resource"), content: warningText, style: "warning", dialogName: "delete-unused-resources", onConfirm: async () => { for (const resource of unusedResources) { await resourceStore.deleteResourceById(resource.id); } }, }); }; const handleDeleteResourceBtnClick = (resource: Resource) => { let warningText = t("resources.warning-text"); if (resource.linkedMemoAmount > 0) { warningText = warningText + `\n${t("resources.linked-amount")}: ${resource.linkedMemoAmount}`; } showCommonDialog({ title: t("resources.delete-resource"), content: warningText, style: "warning", dialogName: "delete-resource-dialog", onConfirm: async () => { await resourceStore.deleteResourceById(resource.id); }, }); }; return ( <>

{t("sidebar.resources")}

{loadingState.isLoading ? (

{t("resources.fetching-data")}

) : (
ID {t("resources.name")}
{resources.length === 0 ? (

{t("resources.no-resources")}

) : ( resources.map((resource) => (
{resource.id} handleRenameBtnClick(resource)}> {resource.filename}
} />
)) )}
)}
); }; export default function showResourcesDialog() { generateDialog( { className: "resources-dialog", dialogName: "resources-dialog", }, ResourcesDialog, {} ); }