2024-01-15 12:33:15 +00:00
|
|
|
'use client'
|
|
|
|
|
2024-02-05 14:32:50 +00:00
|
|
|
import { SEARCH_TYPE, SearchResult } from '@/types/search-types'
|
|
|
|
import { debounce } from '@/utils/debounce'
|
|
|
|
import { useRouter } from 'next/navigation'
|
|
|
|
import { useEffect, useRef, useState } from 'react'
|
2024-01-15 12:33:15 +00:00
|
|
|
import { useHotkeys } from 'react-hotkeys-hook'
|
2024-02-05 20:46:07 +00:00
|
|
|
import { Autocomplete, DropdownOption } from './Autocomplete'
|
2024-01-15 12:33:15 +00:00
|
|
|
|
2024-02-05 14:32:50 +00:00
|
|
|
const HOST_URL =
|
|
|
|
process.env.NODE_ENV === 'development'
|
|
|
|
? 'http://localhost:3001'
|
2024-02-05 14:52:05 +00:00
|
|
|
: process.env.NEXT_PUBLIC_SITE_URL ?? 'https://tldraw.dev'
|
2024-01-15 12:33:15 +00:00
|
|
|
|
2024-02-05 14:32:50 +00:00
|
|
|
export function Search() {
|
|
|
|
const [searchType, setSearchType] = useState<SEARCH_TYPE>(SEARCH_TYPE.NORMAL)
|
|
|
|
const [isLoading, setIsLoading] = useState(false)
|
|
|
|
const [searchResults, setSearchResults] = useState<DropdownOption[]>([])
|
|
|
|
const [query, setQuery] = useState('')
|
|
|
|
const [platform, setPlatform] = useState<'mac' | 'nonMac' | null>()
|
|
|
|
const rInput = useRef<HTMLInputElement>(null)
|
2024-01-15 12:33:15 +00:00
|
|
|
const router = useRouter()
|
|
|
|
|
2024-02-05 14:32:50 +00:00
|
|
|
const handleInputChange = debounce((query: string) => setQuery(query), 200)
|
2024-01-15 12:33:15 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2024-02-05 14:32:50 +00:00
|
|
|
async function handleFetchResults() {
|
|
|
|
if (!query) {
|
|
|
|
return
|
|
|
|
}
|
2024-01-15 12:33:15 +00:00
|
|
|
|
2024-02-05 14:32:50 +00:00
|
|
|
setIsLoading(true)
|
2024-01-15 12:33:15 +00:00
|
|
|
|
2024-02-05 14:32:50 +00:00
|
|
|
try {
|
|
|
|
const endPoint =
|
|
|
|
searchType === SEARCH_TYPE.AI
|
|
|
|
? `${HOST_URL}/api/ai?q=${query}`
|
|
|
|
: `${HOST_URL}/api/search?q=${query}`
|
|
|
|
const res = await fetch(endPoint)
|
|
|
|
if (res.ok) {
|
|
|
|
const json = await res.json()
|
2024-02-06 09:49:31 +00:00
|
|
|
const topExamples = json.results.examples.slice(0, 5)
|
2024-02-05 14:32:50 +00:00
|
|
|
const topArticles = json.results.articles.slice(0, 10)
|
2024-02-06 09:49:31 +00:00
|
|
|
const topAPI = json.results.apiDocs.slice(0, 20)
|
2024-02-05 14:32:50 +00:00
|
|
|
const allResults = topExamples.concat(topArticles).concat(topAPI)
|
2024-02-06 09:49:31 +00:00
|
|
|
|
|
|
|
if (allResults.length) {
|
|
|
|
setSearchResults(
|
|
|
|
allResults.map((result: SearchResult) => ({
|
|
|
|
label: result.title,
|
|
|
|
value: result.url,
|
|
|
|
group: result.sectionType,
|
|
|
|
}))
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
setSearchResults([{ label: 'No results found.', value: '#', group: 'docs' }])
|
|
|
|
}
|
2024-02-05 14:32:50 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
2024-01-15 12:33:15 +00:00
|
|
|
}
|
2024-02-05 14:32:50 +00:00
|
|
|
|
|
|
|
setIsLoading(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
handleFetchResults()
|
|
|
|
}, [query, searchType])
|
|
|
|
|
|
|
|
const handleChange = (url: string) => {
|
|
|
|
router.push(url.startsWith('/') ? url : `/${url}`)
|
|
|
|
}
|
|
|
|
|
2024-02-06 11:50:11 +00:00
|
|
|
// AI is turned off for now.
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2024-02-05 14:32:50 +00:00
|
|
|
const handleSearchTypeChange = () => {
|
2024-02-06 09:49:31 +00:00
|
|
|
setSearchResults([])
|
2024-02-05 14:32:50 +00:00
|
|
|
setSearchType(searchType === SEARCH_TYPE.AI ? SEARCH_TYPE.NORMAL : SEARCH_TYPE.AI)
|
2024-02-05 20:46:07 +00:00
|
|
|
handleInputChange(query)
|
2024-02-05 14:32:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setPlatform(
|
|
|
|
// TODO(mime): we should have a standard hook for this.
|
|
|
|
// And also, we should navigator.userAgentData.platform where available.
|
|
|
|
// eslint-disable-next-line deprecation/deprecation
|
|
|
|
typeof window !== 'undefined' && /mac/i.test(window.navigator.platform) ? 'mac' : 'nonMac'
|
|
|
|
)
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
useHotkeys('meta+k,ctrl+k', (e) => {
|
|
|
|
e.preventDefault()
|
|
|
|
rInput.current?.focus()
|
|
|
|
rInput.current?.select()
|
|
|
|
})
|
2024-01-15 12:33:15 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="search__wrapper">
|
2024-02-05 14:32:50 +00:00
|
|
|
<Autocomplete
|
|
|
|
ref={rInput}
|
2024-02-06 11:50:11 +00:00
|
|
|
// customUI={
|
|
|
|
// <button className="search__ai-toggle" onClick={handleSearchTypeChange}>
|
|
|
|
// {searchType === SEARCH_TYPE.NORMAL ? '✨ Search using AI' : '⭐ Search without AI'}
|
|
|
|
// </button>
|
|
|
|
// }
|
2024-02-05 14:32:50 +00:00
|
|
|
groups={['examples', 'docs', 'reference']}
|
|
|
|
groupsToLabel={{ examples: 'Examples', docs: 'Articles', reference: 'Reference' }}
|
|
|
|
options={searchResults}
|
|
|
|
isLoading={isLoading}
|
|
|
|
onInputChange={handleInputChange}
|
|
|
|
onChange={handleChange}
|
|
|
|
/>
|
|
|
|
{platform && (
|
|
|
|
<span className="search__keyboard">
|
2024-02-13 10:07:29 +00:00
|
|
|
<kbd data-platform={platform === 'mac' ? 'mac' : 'win'}>
|
|
|
|
<span>{platform === 'mac' ? '⌘' : 'Ctrl'}</span>
|
|
|
|
<span>K</span>
|
|
|
|
</kbd>
|
2024-02-05 14:32:50 +00:00
|
|
|
</span>
|
|
|
|
)}
|
2024-01-15 12:33:15 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|