feat: optimize media resource display (#2518)

* feat: optimize media resource display

* fix: type error

* Update web/src/components/MemoResourceListView.tsx

Co-authored-by: boojack <stevenlgtm@gmail.com>

* Update MemoResourceListView.tsx

---------

Co-authored-by: boojack <stevenlgtm@gmail.com>
This commit is contained in:
Zexi 2023-11-17 08:03:26 +08:00 committed by GitHub
parent afd0e72e37
commit 8bc117bce9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 86 additions and 90 deletions

View File

@ -1,4 +1,4 @@
import classNames from "classnames"; import { memo } from "react";
import { absolutifyLink } from "@/helpers/utils"; import { absolutifyLink } from "@/helpers/utils";
import { Resource } from "@/types/proto/api/v2/resource_service"; import { Resource } from "@/types/proto/api/v2/resource_service";
import { getResourceType, getResourceUrl } from "@/utils/resource"; import { getResourceType, getResourceUrl } from "@/utils/resource";
@ -6,106 +6,102 @@ import MemoResource from "./MemoResource";
import showPreviewImageDialog from "./PreviewImageDialog"; import showPreviewImageDialog from "./PreviewImageDialog";
import SquareDiv from "./kit/SquareDiv"; import SquareDiv from "./kit/SquareDiv";
interface Props { const MemoResourceListView = ({ resourceList = [] }: { resourceList: Resource[] }) => {
resourceList: Resource[]; const mediaResources: Resource[] = [];
className?: string; const otherResources: Resource[] = [];
resourceList.forEach((resource) => {
const type = getResourceType(resource);
if (type === "image/*" || type === "video/*") {
mediaResources.push(resource);
return;
} }
const getDefaultProps = (): Props => { otherResources.push(resource);
return {
className: "",
resourceList: [],
};
};
const MemoResourceListView: React.FC<Props> = (props: Props) => {
const { className, resourceList } = {
...getDefaultProps(),
...props,
};
const imageResourceList = resourceList.filter((resource) => getResourceType(resource).startsWith("image"));
const videoResourceList = resourceList.filter((resource) => resource.type.startsWith("video"));
const otherResourceList = resourceList.filter(
(resource) => !imageResourceList.includes(resource) && !videoResourceList.includes(resource)
);
const imgUrls = imageResourceList.map((resource) => {
return getResourceUrl(resource);
}); });
const handleImageClick = (imgUrl: string) => { const handleImageClick = (imgUrl: string) => {
const imgUrls = mediaResources
.filter((resource) => getResourceType(resource) === "image/*")
.map((resource) => getResourceUrl(resource));
const index = imgUrls.findIndex((url) => url === imgUrl); const index = imgUrls.findIndex((url) => url === imgUrl);
showPreviewImageDialog(imgUrls, index); showPreviewImageDialog(imgUrls, index);
}; };
return ( const MediaCard = ({ resource, thumbnail }: { resource: Resource; thumbnail?: boolean }) => {
<> const type = getResourceType(resource);
{imageResourceList.length > 0 &&
(imageResourceList.length === 1 ? (
<div className="mt-2 max-w-full max-h-72 flex justify-center items-center border dark:border-zinc-800 rounded overflow-hidden hide-scrollbar hover:shadow-md">
<img
className="cursor-pointer min-h-full w-auto object-cover"
src={getResourceUrl(imageResourceList[0])}
onClick={() => handleImageClick(getResourceUrl(imageResourceList[0]))}
decoding="async"
/>
</div>
) : (
<div
className={classNames(
"w-full mt-2 grid gap-2 grid-cols-2",
imageResourceList.length === 4 ? "sm:grid-cols-2" : "sm:grid-cols-3"
)}
>
{imageResourceList.map((resource) => {
const url = getResourceUrl(resource); const url = getResourceUrl(resource);
if (type === "image/*") {
return ( return (
<SquareDiv
key={resource.id}
className="flex justify-center items-center border dark:border-zinc-900 rounded overflow-hidden hide-scrollbar hover:shadow-md"
>
<img <img
className="cursor-pointer min-h-full w-auto object-cover" className="cursor-pointer min-h-full w-auto object-cover"
src={resource.externalLink ? url : url + "?thumbnail=1"} src={resource.externalLink ? url : `${url}${thumbnail ? "?thumbnail=1" : ""}`}
onClick={() => handleImageClick(url)} onClick={() => handleImageClick(url)}
decoding="async" decoding="async"
/> />
</SquareDiv>
); );
})} }
</div>
))}
<div className={`w-full flex flex-col justify-start items-start ${className || ""}`}> if (type === "video/*") {
{videoResourceList.length > 0 && (
<div className="w-full grid grid-cols-2 sm:grid-cols-3 gap-2 mt-2">
{videoResourceList.map((resource) => {
const url = getResourceUrl(resource);
return ( return (
<SquareDiv key={resource.id} className="shadow rounded overflow-hidden hide-scrollbar">
<video <video
className="cursor-pointer w-full h-full object-contain bg-zinc-100 dark:bg-zinc-800" className="cursor-pointer w-full h-full object-contain bg-zinc-100 dark:bg-zinc-800"
preload="metadata" preload="metadata"
crossOrigin="anonymous" crossOrigin="anonymous"
src={absolutifyLink(url)} src={absolutifyLink(url)}
controls controls
></video> />
</SquareDiv>
); );
})} }
</div>
)}
</div>
{otherResourceList.length > 0 && ( return <></>;
<div className="w-full flex flex-row justify-start flex-wrap mt-2"> };
{otherResourceList.map((resource) => {
return <MemoResource key={resource.id} className="my-1 mr-2" resource={resource} />; const MediaList = ({ resources = [] }: { resources: Resource[] }) => {
})} if (resources.length === 0) return <></>;
if (resources.length === 1) {
return (
<div className="mt-2 max-w-full max-h-72 flex justify-center items-center border dark:border-zinc-800 rounded overflow-hidden hide-scrollbar hover:shadow-md">
<MediaCard resource={mediaResources[0]} />
</div> </div>
)} );
}
const cards = resources.map((resource) => (
<SquareDiv
key={resource.id}
className="flex justify-center items-center border dark:border-zinc-900 rounded overflow-hidden hide-scrollbar hover:shadow-md"
>
<MediaCard resource={resource} thumbnail />
</SquareDiv>
));
if (resources.length === 2 || resources.length === 4) {
return <div className="w-full mt-2 grid gap-2 grid-cols-2">{cards}</div>;
}
return <div className="w-full mt-2 grid gap-2 grid-cols-2 sm:grid-cols-3">{cards}</div>;
};
const OtherList = ({ resources = [] }: { resources: Resource[] }) => {
if (resources.length === 0) return <></>;
return (
<div className="w-full flex flex-row justify-start flex-wrap mt-2">
{otherResources.map((resource) => (
<MemoResource key={resource.id} className="my-1 mr-2" resource={resource} />
))}
</div>
);
};
return (
<>
<MediaList resources={mediaResources} />
<OtherList resources={otherResources} />
</> </>
); );
}; };
export default MemoResourceListView; export default memo(MemoResourceListView);

View File

@ -113,7 +113,7 @@ const ShareMemoDialog: React.FC<Props> = (props: Props) => {
<span className="w-full px-6 pt-5 pb-2 text-sm text-gray-500">{memo.displayTsStr}</span> <span className="w-full px-6 pt-5 pb-2 text-sm text-gray-500">{memo.displayTsStr}</span>
<div className="w-full px-6 text-base pb-4"> <div className="w-full px-6 text-base pb-4">
<MemoContent content={memo.content} /> <MemoContent content={memo.content} />
<MemoResourceListView className="!grid-cols-2" resourceList={memo.resourceList} /> <MemoResourceListView resourceList={memo.resourceList} />
</div> </div>
<div className="flex flex-row justify-between items-center w-full bg-gray-100 dark:bg-zinc-700 py-4 px-6"> <div className="flex flex-row justify-between items-center w-full bg-gray-100 dark:bg-zinc-700 py-4 px-6">
<div className="flex flex-row justify-start items-center"> <div className="flex flex-row justify-start items-center">