Skip to content

Commit

Permalink
fix: preview page
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <[email protected]>
  • Loading branch information
Innei committed Jun 9, 2024
1 parent 653cac2 commit 2bc2500
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/app/(app)/preview/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const safeParse = (value: string) => {
}
const previewDataAtom = atom<PostModel | NoteModel | null>(null)
export default function PreviewPage() {
// handle preview by storage observer
useIsomorphicLayoutEffect(() => {
const search = location.search
const searchParams = new URLSearchParams(search)
Expand Down Expand Up @@ -105,12 +106,20 @@ export default function PreviewPage() {
}
}, [])

// handle preview by postMessage

useIsomorphicLayoutEffect(() => {
const search = location.search
const searchParams = new URLSearchParams(search)

let targetOrigin = searchParams.get('origin')

// const isInIframe = window.self !== window.top

// if (isInIframe) {
// return
// }

if (!targetOrigin) {
return
}
Expand All @@ -121,6 +130,7 @@ export default function PreviewPage() {

if (!parsedData) return
const PREVIEW_HASH = new URLSearchParams(location.search).get('key')

if (!PREVIEW_HASH) return
if (parsedData.key !== PREVIEW_HASH) {
return
Expand All @@ -132,16 +142,18 @@ export default function PreviewPage() {
JSON.stringify(parsedData.data)
)
return

jotaiStore.set(previewDataAtom, simpleCamelcaseKeys(parsedData.data))
}
}
window.addEventListener('message', handler)

console.info('preview page ready')
window.opener.postMessage('ok', targetOrigin)
const parentWindow = window.opener || window.parent
parentWindow.postMessage('ok', targetOrigin)

const timer = setInterval(() => {
window.opener.postMessage('ok', targetOrigin)
parentWindow.postMessage('ok', targetOrigin)
}, 3000)
return () => {
window.removeEventListener('message', handler)
Expand Down Expand Up @@ -216,7 +228,6 @@ const NotePreview = () => {
next: undefined,
data: {
...data,

created: new Date().toISOString(),
images: data.images ?? [],
count: data.count ?? {
Expand Down

0 comments on commit 2bc2500

Please sign in to comment.