'use client' import { useDocsSearchV2 } from 'common' import { describeSearchQuery } from 'common/search-query-telemetry' import type { DocsSearchV2AskAiOpenedEvent } from 'common/telemetry-constants' import { SearchActionItem, SearchDialog, SearchPanel, type SearchDialogProps, type SearchPanelProps, } from 'docs-search/components' import Link from 'next/link' import { useRouter } from 'next/navigation' import { useEffect } from 'react' import { AskAiIcon } from './AskAiIcon' import { SEARCH_V2_ENDPOINT, SEARCH_V2_MIN_CHARACTERS } from './constants' import type { SearchV2CloseReason } from './useSearchV2Dialog' import { useSendTelemetryEvent } from '@/lib/telemetry' interface SearchV2DialogProps extends Pick { onOpenChange: (open: boolean, closeReason?: SearchV2CloseReason) => void } interface SearchV2PanelProps extends Pick< SearchPanelProps, 'onResultNavigate' | 'onResultLinkNavigate' > { onAskAi?: (options: { triggerType: DocsSearchV2AskAiOpenedEvent['properties']['triggerType'] question: string }) => void } export function SearchV2Dialog({ open, onOpenChange }: SearchV2DialogProps) { const router = useRouter() return ( { router.push(path) onOpenChange(false, 'result_navigated') }} onResultLinkNavigate={() => onOpenChange(false, 'result_navigated')} /> ) } export function SearchV2Panel({ onResultNavigate, onResultLinkNavigate, onAskAi, }: SearchV2PanelProps) { const sendTelemetryEvent = useSendTelemetryEvent() const { searchState, handleDocsSearchDebounced, resetSearch } = useDocsSearchV2({ endpoint: SEARCH_V2_ENDPOINT, }) useEffect(() => { if (searchState.status === 'results' || searchState.status === 'noResults') { sendTelemetryEvent({ action: 'docs_search_v2_search_submitted', properties: { ...describeSearchQuery(searchState.query), hasResults: searchState.status === 'results', }, }) } }, [searchState, sendTelemetryEvent]) return ( } minCharacters={SEARCH_V2_MIN_CHARACTERS} onQueryChange={(query) => { if (query) { handleDocsSearchDebounced(query) } else { resetSearch() } }} onResultActivate={(result, { query }) => sendTelemetryEvent({ action: 'docs_search_v2_result_clicked', properties: { resultPath: result.path, ...describeSearchQuery(query) }, }) } onResultNavigate={(path) => onResultNavigate(path)} onResultLinkNavigate={() => onResultLinkNavigate?.()} renderNoResults={ onAskAi ? ({ query }) => ( <> No results found for “{query}”.{' '} } onSelect={() => onAskAi({ triggerType: 'no_results_item', question: query })} > Ask Supabase AI ) : undefined } renderFooterStart={ onAskAi ? ({ query }) => ( ) : undefined } /> ) }