Skip to content

Commit a8bbb89

Browse files
stipsanjuice49
authored andcommitted
fix(presentation): perspective switching regression (#8383)
1 parent a678039 commit a8bbb89

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

packages/sanity/src/presentation/preview/Preview.tsx

+16-2
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,13 @@ export const Preview = memo(
8282
vercelProtectionBypass,
8383
} = props
8484

85+
const [stablePerspective, setStablePerspective] = useState<typeof perspective | null>(null)
86+
const urlPerspective = stablePerspective === null ? perspective : stablePerspective
8587
const previewUrl = useMemo(() => {
8688
const url = new URL(initialUrl)
8789
// Always set the perspective that's being used, even if preview mode isn't configured
8890
if (!url.searchParams.get(urlSearchParamPreviewPerspective)) {
89-
url.searchParams.set(urlSearchParamPreviewPerspective, perspective)
91+
url.searchParams.set(urlSearchParamPreviewPerspective, urlPerspective)
9092
}
9193

9294
if (vercelProtectionBypass || url.searchParams.get(urlSearchParamVercelProtectionBypass)) {
@@ -102,7 +104,19 @@ export const Preview = memo(
102104
}
103105

104106
return url
105-
}, [initialUrl, perspective, vercelProtectionBypass])
107+
}, [initialUrl, urlPerspective, vercelProtectionBypass])
108+
109+
useEffect(() => {
110+
/**
111+
* If the preview iframe is connected to the loader, we know that switching the perspective can be done without reloading the iframe.
112+
*/
113+
if (loadersConnection === 'connected') {
114+
/**
115+
* Only set the stable perspective if it hasn't been set yet.
116+
*/
117+
setStablePerspective((prev) => (prev === null ? perspective : prev))
118+
}
119+
}, [loadersConnection, perspective])
106120

107121
const {t} = useTranslation(presentationLocaleNamespace)
108122
const {devMode} = usePresentationTool()

0 commit comments

Comments
 (0)