Skip to content

Open-source status page, incident response and uptime monitoring

7.5 KB
Raw Blame
"use client";

import { zodResolver } from "@hookform/resolvers/zod";
import { AudioLines, Inbox, Loading, Mic } from "@openstatus/icons";
import { Button } from "@openstatus/ui/components/ui/button";
import {
  Form,
  FormControl,
  FormField,
  FormItem,
  FormLabel,
} from "@openstatus/ui/components/ui/form";
import { Kbd } from "@openstatus/ui/components/ui/kbd";
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from "@openstatus/ui/components/ui/popover";
import { Textarea } from "@openstatus/ui/components/ui/textarea";
import { useIsMobile } from "@openstatus/ui/hooks/use-mobile";
import { useMutation } from "@tanstack/react-query";
import { useCallback, useEffect, useRef, useState } from "react";
import { useForm } from "react-hook-form";
import { toast } from "sonner";
import { z } from "zod";

import { useTRPC } from "@/lib/trpc/client";

const schema = z.object({
  message: z.string().trim().min(1),
});

export function NavFeedback() {
  const [open, setOpen] = useState(false);
  const isMobile = useIsMobile();
  const form = useForm<z.infer<typeof schema>>({
    resolver: zodResolver(schema),
    defaultValues: {
      message: "",
    },
  });
  const trpc = useTRPC();
  const feedbackMutation = useMutation(trpc.feedback.submit.mutationOptions());
  const [isListening, setIsListening] = useState(false);
  const recognitionRef = useRef<SpeechRecognition | null>(null);

  useEffect(() => {
    if (typeof window === "undefined") return;

    if ("SpeechRecognition" in window || "webkitSpeechRecognition" in window) {
      console.log("speech recognition API supported");
    } else {
      console.log("speech recognition API not supported");
    }

    const SpeechRecognitionCtor =
      // oxlint-disable-next-line typescript/no-explicit-any
      (window as any).webkitSpeechRecognition ||
      // oxlint-disable-next-line typescript/no-explicit-any
      (window as any).SpeechRecognition;

    // Browser not supported
    if (!SpeechRecognitionCtor) return;

    const recognition: SpeechRecognition = new SpeechRecognitionCtor();
    recognition.lang = "en-US";
    recognition.continuous = false;
    recognition.interimResults = false;

    recognition.onresult = (event: SpeechRecognitionEvent) => {
      const transcript = Array.from(event.results)
        .map((r) => r[0].transcript)
        .join(" ");
      form.setValue(
        "message",
        `${form.getValues("message") ?? ""}${transcript} `,
      );
    };

    recognition.onend = () => {
      setIsListening(false);
    };

    recognitionRef.current = recognition;
  }, [form]);

  const toggleListening = () => {
    const recognition = recognitionRef.current;
    if (!recognition) return;
    if (isListening) {
      recognition.stop();
    } else {
      try {
        recognition.start();
        setIsListening(true);
      } catch {
        // recognition already started, ignore
      }
    }
  };

  const onSubmit = useCallback(
    async (values: z.infer<typeof schema>) => {
      const promise = feedbackMutation.mutateAsync({
        ...values,
        source: "widget",
        path: window.location.pathname,
        isMobile,
      });
      toast.promise(promise, {
        loading: "Sending feedback...",
        success: "Feedback sent",
        error: "Failed to send feedback",
      });
      await promise;
    },
    [feedbackMutation, isMobile],
  );

  useEffect(() => {
    if (!open && feedbackMutation.isSuccess) {
      // NOTE: the popover takes 300ms to close, so we need to wait for that
      setTimeout(() => feedbackMutation.reset(), 300);
    }
  }, [open, feedbackMutation]);

  useEffect(() => {
    const down = async (e: KeyboardEvent) => {
      if (open && (e.metaKey || e.ctrlKey) && e.key === "Enter") {
        await form.handleSubmit(onSubmit)();
      }

      const target = e.target as HTMLElement;
      const isTyping =
        target.tagName === "INPUT" ||
        target.tagName === "TEXTAREA" ||
        target.isContentEditable;

      if (isTyping) return;

      if (!open) {
        if (e.metaKey || e.ctrlKey || e.altKey) return;
        if (e.key === "f") {
          e.preventDefault();
          setOpen(true);
        }
        return;
      }
    };
    document.addEventListener("keydown", down);
    return () => document.removeEventListener("keydown", down);
  }, [open, form, onSubmit]);

  useEffect(() => {
    if (!open) {
      form.reset();
    }
  }, [open, form]);

  if (isMobile) {
    return null;
  }

  return (
    <Popover open={open} onOpenChange={setOpen}>
      <PopoverTrigger asChild>
        <Button
          variant="ghost"
          size="sm"
          className="group text-muted-foreground hover:text-foreground data-[state=open]:text-foreground gap-0 px-2 text-sm hover:bg-transparent"
        >
          Feedback{" "}
          <Kbd className="group-hover:text-foreground group-data-[state=open]:text-foreground ml-1 font-mono">
            F
          </Kbd>
        </Button>
      </PopoverTrigger>
      <PopoverContent align="end" className="relative border-none p-0">
        {feedbackMutation.isSuccess ? (
          <div className="border-input flex h-[110px] flex-col items-center justify-center gap-1 rounded-md border p-3 text-base shadow-xs">
            <Inbox className="size-4 shrink-0" />
            <p className="text-center font-medium">Thanks for sharing!</p>
            <p className="text-muted-foreground text-center text-sm">
              We&apos;ll get in touch if there&apos;s a follow-up.
            </p>
          </div>
        ) : (
          <Form {...form}>
            <form onSubmit={form.handleSubmit(onSubmit)}>
              <FormField
                control={form.control}
                name="message"
                render={({ field }) => (
                  <FormItem>
                    <FormLabel className="sr-only">Feedback</FormLabel>
                    <FormControl>
                      <Textarea
                        placeholder="Ideas, bugs, or anything else..."
                        className="field-sizing-fixed h-[110px] resize-none p-3"
                        rows={4}
                        {...field}
                      />
                    </FormControl>
                  </FormItem>
                )}
              />
              {recognitionRef.current && (
                <Button
                  type="button"
                  size="sm"
                  variant="ghost"
                  className="group absolute bottom-1.5 left-1.5 gap-0"
                  onClick={toggleListening}
                >
                  {isListening ? (
                    <AudioLines className="size-4 animate-pulse" />
                  ) : (
                    <Mic className="size-4" />
                  )}
                </Button>
              )}
              <Button
                size="sm"
                variant="ghost"
                className="group absolute right-1.5 bottom-1.5 gap-0"
                type="submit"
                disabled={feedbackMutation.isPending}
              >
                {feedbackMutation.isPending ? (
                  <Loading className="size-4 animate-spin" />
                ) : (
                  <>
                    Send
                    <Kbd className="group-hover:text-foreground ml-1 font-mono">
                      ⌘
                    </Kbd>
                    <Kbd className="group-hover:text-foreground ml-1 font-mono">
                      ↵
                    </Kbd>
                  </>
                )}
              </Button>
            </form>
          </Form>
        )}
      </PopoverContent>
    </Popover>
  );
}