"use client" import * as React from "react" import { useRouter } from "next/navigation" import { cn } from "@/lib/utils" import { ButtonProps, buttonVariants } from "@/components/ui/button" import { toast } from "@/components/ui/use-toast" import { Icons } from "@/components/icons" interface PostCreateButtonProps extends ButtonProps {} export function PostCreateButton({ className, variant, ...props }: PostCreateButtonProps) { const router = useRouter() const [isLoading, setIsLoading] = React.useState(false) async function onClick() { setIsLoading(true) const response = await fetch("/api/posts", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ title: "Untitled Post", }), }) setIsLoading(false) if (!response?.ok) { if (response.status === 402) { return toast({ title: "Limit of 3 posts reached.", description: "Please upgrade to the PRO plan.", variant: "destructive", }) } return toast({ title: "Something went wrong.", description: "Your post was not created. Please try again.", variant: "destructive", }) } const post = await response.json() // This forces a cache invalidation. router.refresh() router.push(`/editor/${post.id}`) } return ( ) }