Files
supabase/apps/docs/features/recommendations/NotFound.client.tsx
Chris Chinchilla 309f4b2612 docs: Track 404 recommendation clicked (#46990)
## I have read the
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
file.

YES


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Added analytics telemetry for documentation 404 recommendation clicks,
recording the clicked destination and the originating page.

* **Improvements**
* Enhanced the ButtonCard component to optionally handle click actions
via an `onClick` callback and forward it to the underlying link element.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-06-17 22:56:53 +08:00

94 lines
2.6 KiB
TypeScript

'use client'
import ButtonCard from '~/components/ButtonCard'
import { useSendTelemetryEvent } from '~/lib/telemetry'
import { useDocsSearch, type DocsSearchResult } from 'common'
import { usePathname } from 'next/navigation'
import { useEffect } from 'react'
import { Button, cn } from 'ui'
import { useSetCommandMenuOpen } from 'ui-patterns/CommandMenu'
import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader'
function SearchButton() {
const setCommandMenuOpen = useSetCommandMenuOpen()
return (
<Button variant="primary" size="small" onClick={() => setCommandMenuOpen(true)}>
Search for page
</Button>
)
}
function Recommendations() {
const pathname = usePathname()
const { searchState: state, handleDocsSearch: handleSearch } = useDocsSearch()
const loading = state.status === 'initial' || state.status === 'loading'
const recommendations =
state.status === 'partialResults' || state.status === 'fullResults' ? state.results : []
useEffect(() => {
if (!pathname) return
const query = decodeURIComponent(pathname.replace(/^\/(?:guides|reference)\//, '')).replace(
/[_\/-]/g,
' '
)
handleSearch(query)
}, [handleSearch, pathname])
return (
<section aria-labelledby="empty-page-recommendations" className="min-h-96 mt-20">
<h2 id="empty-page-recommendations">Are you looking for...?</h2>
{loading && <LoadingState />}
{!loading && recommendations.length === 0 && <NoResults />}
{!loading && recommendations.length > 0 && (
<RecommendationsList recommendations={recommendations} />
)}
</section>
)
}
function LoadingState() {
return <GenericSkeletonLoader />
}
function NoResults() {
return <span>No recommendations found.</span>
}
function RecommendationsList({
recommendations,
}: {
recommendations: Array<Omit<DocsSearchResult, 'sections'>>
}) {
const pathname = usePathname()
const sendTelemetryEvent = useSendTelemetryEvent()
return (
<ul className={cn('not-prose', 'grid grid-cols-[repeat(auto-fit,minmax(300px,1fr))] gap-4')}>
{recommendations
.filter(({ title }) => !!title)
.slice(0, 6)
.map(({ path, title, subtitle, description }) => (
<ButtonCard
key={path}
to={path}
title={title}
description={subtitle || description || undefined}
onClick={() =>
sendTelemetryEvent({
action: 'docs_404_recommendation_clicked',
properties: { destinationPath: path, sourcePath: pathname ?? '' },
})
}
/>
))}
</ul>
)
}
export { Recommendations, SearchButton }