Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

4.5 KB
Raw Blame
import { useMutation } from '@tanstack/react-query';
import { ArrowUpIcon } from 'lucide-react';
import { useEffect, useRef, useState } from 'react';
import { toast } from 'sonner';
import { applyDraft } from './reportSlice';
import { Button } from '@/components/ui/button';
import { fieldSurface } from '@/components/ui/input';
import { Spinner } from '@/components/ui/spinner';
import { useAppParams } from '@/hooks/use-app-params';
import { handleError, useTRPC } from '@/integrations/trpc/react';
import { useDispatch } from '@/redux';
import { cn } from '@/utils/cn';

/** What the box says while the model reads the project and drafts; one line per few seconds. */
const BUSY_STATUS = [
  'Reading your events…',
  'Drafting the report…',
  'Almost there…',
] as const;
const BUSY_STATUS_STEP_MS = 3500;

function useBusyStatus(busy: boolean): string | null {
  const [index, setIndex] = useState(0);
  useEffect(() => {
    if (!busy) {
      setIndex(0);
      return;
    }
    const timer = setInterval(() => {
      setIndex((current) => Math.min(current + 1, BUSY_STATUS.length - 1));
    }, BUSY_STATUS_STEP_MS);
    return () => clearInterval(timer);
  }, [busy]);
  return busy ? BUSY_STATUS[index] : null;
}

/** The empty editor: say what you want and the model drafts a starting point from the project's events. */
export function ReportPrompt() {
  const { projectId } = useAppParams();
  const dispatch = useDispatch();
  const trpc = useTRPC();
  const [text, setText] = useState('');
  const [notice, setNotice] = useState<string | null>(null);
  const textareaRef = useRef<HTMLTextAreaElement>(null);

  const draft = useMutation(
    trpc.chat.draftReport.mutationOptions({
      onSuccess(result) {
        if (!result.report) {
          setNotice(result.summary);
          return;
        }
        dispatch(applyDraft(result.report));
        toast(result.report.name, { description: result.summary });
      },
      onError: handleError,
    })
  );
  const busy = draft.isPending;
  const status = useBusyStatus(busy);

  useEffect(() => {
    textareaRef.current?.focus();
  }, []);

  const submit = () => {
    const value = text.trim();
    if (!value || busy) {
      return;
    }
    setNotice(null);
    draft.mutate({ projectId, prompt: value });
  };

  return (
    <div className="card flex flex-col items-center px-4 py-16 md:py-24">
      <div className="max-w-lg space-y-1 text-center">
        <h2 className="font-semibold text-xl">Describe your report</h2>
        <p className="font-medium text-base text-muted-foreground">
          Say what you want to see and we'll generate a good starting point. You
          can adjust everything afterwards.
        </p>
      </div>

      <form
        className="mt-8 w-full max-w-2xl"
        onSubmit={(event) => {
          event.preventDefault();
          submit();
        }}
      >
        <div className={cn(fieldSurface, 'rounded-2xl', busy && 'opacity-80')}>
          <textarea
            aria-label="Describe the report"
            className="block w-full resize-none bg-transparent px-4 pt-4 pb-2 text-base text-foreground outline-none placeholder:text-muted-foreground/70"
            disabled={busy}
            onChange={(event) => setText(event.target.value)}
            onKeyDown={(event) => {
              if (event.key === 'Enter' && !event.shiftKey) {
                event.preventDefault();
                submit();
              }
            }}
            placeholder="Unique visitors per day for the last 30 days, split by device"
            ref={textareaRef}
            rows={3}
            value={text}
          />
          <div className="flex items-center justify-between gap-2 px-3 pb-3">
            <span
              aria-live="polite"
              className="min-w-0 truncate text-muted-foreground text-xs"
            >
              {status}
            </span>
            <Button
              aria-label="Generate report"
              className="size-8 shrink-0 rounded-full"
              disabled={!text.trim() || busy}
              size="icon"
              type="submit"
            >
              {busy ? (
                <Spinner size="sm" variant="white" />
              ) : (
                <ArrowUpIcon className="size-4" />
              )}
            </Button>
          </div>
        </div>
      </form>

      {notice && (
        <p
          className="mt-3 max-w-2xl rounded-lg bg-def-100 px-3 py-2 text-center text-sm dark:bg-def-200"
          role="status"
        >
          {notice}
        </p>
      )}
    </div>
  );
}