Skip to content

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

2.5 KB
Raw Blame
'use client'

import { Check, Copy } from 'lucide-react'
import { useEffect, useState } from 'react'
import { Button } from 'ui'

interface CopyDocPromptProps {
  title: string
  markdownPath: string
  intent: 'create-app' | 'add-to-project'
}

export function CopyDocPrompt({ title, markdownPath, intent }: CopyDocPromptProps) {
  const [status, setStatus] = useState<'idle' | 'copied' | 'error'>('idle')

  useEffect(() => {
    if (status !== 'copied') return
    const timeout = window.setTimeout(() => setStatus('idle'), 2500)
    return () => window.clearTimeout(timeout)
  }, [status])

  async function copyPrompt() {
    const url = new URL(markdownPath, window.location.origin).href
    const task =
      intent === 'create-app'
        ? `Help me create a new application using ${title} from Supabase Library. Inspect my workspace and create the app in a suitable directory.`
        : `Help me add ${title} from Supabase Library to my existing project. Inspect my project structure and reuse any Supabase client setup that is already in place.`
    const prompt = [
      task,
      `Read ${url} for the complete guide before making changes.`,
      "To add the block's files, prefer the guide's shadcn install command over copying them from the registry JSON; the CLI also installs npm dependencies and related blocks.",
      'Follow the rest of the guide, adapting it to my project, and verify that the setup works.',
    ].join(' ')

    try {
      await navigator.clipboard.writeText(prompt)
      setStatus('copied')
    } catch {
      setStatus('error')
    }
  }

  return (
    <div className="flex max-w-full flex-col items-center">
      <Button
        variant="secondary"
        size="medium"
        // Button's medium icon container forces 20px, overriding the icons' own size.
        className="rounded-full border-0 px-4 [&_svg]:size-4!"
        icon={
          status === 'copied' ? (
            <Check size={16} strokeWidth={2} />
          ) : (
            <Copy size={16} strokeWidth={2} />
          )
        }
        onClick={copyPrompt}
      >
        {status === 'copied' ? 'Prompt copied' : 'Copy agent prompt'}
      </Button>
      <span
        role="status"
        className={status === 'error' ? 'max-w-sm text-xs text-foreground-light' : 'sr-only'}
      >
        {status === 'error'
          ? 'Unable to copy the prompt. Try again.'
          : status === 'copied'
            ? 'Prompt copied to clipboard'
            : ''}
      </span>
    </div>
  )
}