Files
supabase/apps/docs/components/AiTools.tsx

68 lines
2.0 KiB
TypeScript

'use client'
import { useDocsPathname } from '~/features/IaNav'
import { useSendTelemetryEvent } from '~/lib/telemetry'
import { PREFERRED_SOURCE_URL } from 'common/markdown-affordance'
import { type MarkdownAffordancePageType } from 'common/telemetry-constants'
import { AiTools as SharedAiTools, type AiAgent } from 'ui-patterns/AiTools'
function AiTools({
pageType,
articleId,
showAgentSetup = false,
showPreferredSource = false,
pageUrl,
markdownUrl,
className,
}: {
pageType: MarkdownAffordancePageType
articleId: string
showAgentSetup?: boolean
showPreferredSource?: boolean
/** Defaults to the current page; the URL the "Ask" prompts point the assistant at */
pageUrl?: string
/** Defaults to the current path plus `.md` */
markdownUrl?: string
className?: string
}) {
const path = useDocsPathname()
const sendTelemetryEvent = useSendTelemetryEvent()
function handleAgentSetupClick() {
sendTelemetryEvent({ action: 'agent_setup_clicked' })
}
function handleMarkdownCopy() {
sendTelemetryEvent({ action: 'copy_as_markdown_clicked', properties: { pageType } })
}
function handlePreferredSourceClick() {
sendTelemetryEvent({ action: 'preferred_source_clicked', properties: { pageType } })
}
function handleAskAiClick(agent: AiAgent) {
sendTelemetryEvent({ action: 'ask_ai_clicked', properties: { agent, pageType } })
}
function getArticleHtml() {
return document.getElementById(articleId)?.innerHTML ?? ''
}
return (
<SharedAiTools
pageUrl={pageUrl ?? `https://supabase.com/docs${path}`}
markdownUrl={markdownUrl ?? `/docs${path}.md`}
markdownFallback={getArticleHtml}
agentSetupHref={showAgentSetup ? '/docs/guides/ai-tools' : undefined}
preferredSourceHref={showPreferredSource ? PREFERRED_SOURCE_URL : undefined}
onAgentSetupClick={handleAgentSetupClick}
onPreferredSourceClick={handlePreferredSourceClick}
onMarkdownCopy={handleMarkdownCopy}
onAskAiClick={handleAskAiClick}
className={className}
/>
)
}
export { AiTools }