-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: setup overlays in next-pages-router and next-app-router
- Loading branch information
Showing
9 changed files
with
119 additions
and
3 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
54 changes: 54 additions & 0 deletions
54
apps/next-app-router/app/variants/live-store/VisualEditing.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use client' | ||
|
||
import { enableOverlays, HistoryAdapterNavigate } from '@sanity/overlays' | ||
import { usePathname, useRouter, useSearchParams } from 'next/navigation' | ||
import { useEffect, useRef, useState } from 'react' | ||
|
||
export default function VisualEditing() { | ||
const router = useRouter() | ||
const routerRef = useRef(router) | ||
const [navigate, setNavigate] = useState<HistoryAdapterNavigate | undefined>() | ||
|
||
useEffect(() => { | ||
routerRef.current = router | ||
}, [router]) | ||
useEffect(() => { | ||
const disable = enableOverlays({ | ||
allowStudioOrigin: process.env.NEXT_PUBLIC_STUDIO_URL || 'http://localhost:3333', | ||
history: { | ||
subscribe: (navigate) => { | ||
setNavigate(() => navigate) | ||
return () => setNavigate(undefined) | ||
}, | ||
update: (update) => { | ||
switch (update.type) { | ||
case 'push': | ||
return routerRef.current.push(update.url) | ||
case 'pop': | ||
return routerRef.current.back() | ||
case 'replace': | ||
return routerRef.current.replace(update.url) | ||
default: | ||
throw new Error(`Unknown update type: ${update.type}`) | ||
} | ||
}, | ||
}, | ||
}) | ||
return () => disable() | ||
}, []) | ||
|
||
const pathname = usePathname() | ||
const searchParams = useSearchParams() | ||
useEffect(() => { | ||
if (navigate) { | ||
navigate({ | ||
type: 'push', | ||
url: `${pathname}${searchParams?.size ? `?${searchParams}` : ''}`, | ||
}) | ||
} | ||
}, [navigate, pathname, searchParams]) | ||
|
||
|
||
|
||
return null | ||
} |
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
46 changes: 46 additions & 0 deletions
46
apps/next-pages-router/src/variants/live-store/VisualEditing.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { HistoryAdapterNavigate, enableOverlays } from '@sanity/overlays' | ||
import { useRouter } from 'next/router' | ||
import { useEffect, useRef, useState } from 'react' | ||
|
||
|
||
export default function VisualEditing() { | ||
const router = useRouter() | ||
const routerRef = useRef(router) | ||
const [navigate, setNavigate] = useState<HistoryAdapterNavigate | undefined>() | ||
|
||
useEffect(() => { | ||
routerRef.current = router | ||
}, [router]) | ||
useEffect(() => { | ||
if (!router.isReady) return | ||
const disable = enableOverlays({ | ||
allowStudioOrigin: process.env.NEXT_PUBLIC_STUDIO_URL || 'http://localhost:3333', | ||
history: { | ||
subscribe: (navigate) => { | ||
setNavigate(() => navigate) | ||
return () => setNavigate(undefined) | ||
}, | ||
update: (update) => { | ||
switch (update.type) { | ||
case 'push': | ||
return routerRef.current.push(update.url) | ||
case 'pop': | ||
return routerRef.current.back() | ||
case 'replace': | ||
return routerRef.current.replace(update.url) | ||
default: | ||
throw new Error(`Unknown update type: ${update.type}`) | ||
} | ||
}, | ||
}, | ||
}) | ||
return () => disable() | ||
}, [router.isReady]) | ||
useEffect(() => { | ||
if (navigate) { | ||
navigate({ type: 'push', url: router.asPath }) | ||
} | ||
}, [navigate, router.asPath]) | ||
|
||
return null | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
ef08bf6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
preview-kit-next-pages-router – ./apps/next-pages-router
preview-kit-next-pages-router-git-main.sanity.build
preview-kit-next-pages-router.sanity.build
ef08bf6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
preview-kit-next-pages-router-live-store – ./apps/next-pages-router
preview-kit-next-pages-router-live-store.sanity.build
preview-kit-next-pages-router-live-store-git-main.sanity.build
ef08bf6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
preview-kit-next-pages-router-groq-store – ./apps/next-pages-router
preview-kit-next-pages-router-groq-store.sanity.build
preview-kit-next-pages-router-groq-store-git-main.sanity.build
ef08bf6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
preview-kit-next-app-router – ./apps/next-app-router
preview-kit-next-app-router.sanity.build
preview-kit-next-app-router-git-main.sanity.build
ef08bf6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
preview-kit-next-app-router-live-store – ./apps/next-app-router
preview-kit-next-app-router-live-store.sanity.build
preview-kit-next-app-router-live-store-git-main.sanity.build
ef08bf6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
preview-kit-next-app-router-groq-store – ./apps/next-app-router
preview-kit-next-app-router-groq-store-git-main.sanity.build
preview-kit-next-app-router-groq-store.sanity.build