import { Alert, Box, DialogContent, IconButton, ListItemText, Menu, Skeleton, Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Typography, } from "@mui/material"; import React, { useCallback, useEffect, useMemo, useState } from "react"; import { useTranslation } from "react-i18next"; import { useAppDispatch, useAppSelector } from "../../../redux/hooks.ts"; import { confirmOperation } from "../../../redux/thunks/dialog.ts"; import { downloadSingleFile } from "../../../redux/thunks/download.ts"; import { setFileVersion } from "../../../redux/thunks/file.ts"; import { openViewers } from "../../../redux/thunks/viewer.ts"; import { sizeToString } from "../../../util"; import AutoHeight from "../../Common/AutoHeight.tsx"; import { closeVersionControlDialog } from "../../../redux/globalStateSlice.ts"; import { Entity, EntityType, FileResponse } from "../../../api/explorer.ts"; import { deleteVersion, getFileInfo } from "../../../api/api.ts"; import { NoWrapTableCell, StyledTableContainerPaper } from "../../Common/StyledComponents.tsx"; import TimeBadge from "../../Common/TimeBadge.tsx"; import { AnonymousUser } from "../../Common/User/UserAvatar.tsx"; import UserBadge from "../../Common/User/UserBadge.tsx"; import DraggableDialog from "../../Dialogs/DraggableDialog.tsx"; import MoreVertical from "../../Icons/MoreVertical.tsx"; import { SquareMenuItem } from "../ContextMenu/ContextMenu.tsx"; import { FileManagerIndex } from "../FileManager.tsx"; const VersionControl = () => { const { t } = useTranslation(); const dispatch = useAppDispatch(); const [anchorEl, setAnchorEl] = useState(null); const [actionTarget, setActionTarget] = useState(null); const [fileExtended, setFileExtended] = useState(undefined); const [loading, setLoading] = useState(false); const open = useAppSelector((state) => state.globalState.versionControlDialogOpen); const target = useAppSelector((state) => state.globalState.versionControlDialogFile); const highlight = useAppSelector((state) => state.globalState.versionControlHighlight); const onClose = useCallback(() => { if (!loading) { dispatch(closeVersionControlDialog()); } }, [dispatch, loading]); useEffect(() => { if (target && open) { setFileExtended(undefined); dispatch( getFileInfo({ uri: target.path, extended: true, }), ).then((res) => setFileExtended(res)); } }, [target, open]); const versionEntities = useMemo(() => { return fileExtended?.extended_info?.entities?.filter((e) => e.type == EntityType.version); }, [fileExtended?.extended_info?.entities]); const hilightButNotFound = useMemo(() => { return highlight && fileExtended?.extended_info && !versionEntities?.some((e) => e.id == highlight); }, [highlight, fileExtended?.extended_info?.entities]); const handleActionClose = () => { setAnchorEl(null); }; const handleOpenAction = (event: React.MouseEvent, element: Entity) => { setAnchorEl(event.currentTarget); setActionTarget(element); }; const downloadEntity = useCallback(() => { if (!target || !actionTarget) { return; } dispatch(downloadSingleFile(target, actionTarget.id)); setAnchorEl(null); }, [target, actionTarget, dispatch]); const openEntity = useCallback(() => { if (!target || !actionTarget) { return; } dispatch(openViewers(FileManagerIndex.main, target, actionTarget.size, actionTarget.id)); setAnchorEl(null); }, [target, actionTarget, dispatch]); const setAsCurrent = useCallback(() => { if (!target || !actionTarget) { return; } setLoading(true); dispatch(setFileVersion(FileManagerIndex.main, target, actionTarget.id)) .then(() => { setFileExtended((prev) => prev ? { ...prev, primary_entity: actionTarget.id, } : undefined, ); }) .finally(() => { setLoading(false); }); setAnchorEl(null); }, [target, actionTarget, setLoading, dispatch]); const deleteTargetVersion = useCallback(() => { if (!target || !actionTarget) { return; } dispatch(confirmOperation(t("fileManager.deleteVersionWarning"))).then(() => { setLoading(true); dispatch( deleteVersion({ uri: target.path, version: actionTarget.id, }), ) .then(() => { setFileExtended((prev) => prev ? { ...prev, extended_info: prev.extended_info ? { ...prev.extended_info, entities: prev.extended_info.entities?.filter((e) => e.id !== actionTarget.id), } : undefined, } : undefined, ); }) .finally(() => { setLoading(false); }); }); setAnchorEl(null); }, [t, target, actionTarget, setLoading, dispatch]); return ( <> {t("application:fileManager.open")} {t("application:fileManager.download")} {target?.owned && actionTarget?.id !== fileExtended?.primary_entity && ( {t("application:fileManager.setAsCurrent")} )} {target?.owned && actionTarget?.id !== fileExtended?.primary_entity && ( {t("application:fileManager.delete")} )} {hilightButNotFound && ( {t("application:fileManager.versionNotFound")} )} {t("fileManager.actions")} {t("fileManager.createdAt")} {t("fileManager.size")} {t("fileManager.createdBy")} {t("application:fileManager.storagePolicy")} {!fileExtended && ( )} {versionEntities && versionEntities.map((e) => ( highlight == e.id ? `inset 0 0 0 2px ${theme.palette.primary.light}` : "none", "&:last-child td, &:last-child th": { border: 0 }, }} hover > handleOpenAction(event, e)} size={"small"}> {sizeToString(e.size)} {e.storage_policy?.name} ))}
{!versionEntities && fileExtended && ( {t("application:setting.listEmpty")} )}
); }; export default VersionControl;