Skip to content

Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.

3.9 KB
Raw Blame
'use client';

import { useState } from 'react';
import { useTranslations } from 'next-intl';
import { Check, Code, Copy } from 'lucide-react';
import { Card, CardContent } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog';
import { getPublicApiBaseUrl } from '@/config/api';
import { trackingSnippetTag, type ConversionsConsent } from '@/lib/conversion-tracking';

/**
 * The tracking snippet tag for a brand, with conversions when it has them on
 * (CE-017), or null without a code or an API URL.
 */
function snippetTag(trackingCode?: string, conversions?: ConversionsConsent | null): string | null {
  return trackingSnippetTag(getPublicApiBaseUrl(), trackingCode, conversions ?? null);
}

/**
 * Where the tag goes, the tag, and a copy button. The tag wraps inside its
 * box, and the block can shrink below the tag's width: in the dialog's grid
 * it would otherwise widen the dialog past the screen.
 */
function SnippetCode({ snippet }: { snippet: string }) {
  const t = useTranslations('traffic');
  const [copied, setCopied] = useState(false);

  const handleCopy = () => {
    navigator.clipboard.writeText(snippet);
    setCopied(true);
    setTimeout(() => setCopied(false), 2000);
  };

  return (
    <div className="flex min-w-0 items-start gap-3">
      <div className="min-w-0 flex-1">
        <p className="text-xs text-muted-foreground">
          {t.rich('snippetDescription', {
            tag: '</head>',
            code: (chunks) => <code className="text-[11px]">{chunks}</code>,
          })}
        </p>
        <pre className="mt-2 rounded-md bg-muted/50 px-3 py-2 font-mono text-[11px] break-all whitespace-pre-wrap">
          {snippet}
        </pre>
      </div>
      <Button
        variant="ghost"
        size="icon"
        className="h-8 w-8 shrink-0"
        onClick={handleCopy}
        aria-label={t('copySnippet')}
      >
        {copied ? (
          <Check className="h-3.5 w-3.5 text-green-500" />
        ) : (
          <Copy className="h-3.5 w-3.5" />
        )}
      </Button>
    </div>
  );
}

/**
 * The snippet's code, open on the page: only while the snippet hasn't sent a
 * visit in the last week (AT-001). Once it has, the code sits behind the
 * sources strip's Code button instead of staying at the top for good.
 */
export function SnippetBanner({
  trackingCode,
  conversions,
}: {
  trackingCode?: string;
  /** The brand's consent mode when it has conversions on, else null. */
  conversions?: ConversionsConsent | null;
}) {
  const t = useTranslations('traffic');
  const snippet = snippetTag(trackingCode, conversions);
  if (!snippet) return null;

  return (
    <Card className="border-blue-500/20 bg-blue-500/5">
      <CardContent className="flex items-start gap-3 px-4 py-3">
        <Code className="mt-0.5 h-4 w-4 shrink-0 text-blue-500" />
        <div className="min-w-0 flex-1">
          <p className="mb-0.5 text-sm font-medium">{t('snippetTitle')}</p>
          <SnippetCode snippet={snippet} />
        </div>
      </CardContent>
    </Card>
  );
}

/** The snippet's code in a dialog, from the sources strip's Code or Install button. */
export function SnippetDialog({
  trackingCode,
  conversions,
  open,
  onOpenChange,
}: {
  trackingCode?: string;
  /** The brand's consent mode when it has conversions on, else null. */
  conversions?: ConversionsConsent | null;
  open: boolean;
  onOpenChange: (open: boolean) => void;
}) {
  const t = useTranslations('traffic');
  const snippet = snippetTag(trackingCode, conversions);
  if (!snippet) return null;

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="sm:max-w-2xl">
        <DialogHeader>
          <DialogTitle>{t('snippetTitle')}</DialogTitle>
        </DialogHeader>
        <SnippetCode snippet={snippet} />
      </DialogContent>
    </Dialog>
  );
}