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.

improve admin

Steven Tey12eef1c3ad03
diff --git a/apps/web/app/admin.dub.co/components/ban-link.tsx b/apps/web/app/admin.dub.co/components/ban-link.tsx
new file mode 100644
index 0000000000..543696d659
--- /dev/null
+++ b/apps/web/app/admin.dub.co/components/ban-link.tsx
@@ -0,0 +1,74 @@
+"use client";
+
+import { LoadingSpinner } from "@dub/ui";
+import { cn } from "@dub/utils";
+import { useFormStatus } from "react-dom";
+import { toast } from "sonner";
+
+export default function BanLink() {
+ return (
+ <div className="flex flex-col space-y-5">
+ <form
+ action={async (data) =>
+ await fetch(`/api/admin/links/ban?key=${data.get("key")}`, {
+ method: "DELETE",
+ })
+ .then((res) => res.json())
+ .then((res) => {
+ if (res.error) {
+ toast.error(res.error);
+ } else {
+ toast.success("Link has been banned");
+ }
+ })
+ }
+ >
+ <Form />
+ </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"
+ className={cn(
+ "block w-full rounded-r-md border-gray-300 text-sm text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500",
+ pending && "bg-gray-100",
+ )}
+ placeholder="IG47WZs"
+ aria-invalid="true"
+ onPaste={(e: React.ClipboardEvent<HTMLInputElement>) => {
+ e.preventDefault();
+ // if pasting in https://dub.sh/xxx or dub.sh/xxx, extract xxx
+ const text = e.clipboardData.getData("text/plain");
+ if (
+ text.startsWith("https://dub.sh/") ||
+ text.startsWith("dub.sh/")
+ ) {
+ e.currentTarget.value = text
+ .replace("https://dub.sh/", "")
+ .replace("dub.sh/", "");
+ } else {
+ e.currentTarget.value = text;
+ }
+ }}
+ />
+ {pending && (
+ <LoadingSpinner className="absolute inset-y-0 right-2 my-auto h-full w-5 text-gray-400" />
+ )}
+ </div>
+ );
+};
diff --git a/apps/web/app/admin.dub.co/components/refresh-domain.tsx b/apps/web/app/admin.dub.co/components/refresh-domain.tsx
index e268227c77..caa6080969 100644
--- a/apps/web/app/admin.dub.co/components/refresh-domain.tsx
+++ b/apps/web/app/admin.dub.co/components/refresh-domain.tsx
@@ -2,14 +2,10 @@
import { LoadingSpinner } from "@dub/ui";
import { cn } from "@dub/utils";
-import { useState } from "react";
import { useFormStatus } from "react-dom";
import { toast } from "sonner";
-import { UserInfoProps } from "./user-info";
export default function RefreshDomain() {
- const [data, setData] = useState<UserInfoProps | null>(null);
-
return (
<div className="flex flex-col space-y-5">
<form
diff --git a/apps/web/app/admin.dub.co/page.tsx b/apps/web/app/admin.dub.co/page.tsx
index e0315b5118..2d8eabfb4c 100644
--- a/apps/web/app/admin.dub.co/page.tsx
+++ b/apps/web/app/admin.dub.co/page.tsx
@@ -1,4 +1,5 @@
import { constructMetadata } from "@dub/utils";
+import BanLink from "./components/ban-link";
import ImpersonateUser from "./components/impersonate-user";
import ImpersonateWorkspace from "./components/impersonate-workspace";
import RefreshDomain from "./components/refresh-domain";
@@ -30,6 +31,11 @@ export default function AdminPage() {
</p>
<RefreshDomain />
</div>
+ <div className="flex flex-col space-y-4 px-5 py-10">
+ <h2 className="text-xl font-semibold">Ban Link</h2>
+ <p className="text-sm text-gray-500">Ban a dub.sh link</p>
+ <BanLink />
+ </div>
</div>
);
}
diff --git a/apps/web/app/api/admin/links/[linkId]/ban/route.ts b/apps/web/app/api/admin/links/ban/route.ts
similarity index 68%
rename from apps/web/app/api/admin/links/[linkId]/ban/route.ts
rename to apps/web/app/api/admin/links/ban/route.ts
index f4c6cffb13..6923f335de 100644
--- a/apps/web/app/api/admin/links/[linkId]/ban/route.ts
+++ b/apps/web/app/api/admin/links/ban/route.ts
@@ -9,18 +9,20 @@ import {
} from "@dub/utils";
import { NextResponse } from "next/server";
-// DELETE /api/admin/links/[linkId]/ban – ban a link
-export const DELETE = withAdmin(async ({ params }) => {
- const { linkId } = params as { linkId: string };
+// DELETE /api/admin/links/ban – ban a dub.sh link by key
+export const DELETE = withAdmin(async ({ searchParams }) => {
+ const { key } = searchParams as { linkId?: string; key?: string };
+
+ if (!key) {
+ return NextResponse.json({ error: "No key provided" }, { status: 400 });
+ }
const link = await prisma.link.findUnique({
- where: {
- id: linkId,
- },
+ where: { domain_key: { domain: "dub.sh", key } },
});
if (!link) {
- return NextResponse.next();
+ return NextResponse.json({ error: "Link not found" }, { status: 404 });
}
const domain = getDomainWithoutWWW(link.url);
@@ -28,7 +30,7 @@ export const DELETE = withAdmin(async ({ params }) => {
const response = await Promise.all([
prisma.link.update({
where: {
- id: linkId,
+ id: link.id,
},
data: {
userId: LEGAL_USER_ID,
diff --git a/apps/web/ui/links/link-card.tsx b/apps/web/ui/links/link-card.tsx
index b7a17734d5..27f7206588 100644
--- a/apps/web/ui/links/link-card.tsx
+++ b/apps/web/ui/links/link-card.tsx
@@ -625,13 +625,13 @@ export default function LinkCard({
) &&
(setOpenPopover(false),
toast.promise(
- fetch(`/api/admin/links/${id}/ban`, {
+ fetch(`/api/admin/links/ban?key=${key}`, {
method: "DELETE",
}).then(async () => {
await mutate(
(key) =>
typeof key === "string" &&
- key.startsWith("/api/admin/links"),
+ key.startsWith("/api/admin/links/ban"),
undefined,
{ revalidate: true },
);