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 UX to prevent accidental deletes

Steven Tey29187371960b
diff --git a/apps/web/app/api/tokens/route.ts b/apps/web/app/api/tokens/route.ts
index 2c98a2be5e..8c48b0f208 100644
--- a/apps/web/app/api/tokens/route.ts
+++ b/apps/web/app/api/tokens/route.ts
@@ -13,16 +13,25 @@ import { Prisma, User } from "@dub/prisma/client";
import { nanoid } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
+import { z } from "zod";
const MAX_WORKSPACE_TOKENS = 100;
+const getTokensQuerySchema = z.object({
+ userId: z.string().optional(),
+});
// GET /api/tokens - get all tokens for a workspace
export const GET = withWorkspace(
- async ({ workspace }) => {
+ async ({ workspace, searchParams }) => {
+ const { userId } = getTokensQuerySchema.parse(searchParams);
+
const tokens = await prisma.restrictedToken.findMany({
where: {
projectId: workspace.id,
installationId: null,
+ ...(userId && {
+ userId,
+ }),
},
select: {
id: true,
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/members/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/members/page-client.tsx
index 1e204e0e95..7e0e15963a 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/members/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/members/page-client.tsx
@@ -386,7 +386,6 @@ function RowMenuButton({
hasPassword: false,
provider: null,
},
- invite: isInvite,
});
const isCurrentUser = session?.user?.email === user.email;
diff --git a/apps/web/ui/modals/delete-domain-modal.tsx b/apps/web/ui/modals/delete-domain-modal.tsx
index a71fd8e5d2..e222c5057d 100644
--- a/apps/web/ui/modals/delete-domain-modal.tsx
+++ b/apps/web/ui/modals/delete-domain-modal.tsx
@@ -1,7 +1,8 @@
import { mutatePrefix } from "@/lib/swr/mutate";
import useWorkspace from "@/lib/swr/use-workspace";
import { DomainProps } from "@/lib/types";
-import { Button, LinkLogo, Modal, useMediaQuery } from "@dub/ui";
+import { Button, Modal, useMediaQuery } from "@dub/ui";
+import { Globe } from "@dub/ui/icons";
import {
Dispatch,
SetStateAction,
@@ -22,78 +23,95 @@ function DeleteDomainModal({
}) {
const { id } = useWorkspace();
const [deleting, setDeleting] = useState(false);
+ const [verification, setVerification] = useState("");
const domain = props.slug;
+ const confirmationText = "confirm delete domain";
+ const isVerified = verification === confirmationText;
+
const { isMobile } = useMediaQuery();
+ const deleteDomain = async () => {
+ setDeleting(true);
+ const res = await fetch(`/api/domains/${domain}?workspaceId=${id}`, {
+ method: "DELETE",
+ });
+ setDeleting(false);
+ if (res.status === 200) {
+ await mutatePrefix("/api/domains");
+ setShowDeleteDomainModal(false);
+ toast.success("Successfully deleted domain!");
+ } else {
+ const { error } = await res.json();
+ toast.error(error.message);
+ }
+ };
+
return (
<Modal
showModal={showDeleteDomainModal}
setShowModal={setShowDeleteDomainModal}
+ className="max-w-md"
>
- <div className="flex flex-col items-center justify-center space-y-3 border-b border-neutral-200 px-4 py-4 pt-8 text-center sm:px-16">
- <LinkLogo apexDomain={domain} />
+ <div className="space-y-2 border-b border-neutral-200 px-4 py-4 sm:px-6">
<h3 className="text-lg font-medium">Delete {domain}</h3>
- <div className="space-y-2 text-sm text-neutral-500">
- <p>
- Deleting this domain will delete all associated links as well as
- their analytics, permanently.
- </p>
- {Boolean(props.registeredDomain) && (
- <p>The domain will also be provisioned back to Dub.</p>
- )}
- <p>
- <strong className="font-semibold text-neutral-700">
- This action can't be undone
- </strong>{" "}
- – proceed with caution.
- </p>
- </div>
+ <p className="text-sm text-neutral-500">
+ Deleting this domain will delete all associated links as well as their
+ analytics, permanently.
+ {Boolean(props.registeredDomain) &&
+ " The domain will also be provisioned back to Dub."}{" "}
+ <strong className="font-semibold text-neutral-700">
+ This action can't be undone
+ </strong>{" "}
+ – proceed with caution.
+ </p>
</div>
<form
onSubmit={async (e) => {
e.preventDefault();
- setDeleting(true);
- fetch(`/api/domains/${domain}?workspaceId=${id}`, {
- method: "DELETE",
- }).then(async (res) => {
- if (res.status === 200) {
- await mutatePrefix("/api/domains");
- setShowDeleteDomainModal(false);
- toast.success("Successfully deleted domain!");
- } else {
- const { error } = await res.json();
- toast.error(error.message);
- }
- setDeleting(false);
- });
+ await deleteDomain();
}}
- className="flex flex-col space-y-3 bg-neutral-50 px-4 py-8 text-left sm:px-16"
+ className="flex flex-col space-y-4 bg-neutral-50 px-4 py-4 sm:px-6"
>
+ <div className="relative flex items-center gap-3 rounded-md border border-neutral-300 bg-white p-4">
+ <Globe className="size-5 text-neutral-500" />
+
+ <h3 className="line-clamp-1 text-sm font-medium text-neutral-600">
+ {domain}
+ </h3>
+ </div>
+
<div>
<label
htmlFor="verification"
className="block text-sm text-neutral-700"
>
To verify, type{" "}
- <span className="font-semibold">confirm delete {domain}</span> below
+ <span className="font-semibold">{confirmationText}</span> below
</label>
<div className="relative mt-1 rounded-md shadow-sm">
<input
type="text"
name="verification"
id="verification"
- pattern={`confirm delete ${domain}`}
+ pattern={confirmationText}
required
autoFocus={!isMobile}
autoComplete="off"
+ value={verification}
+ onChange={(e) => setVerification(e.target.value)}
className="block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-300 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
/>
</div>
</div>
- <Button variant="danger" text="Confirm delete" loading={deleting} />
+ <Button
+ text="Delete"
+ variant="danger"
+ loading={deleting}
+ disabled={!isVerified}
+ />
</form>
</Modal>
);
@@ -110,7 +128,7 @@ export function useDeleteDomainModal({ props }: { props?: DomainProps }) {
props={props}
/>
) : null;
- }, [showDeleteDomainModal, setShowDeleteDomainModal]);
+ }, [showDeleteDomainModal, props]);
return useMemo(
() => ({
diff --git a/apps/web/ui/modals/delete-token-modal.tsx b/apps/web/ui/modals/delete-token-modal.tsx
index a5c8894414..a6825850d0 100644
--- a/apps/web/ui/modals/delete-token-modal.tsx
+++ b/apps/web/ui/modals/delete-token-modal.tsx
@@ -24,6 +24,7 @@ function DeleteTokenModal({
const { isMobile } = useMediaQuery();
const { id: workspaceId } = useWorkspace();
const [removing, setRemoving] = useState(false);
+ const [verification, setVerification] = useState("");
// Determine the endpoint
const isRestrictedToken = "scopes" in token ? true : false;
@@ -42,6 +43,26 @@ function DeleteTokenModal({
}
}, [isRestrictedToken]);
+ const confirmationText = "confirm delete token";
+ const isVerified = verification === confirmationText;
+
+ const deleteToken = async () => {
+ setRemoving(true);
+ const res = await fetch(endpoint.url, {
+ method: "DELETE",
+ headers: { "Content-Type": "application/json" },
+ });
+ setRemoving(false);
+ if (res.status === 200) {
+ toast.success(`Successfully deleted API key`);
+ mutate(endpoint.mutate);
+ setShowDeleteTokenModal(false);
+ } else {
+ const { error } = await res.json();
+ toast.error(error.message);
+ }
+ };
+
return (
<Modal
showModal={showDeleteTokenModal}
@@ -56,7 +77,13 @@ function DeleteTokenModal({
</p>
</div>
- <div className="flex flex-col space-y-4 bg-neutral-50 px-4 py-4 sm:px-6">
+ <form
+ onSubmit={async (e) => {
+ e.preventDefault();
+ await deleteToken();
+ }}
+ className="flex flex-col space-y-4 bg-neutral-50 px-4 py-4 sm:px-6"
+ >
<div className="relative flex items-center gap-2 space-x-3 rounded-md border border-neutral-300 bg-white px-4 py-2">
<Key className="size-5 text-neutral-500" />
@@ -95,30 +122,37 @@ function DeleteTokenModal({
</div>
</div>
+ <div>
+ <label
+ htmlFor="verification"
+ className="block text-sm text-neutral-700"
+ >
+ To verify, type{" "}
+ <span className="font-semibold">{confirmationText}</span> below
+ </label>
+ <div className="relative mt-1 rounded-md shadow-sm">
+ <input
+ type="text"
+ name="verification"
+ id="verification"
+ pattern={confirmationText}
+ required
+ autoFocus={!isMobile}
+ autoComplete="off"
+ value={verification}
+ onChange={(e) => setVerification(e.target.value)}
+ className="block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-300 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
+ />
+ </div>
+ </div>
+
<Button
text="Delete"
variant="danger"
- autoFocus={!isMobile}
loading={removing}
- onClick={() => {
- setRemoving(true);
- fetch(endpoint.url, {
- method: "DELETE",
- headers: { "Content-Type": "application/json" },
- }).then(async (res) => {
- setRemoving(false);
- if (res.status === 200) {
- toast.success(`Successfully deleted API key`);
- mutate(endpoint.mutate);
- setShowDeleteTokenModal(false);
- } else {
- const { error } = await res.json();
- toast.error(error.message);
- }
- });
- }}
+ disabled={!isVerified}
/>
- </div>
+ </form>
</Modal>
);
}
diff --git a/apps/web/ui/modals/delete-workspace-modal.tsx b/apps/web/ui/modals/delete-workspace-modal.tsx
index fd4008f32f..3fda6766f1 100644
--- a/apps/web/ui/modals/delete-workspace-modal.tsx
+++ b/apps/web/ui/modals/delete-workspace-modal.tsx
@@ -1,5 +1,5 @@
import useWorkspace from "@/lib/swr/use-workspace";
-import { Button, Logo, Modal, useMediaQuery } from "@dub/ui";
+import { BlurImage, Button, Logo, Modal, useMediaQuery } from "@dub/ui";
import { cn } from "@dub/utils";
import { useSession } from "next-auth/react";
import { useParams, useRouter } from "next/navigation";
@@ -23,9 +23,16 @@ function DeleteWorkspaceModal({
const { update } = useSession();
const router = useRouter();
const { slug } = useParams() as { slug: string };
- const { id, isOwner } = useWorkspace();
+ const { id, isOwner, name, logo } = useWorkspace();
const [deleting, setDeleting] = useState(false);
+ const [workspaceSlugVerification, setWorkspaceSlugVerification] =
+ useState("");
+ const [verification, setVerification] = useState("");
+
+ const confirmationText = "confirm delete workspace";
+ const isVerified = verification === confirmationText;
+ const isSlugVerified = workspaceSlugVerification === slug;
async function deleteWorkspace() {
return new Promise((resolve, reject) => {
@@ -55,12 +62,12 @@ function DeleteWorkspaceModal({
<Modal
showModal={showDeleteWorkspaceModal}
setShowModal={setShowDeleteWorkspaceModal}
+ className="max-w-md"
>
- <div className="flex flex-col items-center justify-center space-y-3 border-b border-neutral-200 px-4 py-4 pt-8 sm:px-16">
- <Logo />
+ <div className="space-y-2 border-b border-neutral-200 px-4 py-4 sm:px-6">
<h3 className="text-lg font-medium">Delete Workspace</h3>
- <p className="text-center text-sm text-neutral-500">
- Warning: This will permanently delete your workspace, custom domain,
+ <p className="text-sm text-neutral-500">
+ Warning: This will permanently delete your workspace, custom domains,
and all associated links and their respective analytics.
</p>
</div>
@@ -74,16 +81,38 @@ function DeleteWorkspaceModal({
error: (err) => err,
});
}}
- className="flex flex-col space-y-6 bg-neutral-50 px-4 py-8 text-left sm:px-16"
+ className="flex flex-col space-y-4 bg-neutral-50 px-4 py-4 sm:px-6"
>
+ <div className="relative flex items-center gap-3 rounded-md border border-neutral-300 bg-white px-4 py-2">
+ {logo ? (
+ <BlurImage
+ src={logo}
+ alt="Workspace logo"
+ className="size-7 rounded-full"
+ width={20}
+ height={20}
+ />
+ ) : (
+ <Logo className="size-7 text-neutral-500" />
+ )}
+
+ <div className="flex flex-1 flex-col gap-0.5">
+ <h3 className="line-clamp-1 text-sm font-medium text-neutral-600">
+ {name || slug}
+ </h3>
+ <p className="text-xs font-medium text-neutral-500">
+ app.dub.co/{slug}
+ </p>
+ </div>
+ </div>
+
<div>
<label
htmlFor="workspace-slug"
- className="block text-sm font-medium text-neutral-700"
+ className="block text-sm text-neutral-700"
>
Enter the workspace slug{" "}
- <span className="font-semibold text-black">{slug}</span> to
- continue:
+ <span className="font-semibold">{slug}</span> to continue:
</label>
<div className="relative mt-1 rounded-md shadow-sm">
<input
@@ -95,8 +124,10 @@ function DeleteWorkspaceModal({
pattern={slug}
required
disabled={!isOwner}
+ value={workspaceSlugVerification}
+ onChange={(e) => setWorkspaceSlugVerification(e.target.value)}
className={cn(
- "block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
+ "block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-300 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
{
"cursor-not-allowed bg-neutral-100": !isOwner,
},
@@ -111,22 +142,21 @@ function DeleteWorkspaceModal({
className="block text-sm text-neutral-700"
>
To verify, type{" "}
- <span className="font-semibold text-black">
- confirm delete workspace
- </span>{" "}
- below
+ <span className="font-semibold">{confirmationText}</span> below
</label>
<div className="relative mt-1 rounded-md shadow-sm">
<input
type="text"
name="verification"
id="verification"
- pattern="confirm delete workspace"
+ pattern={confirmationText}
required
autoComplete="off"
disabled={!isOwner}
+ value={verification}
+ onChange={(e) => setVerification(e.target.value)}
className={cn(
- "block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
+ "block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-300 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
{
"cursor-not-allowed bg-neutral-100": !isOwner,
},
@@ -136,9 +166,10 @@ function DeleteWorkspaceModal({
</div>
<Button
- text="Confirm delete workspace"
+ text="Delete"
variant="danger"
loading={deleting}
+ disabled={!isVerified || !isSlugVerified || !isOwner}
{...(!isOwner && {
disabledTooltip: "Only workspace owners can delete a workspace.",
})}
diff --git a/apps/web/ui/modals/remove-workspace-user-modal.tsx b/apps/web/ui/modals/remove-workspace-user-modal.tsx
index 8ce7070925..3ee137298f 100644
--- a/apps/web/ui/modals/remove-workspace-user-modal.tsx
+++ b/apps/web/ui/modals/remove-workspace-user-modal.tsx
@@ -1,9 +1,9 @@
import { mutatePrefix } from "@/lib/swr/mutate";
import useWorkspace from "@/lib/swr/use-workspace";
-import { UserProps } from "@/lib/types";
+import { TokenProps, UserProps } from "@/lib/types";
import { Avatar, Button, Modal, useMediaQuery } from "@dub/ui";
import { TriangleWarning } from "@dub/ui/icons";
-import { timeAgo } from "@dub/utils";
+import { fetcher, timeAgo } from "@dub/utils";
import { useSession } from "next-auth/react";
import { useRouter, useSearchParams } from "next/navigation";
@@ -15,7 +15,7 @@ import {
useState,
} from "react";
import { toast } from "sonner";
-import { mutate } from "swr";
+import useSWR, { mutate } from "swr";
function RemoveWorkspaceUserModal({
showRemoveWorkspaceUserModal,
@@ -24,18 +24,24 @@ function RemoveWorkspaceUserModal({
}: {
showRemoveWorkspaceUserModal: boolean;
setShowRemoveWorkspaceUserModal: Dispatch<SetStateAction<boolean>>;
- user: UserProps & { restrictedTokens?: { name: string; lastUsed: Date }[] };
+ user: Pick<UserProps, "id" | "name" | "email" | "image">;
}) {
const router = useRouter();
const { isMobile } = useMediaQuery();
const { data: session } = useSession();
const [removing, setRemoving] = useState(false);
+ const [verification, setVerification] = useState("");
const { id: workspaceId, name: workspaceName } = useWorkspace();
const searchParams = useSearchParams();
const isInvite = searchParams.get("status") === "invited";
- const RemoveWorkspaceUser = async () => {
+ const { data: restrictedTokens } = useSWR<TokenProps[]>(
+ `/api/tokens?workspaceId=${workspaceId}&userId=${user.id}`,
+ fetcher<TokenProps[]>,
+ );
+
+ const removeWorkspaceUser = async () => {
setRemoving(true);
const response = await fetch(
@@ -78,6 +84,93 @@ function RemoveWorkspaceUserModal({
const self = session?.user?.email === user.email;
+ const confirmationText = "confirm remove user";
+ const isVerified = verification === confirmationText;
+
+ const content = (
+ <>
+ {restrictedTokens && restrictedTokens.length > 0 && (
+ <div className="space-y-3 rounded-lg border border-amber-200 bg-amber-50 p-4">
+ <div className="flex-shrink-0">
+ <TriangleWarning className="size-5 text-amber-400" />
+ </div>
+
+ <h3 className="text-sm font-semibold leading-5 text-amber-900">
+ Warning: Active tokens detected
+ </h3>
+
+ <p className="text-sm font-normal text-amber-900">
+ {self ? "You have" : "This user has"} {restrictedTokens.length}{" "}
+ active tokens. {self ? "Leaving" : "Removing this user"} will
+ invalidate these tokens and may disrupt the integration.
+ </p>
+
+ <div>
+ <ul className="mt-6 space-y-2">
+ {restrictedTokens.map((token, index) => (
+ <li key={index} className="flex items-center justify-between">
+ <span className="text-sm font-medium leading-4 text-amber-900">
+ {token.name}
+ </span>
+ <span className="text-xs font-normal leading-4 text-amber-700">
+ used {timeAgo(token.lastUsed, { withAgo: true })}
+ </span>
+ </li>
+ ))}
+ </ul>
+ </div>
+ </div>
+ )}
+
+ <div className="relative flex items-center gap-2 space-x-3 rounded-md border border-neutral-300 bg-white px-4 py-2">
+ <div className="flex items-center gap-2">
+ <Avatar user={user} className="size-10" />
+ <div className="flex flex-col">
+ <p className="text-sm font-medium text-neutral-900">
+ {user.name || user.email}
+ </p>
+ <p className="text-xs text-neutral-500">{user.email}</p>
+ </div>
+ </div>
+ </div>
+
+ {!isInvite && (
+ <div>
+ <label
+ htmlFor="verification"
+ className="block text-sm text-neutral-700"
+ >
+ To verify, type{" "}
+ <span className="font-semibold">{confirmationText}</span> below
+ </label>
+ <div className="relative mt-1 rounded-md shadow-sm">
+ <input
+ type="text"
+ name="verification"
+ id="verification"
+ pattern={confirmationText}
+ required
+ autoFocus={!isMobile}
+ autoComplete="off"
+ value={verification}
+ onChange={(e) => setVerification(e.target.value)}
+ className="block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-300 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
+ />
+ </div>
+ </div>
+ )}
+
+ <Button
+ text={self ? "Leave" : isInvite ? "Revoke" : "Remove"}
+ variant="danger"
+ autoFocus={isInvite && !isMobile}
+ loading={removing}
+ disabled={!isInvite && !isVerified}
+ onClick={isInvite ? removeWorkspaceUser : undefined}
+ />
+ </>
+ );
+
return (
<Modal
showModal={showRemoveWorkspaceUserModal}
@@ -110,72 +203,26 @@ function RemoveWorkspaceUserModal({
</p>
</div>
- <div className="flex flex-col space-y-4 bg-neutral-50 px-4 py-4 sm:px-6">
- {user.restrictedTokens && user.restrictedTokens.length > 0 && (
- <div className="space-y-3 rounded-lg border border-amber-200 bg-amber-50 p-4">
- <div className="flex-shrink-0">
- <TriangleWarning className="size-5 text-amber-400" />
- </div>
-
- <h3 className="text-sm font-semibold leading-5 text-amber-900">
- Warning: Active tokens detected
- </h3>
-
- <p className="text-sm font-normal text-amber-900">
- {self ? "You have" : "This user has"}{" "}
- {user.restrictedTokens.length} active tokens.{" "}
- {self ? "Leaving" : "Removing this user"} will invalidate these
- tokens and may disrupt the integration.
- </p>
-
- <div>
- <ul className="mt-6 space-y-2">
- {user.restrictedTokens.map((token, index) => (
- <li key={index} className="flex items-center justify-between">
- <span className="text-sm font-medium leading-4 text-amber-900">
- {token.name}
- </span>
- <span className="text-xs font-normal leading-4 text-amber-700">
- used {timeAgo(token.lastUsed, { withAgo: true })}
- </span>
- </li>
- ))}
- </ul>
- </div>
- </div>
- )}
-
- <div className="relative flex items-center gap-2 space-x-3 rounded-md border border-neutral-300 bg-white px-4 py-2">
- <div className="flex items-center gap-2">
- <Avatar user={user} className="size-10" />
- <div className="flex flex-col">
- <p className="text-sm font-medium text-neutral-900">
- {user.name || user.email}
- </p>
- <p className="text-xs text-neutral-500">{user.email}</p>
- </div>
- </div>
+ {isInvite ? (
+ <div className="flex flex-col space-y-4 bg-neutral-50 px-4 py-4 sm:px-6">
+ {content}
</div>
-
- <Button
- text={self ? "Leave" : "Remove"}
- variant="danger"
- autoFocus={!isMobile}
- loading={removing}
- onClick={RemoveWorkspaceUser}
- />
- </div>
+ ) : (
+ <form
+ onSubmit={async (e) => {
+ e.preventDefault();
+ await removeWorkspaceUser();
+ }}
+ className="flex flex-col space-y-4 bg-neutral-50 px-4 py-4 sm:px-6"
+ >
+ {content}
+ </form>
+ )}
</Modal>
);
}
-export function useRemoveWorkspaceUserModal({
- user,
- invite,
-}: {
- user: UserProps & { restrictedTokens?: { name: string; lastUsed: Date }[] };
- invite?: boolean;
-}) {
+export function useRemoveWorkspaceUserModal({ user }: { user: UserProps }) {
const [showRemoveWorkspaceUserModal, setShowRemoveWorkspaceUserModal] =
useState(false);