diff --git a/common/utilities.js b/common/utilities.js index b3a62da8..bfb49245 100644 --- a/common/utilities.js +++ b/common/utilities.js @@ -10,29 +10,6 @@ import moment from "moment"; //For API related utility functions, see common/user-behaviors.js //And for uploading related utility functions, see common/file-utilities.js -export const getImageUrlIfExists = (file, sizeLimit = null) => { - if (!file) return; - const coverImage = file.coverImage; - if (coverImage) { - if (sizeLimit && coverImage.size && coverImage.size > sizeLimit) { - return; - } - if (coverImage.url) { - return coverImage.url; - } - if (coverImage.cid) { - return Strings.getURLfromCID(coverImage.cid); - } - } - - if (Validations.isPreviewableImage(file.type)) { - if (sizeLimit && file.size > sizeLimit) { - return; - } - return Strings.getURLfromCID(file.cid); - } -}; - export const generateNumberByStep = ({ min, max, step = 1 }) => { var numbers = []; for (var n = min; n <= max; n += step) { @@ -170,15 +147,22 @@ export const clamp = (value, min, max) => { return value; }; -export const getCoverImageUrlIfExists = (coverImage) => { - if (!coverImage) return; - if (Validations.isPreviewableImage(coverImage.type)) { +export const getImageUrlIfExists = (file, sizeLimit = null) => { + if (!file) return; + if (Validations.isPreviewableImage(file.type)) { + if (sizeLimit && file.size && file.size > sizeLimit) { + return; + } + return Strings.getURLfromCID(file.cid); + } + let coverImage = file.coverImage; + if (coverImage) { + if (sizeLimit && coverImage.size && coverImage.size > sizeLimit) { + return; + } return Strings.getURLfromCID(coverImage.cid); } - if (coverImage.coverImage) { - return Strings.getURLfromCID(coverImage.coverImage.cid); - } - if (coverImage.linkImage) { - return coverImage.linkImage; + if (file.linkImage) { + return file.linkImage; } }; diff --git a/components/core/CollectionPreviewBlock/index.js b/components/core/CollectionPreviewBlock/index.js index 26d62f70..c7985cbe 100644 --- a/components/core/CollectionPreviewBlock/index.js +++ b/components/core/CollectionPreviewBlock/index.js @@ -3,6 +3,7 @@ import * as Styles from "~/common/styles"; import * as Constants from "~/common/constants"; import * as SVG from "~/common/svg"; import * as Validations from "~/common/validations"; +import * as Utilities from "~/common/utilities"; import { css } from "@emotion/react"; import { FollowButton, ShareButton } from "~/components/core/CollectionPreviewBlock/components"; @@ -333,9 +334,9 @@ const useAnimateDescription = ({ }; const getObjectToPreview = (coverImage) => { - let isImage = - Validations.isPreviewableImage(coverImage.type) || - (coverImage.coverImage && Validations.isPreviewableImage(coverImage.coverImage.type)); + if (!coverImage) return { type: "EMPTY" }; - return { object: coverImage, type: isImage ? "IMAGE" : "PLACEHOLDER" }; + let coverImageUrl = Utilities.getImageUrlIfExists(coverImage); + + return { object: coverImage, type: coverImageUrl ? "IMAGE" : "PLACEHOLDER" }; }; diff --git a/node_common/utilities.js b/node_common/utilities.js index 4b24ee1a..1212b740 100644 --- a/node_common/utilities.js +++ b/node_common/utilities.js @@ -411,7 +411,7 @@ export const selectSlateCoverImage = (objects) => { let selectedObject; if (!objects.length) return null; for (let object of objects) { - if (Utilities.getCoverImageUrlIfExists(object)) { + if (Utilities.getImageUrlIfExists(object)) { selectedObject = object; break; } @@ -422,7 +422,7 @@ export const selectSlateCoverImage = (objects) => { }; export const addToSlateCheckCoverImage = async (slate, filesAdded) => { - if (Utilities.getCoverImageUrlIfExists(slate.coverImage)) { + if (Utilities.getImageUrlIfExists(slate.coverImage)) { return; } if (!filesAdded) return; diff --git a/scenes/SceneSlate.js b/scenes/SceneSlate.js index 85df93a6..1ddec403 100644 --- a/scenes/SceneSlate.js +++ b/scenes/SceneSlate.js @@ -230,7 +230,7 @@ export default class SceneSlate extends React.Component { description = `View the collection ${name} on Slate`; } title = `${name} • Slate`; - imageUrl = slate.coverImage?.cid ? Strings.getURLfromCID(slate.coverImage.cid) : null; + image = Utilities.getImageUrlIfExists(slate.coverImage); // const objects = slate.objects; // if (!image && objects) { // for (let i = 0; i < objects.length; i++) {