import { Header } from '@/components/Header'
import { Sidebar } from '@/components/Sidebar'
import { SearchResult } from '@/types/search-types'
import { getDb } from '@/utils/ContentDatabase'
import Link from 'next/link'
import process from 'process'
const HOST_URL =
process.env.NODE_ENV === 'development'
? 'http://localhost:3001'
: process.env.NEXT_PUBLIC_SITE_URL ?? 'https://www.tldraw.dev'
export default async function SearchResultsPage({
searchParams,
}: {
searchParams: { q: string; t: string }
}) {
const query = searchParams.q?.toString() as string
const type = searchParams.t?.toString() as string
const db = await getDb()
const sidebar = await db.getSidebarContentList({})
let results: {
articles: SearchResult[]
apiDocs: SearchResult[]
error: null | string
} = {
articles: [],
apiDocs: [],
error: null,
}
if (query) {
const endPoint =
type === 'ai' ? `${HOST_URL}/api/ai?q=${query}` : `${HOST_URL}/api/search?q=${query}`
const res = await fetch(endPoint)
if (!res.ok) {
results.error = await res.text()
} else {
const json = await res.json()
results = json.results
}
}
return (
<>
{`Found ${
results.articles.length + results.apiDocs.length
} results for "${query}"`}
API Docs
>
)}
{results.apiDocs.length > 0 &&