-
Notifications
You must be signed in to change notification settings - Fork 430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: move to system-only release documents #7824
Merged
+101
−123
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
import {type ListenEvent, type ListenOptions, type SanityClient} from '@sanity/client' | ||
import {ClientError, type SanityClient} from '@sanity/client' | ||
import { | ||
BehaviorSubject, | ||
catchError, | ||
concat, | ||
concatWith, | ||
EMPTY, | ||
filter, | ||
map, | ||
from, | ||
merge, | ||
type Observable, | ||
of, | ||
|
@@ -16,24 +18,23 @@ import { | |
tap, | ||
timeout, | ||
} from 'rxjs' | ||
import {startWith} from 'rxjs/operators' | ||
import {mergeMapArray} from 'rxjs-mergemap-array' | ||
import {map, mergeMap, startWith, toArray} from 'rxjs/operators' | ||
|
||
import {type DocumentPreviewStore} from '../../preview' | ||
import {listenQuery} from '../../store/_legacy' | ||
import {RELEASE_METADATA_TMP_DOC_PATH} from './constants' | ||
import {RELEASE_DOCUMENT_TYPE, RELEASE_DOCUMENTS_PATH} from './constants' | ||
import {createReleaseMetadataAggregator} from './createReleaseMetadataAggregator' | ||
import {requestAction} from './createReleaseOperationStore' | ||
import {releasesReducer, type ReleasesReducerAction, type ReleasesReducerState} from './reducer' | ||
import {type ReleaseDocument, type ReleaseStore, type ReleaseSystemDocument} from './types' | ||
import {type ReleaseDocument, type ReleaseStore} from './types' | ||
|
||
type ActionWrapper = {action: ReleasesReducerAction} | ||
type EventWrapper = {event: ListenEvent<ReleaseDocument>} | ||
type ResponseWrapper = {response: ReleaseDocument[]} | ||
|
||
export const SORT_FIELD = '_createdAt' | ||
export const SORT_ORDER = 'desc' | ||
|
||
const QUERY_FILTERS = [`_type=="system.release" && (_id in path("_.**"))`] | ||
const QUERY_FILTER = `_type=="${RELEASE_DOCUMENT_TYPE}"(_id in path("${RELEASE_DOCUMENTS_PATH}.*"))` | ||
|
||
// TODO: Extend the projection with the fields needed | ||
const QUERY_PROJECTION = `{ | ||
|
@@ -43,21 +44,56 @@ const QUERY_PROJECTION = `{ | |
// Newest releases first | ||
const QUERY_SORT_ORDER = `order(${SORT_FIELD} ${SORT_ORDER})` | ||
|
||
const QUERY = `*[${QUERY_FILTERS.join(' && ')}] ${QUERY_PROJECTION} | ${QUERY_SORT_ORDER}` | ||
|
||
const LISTEN_OPTIONS: ListenOptions = { | ||
events: ['welcome', 'mutation', 'reconnect'], | ||
includeResult: true, | ||
visibility: 'query', | ||
tag: 'releases.listen', | ||
} | ||
const QUERY = `*[${QUERY_FILTER}] ${QUERY_PROJECTION} | ${QUERY_SORT_ORDER}` | ||
|
||
const INITIAL_STATE: ReleasesReducerState = { | ||
releases: new Map(), | ||
state: 'loaded' as const, | ||
releaseStack: [], | ||
} | ||
|
||
const RELEASE_METADATA_TMP_DOC_PATH = 'system-tmp-releases' | ||
// todo: remove this after first tagged release | ||
function migrateWith(client: SanityClient) { | ||
return client.observable.fetch(`*[_id in path('${RELEASE_METADATA_TMP_DOC_PATH}.**')]`).pipe( | ||
tap((tmpDocs: ReleaseDocument[]) => { | ||
// eslint-disable-next-line | ||
console.log('Migrating %d release documents', tmpDocs.length) | ||
}), | ||
mergeMap((tmpDocs: ReleaseDocument[]) => { | ||
if (tmpDocs.length === 0) { | ||
return EMPTY | ||
} | ||
return from(tmpDocs).pipe( | ||
mergeMap(async (tmpDoc) => { | ||
const releaseId = tmpDoc._id.slice(RELEASE_METADATA_TMP_DOC_PATH.length + 1) | ||
await requestAction(client, { | ||
actionType: 'sanity.action.release.edit', | ||
releaseId, | ||
patch: { | ||
set: {userMetadata: tmpDoc.metadata}, | ||
}, | ||
}).catch((err) => { | ||
if (err instanceof ClientError) { | ||
if (err.details.description == `Release "${releaseId}" was not found`) { | ||
// ignore | ||
return | ||
} | ||
} | ||
throw err | ||
}) | ||
await client.delete(tmpDoc._id) | ||
}, 2), | ||
) | ||
}), | ||
toArray(), | ||
tap((migrated) => { | ||
// eslint-disable-next-line | ||
console.log('Migrated %d releases', migrated.length) | ||
}), | ||
mergeMap(() => EMPTY), | ||
) | ||
} | ||
/** | ||
* The releases store is initialised lazily when first subscribed to. Upon subscription, it will | ||
* fetch a list of releases and create a listener to keep the locally held state fresh. | ||
|
@@ -70,7 +106,7 @@ export function createReleaseStore(context: { | |
previewStore: DocumentPreviewStore | ||
client: SanityClient | ||
}): ReleaseStore { | ||
const {client, previewStore} = context | ||
const {client} = context | ||
|
||
const dispatch$ = new Subject<ReleasesReducerAction>() | ||
const fetchPending$ = new BehaviorSubject<boolean>(false) | ||
|
@@ -100,29 +136,18 @@ export function createReleaseStore(context: { | |
resetOnSuccess: true, | ||
}), | ||
tap(() => fetchPending$.next(false)), | ||
mergeMapArray((releaseSystemDocument: ReleaseSystemDocument) => | ||
previewStore | ||
// Temporary release metadata document | ||
.unstable_observeDocument( | ||
`${RELEASE_METADATA_TMP_DOC_PATH}.${releaseSystemDocument.name}`, | ||
) | ||
.pipe( | ||
map((metadataDocument) => ({ | ||
metadataDocument, | ||
releaseSystemDocument, | ||
})), | ||
), | ||
), | ||
map((results) => | ||
results.flatMap(({metadataDocument, releaseSystemDocument}): ReleaseDocument[] => { | ||
return metadataDocument && releaseSystemDocument | ||
? {...(metadataDocument as any), ...releaseSystemDocument} | ||
: [] | ||
}), | ||
map((releases) => | ||
releases.map( | ||
(releaseDoc: ReleaseDocument): ReleaseDocument => ({ | ||
...releaseDoc, | ||
metadata: {...(releaseDoc as any).userMetadata, ...releaseDoc.metadata}, | ||
}), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. userMetadata is going to be renamed to |
||
), | ||
), | ||
map((response) => ({response})), | ||
map((releases) => ({response: releases})), | ||
), | ||
), | ||
|
||
catchError((error) => | ||
of<ActionWrapper>({ | ||
action: { | ||
|
@@ -154,7 +179,8 @@ export function createReleaseStore(context: { | |
), | ||
) | ||
|
||
const state$ = merge(listFetch$, dispatch$).pipe( | ||
const migrateTmpReleases = process.env.NODE_ENV === 'development' ? migrateWith(client) : EMPTY | ||
const state$ = concat(migrateTmpReleases, merge(listFetch$, dispatch$)).pipe( | ||
filter((action): action is ReleasesReducerAction => typeof action !== 'undefined'), | ||
scan((state, action) => releasesReducer(state, action), INITIAL_STATE), | ||
startWith(INITIAL_STATE), | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
import {useMemo} from 'react' | ||
|
||
import {useClient} from '../../hooks' | ||
import {useCurrentUser} from '../../store/user' | ||
import {DEFAULT_STUDIO_CLIENT_OPTIONS} from '../../studioClient' | ||
import {createReleaseOperationsStore} from './createReleaseOperationStore' | ||
|
||
|
@@ -10,15 +9,11 @@ import {createReleaseOperationsStore} from './createReleaseOperationStore' | |
*/ | ||
export function useReleaseOperations() { | ||
const studioClient = useClient(DEFAULT_STUDIO_CLIENT_OPTIONS) | ||
const currentUser = useCurrentUser() | ||
|
||
return useMemo( | ||
() => | ||
createReleaseOperationsStore({ | ||
client: studioClient, | ||
// todo: is this non-null assertion safe? | ||
currentUser: currentUser!, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. release store doesn't require current user anymore |
||
}), | ||
[currentUser, studioClient], | ||
[studioClient], | ||
) | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this migrates from old system-tmp-releases docments in the background (for projects that may have created them during testing). We should remove this shortly