Skip to content

Commit

Permalink
chore: update resource view
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyjoygh committed Mar 5, 2025
1 parent 92a44e2 commit c668073
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions web/src/components/MemoResourceListView.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { memo } from "react";
import { Resource } from "@/types/proto/api/v1/resource_service";
import { cn } from "@/utils";
import { getResourceType, getResourceUrl } from "@/utils/resource";
import MemoResource from "./MemoResource";
import showPreviewImageDialog from "./PreviewImageDialog";
import SquareDiv from "./kit/SquareDiv";

const MemoResourceListView = ({ resources = [] }: { resources: Resource[] }) => {
const mediaResources: Resource[] = [];
Expand All @@ -27,14 +27,14 @@ const MemoResourceListView = ({ resources = [] }: { resources: Resource[] }) =>
showPreviewImageDialog(imgUrls, index);
};

const MediaCard = ({ resource }: { resource: Resource }) => {
const MediaCard = ({ resource, className }: { resource: Resource; className?: string }) => {
const type = getResourceType(resource);
const resourceUrl = getResourceUrl(resource);

if (type === "image/*") {
return (
<img
className="cursor-pointer min-h-full w-auto object-cover"
className={cn("cursor-pointer h-full w-auto rounded-lg border dark:border-zinc-800 object-contain hover:opacity-80", className)}
src={resource.externalLink ? resourceUrl : resourceUrl + "?thumbnail=true"}
onClick={() => handleImageClick(resourceUrl)}
decoding="async"
Expand All @@ -44,7 +44,10 @@ const MemoResourceListView = ({ resources = [] }: { resources: Resource[] }) =>
} else if (type === "video/*") {
return (
<video
className="cursor-pointer w-full h-full object-contain bg-zinc-100 dark:bg-zinc-800"
className={cn(
"cursor-pointer h-full w-auto rounded-lg border dark:border-zinc-800 object-contain bg-zinc-100 dark:bg-zinc-800",
className,
)}
preload="metadata"
crossOrigin="anonymous"
src={resourceUrl}
Expand All @@ -57,37 +60,31 @@ const MemoResourceListView = ({ resources = [] }: { resources: Resource[] }) =>
};

const MediaList = ({ resources = [] }: { resources: Resource[] }) => {
if (resources.length === 0) return <></>;

if (resources.length === 1) {
const resource = mediaResources[0];
return (
<div className="max-w-full flex justify-center items-center border dark:border-zinc-800 rounded overflow-hidden hide-scrollbar hover:shadow-md">
<MediaCard resource={mediaResources[0]} />
<div className="max-w-full flex flex-col justify-start items-start overflow-hidden hide-scrollbar">
<MediaCard className="max-h-64" resource={resource} />
<span className="max-w-full text-xs pl-1 text-gray-400 dark:text-zinc-500 truncate">{resource.filename}</span>
</div>
);
}

const cards = resources.map((resource) => (
<SquareDiv
key={resource.name}
className="flex justify-center items-center border dark:border-zinc-900 rounded overflow-hidden hide-scrollbar hover:shadow-md"
>
<MediaCard resource={resource} />
</SquareDiv>
<div key={resource.name} className="max-w-[70%] flex flex-col justify-start items-start shrink-0">
<MediaCard className="max-h-64" resource={resource} />
<span className="max-w-full text-xs pl-1 text-gray-400 dark:text-zinc-500 truncate">{resource.filename}</span>
</div>
));

if (resources.length === 2 || resources.length === 4) {
return <div className="w-full grid gap-2 grid-cols-2">{cards}</div>;
}

return <div className="w-full grid gap-2 grid-cols-2 sm:grid-cols-3">{cards}</div>;
return <div className="w-full flex flex-row justify-start overflow-auto gap-2">{cards}</div>;
};

const OtherList = ({ resources = [] }: { resources: Resource[] }) => {
if (resources.length === 0) return <></>;

return (
<div className="w-full flex flex-row justify-start flex-wrap gap-2">
<div className="w-full flex flex-row justify-start overflow-auto gap-2">
{otherResources.map((resource) => (
<MemoResource key={resource.name} resource={resource} />
))}
Expand All @@ -97,7 +94,7 @@ const MemoResourceListView = ({ resources = [] }: { resources: Resource[] }) =>

return (
<>
<MediaList resources={mediaResources} />
{mediaResources.length > 0 && <MediaList resources={mediaResources} />}
<OtherList resources={otherResources} />
</>
);
Expand Down

0 comments on commit c668073

Please sign in to comment.