diff --git a/packages/sanity/src/core/index.ts b/packages/sanity/src/core/index.ts index fb778de8f60..2267d06ea61 100644 --- a/packages/sanity/src/core/index.ts +++ b/packages/sanity/src/core/index.ts @@ -44,7 +44,7 @@ export { export * from './scheduledPublishing' export * from './schema' export type {SearchFactoryOptions, SearchOptions, SearchSort, SearchTerms} from './search' -export {createSearch, getSearchableTypes} from './search' +export {createSearch, getSearchableTypes, isPerspectiveRaw} from './search' export * from './store' export * from './studio' export * from './studioClient' diff --git a/packages/sanity/src/core/search/common/index.ts b/packages/sanity/src/core/search/common/index.ts index c8242bfd8a4..9c2bd22040f 100644 --- a/packages/sanity/src/core/search/common/index.ts +++ b/packages/sanity/src/core/search/common/index.ts @@ -1,3 +1,4 @@ export * from './deriveSearchWeightsFromType' export * from './getSearchableTypes' +export * from './isPerspectiveRaw' export * from './types' diff --git a/packages/sanity/src/core/search/common/isPerspectiveRaw.ts b/packages/sanity/src/core/search/common/isPerspectiveRaw.ts new file mode 100644 index 00000000000..be45eec9cc5 --- /dev/null +++ b/packages/sanity/src/core/search/common/isPerspectiveRaw.ts @@ -0,0 +1,13 @@ +/** + * Check if a perspective is 'raw' + * + * @param perspective - the id of the perspective + * @returns true if the perspective is 'raw' + * + * @internal + */ +export function isPerspectiveRaw(perspective: string | undefined): boolean { + if (!perspective) return false + + return perspective === 'raw' +} diff --git a/packages/sanity/src/core/search/text-search/createTextSearch.ts b/packages/sanity/src/core/search/text-search/createTextSearch.ts index 3acd66bbe1f..1c8401347d6 100644 --- a/packages/sanity/src/core/search/text-search/createTextSearch.ts +++ b/packages/sanity/src/core/search/text-search/createTextSearch.ts @@ -5,6 +5,7 @@ import {map} from 'rxjs/operators' import {removeDupes} from '../../util/draftUtils' import { deriveSearchWeightsFromType, + isPerspectiveRaw, type SearchOptions, type SearchPath, type SearchSort, @@ -132,7 +133,7 @@ export const createTextSearch: SearchStrategyFactory = ( factoryOptions, ) => { const {perspective} = factoryOptions - const isRaw = perspective === 'raw' + const isRaw = isPerspectiveRaw(perspective) // Search currently supports both strings (reference + cross dataset reference inputs) // or a SearchTerms object (omnisearch). diff --git a/packages/sanity/src/core/search/weighted/createSearchQuery.ts b/packages/sanity/src/core/search/weighted/createSearchQuery.ts index 68ea253bda8..eef78811fdc 100644 --- a/packages/sanity/src/core/search/weighted/createSearchQuery.ts +++ b/packages/sanity/src/core/search/weighted/createSearchQuery.ts @@ -4,6 +4,7 @@ import {compact, flatten, flow, toLower, trim, union, uniq, words} from 'lodash' import { deriveSearchWeightsFromType, + isPerspectiveRaw, type SearchFactoryOptions, type SearchOptions, type SearchPath, @@ -128,7 +129,7 @@ export function createSearchQuery( // Extract search terms from string query, factoring in phrases wrapped in quotes const terms = extractTermsFromQuery(searchTerms.query) const {perspective} = searchOpts - const isRaw = perspective === 'raw' + const isRaw = isPerspectiveRaw(perspective) // Construct search filters used in this GROQ query const filters = [ diff --git a/packages/sanity/src/core/studio/components/navbar/search/components/searchResults/item/SearchResultItemPreview.tsx b/packages/sanity/src/core/studio/components/navbar/search/components/searchResults/item/SearchResultItemPreview.tsx index dd0cc24c04e..5d60ede6ae8 100644 --- a/packages/sanity/src/core/studio/components/navbar/search/components/searchResults/item/SearchResultItemPreview.tsx +++ b/packages/sanity/src/core/studio/components/navbar/search/components/searchResults/item/SearchResultItemPreview.tsx @@ -3,7 +3,7 @@ import {type SchemaType} from '@sanity/types' import {Badge, Box, Flex} from '@sanity/ui' import {useMemo} from 'react' import {useObservable} from 'react-rx' -import {useReleases, useSearchState} from 'sanity' +import {isPerspectiveRaw, useReleases, useSearchState} from 'sanity' import {styled} from 'styled-components' import {type GeneralPreviewLayoutKey} from '../../../../../../../components' @@ -54,7 +54,7 @@ export function SearchResultItemPreview({ const releases = useReleases() const {bundlesPerspective} = usePerspective() const {state} = useSearchState() - const isRaw = state.perspective === 'raw' + const isRaw = isPerspectiveRaw(state.perspective) const observable = useMemo( () => diff --git a/packages/sanity/src/core/studio/components/navbar/search/contexts/search/SearchProvider.tsx b/packages/sanity/src/core/studio/components/navbar/search/contexts/search/SearchProvider.tsx index 4f351d8d66a..ead9d713813 100644 --- a/packages/sanity/src/core/studio/components/navbar/search/contexts/search/SearchProvider.tsx +++ b/packages/sanity/src/core/studio/components/navbar/search/contexts/search/SearchProvider.tsx @@ -6,7 +6,7 @@ import {type CommandListHandle} from '../../../../../../components' import {useSchema} from '../../../../../../hooks' import {usePerspective} from '../../../../../../releases/hooks/usePerspective' import {useReleases} from '../../../../../../releases/store/useReleases' -import {type SearchTerms} from '../../../../../../search' +import {isPerspectiveRaw, type SearchTerms} from '../../../../../../search' import {useCurrentUser} from '../../../../../../store' import {resolvePerspectiveOptions} from '../../../../../../util/resolvePerspective' import {useSource} from '../../../../../source' @@ -135,10 +135,9 @@ export function SearchProvider({ ordering?.customMeasurementLabel || `${ordering.sort?.field} ${ordering.sort?.direction}` } - const perspectives = - state.perspective === 'raw' - ? {bundlePerspective: undefined, perspective: undefined} - : resolvePerspectiveOptions(bundlesPerspective) + const perspectives = isPerspectiveRaw(state.perspective) + ? {bundlePerspective: undefined, perspective: undefined} + : resolvePerspectiveOptions(bundlesPerspective) handleSearch({ options: {