import { absolutifyLink } from "../helpers/utils"; import Icon from "./Icon"; import SquareDiv from "./common/SquareDiv"; import showPreviewImageDialog from "./PreviewImageDialog"; import "../less/memo-resources.less"; interface Props { resourceList: Resource[]; className?: string; } const getDefaultProps = (): Props => { return { className: "", resourceList: [], }; }; const MemoResources: React.FC = (props: Props) => { const { className, resourceList } = { ...getDefaultProps(), ...props, }; const availableResourceList = resourceList.filter((resource) => resource.type.startsWith("image") || resource.type.startsWith("video")); const otherResourceList = resourceList.filter((resource) => !availableResourceList.includes(resource)); const handlePreviewBtnClick = (resource: Resource) => { const resourceUrl = `${window.location.origin}/o/r/${resource.id}/${resource.filename}`; window.open(resourceUrl); }; const imgUrls = availableResourceList .filter((resource) => resource.type.startsWith("image")) .map((resource) => { return `/o/r/${resource.id}/${resource.filename}`; }); const handleImageClick = (imgUrl: string) => { const index = imgUrls.findIndex((url) => url === imgUrl); showPreviewImageDialog(imgUrls, index); }; return ( <>
{availableResourceList.length > 0 && (
{availableResourceList.map((resource) => { const url = `/o/r/${resource.id}/${resource.filename}`; if (resource.type.startsWith("image")) { return ( handleImageClick(url)} decoding="async" loading="lazy" /> ); } else if (resource.type.startsWith("video")) { return ( ); } else { return null; } })}
)}
{otherResourceList.map((resource) => { return (
handlePreviewBtnClick(resource)}> {resource.filename}
); })}
); }; export default MemoResources;