{"version":3,"file":"PostMessagePreviewSnapshots.mjs","sources":["../../src/presentation/editor/PostMessagePreviewSnapshots.tsx"],"sourcesContent":["import {type ClientPerspective} from '@sanity/client'\nimport {type PreviewSnapshot} from '@sanity/presentation-comlink'\nimport {type FC, memo, useEffect, useMemo, useRef} from 'react'\nimport {\n  combineLatest,\n  debounceTime,\n  filter,\n  map,\n  merge,\n  NEVER,\n  share,\n  skipWhile,\n  Subject,\n  switchMap,\n  takeUntil,\n} from 'rxjs'\nimport {\n  getDraftId,\n  getPublishedId,\n  type PreviewValue,\n  useDocumentPreviewStore,\n  useSchema,\n} from 'sanity'\n\nimport {type VisualEditingConnection} from '../types'\n\ntype Ref = {\n  _id: string\n  _type: string\n}\n\nexport interface PostMessagePreviewsProps {\n  comlink: VisualEditingConnection\n  perspective: ClientPerspective\n  refs: Ref[]\n}\n\nconst PostMessagePreviews: FC<PostMessagePreviewsProps> = (props) => {\n  const {comlink, refs, perspective} = props\n  const documentPreviewStore = useDocumentPreviewStore()\n  const schema = useSchema()\n\n  const refsSubject = useMemo(() => new Subject<Ref[]>(), [])\n\n  const previews$ = useMemo(() => {\n    return refsSubject.asObservable().pipe(\n      switchMap(\n        (\n          // eslint-disable-next-line @typescript-eslint/no-shadow\n          refs,\n        ) => {\n          return combineLatest(\n            refs.map((ref) => {\n              const draftRef = {...ref, _id: getDraftId(ref._id)}\n              const draft$ =\n                perspective === 'published'\n                  ? // Don't emit if not displaying drafts\n                    NEVER\n                  : documentPreviewStore\n                      .observeForPreview(draftRef, schema.get(draftRef._type)!)\n                      .pipe(\n                        // Share to prevent double subscribe in the merge\n                        share(),\n                        // Don't emit if no snapshot is returned\n                        // eslint-disable-next-line max-nested-callbacks\n                        skipWhile((p) => p.snapshot === null),\n                      )\n\n              const publishedRef = {...ref, _id: getPublishedId(ref._id)}\n              const published$ = documentPreviewStore.observeForPreview(\n                publishedRef,\n                schema.get(publishedRef._type)!,\n              )\n\n              return merge(published$.pipe(takeUntil(draft$)), draft$).pipe(\n                // eslint-disable-next-line max-nested-callbacks\n                filter((p) => !!p.snapshot),\n                // eslint-disable-next-line max-nested-callbacks\n                map((p) => {\n                  const snapshot = p.snapshot as PreviewValue & {\n                    _id: string\n                  }\n                  return {\n                    _id: getPublishedId(snapshot._id),\n                    title: snapshot.title,\n                    subtitle: snapshot.subtitle,\n                    description: snapshot.description,\n                    imageUrl: snapshot.imageUrl,\n                  } as PreviewSnapshot\n                }),\n              )\n            }),\n          )\n        },\n      ),\n      debounceTime(0),\n    )\n  }, [documentPreviewStore, refsSubject, schema, perspective])\n\n  const lastSnapshots = useRef<PreviewSnapshot[]>([])\n\n  // Stream preview snapshots when updates are received, and store the last set\n  // of snapshots so they can be returned if explicitly requested\n  useEffect(() => {\n    const sub = previews$.subscribe((snapshots) => {\n      comlink.post('presentation/preview-snapshots', {snapshots})\n      lastSnapshots.current = snapshots\n    })\n\n    return () => {\n      sub.unsubscribe()\n    }\n  }, [comlink, previews$])\n\n  // Respond to explict requests for preview snapshots. Streaming will not\n  // always suffice as the previews$ subscriber will not be called if the app\n  // reloads but Presentation does not.\n  useEffect(() => {\n    return comlink.on('visual-editing/preview-snapshots', () => ({\n      snapshots: lastSnapshots.current,\n    }))\n  }, [comlink])\n\n  useEffect(() => {\n    refsSubject.next(refs)\n  }, [refs, refsSubject])\n\n  return null\n}\n\nexport default memo(PostMessagePreviews)\n"],"names":["PostMessagePreviews","props","comlink","refs","perspective","documentPreviewStore","useDocumentPreviewStore","schema","useSchema","refsSubject","useMemo","Subject","previews$","asObservable","pipe","switchMap","combineLatest","map","ref","draftRef","_id","getDraftId","draft$","NEVER","observeForPreview","get","_type","share","skipWhile","p","snapshot","publishedRef","getPublishedId","published$","merge","takeUntil","filter","title","subtitle","description","imageUrl","debounceTime","lastSnapshots","useRef","useEffect","sub","subscribe","snapshots","post","current","unsubscribe","on","next","memo"],"mappings":";;;AAqCA,MAAMA,sBAAqDC,CAAU,UAAA;AAC7D,QAAA;AAAA,IAACC;AAAAA,I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