Skip to content

Commit

Permalink
refactor(sanity): add helper method to check perspective
Browse files Browse the repository at this point in the history
  • Loading branch information
RitaDias committed Nov 18, 2024
1 parent 06e71a2 commit 96963ed
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/sanity/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
1 change: 1 addition & 0 deletions packages/sanity/src/core/search/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './deriveSearchWeightsFromType'
export * from './getSearchableTypes'
export * from './isPerspectiveRaw'
export * from './types'
13 changes: 13 additions & 0 deletions packages/sanity/src/core/search/common/isPerspectiveRaw.ts
Original file line number Diff line number Diff line change
@@ -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'
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {map} from 'rxjs/operators'
import {removeDupes} from '../../util/draftUtils'
import {
deriveSearchWeightsFromType,
isPerspectiveRaw,
type SearchOptions,
type SearchPath,
type SearchSort,
Expand Down Expand Up @@ -132,7 +133,7 @@ export const createTextSearch: SearchStrategyFactory<TextSearchResults> = (
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).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {compact, flatten, flow, toLower, trim, union, uniq, words} from 'lodash'

import {
deriveSearchWeightsFromType,
isPerspectiveRaw,
type SearchFactoryOptions,
type SearchOptions,
type SearchPath,
Expand Down Expand Up @@ -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 = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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(
() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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: {
Expand Down

0 comments on commit 96963ed

Please sign in to comment.