'use client'
import ButtonCard from '~/components/ButtonCard'
import { useSendTelemetryEvent } from '~/lib/telemetry'
import { useDocsSearchV2, type DocsSearchV2Result } from 'common'
import { usePathname } from 'next/navigation'
import { useEffect } from 'react'
import { Button, cn } from 'ui'
import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader'
import { SearchV2Dialog, useSearchV2Dialog } from '@/features/SearchV2'
import { SEARCH_V2_ENDPOINT } from '@/features/SearchV2/constants'
function SearchButton() {
const { open, openDialog, handleOpenChange } = useSearchV2Dialog()
return (
<>
>
)
}
function Recommendations() {
const pathname = usePathname()
const { searchState: state, handleDocsSearch: handleSearch } = useDocsSearchV2({
endpoint: SEARCH_V2_ENDPOINT,
})
const loading = state.status === 'initial' || state.status === 'loading'
const recommendations = state.status === 'results' ? uniqueByPath(state.results) : []
useEffect(() => {
if (!pathname) return
const query = decodeURIComponent(pathname.replace(/^\/(?:guides|reference)\//, '')).replace(
/[_\/-]/g,
' '
)
handleSearch(query)
}, [handleSearch, pathname])
return (
Are you looking for...?
{loading && }
{!loading && recommendations.length === 0 && }
{!loading && recommendations.length > 0 && (
)}
)
}
// results are per heading, so a page can match more than once
function uniqueByPath(results: DocsSearchV2Result[]) {
return results.filter(
(result, index) => results.findIndex(({ path }) => path === result.path) === index
)
}
function LoadingState() {
return
}
function NoResults() {
return No recommendations found.
}
function RecommendationsList({ recommendations }: { recommendations: DocsSearchV2Result[] }) {
const pathname = usePathname()
const sendTelemetryEvent = useSendTelemetryEvent()
return (
{recommendations
.filter(({ title }) => !!title)
.slice(0, 6)
.map(({ path, title, excerpt }) => (
sendTelemetryEvent({
action: 'docs_404_recommendation_clicked',
properties: { destinationPath: path, sourcePath: pathname ?? '' },
})
}
/>
))}
)
}
export { Recommendations, SearchButton }