Skip to content

[Content] Improve image thumbnails #3224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
9189516
Refactor ImageCell component to use FileTypePreview and improve file …
finnar-bin Feb 14, 2025
264d15e
Add skeleton loader to ImageCell component for improved loading state
finnar-bin Feb 14, 2025
3d2cb3b
Merge branch 'dev' into fix/3206-update-thumbnail-component
finnar-bin Feb 19, 2025
b57b71f
Merge branch 'dev' into fix/3206-update-thumbnail-component
finnar-bin Feb 28, 2025
79ddf64
Use cached media files to get file data
finnar-bin Feb 28, 2025
5bcb0c8
Fix issue where ecoID is incorrect
finnar-bin Feb 28, 2025
ffa91b8
Merge branch 'dev' into fix/3206-update-thumbnail-component
finnar-bin Mar 11, 2025
67aa113
Attempt to search for file data if cache doesn't have the data for it
finnar-bin Mar 11, 2025
c48c421
Merge branch 'dev' into fix/3206-update-thumbnail-component
finnar-bin Mar 19, 2025
6393ccd
Merge branch 'dev' into fix/3206-update-thumbnail-component
finnar-bin Mar 20, 2025
2b29590
Merge branch 'dev' into fix/3206-update-thumbnail-component
finnar-bin Mar 24, 2025
a9913db
Merge branch 'dev' into fix/3206-update-thumbnail-component
finnar-bin Mar 26, 2025
2d0f36d
Merge branch 'dev' into fix/3206-update-thumbnail-component
finnar-bin Mar 27, 2025
8dc3773
Merge branch 'dev' into fix/3206-update-thumbnail-component
finnar-bin Apr 1, 2025
3586db7
Merge branch 'dev' into fix/3206-update-thumbnail-component
finnar-bin Apr 4, 2025
c8c0b37
Merge branch 'dev' into fix/3206-update-thumbnail-component
finnar-bin Apr 7, 2025
98f0ad2
Merge branch 'dev' into fix/3206-update-thumbnail-component
finnar-bin Apr 21, 2025
c495d15
Merge branch 'dev' into fix/3206-update-thumbnail-component
finnar-bin Apr 22, 2025
a99e6a0
Remove need to do separate api calls to fetch image data
finnar-bin Apr 22, 2025
6812a14
Merge branch 'dev' into fix/3206-update-thumbnail-component
finnar-bin Apr 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,16 +1,58 @@
import { GridRenderCellParams } from "@mui/x-data-grid-pro";
import { Box, Stack } from "@mui/material";
import { Box, Skeleton, Stack } from "@mui/material";
import { ImageRounded } from "@mui/icons-material";
import { useState } from "react";
import { useSelector } from "react-redux";

import { FileTypePreview } from "../../../../../../media/src/app/components/FileModal/FileTypePreview";
import {
useGetAllBinFilesQuery,
useGetBinsQuery,
useLazyGetFileQuery,
} from "../../../../../../../shell/services/mediaManager";
import { AppState } from "../../../../../../../shell/store/types";
import { useEffect, useMemo, useState } from "react";
import { File } from "../../../../../../../shell/services/types";

type ImageCellProps = { params: GridRenderCellParams };
export const ImageCell = ({ params }: ImageCellProps) => {
const [hasError, setHasError] = useState(false);
const handleImageError = () => {
setHasError(true);
};
const [fileData, setFileData] = useState<File>(null);
const instance = useSelector((state: AppState) => state.instance);
const isFileZUID = !!params.value?.startsWith("3-");

const { data: bins, isFetching: isFetchingBins } = useGetBinsQuery({
instanceId: instance?.ID,
ecoId: instance?.ecoID,
});
const [getFile] = useLazyGetFileQuery();

// Query below will not necessarily be made on every render as this
// is already performed on component load, we're simply accessing the cached data
const { data: allMediaFiles, isFetching: isFetchingAllMediaFiles } =
useGetAllBinFilesQuery(
bins?.map((bin) => bin.id),
{ skip: !bins?.length }
);
useEffect(() => {
if (isFetchingAllMediaFiles || !isFileZUID) return;

const matchedFile = allMediaFiles?.find((file) => file.id === params.value);

if (!params.value || hasError) {
if (!matchedFile) {
// If cache doesn't have the file data, attempt to look it up from the server
getFile(params.value)
.unwrap()
.then((res) => {
setFileData(res);
})
.catch((err: any) => {
console.error(err);
});
} else {
setFileData(matchedFile);
}
}, [allMediaFiles, params.value, isFetchingAllMediaFiles]);

if (!params.value) {
return (
<Stack
sx={{
Expand All @@ -28,18 +70,55 @@ export const ImageCell = ({ params }: ImageCellProps) => {
);
}

if (isFileZUID) {
if (isFetchingAllMediaFiles || isFetchingBins) {
return (
<Skeleton
variant="rectangular"
width="100%"
height="100%"
animation="wave"
/>
);
}

return (
<Box
sx={{
height: "100%",
width: "100%",

"[data-cy='file-preview']": {
width: "100%",
},
}}
>
<FileTypePreview
isMediaThumbnail
src={fileData?.url}
filename={fileData?.filename}
updatedAt={fileData?.updated_at}
/>
</Box>
);
}

return (
<Box
component="img"
sx={{
backgroundColor: (theme) => theme.palette.grey[100],
objectFit: "contain",
zIndex: -1,
height: "100%",
width: "100%",

"[data-cy='file-preview']": {
width: "100%",
},
}}
width="68px"
height="58px"
src={params.value}
onError={handleImageError}
/>
>
<FileTypePreview
isMediaThumbnail
src={params?.value}
filename={params?.value?.split("/").pop()}
/>
</Box>
);
};
9 changes: 1 addition & 8 deletions src/apps/content-editor/src/app/views/ItemList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,7 @@ export const ItemList = () => {
const value = data[key] as string;
switch (fieldType) {
case "images":
clonedItem.data[key] = value?.split(",")[0]?.startsWith("3-")
? `${
// @ts-ignore
CONFIG.SERVICE_MEDIA_RESOLVER
}/resolve/${
value?.split(",")[0]
}/getimage/?w=${68}&h=auto&type=fit`
: value?.split(",")?.[0];
clonedItem.data[key] = value?.split(",")?.[0];
break;
case "internal_link":
case "one_to_one":
Expand Down
2 changes: 1 addition & 1 deletion src/shell/components/Favicon/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const Favicon = ({ onCloseFaviconModal }: FaviconProps) => {
] = useCreateHeadTagMutation();
const { data: bins, isFetching: isFetchingBins } = useGetBinsQuery({
instanceId: instance?.ID,
ecoId: instance?.ecoId,
ecoId: instance?.ecoID,
});
const { data: allMediaFiles, isFetching: isFetchingAllMediaFiles } =
useGetAllBinFilesQuery(
Expand Down
Loading