Skip to content

Commit 4159bca

Browse files
authoredMar 4, 2024
chore(search): refactor search to export necessary components and providers. (#5900)
* chore(search): refactor search to export necessary components and providers * chore(search): add ItemSelectHandler type
1 parent 3670459 commit 4159bca

File tree

12 files changed

+60
-8
lines changed

12 files changed

+60
-8
lines changed
 

‎packages/sanity/src/core/studio/components/navbar/search/components/SearchPopover.tsx

+22-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@ import {SearchWrapper} from './common/SearchWrapper'
1818
import {Filters} from './filters/Filters'
1919
import {RecentSearches} from './recentSearches/RecentSearches'
2020
import {SearchHeader} from './SearchHeader'
21+
import {type ItemSelectHandler} from './searchResults/item/SearchResultItem'
2122
import {SearchResults} from './searchResults/SearchResults'
2223

24+
/**
25+
* @internal
26+
*/
2327
export interface SearchPopoverProps {
2428
disableFocusLock?: boolean
29+
disableIntentLink?: boolean
2530
onClose: () => void
31+
onItemSelect?: ItemSelectHandler
2632
onOpen: () => void
2733
open: boolean
2834
}
@@ -66,7 +72,17 @@ const SearchMotionCard = styled(motion(Card))`
6672
width: min(calc(100vw - ${POPOVER_INPUT_PADDING * 2}px), ${POPOVER_MAX_WIDTH}px);
6773
`
6874

69-
export function SearchPopover({disableFocusLock, onClose, onOpen, open}: SearchPopoverProps) {
75+
/**
76+
* @internal
77+
*/
78+
export function SearchPopover({
79+
disableFocusLock,
80+
disableIntentLink,
81+
onClose,
82+
onItemSelect,
83+
onOpen,
84+
open,
85+
}: SearchPopoverProps) {
7086
const [inputElement, setInputElement] = useState<HTMLInputElement | null>(null)
7187

7288
const popoverElement = useRef<HTMLElement | null>(null)
@@ -134,7 +150,11 @@ export function SearchPopover({disableFocusLock, onClose, onOpen, open}: SearchP
134150
</Card>
135151
)}
136152
{hasValidTerms ? (
137-
<SearchResults inputElement={inputElement} />
153+
<SearchResults
154+
inputElement={inputElement}
155+
onItemSelect={onItemSelect}
156+
disableIntentLink={disableIntentLink}
157+
/>
138158
) : (
139159
<RecentSearches inputElement={inputElement} />
140160
)}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './SearchPopover'
2+
export * from './searchResults'

‎packages/sanity/src/core/studio/components/navbar/search/components/searchResults/SearchResults.tsx

+7-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {NoResults} from '../NoResults'
1111
import {SearchError} from '../SearchError'
1212
import {SortMenu} from '../SortMenu'
1313
import {DebugOverlay} from './item/DebugOverlay'
14-
import {SearchResultItem} from './item/SearchResultItem'
14+
import {type ItemSelectHandler, SearchResultItem} from './item/SearchResultItem'
1515

1616
const VIRTUAL_LIST_SEARCH_RESULT_ITEM_HEIGHT = 57 // px
1717
const VIRTUAL_LIST_OVERSCAN = 4
@@ -26,10 +26,12 @@ const SearchResultsInnerFlex = styled(Flex)<{$loading: boolean}>`
2626
`
2727

2828
interface SearchResultsProps {
29+
disableIntentLink?: boolean
2930
inputElement: HTMLInputElement | null
31+
onItemSelect?: ItemSelectHandler
3032
}
3133

32-
export function SearchResults({inputElement}: SearchResultsProps) {
34+
export function SearchResults({disableIntentLink, inputElement, onItemSelect}: SearchResultsProps) {
3335
const {
3436
dispatch,
3537
onClose,
@@ -59,16 +61,18 @@ export function SearchResults({inputElement}: SearchResultsProps) {
5961
return (
6062
<>
6163
<SearchResultItem
64+
disableIntentLink={disableIntentLink}
6265
documentId={getPublishedId(item.hit._id) || ''}
6366
documentType={item.hit._type}
6467
onClick={handleSearchResultClick}
68+
onItemSelect={onItemSelect}
6569
paddingY={1}
6670
/>
6771
{debug && <DebugOverlay data={item} />}
6872
</>
6973
)
7074
},
71-
[debug, handleSearchResultClick],
75+
[debug, disableIntentLink, handleSearchResultClick, onItemSelect],
7276
)
7377

7478
return (
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './item'

‎packages/sanity/src/core/studio/components/navbar/search/components/searchResults/item/SearchResultItem.tsx

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1+
import {type SanityDocumentLike} from '@sanity/types'
12
import {Box, type ResponsiveMarginProps, type ResponsivePaddingProps} from '@sanity/ui'
23
import {type MouseEvent, useCallback, useMemo} from 'react'
34
import {useIntentLink} from 'sanity/router'
45

56
import {type GeneralPreviewLayoutKey, PreviewCard} from '../../../../../../../components'
67
import {useSchema} from '../../../../../../../hooks'
78
import {useDocumentPresence} from '../../../../../../../store'
8-
import SearchResultItemPreview from './SearchResultItemPreview'
9+
import {SearchResultItemPreview} from './SearchResultItemPreview'
10+
11+
export type ItemSelectHandler = (item: Pick<SanityDocumentLike, '_id' | '_type'>) => void
912

1013
interface SearchResultItemProps extends ResponsiveMarginProps, ResponsivePaddingProps {
1114
disableIntentLink?: boolean
1215
documentId: string
1316
documentType: string
1417
layout?: GeneralPreviewLayoutKey
1518
onClick?: () => void
19+
onItemSelect?: ItemSelectHandler
1620
}
1721

1822
export function SearchResultItem({
@@ -21,6 +25,7 @@ export function SearchResultItem({
2125
documentType,
2226
layout,
2327
onClick,
28+
onItemSelect,
2429
...rest
2530
}: SearchResultItemProps) {
2631
const schema = useSchema()
@@ -35,12 +40,13 @@ export function SearchResultItem({
3540

3641
const handleClick = useCallback(
3742
(e: MouseEvent<HTMLElement>) => {
43+
onItemSelect?.({_id: documentId, _type: documentType})
3844
if (!disableIntentLink) {
3945
onIntentClick(e)
4046
}
4147
onClick?.()
4248
},
43-
[disableIntentLink, onClick, onIntentClick],
49+
[onItemSelect, documentId, documentType, disableIntentLink, onClick, onIntentClick],
4450
)
4551

4652
if (!type) return null

‎packages/sanity/src/core/studio/components/navbar/search/components/searchResults/item/SearchResultItemPreview.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ const SearchResultItemPreviewBox = styled(Box)`
3535
}
3636
`
3737

38-
export default function SearchResultItemPreview({
38+
/**
39+
* @internal
40+
*/
41+
export function SearchResultItemPreview({
3942
documentId,
4043
layout,
4144
presence,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './SearchResultItemPreview'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './search'

‎packages/sanity/src/core/studio/components/navbar/search/contexts/search/SearchContext.ts

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import {type CommandListHandle} from '../../../../../../components/commandList/t
44
import {type RecentSearchesStore} from '../../datastores/recentSearches'
55
import {type SearchAction, type SearchReducerState} from './reducer'
66

7+
/**
8+
* @internal
9+
*/
710
export interface SearchContextValue {
811
dispatch: Dispatch<SearchAction>
912
onClose: (() => void) | null
@@ -14,4 +17,7 @@ export interface SearchContextValue {
1417
state: SearchReducerState
1518
}
1619

20+
/**
21+
* @internal
22+
*/
1723
export const SearchContext = createContext<SearchContextValue | undefined>(undefined)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './SearchContext'
2+
export * from './SearchProvider'
3+
export * from './useSearchState'

‎packages/sanity/src/core/studio/components/navbar/search/contexts/search/useSearchState.ts

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import {useContext} from 'react'
22

33
import {SearchContext, type SearchContextValue} from './SearchContext'
44

5+
/**
6+
* @internal
7+
*/
58
export function useSearchState(): SearchContextValue {
69
const context = useContext(SearchContext)
710

‎packages/sanity/src/core/studio/components/navbar/search/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export * from './components'
2+
export * from './contexts'
13
export {
24
defineSearchFilter,
35
defineSearchFilterOperators,

0 commit comments

Comments
 (0)
Please sign in to comment.