The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
admin: delete anonymous links
Steven Teye4cb50535d20
diff --git a/apps/web/app/admin.dub.co/actions.ts b/apps/web/app/admin.dub.co/actions.tsindex 088023c06a..275ed7271f 100644--- a/apps/web/app/admin.dub.co/actions.ts+++ b/apps/web/app/admin.dub.co/actions.ts@@ -1,5 +1,5 @@"use server";-import { deleteUserLinks } from "@/lib/api/links";+import { deleteLink, deleteUserLinks } from "@/lib/api/links";import { deleteProject } from "@/lib/api/project";import { getSession, hashToken } from "@/lib/auth";import prisma from "@/lib/prisma";@@ -292,3 +292,85 @@ export async function banUser(data: FormData) {return true;}++export async function getLinkByKey(data: FormData) {+ const key = data.get("key") as string;++ if (!(await isAdmin())) {+ return {+ error: "Unauthorized",+ };+ }++ const link = await prisma.link.findUnique({+ where: {+ domain_key: {+ domain: "dub.sh",+ key,+ },+ },+ select: {+ key: true,+ url: true,+ },+ });+ if (!link) {+ return {+ error: "No link found",+ };+ }++ return {+ key: link?.key as string,+ url: link?.url as string,+ domain: getDomainWithoutWWW(link?.url as string),+ };+}++export async function deleteAndBlacklistLink(data: FormData) {+ const key = data.get("key") as string;+ const hostnames = data.getAll("hostname") as string[];++ if (!(await isAdmin())) {+ return {+ error: "Unauthorized",+ };+ }++ const blacklistedDomains = (await get("domains")) as string[];++ const response = await Promise.allSettled([+ deleteLink({ key }),+ fetch(+ `https://api.vercel.com/v1/edge-config/${process.env.EDGE_CONFIG_ID}/items?teamId=${process.env.TEAM_ID_VERCEL}`,+ {+ method: "PATCH",+ headers: {+ Authorization: `Bearer ${process.env.AUTH_BEARER_TOKEN}`,+ "Content-Type": "application/json",+ },+ body: JSON.stringify({+ items: [+ {+ operation: "update",+ key: "domains",+ value: [...blacklistedDomains, ...hostnames],+ },+ ],+ }),+ },+ ),+ ]);++ console.log(+ JSON.stringify(+ {+ response,+ },+ null,+ 2,+ ),+ );++ return true;+}diff --git a/apps/web/app/admin.dub.co/components/ban.tsx b/apps/web/app/admin.dub.co/components/ban.tsxindex 75820c44cd..282ab87b9c 100644--- a/apps/web/app/admin.dub.co/components/ban.tsx+++ b/apps/web/app/admin.dub.co/components/ban.tsx@@ -19,8 +19,6 @@ export default function BanUser() {const [copied, setCopied] = useState(false);const router = useRouter();- console.log(data?.proProjectSlugs);-return (<div className="flex flex-col space-y-5"><form@@ -144,7 +142,6 @@ const Form = () => {name="key"id="key"type="text"- autoFocusrequireddisabled={pending}autoComplete="off"diff --git a/apps/web/app/admin.dub.co/components/delete-link.tsx b/apps/web/app/admin.dub.co/components/delete-link.tsxnew file mode 100644index 0000000000..b281d58d87--- /dev/null+++ b/apps/web/app/admin.dub.co/components/delete-link.tsx@@ -0,0 +1,119 @@+"use client";++import { Button, Copy, LoadingSpinner } from "@dub/ui";+import { cn } from "@dub/utils";+import { useRouter } from "next/navigation";+import { useState } from "react";+import { useFormStatus } from "react-dom";+import { toast } from "sonner";+import { deleteAndBlacklistLink, getLinkByKey } from "../actions";++export default function DeleteLink() {+ const [data, setData] = useState<{+ key: string;+ url: string;+ domain: string;+ } | null>(null);+ const router = useRouter();++ return (+ <div className="flex flex-col space-y-5">+ <form+ action={(data) =>+ getLinkByKey(data).then((res) => {+ if (res.error) {+ toast.error(res.error);+ } else {+ // @ts-ignore+ setData(res);+ }+ })+ }+ >+ <Form />+ </form>+ {data && (+ <form+ action={(formData) => {+ deleteAndBlacklistLink(formData).then(() => {+ toast.success("Successfully deleted link and blacklisted domain");+ router.refresh();+ });+ }}+ className="flex w-full flex-col space-y-4 rounded-md border-t border-gray-200 py-4"+ >+ <div className="flex space-x-2">+ <input+ type="text"+ name="key"+ id={data.key}+ value={data.key}+ readOnly+ className="max-w-fit rounded-md border-gray-300 text-sm text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500"+ />+ <div className="w-full rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-900">+ {data.url}+ </div>+ </div>+ <div key={data.domain} className="space-x-2">+ <input+ type="checkbox"+ id={data.domain}+ name="hostname"+ value={data.domain}+ defaultChecked+ />+ <label htmlFor={data.domain}>{data.domain}</label>+ </div>+ <BanButton />+ </form>+ )}+ </div>+ );+}++const Form = () => {+ const { pending } = useFormStatus();++ return (+ <div className="relative flex w-full rounded-md shadow-sm">+ <span className="inline-flex items-center rounded-l-md border border-r-0 border-gray-300 bg-gray-50 px-5 text-gray-500 sm:text-sm">+ dub.sh+ </span>+ <input+ name="key"+ id="key"+ type="text"+ required+ disabled={pending}+ autoComplete="off"+ onPaste={(e) => {+ // replace http://dub.sh/ or https://dub.sh/ with nothing+ const pastedText = e.clipboardData+ .getData("text")+ .replace(/https?:\/\/dub\.sh\//, "");+ e.currentTarget.value = pastedText;++ // Prevent the default paste behavior+ e.preventDefault();+ }}+ className={cn(+ "block w-full rounded-r-md border-gray-300 text-sm text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500",+ pending && "bg-gray-100",+ )}+ placeholder="xxx"+ aria-invalid="true"+ />+ {pending && (+ <LoadingSpinner className="absolute inset-y-0 right-2 my-auto h-full w-5 text-gray-400" />+ )}+ </div>+ );+};++const BanButton = () => {+ const { pending } = useFormStatus();+ return (+ <Button text="Delete and blacklist" loading={pending} variant="danger" />+ );+};diff --git a/apps/web/app/admin.dub.co/page.tsx b/apps/web/app/admin.dub.co/page.tsxindex e8563f965f..096fd3aa0c 100644--- a/apps/web/app/admin.dub.co/page.tsx+++ b/apps/web/app/admin.dub.co/page.tsx@@ -2,6 +2,7 @@ import { constructMetadata } from "@dub/utils";import BanUser from "./components/ban";import ImpersonateProject from "./components/impersonate-project";import ImpersonateUser from "./components/impersonate-user";+import DeleteLink from "./components/delete-link";export const metadata = constructMetadata({title: "Dub Admin",@@ -30,6 +31,13 @@ export default function AdminPage() {</p><BanUser /></div>+ <div className="flex flex-col space-y-4 px-5 py-10">+ <h2 className="text-xl font-semibold">Delete Anonymous Links</h2>+ <p className="text-sm text-gray-500">+ Delete anonymous dub.sh links and blacklists the domain.+ </p>+ <DeleteLink />+ </div></div>);}