-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): support remote mutations in version events
- Loading branch information
1 parent
5182f1c
commit 852f7d5
Showing
5 changed files
with
181 additions
and
19 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
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
92 changes: 92 additions & 0 deletions
92
packages/sanity/src/core/store/events/useRemoteMutations.ts
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,92 @@ | ||
import {type SanityClient} from '@sanity/client' | ||
import {useEffect, useMemo, useRef} from 'react' | ||
import {of, type Subscription, switchMap} from 'rxjs' | ||
|
||
import {useWorkspace} from '../../studio/workspace' | ||
import { | ||
getDraftId, | ||
getPublishedId, | ||
isDraftId, | ||
isPublishedId, | ||
isVersionId, | ||
} from '../../util/draftUtils' | ||
import {type DocumentRemoteMutationEvent} from '../_legacy/document/buffered-doc/types' | ||
import {remoteSnapshots, type WithVersion} from '../_legacy/document/document-pair' | ||
import {fetchFeatureToggle} from '../_legacy/document/document-pair/utils/fetchFeatureToggle' | ||
|
||
/** | ||
* This hooks takes care of listening to the transactions | ||
* received for a single document, it doesn't take the documentPair. | ||
*/ | ||
export function useRemoteMutations({ | ||
client, | ||
onUpdate, | ||
documentId, | ||
documentType, | ||
}: { | ||
client: SanityClient | ||
documentId: string | ||
documentType: string | ||
onUpdate: (event: WithVersion<DocumentRemoteMutationEvent>) => void | ||
}) { | ||
const snapshotsSubscriptionRef = useRef<Subscription | null>(null) | ||
const workspace = useWorkspace() | ||
|
||
const serverActionsEnabled = useMemo(() => { | ||
const configFlag = workspace.__internal_serverDocumentActions?.enabled | ||
// If it's explicitly set, let it override the feature toggle | ||
return typeof configFlag === 'boolean' ? of(configFlag as boolean) : fetchFeatureToggle(client) | ||
}, [client, workspace.__internal_serverDocumentActions?.enabled]) | ||
|
||
/** | ||
* Fetch document snapshots and update the mutable controller. | ||
* Unsubscribes on clean up, preventing double fetches in strict mode. | ||
*/ | ||
useEffect(() => { | ||
if (!snapshotsSubscriptionRef.current) { | ||
snapshotsSubscriptionRef.current = remoteSnapshots( | ||
client, | ||
{ | ||
draftId: getDraftId(documentId), | ||
publishedId: getPublishedId(documentId), | ||
...(isVersionId(documentId) | ||
? { | ||
versionId: documentId, | ||
} | ||
: {}), | ||
}, | ||
documentType, | ||
serverActionsEnabled, | ||
) | ||
.pipe( | ||
switchMap((event) => { | ||
// Type could be 'snapshot' or 'remoteMutation', we don't want the snapshots | ||
if (event.type !== 'remoteMutation') { | ||
return of(null) | ||
} | ||
// This is to only get the events we're interested in. | ||
// The eventsStore works for only 1 document at a time, not for the docPair. | ||
if (isVersionId(documentId) && event.version === 'version') { | ||
return of(event) | ||
} | ||
if (isPublishedId(documentId) && event.version === 'published') { | ||
return of(event) | ||
} | ||
if (isDraftId(documentId) && event.version === 'draft') { | ||
return of(event) | ||
} | ||
return of(null) | ||
}), | ||
) | ||
.subscribe((ev) => { | ||
if (ev) onUpdate(ev) | ||
}) | ||
} | ||
return () => { | ||
if (snapshotsSubscriptionRef.current) { | ||
snapshotsSubscriptionRef.current.unsubscribe() | ||
snapshotsSubscriptionRef.current = null | ||
} | ||
} | ||
}, [client, documentId, documentType, onUpdate, serverActionsEnabled]) | ||
} |
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