import React from "react";
import VersionListSkeleton from "./VersionListSkeleton";
import { getDocuementsStatus } from "./Documents";
import Avatar from "../common/Avatar";
import { formatUTCToLocalTime } from "../../utils/dateUtils";
import { useHasUserPermission } from "../../hooks/useHasUserPermission";
import { DOWNLOAD_DOCUMENT, VERIFY_DOCUMENT } from "../../utils/constants";
import { FileIcon } from "../../utils/FileIcon";
const DocumentVersionList = ({
versionLoding,
versionList,
isPending,
setOpenDocument,
VerifyDocument,
RejectDocument,
showVersions,
latestDoc,
}) => {
const canVerifyDocument = useHasUserPermission(VERIFY_DOCUMENT);
const canDownloadDocument = useHasUserPermission(DOWNLOAD_DOCUMENT);
const handleOpenDocument = () => {
if (canDownloadDocument) {
setOpenDocument(true);
}
};
if (versionLoding) {
return ;
}
const sortedVersions = versionList?.data
? [...versionList.data].sort((a, b) => b.version - a.version)
: [];
const currentDoc = sortedVersions.length ? sortedVersions[0] : latestDoc;
if (!showVersions) {
if (!currentDoc) {
return
No documents available.
;
}
return (
<>
{/* Left Side: Document Details */}
{currentDoc.name}
{getDocuementsStatus(currentDoc.isVerified)}
File Size: {currentDoc.fileSize} Kb
Uploaded by
{`${currentDoc.uploadedBy?.firstName ?? ""} ${currentDoc.uploadedBy?.lastName ?? ""}`.trim() || "N/A"}
{formatUTCToLocalTime(currentDoc?.uploadedAt)}
latest
{/* Right Side: Status and Info */}
{/*
latest
*/}
{currentDoc?.updatedBy && (
<>
Updated by
{`${currentDoc.updatedBy?.firstName ?? ""} ${currentDoc.updatedBy?.lastName ?? ""}`.trim() || "N/A"}
>
)}
{/* Conditionally render updated date */}
{currentDoc?.updatedAt && (
{formatUTCToLocalTime(currentDoc?.updatedAt)}
)}
{/* Buttons Div - only for the latest pending document when showVersions is false */}
{currentDoc.isVerified === null && canVerifyDocument && (
{isPending ? (
Please Wait...
) : (
<>
>
)}
)}
>
);
}
// If showVersions is true, display all versions
if (!sortedVersions.length) {
return No documents available.
;
}
return (
{sortedVersions.map((document, index) => (
{document.name}
{getDocuementsStatus(document.isVerified)}
File Size: {document.fileSize} Kb
Uploaded by
{`${document.uploadedBy?.firstName ?? ""} ${document.uploadedBy?.lastName ?? ""}`.trim() || "N/A"}
{formatUTCToLocalTime(document?.uploadedAt)}
{document?.verifiedAt && (
{document.isVerified ? "Approved by" : "Rejected by"}
{`${document.verifiedBy?.firstName ?? ""} ${document.verifiedBy?.lastName ?? ""}`.trim() || "N/A"}
)}
{/* Conditionally render verified date */}
{document?.verifiedAt && (
{formatUTCToLocalTime(document?.verifiedAt)}
)}
version {document.version}
{document.isLatest && (
latest
)}
{/* Right Side: Status and Actions */}
{/*
version {document.version}
*/}
{document.isLatest && (
latest
)}
{document?.updatedBy && (
<>
Updated by
{`${document.updatedBy?.firstName ?? ""} ${document.updatedBy?.lastName ?? ""}`.trim() || "N/A"}
>
)}
{/* Conditionally render updated date */}
{document?.updatedAt && (
{formatUTCToLocalTime(document?.updatedAt)}
)}
))}
);
};
export default DocumentVersionList;