Skip to content

Commit

Permalink
fix: restore support for defaultOrdering. (#7626)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge authored Oct 17, 2024
1 parent 4ffa079 commit 02da757
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
11 changes: 11 additions & 0 deletions dev/test-studio/structure/resolveStructure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,17 @@ export const structure: StructureResolver = (S, {schema, documentStore, i18n}) =

S.divider(),

S.listItem()
.title('Default ordering test')
.id('default-ordering')
.child(() =>
S.documentTypeList('species')
.defaultOrdering([{field: 'species', direction: 'asc'}])
.title('Species')
.id('default-ordering-list')
.filter('_type == $type'),
),

...S.documentTypeListItems()
.filter((listItem) => {
const id = listItem.getId()
Expand Down
14 changes: 8 additions & 6 deletions packages/sanity/src/core/store/key-value/localStorageSWR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import {type KeyValueStore, type KeyValueStoreValue} from './types'
*/
export function withLocalStorageSWR(wrappedStore: KeyValueStore): KeyValueStore {
function getKey(key: string) {
return merge(of(localStoreStorage.getKey(key)), wrappedStore.getKey(key)).pipe(
distinctUntilChanged(isEqual),
tap((value) => {
localStoreStorage.setKey(key, value)
}),
)
return merge(
of(localStoreStorage.getKey(key)),
wrappedStore.getKey(key).pipe(
tap((wrappedStoreValue) => {
localStoreStorage.setKey(key, wrappedStoreValue)
}),
),
).pipe(distinctUntilChanged(isEqual))
}
function setKey(key: string, nextValue: KeyValueStoreValue) {
localStoreStorage.setKey(key, nextValue)
Expand Down
7 changes: 5 additions & 2 deletions packages/sanity/src/structure/useStructureToolSetting.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {useCallback, useMemo} from 'react'
import {useObservable} from 'react-rx'
import {map} from 'rxjs/operators'
import {useKeyValueStore} from 'sanity'

const STRUCTURE_TOOL_NAMESPACE = 'studio.structure-tool'
Expand All @@ -17,8 +18,10 @@ export function useStructureToolSetting<ValueType>(
const keyValueStoreKey = [STRUCTURE_TOOL_NAMESPACE, namespace, key].filter(Boolean).join('.')

const value$ = useMemo(() => {
return keyValueStore.getKey(keyValueStoreKey)
}, [keyValueStore, keyValueStoreKey])
return keyValueStore
.getKey(keyValueStoreKey)
.pipe(map((value) => (value === null ? defaultValue : value)))
}, [defaultValue, keyValueStore, keyValueStoreKey])

const value = useObservable(value$, defaultValue) as ValueType
const set = useCallback(
Expand Down

0 comments on commit 02da757

Please sign in to comment.