-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(SanityImage): support image assets from shared Sanity project
- Loading branch information
Showing
15 changed files
with
131 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
"use client"; | ||
|
||
import { SanityImageSource } from "@sanity/image-url/lib/types/types"; | ||
import Image from "next/image"; | ||
import { UseNextSanityImageProps, useNextSanityImage } from "next-sanity-image"; | ||
import { useEffect, useState } from "react"; | ||
|
||
import { client } from "studio/lib/client"; | ||
import { IImage } from "studio/lib/interfaces/media"; | ||
import { sharedClient } from "studioShared/lib/client"; | ||
|
||
/** | ||
* Builds Next.js props for a given Sanity image from an unknown Sanity project. | ||
* | ||
* Each generated image source is checked for existence in order to pick the correct source project. | ||
* | ||
* NOTE: It is assumed that an image id is only valid for a single project. | ||
* The result of providing an id that resolves to a valid image in multiple projects is therefore not well-defined. | ||
* | ||
* @param image asset props for image from unknown Sanity project | ||
*/ | ||
const useNextSanityGlobalImage = ( | ||
image: SanityImageSource, | ||
): UseNextSanityImageProps | null => { | ||
const studioImage = useNextSanityImage(client, image); | ||
const sharedImage = useNextSanityImage(sharedClient, image); | ||
|
||
const [globalImage, setGlobalImage] = | ||
useState<UseNextSanityImageProps | null>(null); | ||
|
||
useEffect(() => { | ||
fetch(studioImage.src).then((r) => r.ok && setGlobalImage(studioImage)); | ||
fetch(sharedImage.src).then((r) => r.ok && setGlobalImage(sharedImage)); | ||
}, [studioImage, sharedImage]); | ||
|
||
return globalImage; | ||
}; | ||
|
||
const SanityAssetImage = ({ image }: { image: IImage }) => { | ||
const imageProps = useNextSanityGlobalImage(image); | ||
const objectPosition = image.hotspot | ||
? `${image.hotspot.x * 100}% ${image.hotspot.y * 100}%` | ||
: "50% 50%"; // Default to center if no hotspot is defined | ||
if (!imageProps) { | ||
return null; | ||
} | ||
return ( | ||
<Image | ||
alt={image?.alt || ""} | ||
{...imageProps} | ||
width={imageProps.width} | ||
height={imageProps.height} | ||
style={{ | ||
objectFit: "cover", | ||
objectPosition, | ||
maxWidth: "100%", | ||
maxHeight: "100%", | ||
height: "auto", | ||
width: "auto", | ||
}} | ||
/> | ||
); | ||
}; | ||
|
||
export function SanityImage({ image }: { image: IImage }) { | ||
if (image?.src) { | ||
return ( | ||
<Image | ||
alt={image?.alt || ""} | ||
src={image.src.src} | ||
style={{ objectFit: "cover", width: "100%", height: "100%" }} | ||
/> | ||
); | ||
} | ||
return <SanityAssetImage image={image} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 6 additions & 3 deletions
9
src/components/sections/imageSection/ImageSectionComponent.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.