Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

1.3 KB
Raw Blame
"use client";

import { mutatePrefix } from "@/lib/swr/mutate";
import { Button, useKeyboardShortcut } from "@dub/ui";
import { useParams, useRouter } from "next/navigation";
import { useState } from "react";
import { toast } from "sonner";

export function AcceptInviteButton() {
  const { slug } = useParams<{ slug: string }>();
  const router = useRouter();

  const [isAccepting, setIsAccepting] = useState(false);

  const acceptInvite = async () => {
    setIsAccepting(true);

    try {
      const response = await fetch(`/api/workspaces/${slug}/invites/accept`, {
        method: "POST",
        headers: { "Content-Type": "application/json" },
      });

      if (!response.ok) {
        const { error } = await response.json();
        toast.error(error.message || "Failed to accept invite.");
        setIsAccepting(false);
        return;
      }

      await mutatePrefix("/api/workspaces");
      router.replace(`/${slug}`);
      toast.success("You now are a part of this workspace!");
    } catch (e) {
      setIsAccepting(false);
    }
  };

  useKeyboardShortcut("a", acceptInvite, {
    enabled: !isAccepting,
  });

  return (
    <Button
      onClick={acceptInvite}
      loading={isAccepting}
      text="Accept invite"
      shortcut="A"
      className="h-9 rounded-lg [&>div]:flex-initial"
    />
  );
}