"use client" import * as React from "react" import { useRouter } from "next/navigation" import { Post } from "@prisma/client" import { cn } from "@/lib/utils" import { Icons } from "@/components/icons" import toast from "@/components/ui/toast" async function createPost(): Promise> { const response = await fetch("/api/posts", { method: "POST", body: JSON.stringify({ title: "Untitled Post", }), }) if (!response?.ok) { toast({ title: "Something went wrong.", message: "Your post was not created. Please try again.", type: "error", }) } return await response.json() } interface PostCreateButtonProps extends React.HTMLAttributes {} export function PostCreateButton({ className, ...props }: PostCreateButtonProps) { const router = useRouter() const [isLoading, setIsLoading] = React.useState(false) async function onClick() { setIsLoading(!isLoading) const post = await createPost() router.push(`/editor/${post.id}`) } return ( ) }