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.

improved admin dashboard

Steven Tey9c5447232bea
diff --git a/apps/web/app/admin.dub.co/actions.ts b/apps/web/app/admin.dub.co/actions.ts
index 23967df5fb..af4c525ed1 100644
--- a/apps/web/app/admin.dub.co/actions.ts
+++ b/apps/web/app/admin.dub.co/actions.ts
@@ -3,7 +3,7 @@
import { deleteProject } from "@/lib/api/projects";
import { getSession, hashToken } from "@/lib/auth";
import prisma from "@/lib/prisma";
-import { DUB_PROJECT_ID, getDomainWithoutWWW } from "@dub/utils";
+import { DUB_DOMAINS, DUB_PROJECT_ID, getDomainWithoutWWW } from "@dub/utils";
import { randomBytes } from "crypto";
import cloudinary from "cloudinary";
import { get } from "@vercel/edge-config";
@@ -44,37 +44,8 @@ async function getImpersonateUrl(email: string) {
return `${process.env.NEXTAUTH_URL}/api/auth/callback/email?${params}`;
}
-export async function getUser(data: FormData) {
+export async function getUserOrProjectOwner(data: FormData) {
const email = data.get("email") as string;
-
- if (!(await isAdmin())) {
- return {
- error: "Unauthorized",
- };
- }
-
- const response = await prisma.user.findUnique({
- where: {
- email,
- },
- select: {
- email: true,
- },
- });
-
- if (!response?.email) {
- return {
- error: "No user found",
- };
- }
-
- return {
- email: response.email,
- impersonateUrl: await getImpersonateUrl(response.email),
- };
-}
-
-export async function getProjectOwner(data: FormData) {
const slug = data.get("slug") as string;
if (!(await isAdmin())) {
@@ -84,76 +55,42 @@ export async function getProjectOwner(data: FormData) {
}
const response = await prisma.user.findFirst({
- where: {
- projects: {
- some: {
- project: {
- slug,
+ where: email
+ ? { email }
+ : {
+ projects: {
+ some: {
+ project: {
+ slug,
+ },
+ role: "owner",
+ },
},
- role: "owner",
},
- },
- },
- select: {
- email: true,
- },
- });
-
- if (!response?.email) {
- return {
- error: "No user found",
- };
- }
-
- return {
- email: response.email,
- impersonateUrl: await getImpersonateUrl(response.email),
- };
-}
-
-export async function getUserByKey(data: FormData) {
- const key = data.get("key") as string;
-
- if (!(await isAdmin())) {
- return {
- error: "Unauthorized",
- };
- }
-
- const response = await prisma.user.findFirst({
- where: {
- links: {
- some: {
- domain: "dub.sh",
- key,
- },
- },
- },
select: {
email: true,
links: {
where: {
- domain: "dub.sh",
+ domain: {
+ in: DUB_DOMAINS.map((domain) => domain.slug),
+ },
},
select: {
- key: true,
- url: true,
+ domain: true,
},
},
projects: {
- where: {
- role: "owner",
- },
select: {
project: {
select: {
name: true,
slug: true,
plan: true,
- domains: {
+ usage: true,
+ _count: {
select: {
- slug: true,
- verified: true,
+ domains: true,
+ links: true,
},
},
},
@@ -162,36 +99,31 @@ export async function getUserByKey(data: FormData) {
},
},
});
+
if (!response?.email) {
return {
error: "No user found",
};
}
- const { email, links, projects } = response;
-
- const hostnames = new Set<string>();
-
- links.map((link) => {
- const hostname = getDomainWithoutWWW(link.url);
- hostname && hostnames.add(hostname);
- });
-
- const verifiedDomains = projects
- .filter(({ project }) => {
- return project.domains.some(({ verified }) => verified);
- })
- .flatMap(({ project }) => project.domains.map(({ slug }) => slug));
-
return {
- email: response?.email as string,
- hostnames: Array.from(hostnames),
- proProjectSlugs:
- projects
- .filter(({ project }) => project.plan === "pro")
- .map(({ project }) => project.slug) || [],
- verifiedDomains: verifiedDomains || [],
- impersonateUrl: await getImpersonateUrl(email),
+ email: response.email,
+ // object with domain slugs as keys and the count of links as values
+ defaultDomainLinks: response.links.reduce((acc, { domain }) => {
+ if (acc[domain]) {
+ acc[domain]++;
+ } else {
+ acc[domain] = 1;
+ }
+ return acc;
+ }, {} as Record<string, number>),
+ projects: response.projects.map(({ project }) => ({
+ ...project,
+ clicks: project.usage,
+ domains: project._count.domains,
+ links: project._count.links,
+ })),
+ impersonateUrl: await getImpersonateUrl(response.email),
};
}
diff --git a/apps/web/app/admin.dub.co/components/ban.tsx b/apps/web/app/admin.dub.co/components/ban.tsx
deleted file mode 100644
index 282ab87b9c..0000000000
--- a/apps/web/app/admin.dub.co/components/ban.tsx
+++ /dev/null
@@ -1,175 +0,0 @@
-"use client";
-
-import { Button, Copy, LoadingSpinner, Tick } 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 { banUser, getUserByKey } from "../actions";
-
-export default function BanUser() {
- const [data, setData] = useState<{
- email: string;
- hostnames: string[];
- proProjectSlugs: string[];
- verifiedDomains: string[];
- impersonateUrl: string;
- } | null>(null);
- const [copied, setCopied] = useState(false);
- const router = useRouter();
-
- return (
- <div className="flex flex-col space-y-5">
- <form
- action={(data) =>
- getUserByKey(data).then((res) => {
- if (res.error) {
- toast.error(res.error);
- } else {
- // @ts-ignore
- setData(res);
- }
- })
- }
- >
- <Form />
- </form>
- {data && (
- <form
- action={(formData) => {
- if (
- data.proProjectSlugs.length > 0 ||
- data.verifiedDomains.length > 0
- ) {
- if (
- !confirm(
- `This user has ${data.proProjectSlugs.length} pro projects and ${data.verifiedDomains.length} verified domains. Are you sure you want to ban them?`,
- )
- ) {
- return;
- }
- }
- banUser(formData).then(() => {
- toast.success("Successfully banned user");
- router.refresh();
- });
- }}
- className="flex w-full flex-col space-y-4 rounded-md border-t border-gray-200 py-4"
- >
- <div className="flex w-full items-center space-x-3">
- <input
- type="email"
- name="email"
- id="email"
- value={data.email}
- readOnly
- className="w-full rounded-md border-gray-300 text-sm text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500"
- />
- <button
- type="button"
- onClick={() => {
- setCopied(true);
- navigator.clipboard.writeText(data.impersonateUrl);
- toast.success("Copied to clipboard");
- setTimeout(() => {
- setCopied(false);
- }, 3000);
- }}
- className="rounded-md border border-gray-300 p-2"
- >
- {copied ? (
- <Tick className="h-5 w-5 text-gray-500" />
- ) : (
- <Copy className="h-5 w-5 text-gray-500" />
- )}
- </button>
- </div>
- {data.proProjectSlugs.length > 0 && (
- <div className="flex flex-col space-y-2">
- <p className="text-sm font-semibold">Pro Projects</p>
- {data.proProjectSlugs.map((slug) => (
- <div
- key={slug}
- className="rounded-md border border-gray-300 p-2 text-sm text-gray-500"
- >
- {slug}
- </div>
- ))}
- </div>
- )}
- {data.verifiedDomains.length > 0 && (
- <div className="flex flex-col space-y-2">
- <p className="text-sm font-semibold">Verified Domains</p>
- {data.verifiedDomains.map((domain) => (
- <div
- key={domain}
- className="rounded-md border border-gray-300 p-2 text-sm text-gray-500"
- >
- {domain}
- </div>
- ))}
- </div>
- )}
- {data.hostnames.map((hostname) => (
- <div key={hostname} className="space-x-2">
- <input
- type="checkbox"
- id={hostname}
- name="hostname"
- value={hostname}
- defaultChecked
- />
- <label htmlFor={hostname}>{hostname}</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="Sjw30s0"
- 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="Confirm Ban" loading={pending} variant="danger" />;
-};
diff --git a/apps/web/app/admin.dub.co/components/impersonate-project.tsx b/apps/web/app/admin.dub.co/components/impersonate-project.tsx
index d8e89e688e..65fd2f773b 100644
--- a/apps/web/app/admin.dub.co/components/impersonate-project.tsx
+++ b/apps/web/app/admin.dub.co/components/impersonate-project.tsx
@@ -1,24 +1,21 @@
"use client";
-import { Copy, LoadingSpinner, Tick } from "@dub/ui";
+import { LoadingSpinner } from "@dub/ui";
import { cn } from "@dub/utils";
import { useState } from "react";
import { useFormStatus } from "react-dom";
import { toast } from "sonner";
-import { getProjectOwner } from "../actions";
+import { getUserOrProjectOwner } from "../actions";
+import UserInfo, { UserInfoProps } from "./user-info";
export default function ImpersonateProject() {
- const [data, setData] = useState<{
- email: string;
- impersonateUrl: string;
- } | null>(null);
- const [copied, setCopied] = useState(false);
+ const [data, setData] = useState<UserInfoProps | null>(null);
return (
<div className="flex flex-col space-y-5">
<form
action={(data) =>
- getProjectOwner(data).then((res) => {
+ getUserOrProjectOwner(data).then((res) => {
if (res.error) {
toast.error(res.error);
} else {
@@ -30,36 +27,7 @@ export default function ImpersonateProject() {
>
<Form />
</form>
- {data && (
- <div className="flex w-full items-center space-x-3">
- <input
- type="email"
- name="email"
- id="email"
- value={data.email}
- readOnly
- className="w-full rounded-md border-gray-300 text-sm text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500"
- />
- <button
- type="button"
- onClick={() => {
- setCopied(true);
- navigator.clipboard.writeText(data.impersonateUrl);
- toast.success("Copied to clipboard");
- setTimeout(() => {
- setCopied(false);
- }, 3000);
- }}
- className="rounded-md border border-gray-300 p-2"
- >
- {copied ? (
- <Tick className="h-5 w-5 text-gray-500" />
- ) : (
- <Copy className="h-5 w-5 text-gray-500" />
- )}
- </button>
- </div>
- )}
+ {data && <UserInfo data={data} />}
</div>
);
}
diff --git a/apps/web/app/admin.dub.co/components/impersonate-user.tsx b/apps/web/app/admin.dub.co/components/impersonate-user.tsx
index 5cfe6f2a02..2753c1606f 100644
--- a/apps/web/app/admin.dub.co/components/impersonate-user.tsx
+++ b/apps/web/app/admin.dub.co/components/impersonate-user.tsx
@@ -1,24 +1,21 @@
"use client";
-import { Copy, LoadingSpinner, Tick } from "@dub/ui";
+import { Button, LoadingSpinner } from "@dub/ui";
import { cn } from "@dub/utils";
import { useState } from "react";
import { useFormStatus } from "react-dom";
import { toast } from "sonner";
-import { getUser } from "../actions";
+import { banUser, getUserOrProjectOwner } from "../actions";
+import UserInfo, { UserInfoProps } from "./user-info";
export default function ImpersonateUser() {
- const [data, setData] = useState<{
- email: string;
- impersonateUrl: string;
- } | null>(null);
- const [copied, setCopied] = useState(false);
+ const [data, setData] = useState<UserInfoProps | null>(null);
return (
<div className="flex flex-col space-y-5">
<form
action={(data) =>
- getUser(data).then((res) => {
+ getUserOrProjectOwner(data).then((res) => {
if (res.error) {
toast.error(res.error);
} else {
@@ -31,34 +28,25 @@ export default function ImpersonateUser() {
<Form />
</form>
{data && (
- <div className="flex w-full items-center space-x-3">
- <input
- type="email"
- name="email"
- id="email"
- value={data.email}
- readOnly
- className="w-full rounded-md border-gray-300 text-sm text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500"
- />
- <button
- type="button"
- onClick={() => {
- setCopied(true);
- navigator.clipboard.writeText(data.impersonateUrl);
- toast.success("Copied to clipboard");
- setTimeout(() => {
- setCopied(false);
- }, 3000);
- }}
- className="rounded-md border border-gray-300 p-2"
- >
- {copied ? (
- <Tick className="h-5 w-5 text-gray-500" />
- ) : (
- <Copy className="h-5 w-5 text-gray-500" />
- )}
- </button>
- </div>
+ <form
+ action={(formData) => {
+ if (
+ !confirm(
+ `This will ban the user ${data.email} and delete all their projects and links. Are you sure?`,
+ )
+ ) {
+ return;
+ }
+ banUser(formData).then(() => {
+ toast.success("Successfully banned user");
+ });
+ }}
+ >
+ <UserInfo data={data} />
+ <div className="mt-4">
+ <BanButton />
+ </div>
+ </form>
)}
</div>
);
@@ -89,3 +77,8 @@ const Form = () => {
</div>
);
};
+
+const BanButton = () => {
+ const { pending } = useFormStatus();
+ return <Button text="Confirm Ban" loading={pending} variant="danger" />;
+};
diff --git a/apps/web/app/admin.dub.co/components/user-info.tsx b/apps/web/app/admin.dub.co/components/user-info.tsx
new file mode 100644
index 0000000000..fab43334cf
--- /dev/null
+++ b/apps/web/app/admin.dub.co/components/user-info.tsx
@@ -0,0 +1,104 @@
+"use client";
+
+import { Badge, Copy, Tick } from "@dub/ui";
+import { capitalize, cn, nFormatter } from "@dub/utils";
+import { useState } from "react";
+import { toast } from "sonner";
+import { Globe } from "lucide-react";
+
+export interface UserInfoProps {
+ email: string;
+ defaultDomainLinks: Record<string, number>;
+ projects: {
+ name: string;
+ slug: string;
+ plan: string;
+ clicks: number;
+ domains: number;
+ links: number;
+ }[];
+ impersonateUrl: string;
+}
+
+export default function UserInfo({ data }: { data: UserInfoProps }) {
+ const [copied, setCopied] = useState(false);
+
+ return (
+ <>
+ <div className="flex w-full items-center space-x-3">
+ <input
+ type="email"
+ name="email"
+ id="email"
+ value={data.email}
+ readOnly
+ className="w-full rounded-md border-gray-300 text-sm text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500"
+ />
+ <button
+ type="button"
+ onClick={() => {
+ setCopied(true);
+ navigator.clipboard.writeText(data.impersonateUrl);
+ toast.success("Copied to clipboard");
+ setTimeout(() => {
+ setCopied(false);
+ }, 3000);
+ }}
+ className="rounded-md border border-gray-300 p-2"
+ >
+ {copied ? (
+ <Tick className="h-5 w-5 text-gray-500" />
+ ) : (
+ <Copy className="h-5 w-5 text-gray-500" />
+ )}
+ </button>
+ </div>
+ {Object.keys(data.defaultDomainLinks).length > 0 && (
+ <div className="mt-2 grid divide-y divide-gray-200">
+ {Object.entries(data.defaultDomainLinks).map(([domain, count]) => (
+ <div key={domain} className="flex justify-between py-2">
+ <div className="flex items-center space-x-2">
+ <Globe className="h-4 w-4" />
+ <span className="font-semibold text-gray-700">{domain}</span>
+ </div>
+ <span className="text-gray-500">{count}</span>
+ </div>
+ ))}
+ </div>
+ )}
+ <div className="mt-2 grid grid-cols-2 gap-4">
+ {data.projects.map((project) => (
+ <div
+ key={project.slug}
+ className="flex flex-col space-y-2 rounded-lg border border-gray-200 p-2"
+ >
+ <div className="flex items-center space-x-2">
+ <p className="font-semibold">{project.name}</p>
+ <Badge className="lowercase">{project.slug}</Badge>
+ </div>
+ <div className="flex justify-between text-sm">
+ <span className="font-medium text-gray-700">Plan</span>
+ <span className="text-gray-500">{capitalize(project.plan)}</span>
+ </div>
+ <div className="flex justify-between text-sm">
+ <span className="font-medium text-gray-700">Domains</span>
+ <span className="text-gray-500">{project.domains}</span>
+ </div>
+ <div className="flex justify-between text-sm">
+ <span className="font-medium text-gray-700">Links</span>
+ <span className="text-gray-500">
+ {nFormatter(project.links, { full: true })}
+ </span>
+ </div>
+ <div className="flex justify-between text-sm">
+ <span className="font-medium text-gray-700">Clicks</span>
+ <span className="text-gray-500">
+ {nFormatter(project.clicks, { full: true })}
+ </span>
+ </div>
+ </div>
+ ))}
+ </div>
+ </>
+ );
+}
diff --git a/apps/web/app/admin.dub.co/page.tsx b/apps/web/app/admin.dub.co/page.tsx
index e8563f965f..8483d031b9 100644
--- a/apps/web/app/admin.dub.co/page.tsx
+++ b/apps/web/app/admin.dub.co/page.tsx
@@ -1,5 +1,4 @@
import { constructMetadata } from "@dub/utils";
-import BanUser from "./components/ban";
import ImpersonateProject from "./components/impersonate-project";
import ImpersonateUser from "./components/impersonate-user";
@@ -23,13 +22,6 @@ export default function AdminPage() {
</p>
<ImpersonateProject />
</div>
- <div className="flex flex-col space-y-4 px-5 py-10">
- <h2 className="text-xl font-semibold">Ban Users</h2>
- <p className="text-sm text-gray-500">
- Ban users from Dub. This will prevent them from logging in.
- </p>
- <BanUser />
- </div>
</div>
);
}
diff --git a/apps/web/ui/links/link-card.tsx b/apps/web/ui/links/link-card.tsx
index fc30d4e063..bc697c1daa 100644
--- a/apps/web/ui/links/link-card.tsx
+++ b/apps/web/ui/links/link-card.tsx
@@ -37,6 +37,7 @@ import {
CopyPlus,
Edit3,
EyeOff,
+ Mail,
MessageCircle,
QrCode,
TimerOff,
@@ -344,9 +345,19 @@ export default function LinkCard({
content={
<div className="w-full p-4">
<Avatar user={user} className="h-10 w-10" />
- <p className="mt-2 text-sm font-semibold text-gray-700">
- {user?.name || user?.email || "Anonymous User"}
- </p>
+ <div className="mt-2 flex items-center space-x-1.5">
+ <p className="text-sm font-semibold text-gray-700">
+ {user?.name || user?.email || "Anonymous User"}
+ </p>
+ {!slug && // this is only shown in admin mode (where there's no slug)
+ user?.email && (
+ <CopyButton
+ value={user.email}
+ icon={Mail}
+ className="[&>*]:w-3 [&>*]:h-3"
+ />
+ )}
+ </div>
<p className="mt-1 text-xs text-gray-500">
Created{" "}
{new Date(createdAt).toLocaleDateString("en-us", {
@@ -388,7 +399,7 @@ export default function LinkCard({
href={url}
target="_blank"
rel="noopener noreferrer"
- className="xs:block hidden max-w-[140px] truncate text-sm font-medium text-gray-700 underline-offset-2 hover:underline sm:max-w-[300px] md:max-w-[360px] xl:max-w-[440px]"
+ className="xs:block hidden max-w-[140px] truncate text-sm font-medium text-gray-700 underline-offset-2 hover:underline sm:max-w-[300px] md:max-w-[360px] xl:max-w-[420px]"
>
{url}
</a>
diff --git a/packages/ui/src/copy-button.tsx b/packages/ui/src/copy-button.tsx
index 3e58cbed35..023db65949 100644
--- a/packages/ui/src/copy-button.tsx
+++ b/packages/ui/src/copy-button.tsx
@@ -1,6 +1,7 @@
"use client";
import { cn } from "@dub/utils";
+import { LucideIcon } from "lucide-react";
import { useState } from "react";
import { toast } from "sonner";
import { Copy, Tick } from "./icons";
@@ -8,11 +9,14 @@ import { Copy, Tick } from "./icons";
export function CopyButton({
value,
className,
+ icon,
}: {
value: string;
className?: string;
+ icon?: LucideIcon;
}) {
const [copied, setCopied] = useState(false);
+ const Comp = icon || Copy;
return (
<button
onClick={(e) => {
@@ -32,7 +36,7 @@ export function CopyButton({
{copied ? (
<Tick className="text-gray-700 transition-all group-hover:text-blue-800" />
) : (
- <Copy className="text-gray-700 transition-all group-hover:text-blue-800" />
+ <Comp className="text-gray-700 transition-all group-hover:text-blue-800" />
)}
</button>
);