Skip to content

Commit

Permalink
fix(presentation): remove media from preview snapshot payload (#2079)
Browse files Browse the repository at this point in the history
  • Loading branch information
rdunk authored Nov 4, 2024
1 parent 529fb5b commit f1d04b3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
28 changes: 19 additions & 9 deletions packages/presentation/src/editor/PostMessagePreviewSnapshots.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type {PreviewSnapshot} from '@repo/visual-editing-helpers'
import type {ClientPerspective} from '@sanity/client'
import {memo, useEffect, useMemo, type FC} from 'react'
import {
combineLatest,
debounceTime,
filter,
map,
merge,
NEVER,
share,
Expand Down Expand Up @@ -58,7 +61,21 @@ const PostMessagePreviews: FC<PostMessagePreviewsProps> = (props) => {
schema.get(publishedRef._type)!,
)

return merge(published$.pipe(takeUntil(draft$)), draft$)
return merge(published$.pipe(takeUntil(draft$)), draft$).pipe(
filter((p) => !!p.snapshot),
map((p) => {
const snapshot = p.snapshot as PreviewValue & {
_id: string
}
return {
_id: getPublishedId(snapshot._id),
title: snapshot.title,
subtitle: snapshot.subtitle,
description: snapshot.description,
imageUrl: snapshot.imageUrl,
} as PreviewSnapshot
}),
)
}),
)
}),
Expand All @@ -70,14 +87,7 @@ const PostMessagePreviews: FC<PostMessagePreviewsProps> = (props) => {
const sub = previews$.subscribe((snapshots) => {
comlink.post({
type: 'presentation/preview-snapshots',
data: {
snapshots: snapshots
.filter((s) => s.snapshot)
.map((s) => {
const snapshot = s.snapshot as PreviewValue & {_id: string}
return {...snapshot, _id: getPublishedId(snapshot._id)}
}),
},
data: {snapshots},
})
})

Expand Down
12 changes: 11 additions & 1 deletion packages/visual-editing-helpers/src/types/comlink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ export type HistoryRefresh =
}
}

/**
* @public
*/
export type PreviewSnapshot = {
// Explicitly exclude media, as it's not serializable
[K in keyof Omit<PreviewValue, 'media'>]?: Omit<PreviewValue, 'media'>[K]
} & {
_id: string
}

/**
* @public
*/
Expand Down Expand Up @@ -120,7 +130,7 @@ export type VisualEditingControllerMsg =
| {
type: 'presentation/preview-snapshots'
data: {
snapshots: Array<PreviewValue & {_id: string}>
snapshots: PreviewSnapshot[]
}
}
| {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {PreviewValue} from '@sanity/types'
import type {PreviewSnapshot} from '@repo/visual-editing-helpers'
import {createContext} from 'react'

export type PreviewSnapshotsContextValue = Array<PreviewValue & {_id: string}>
export type PreviewSnapshotsContextValue = PreviewSnapshot[]

export const PreviewSnapshotsContext = createContext<PreviewSnapshotsContextValue | null>(null)

0 comments on commit f1d04b3

Please sign in to comment.