Skip to content
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(sanity): fix type issues with collate, update tests and readQueryResult #7587

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export function referenceSearch(
})
return search(textTerm, {includeDrafts: true}).pipe(
map(({hits}) => hits.map(({hit}) => hit)),
map((docs) => collate(docs)),
map((docs) => collate({documents: docs})),
// pick the 100 best matches
map((collated) => collated.slice(0, 100)),
mergeMap((collated) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function search(
isCrossDataset: true,
}).pipe(
map(({hits}) => hits.map(({hit}) => hit)),
map((docs) => collate(docs)),
map((docs) => collate({documents: docs})),
map((collated) =>
collated.map((entry) => ({
id: entry.id,
Expand Down
12 changes: 6 additions & 6 deletions packages/sanity/src/core/util/draftUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {describe, expect, it, test} from '@jest/globals'
import {type SanityDocument} from '@sanity/types'
import {type SanityDocument, type SanityDocumentLike} from '@sanity/types'

import {
collate,
Expand All @@ -11,12 +11,12 @@ import {
} from './draftUtils'

test('collate()', () => {
const foo = {_type: 'foo', _id: 'foo'}
const fooDraft = {_type: 'foo', _id: 'drafts.foo'}
const barDraft = {_type: 'foo', _id: 'drafts.bar'}
const baz = {_type: 'foo', _id: 'baz'}
const foo = {_type: 'foo', _id: 'foo'} as SanityDocumentLike
const fooDraft = {_type: 'foo', _id: 'drafts.foo'} as SanityDocumentLike
const barDraft = {_type: 'foo', _id: 'drafts.bar'} as SanityDocumentLike
const baz = {_type: 'foo', _id: 'baz'} as SanityDocumentLike

expect(collate([foo, fooDraft, barDraft, baz])).toEqual([
expect(collate({documents: [foo, fooDraft, barDraft, baz]})).toEqual([
{type: 'foo', id: 'foo', draft: fooDraft, published: foo},
{type: 'foo', id: 'bar', draft: barDraft},
{type: 'foo', id: 'baz', published: baz},
Expand Down
8 changes: 5 additions & 3 deletions packages/sanity/src/core/util/draftUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,10 @@ export interface CollatedHit<T extends {_id: string} = {_id: string}> {
}

/** @internal */
export function collate<T extends {_id: string; _type: string}>(documents: T[]): CollatedHit<T>[] {
const byId = documents.reduce((res, doc) => {
export function collate<T extends {_id: string; _type: string}>(documents: {
documents: SanityDocumentLike[]
}): CollatedHit<T>[] {
const byId = documents.documents?.reduce((res, doc) => {
const publishedId = getPublishedId(doc._id)
let entry = res.get(publishedId)
if (!entry) {
Expand All @@ -199,7 +201,7 @@ export function collate<T extends {_id: string; _type: string}>(documents: T[]):
/** @internal */
// Removes published documents that also has a draft
export function removeDupes(documents: SanityDocumentLike[]): SanityDocumentLike[] {
return collate(documents)
return collate({documents})
.map((entry) => entry.draft || entry.published)
.filter(isNonNullable)
}
4 changes: 2 additions & 2 deletions packages/sanity/src/structure/panes/documentList/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export function getDocumentKey(value: DocumentListPaneItem, index: number): stri
}

export function removePublishedWithDrafts(documents: SanityDocumentLike[]): DocumentListPaneItem[] {
return collate(documents).map((entry) => {
return collate({documents}).map((entry) => {
const doc = entry.draft || entry.published
const isVersion = doc?.id && isVersionId(doc._id)
const isVersion = doc?._id && isVersionId(doc._id)
const hasDraft = Boolean(entry.draft)

return {
Expand Down
6 changes: 6 additions & 0 deletions packages/sanity/src/structure/panes/documentList/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import {type SanityDocumentLike} from '@sanity/types'
import {type SearchSort} from 'sanity'

export interface QueryResult {
error: {message: string} | null
onRetry?: () => void
result: {documents: SanityDocumentLike[]} | null
}

export interface DocumentListPaneItem extends SanityDocumentLike {
hasPublished: boolean
hasDraft: boolean
Expand Down
Loading