MCPcopy Create free account
hub / github.com/arktypeio/arktype / SearchPanel

Function SearchPanel

ark/docs/components/shell/SearchPanel.tsx:182–396  ·  view source on GitHub ↗
({
	initialQuery,
	entered,
	moved,
	close
}: SearchPanelProps)

Source from the content-addressed store, hash-verified

180}
181
182export const SearchPanel = ({
183 initialQuery,
184 entered,
185 moved,
186 close
187}: SearchPanelProps) => {
188 const [loaded, setLoaded] = useState<Loaded>()
189 const [failed, setFailed] = useState(false)
190 const [query, setQuery] = useState(initialQuery)
191 // an Enter pressed before there were results opens the first one to arrive
192 const [pending, setPending] = useState(entered ?? false)
193 const [scope, setScope] = useState<number>()
194 // arrows pressed before results arrive move from the first one when they do
195 const [selected, setSelected] = useState(moved ?? 0)
196 const input = useRef<HTMLInputElement>(null)
197 const router = useRouter()
198
199 useEffect(() => {
200 load().then(setLoaded, () => {
201 loading = undefined
202 setFailed(true)
203 })
204 }, [])
205
206 // before a paint or a keystroke, so the query typed while this loaded continues here
207 useLayoutEffect(() => {
208 const element = input.current
209 if (!element) return
210 element.focus()
211 element.setSelectionRange(element.value.length, element.value.length)
212 }, [])
213
214 const { groups, count } = useMemo(
215 () => (loaded ? resultsOf(loaded, query, scope) : { groups: [], count: 0 }),
216 [loaded, query, scope]
217 )
218 const hits = groups.flatMap(group => group.hits)
219 const active = hits[mod(selected, hits.length)]
220
221 useEffect(() => {
222 if (active)
223 document.getElementById(active.key)?.scrollIntoView({ block: "nearest" })
224 }, [active])
225
226 const go = (href: string) => {
227 close()
228 router.push(href)
229 }
230
231 useEffect(() => {
232 if (!pending || !loaded) return
233 setPending(false)
234 if (active) go(active.href)
235 })
236
237 const scopes =
238 loaded ?
239 [

Callers

nothing calls this directly

Calls 8

withBaseFunction · 0.90
modFunction · 0.85
goFunction · 0.85
chooseScopeFunction · 0.85
loadFunction · 0.70
resultsOfFunction · 0.70
mapMethod · 0.65
hasMethod · 0.45

Tested by

no test coverage detected