Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

2.6 KB
Raw Blame
'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 }