Skip to content

Commit

Permalink
fix: navigate to document when selecting it in index tool search
Browse files Browse the repository at this point in the history
  • Loading branch information
snorrees committed Sep 14, 2023
1 parent 5c32ab6 commit f7f6d26
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
20 changes: 19 additions & 1 deletion src/embeddingsIndexDashboard/QueryIndex.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {useCallback, useMemo} from 'react'
import {SemanticSearchAutocomplete} from '../referenceInput/SemanticSearchAutocomplete'
import {EmbeddingsIndexConfig} from '../schemas/typeDefExtensions'
import {useRouter} from 'sanity/router'
import {QueryResult} from '../api/embeddingsApi'

export function QueryIndex(props: {indexName: string}) {
const {indexName} = props
Expand All @@ -9,5 +11,21 @@ export function QueryIndex(props: {indexName: string}) {
() => ({indexName, maxResults: 8}),
[indexName],
)
return <SemanticSearchAutocomplete getEmptySearchValue={getEmpty} indexConfig={indexConfig} />

const {resolveIntentLink, navigateUrl} = useRouter()
const onSelect = useCallback(
(hit: QueryResult) => {
navigateUrl({
path: resolveIntentLink('edit', {id: hit.value.documentId, type: hit.value.type}),
})
},
[resolveIntentLink, navigateUrl],
)
return (
<SemanticSearchAutocomplete
getEmptySearchValue={getEmpty}
indexConfig={indexConfig}
onSelect={onSelect}
/>
)
}
13 changes: 9 additions & 4 deletions src/referenceInput/SemanticSearchAutocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface SemanticSearchAutocompleteProps {
getEmptySearchValue: () => string
typeFilter?: string[]
filterResult?: (hit: QueryResult) => boolean
onChange?: (value: string) => void
onSelect?: (value: QueryResult) => void
onFocus?: FocusEventHandler<HTMLInputElement>
onBlur?: FocusEventHandler<HTMLInputElement>
readOnly?: boolean
Expand Down Expand Up @@ -52,7 +52,7 @@ export const SemanticSearchAutocomplete = forwardRef(function SemanticSearchAuto
readOnly,
onFocus,
onBlur,
onChange,
onSelect,
typeFilter,
} = props
const id = useId()
Expand Down Expand Up @@ -138,9 +138,14 @@ export const SemanticSearchAutocomplete = forwardRef(function SemanticSearchAuto
setOptions(NO_OPTIONS)
return
}
onChange?.(value)
const option = (options as Option[])
.filter((r): r is Option => 'result' in r)
.find((r) => r.result.value.documentId === value)
if (option && onSelect) {
onSelect(option.result)
}
},
[onChange],
[onSelect, options],
)

return (
Expand Down
9 changes: 4 additions & 5 deletions src/referenceInput/SemanticSearchReferenceInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,16 @@ function SemanticSearchInput(props: ObjectInputProps & {indexConfig: EmbeddingsI
const handleBlur = useCallback(() => onPathFocus([]), [onPathFocus])

const handleChange = useCallback(
(nextId: string) => {
if (!nextId) {
(result: QueryResult) => {
if (!result) {
onChange(unset())
onPathFocus([])
return
}

const patches = [
setIfMissing({}),
set(schemaType.name, ['_type']),
set(publicId(nextId), ['_ref']),
set(publicId(result.value.documentId), ['_ref']),
unset(['_weak']),
unset(['_strengthenOnPublish']),
]
Expand Down Expand Up @@ -150,7 +149,7 @@ function SemanticSearchInput(props: ObjectInputProps & {indexConfig: EmbeddingsI
ref={autocompleteRef}
typeFilter={typeFilter}
indexConfig={indexConfig}
onChange={handleChange}
onSelect={handleChange}
onFocus={handleFocus}
onBlur={handleBlur}
getEmptySearchValue={getEmptySearchValue}
Expand Down

0 comments on commit f7f6d26

Please sign in to comment.