|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import clsx from "clsx"; |
| 4 | +import { AlertTriangle } from "lucide-react"; |
| 5 | +import { useParams } from "next/navigation"; |
| 6 | +import { useState } from "react"; |
| 7 | +import { experimental_useFormStatus as useFormStatus } from "react-dom"; |
| 8 | +import LoadingDots from "./icons/loading-dots"; |
| 9 | +import va from "@vercel/analytics"; |
| 10 | +import { toast } from "sonner"; |
| 11 | + |
| 12 | +export default function ReportAbuse() { |
| 13 | + const [open, setOpen] = useState(false); |
| 14 | + const { domain, slug } = useParams() as { domain: string; slug?: string }; |
| 15 | + const url = slug ? `https://${domain}/${slug}` : `https://${domain}`; |
| 16 | + |
| 17 | + return ( |
| 18 | + <div className="fixed bottom-5 right-5"> |
| 19 | + <button |
| 20 | + className="rounded-full bg-black p-4 text-white shadow-lg transition-all hover:-translate-y-1 hover:shadow-2xl active:translate-y-0 active:shadow-sm" |
| 21 | + onClick={() => setOpen(!open)} |
| 22 | + > |
| 23 | + <AlertTriangle size={24} /> |
| 24 | + </button> |
| 25 | + {open && ( |
| 26 | + <form |
| 27 | + action={async (formData) => { |
| 28 | + const url = formData.get("url") as string; |
| 29 | + va.track("Reported Abuse", { url }); |
| 30 | + // artificial 1s delay |
| 31 | + await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 32 | + setOpen(false); |
| 33 | + toast.success( |
| 34 | + "Successfully reported abuse – thank you for helping us keep the internet safe!", |
| 35 | + ); |
| 36 | + }} |
| 37 | + className="absolute bottom-20 right-2 flex w-96 flex-col space-y-6 rounded-lg border border-stone-200 bg-white p-8 shadow-lg animate-in slide-in-from-bottom-5" |
| 38 | + > |
| 39 | + <div> |
| 40 | + <h2 className="font-cal text-xl leading-7 text-stone-900"> |
| 41 | + Report Abuse |
| 42 | + </h2> |
| 43 | + <p className="mt-2 text-sm leading-6 text-stone-600"> |
| 44 | + Found a site with abusive content? Let us know! |
| 45 | + </p> |
| 46 | + </div> |
| 47 | + |
| 48 | + <div> |
| 49 | + <label |
| 50 | + htmlFor="domain" |
| 51 | + className="block text-sm font-medium leading-6 text-stone-900" |
| 52 | + > |
| 53 | + URL to report |
| 54 | + </label> |
| 55 | + <div className="mt-2"> |
| 56 | + <input |
| 57 | + type="text" |
| 58 | + name="url" |
| 59 | + id="url" |
| 60 | + readOnly |
| 61 | + value={url} |
| 62 | + className="block w-full cursor-not-allowed rounded-md border border-stone-200 bg-stone-100 py-1.5 text-stone-900 shadow-sm ring-0 focus:outline-none sm:text-sm sm:leading-6" |
| 63 | + /> |
| 64 | + </div> |
| 65 | + </div> |
| 66 | + |
| 67 | + <SubmitButton /> |
| 68 | + </form> |
| 69 | + )} |
| 70 | + </div> |
| 71 | + ); |
| 72 | +} |
| 73 | + |
| 74 | +function SubmitButton() { |
| 75 | + const { pending } = useFormStatus(); |
| 76 | + return ( |
| 77 | + <button |
| 78 | + className={clsx( |
| 79 | + "h flex h-8 w-full items-center justify-center space-x-2 rounded-md border text-sm transition-all focus:outline-none sm:h-10", |
| 80 | + pending |
| 81 | + ? "cursor-not-allowed border-stone-200 bg-stone-100 text-stone-400" |
| 82 | + : "border-black bg-black text-white hover:bg-white hover:text-black", |
| 83 | + )} |
| 84 | + disabled={pending} |
| 85 | + > |
| 86 | + {pending ? <LoadingDots color="#808080" /> : <p>Report Abuse</p>} |
| 87 | + </button> |
| 88 | + ); |
| 89 | +} |
0 commit comments