Skip to content

Commit

Permalink
fix(base): return ready state from useEditState()
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge committed Oct 19, 2021
1 parent d0468f5 commit 0326d9c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {SanityDocument} from '@sanity/types'
import {Observable} from 'rxjs'
import {map, publishReplay, refCount} from 'rxjs/operators'
import {map, publishReplay, refCount, startWith} from 'rxjs/operators'
import {IdPair} from '../types'
import {memoize} from '../utils/createMemoizer'
import {isLiveEditEnabled} from './utils/isLiveEditEnabled'
Expand All @@ -12,18 +12,29 @@ export interface EditStateFor {
draft: SanityDocument | null
published: SanityDocument | null
liveEdit: boolean
ready: boolean
}

export const editState = memoize(
(idPair: IdPair, typeName: string): Observable<EditStateFor> => {
const liveEdit = isLiveEditEnabled(typeName)
return operationArgs(idPair, typeName).pipe(
map(({snapshots}) => ({
id: idPair.publishedId,
type: typeName,
draft: snapshots.draft,
published: snapshots.published,
liveEdit: isLiveEditEnabled(typeName),
liveEdit,
ready: true,
})),
startWith({
id: idPair.publishedId,
type: typeName,
draft: null,
published: null,
liveEdit,
ready: false,
}),
publishReplay(1),
refCount()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export const DocumentPaneProvider = function DocumentPaneProvider(
const compareValue: Partial<SanityDocument> | null = changesOpen
? historyController.sinceAttributes()
: editState?.published || null
const ready = connectionState === 'connected' && !patch.disabled
const ready = connectionState === 'connected' && editState.ready
const displayed: Partial<SanityDocument> | null = useMemo(
() => {
return historyController.onOlderRevision() ? historyController.displayed() : value
Expand Down
18 changes: 4 additions & 14 deletions packages/@sanity/react-hooks/src/useEditState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,10 @@
import type {EditStateFor} from '@sanity/base/_internal'
import documentStore from 'part:@sanity/base/datastore/document'
import {useMemoObservable} from 'react-rx'
import {startWith} from 'rxjs/operators'

export function useEditState(publishedDocId: string, docTypeName: string): EditStateFor {
return useMemoObservable(
() =>
documentStore.pair.editState(publishedDocId, docTypeName).pipe(
startWith({
id: publishedDocId,
type: docTypeName,
draft: null,
published: null,
liveEdit: false,
})
),
[publishedDocId, docTypeName]
) as EditStateFor
return useMemoObservable(() => documentStore.pair.editState(publishedDocId, docTypeName), [
publishedDocId,
docTypeName,
]) as EditStateFor
}

3 comments on commit 0326d9c

@vercel
Copy link

@vercel vercel bot commented on 0326d9c Oct 19, 2021

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:

studio-workshop – ./dev/workshop

studio-workshop.sanity.build
studio-workshop-git-next.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 0326d9c Oct 19, 2021

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:

test-studio – ./

test-studio.sanity.build
test-studio-git-next.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 0326d9c Oct 19, 2021

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:

perf-studio – ./

perf-studio.sanity.build
perf-studio-git-next.sanity.build

Please sign in to comment.