Skip to content

Commit

Permalink
wip: adopt bundlePerspective
Browse files Browse the repository at this point in the history
  • Loading branch information
juice49 committed Aug 23, 2024
1 parent 84f8d11 commit 2780b82
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
1 change: 1 addition & 0 deletions packages/sanity/src/core/search/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export type SearchOptions = {
cursor?: string
limit?: number
perspective?: string
bundlePerspective?: string
isCrossDataset?: boolean
queryType?: 'prefixLast' | 'prefixNone'
}
Expand Down
8 changes: 6 additions & 2 deletions packages/sanity/src/core/search/weighted/createSearchQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export function createSearchQuery(
// Default to `_id asc` (GROQ default) if no search sort is provided
const sortOrder = toOrderClause(searchOpts?.sort || [{field: '_id', direction: 'asc'}])

const projectionFields = ['_type', '_id', '_version']
const projectionFields = ['_type', '_id', '_originalId', '_version']
const selection = selections.length > 0 ? `...select(${selections.join(',\n')})` : ''
const finalProjection = projectionFields.join(', ') + (selection ? `, ${selection}` : '')

Expand Down Expand Up @@ -186,7 +186,11 @@ export function createSearchQuery(
__limit: limit,
...(params || {}),
},
options: {tag, perspective: searchOpts.perspective},
options: {
tag,
perspective: searchOpts.perspective,
bundlePerspective: searchOpts.bundlePerspective,
},
searchSpec: specs,
terms,
}
Expand Down
19 changes: 8 additions & 11 deletions packages/sanity/src/core/util/draftUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {type SanityDocument, type SanityDocumentLike} from '@sanity/types'

import {getBundleSlug} from '../bundles/util/util'
import {getBundleSlug} from '../bundles'
import {isNonNullable} from './isNonNullable'

/** @internal */
Expand Down Expand Up @@ -175,6 +175,7 @@ interface CollateOptions {
export function collate<
T extends {
_id: string
_originalId: string
_type: string
},
>(documents: T[], {bundlePerspective}: CollateOptions = {}): CollatedHit<T>[] {
Expand All @@ -200,21 +201,17 @@ export function collate<
}

if (!isVersion) {
entry[publishedId === doc._id ? 'published' : 'draft'] = doc
const isPublished = publishedId === doc._id
entry[isPublished ? 'published' : 'draft'] = doc
if (isPublished) {
entry.version = doc
}
}

return res
}, new Map())

return (
Array.from(byId.values())
// Remove entries that have no data, because all the following conditions are true:
//
// 1. They have no published version.
// 2. They have no draft version.
// 3. They have a version, but not the one that is currently checked out.
.filter((entry) => entry.published ?? entry.version ?? entry.draft)
)
return Array.from(byId.values())
}

/** @internal */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ export function listenSearchQuery(options: ListenQueryOptions): Observable<Sanit
comments: [`findability-source: ${searchQuery ? 'list-query' : 'list'}`],
limit,
perspective: omitBundlePerspective(perspective),
bundlePerspective: perspective?.startsWith('bundle.')
? perspective.split('bundle.').at(1)
: undefined,
skipSortByScore: true,
sort: sortBy,
}
Expand Down

0 comments on commit 2780b82

Please sign in to comment.