Skip to content

Commit

Permalink
fix(desk): always use default ordering on first rendering if any and …
Browse files Browse the repository at this point in the history
…update order on sort
  • Loading branch information
vicmeow authored and rexxars committed Dec 19, 2022
1 parent 299ac06 commit 663cc67
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
Expand Up @@ -55,10 +55,16 @@ export const DocumentListPane = memo(function DocumentListPane(props: DocumentLi
'layout',
defaultLayout
)

// Ensure that we use the defaultOrdering value from structure builder if any as the default
const defaultSortOrder = useMemo(() => {
return defaultOrdering?.length > 0 ? {by: defaultOrdering} : DEFAULT_ORDERING
}, [])

const [sortOrderRaw, setSortOrder] = useDeskToolSetting<SortOrder>(
typeName,
'sortOrder',
DEFAULT_ORDERING
defaultSortOrder
)

const sortWithOrderingFn =
Expand Down
Expand Up @@ -29,7 +29,7 @@ interface DocumentListState {
* @internal
*/
export function useDocumentList(opts: UseDocumentListOpts): DocumentListState {
const {apiVersion, defaultOrdering, filter, params, sortOrder} = opts
const {apiVersion, filter, params, sortOrder} = opts
const client = useClient(DEFAULT_STUDIO_CLIENT_OPTIONS)
const [fullList, setFullList] = useState(false)
const fullListRef = useRef(fullList)
Expand All @@ -47,7 +47,7 @@ export function useDocumentList(opts: UseDocumentListOpts): DocumentListState {
const extendedProjection = sortOrder?.extendedProjection
const projectionFields = ['_id', '_type']
const finalProjection = projectionFields.join(',')
const sortBy = defaultOrdering || sortOrder?.by || []
const sortBy = sortOrder?.by || []
const limit = fullList ? FULL_LIST_LIMIT : PARTIAL_PAGE_LIMIT
const sort = sortBy.length > 0 ? sortBy : DEFAULT_ORDERING.by
const order = toOrderClause(sort)
Expand All @@ -62,7 +62,7 @@ export function useDocumentList(opts: UseDocumentListOpts): DocumentListState {
}

return `*[${filter}]|order(${order})[0...${limit}]{${finalProjection}}`
}, [defaultOrdering, filter, fullList, sortOrder])
}, [filter, fullList, sortOrder])

const handleListChange = useCallback(
({toIndex}: VirtualListChangeOpts) => {
Expand Down

0 comments on commit 663cc67

Please sign in to comment.