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 27, 2024
1 parent 9ab37af commit 0203132
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 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
17 changes: 7 additions & 10 deletions packages/sanity/src/core/util/draftUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ interface CollateOptions {
export function collate<
T extends {
_id: string
_originalId: string
_type: string
},
>(documents: T[], {bundlePerspective}: CollateOptions = {}): CollatedHit<T>[] {
Expand All @@ -213,21 +214,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 @@ -69,8 +69,8 @@ export function PaneItemPreview(props: PaneItemPreviewProps) {
})

const isInPerspective = useMemo(
() => getDocumentIsInPerspective(value._id, perspective),
[perspective, value._id],
() => getDocumentIsInPerspective(value._originalId ?? value._id, perspective),
[perspective, value._originalId, value._id],
)

const status = isLoading ? null : (
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 0203132

Please sign in to comment.