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.

Wire up the disconnect button on the UI

Kiran Kbd0cabd5dc45
diff --git a/apps/web/app/api/oauth-apps/authorized/route.ts b/apps/web/app/api/oauth-apps/authorized/route.ts
index f4c40d4118..8eb10891bb 100644
--- a/apps/web/app/api/oauth-apps/authorized/route.ts
+++ b/apps/web/app/api/oauth-apps/authorized/route.ts
@@ -5,7 +5,7 @@ import { NextResponse } from "next/server";
// GET /api/oauth-apps/authorized - applications authorized to access workspace resources
export const GET = withWorkspace(
async ({ workspace }) => {
- const appsAuthorized = await prisma.oAuthAuthorizedApp.findMany({
+ const result = await prisma.oAuthAuthorizedApp.findMany({
where: {
projectId: workspace.id,
},
@@ -29,6 +29,19 @@ export const GET = withWorkspace(
},
});
+ const appsAuthorized = result.map((app) => ({
+ id: app.id,
+ createdAt: app.createdAt,
+ clientId: app.oAuthClient.clientId,
+ name: app.oAuthClient.name,
+ developer: app.oAuthClient.developer,
+ website: app.oAuthClient.website,
+ user: {
+ id: app.user.id,
+ name: app.user.name,
+ },
+ }));
+
return NextResponse.json(appsAuthorized);
},
{
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/integrations/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/integrations/page-client.tsx
index 28aa53bc5a..b8a8c2e65c 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/integrations/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/integrations/page-client.tsx
@@ -2,6 +2,7 @@
import useWorkspace from "@/lib/swr/use-workspace";
import { OAuthAuthorizedAppProps } from "@/lib/types";
+import { useDeleteAppModal } from "@/ui/modals/delete-oauth-app-modal";
import EmptyState from "@/ui/shared/empty-state";
import { Delete } from "@/ui/shared/icons";
import { Button, LoadingSpinner, Popover, TokenAvatar } from "@dub/ui";
@@ -63,15 +64,22 @@ export default function AuthorizedAppsPageClient() {
const AppRow = (app: OAuthAuthorizedAppProps) => {
const [openPopover, setOpenPopover] = useState(false);
+ const { DeleteAppModal, setShowDeleteAppModal } = useDeleteAppModal({
+ app: {
+ clientId: app.clientId,
+ name: app.name,
+ },
+ appType: "authorized",
+ });
+
return (
<>
+ <DeleteAppModal />
<div className="relative grid grid-cols-5 items-center px-5 py-3 sm:px-10">
<div className="col-span-3 flex items-center space-x-3">
- <TokenAvatar id={app.oAuthClient.name} />
+ <TokenAvatar id={app.name} />
<div className="flex flex-col space-y-px">
- <p className="font-semibold text-gray-700">
- {app.oAuthClient.name}
- </p>
+ <p className="font-semibold text-gray-700">{app.name}</p>
<div className="flex items-center gap-x-2">
<p className="text-sm text-gray-500" suppressHydrationWarning>
Authorized by {app.user.name} on {formatDate(app.createdAt)}
@@ -90,7 +98,7 @@ const AppRow = (app: OAuthAuthorizedAppProps) => {
className="h-9 justify-start px-2 font-medium"
onClick={() => {
setOpenPopover(false);
- alert("Not implemented yet");
+ setShowDeleteAppModal(true);
}}
/>
</div>
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/oauth-apps/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/oauth-apps/page-client.tsx
index 0b49e0f154..77c1004a7b 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/oauth-apps/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/oauth-apps/page-client.tsx
@@ -99,7 +99,11 @@ const AppRow = (app: OAuthAppProps) => {
});
const { DeleteAppModal, setShowDeleteAppModal } = useDeleteAppModal({
- app,
+ app: {
+ clientId: app.clientId,
+ name: app.name,
+ },
+ appType: "published",
});
const copyClientId = () => {
diff --git a/apps/web/lib/zod/schemas/oauth.ts b/apps/web/lib/zod/schemas/oauth.ts
index 4c90251ef1..5cf77f4842 100644
--- a/apps/web/lib/zod/schemas/oauth.ts
+++ b/apps/web/lib/zod/schemas/oauth.ts
@@ -84,17 +84,20 @@ export const tokenGrantSchema = z.discriminatedUnion(
},
);
-export const oAuthAuthorizedAppSchema = z.object({
- id: z.string(),
- createdAt: z.date(),
- oAuthClient: oAuthClientSchema.pick({
- clientId: true,
- name: true,
- developer: true,
- website: true,
- }),
- user: z.object({
+export const oAuthAuthorizedAppSchema = z
+ .object({
id: z.string(),
- name: z.string(),
- }),
-});
+ createdAt: z.date(),
+ user: z.object({
+ id: z.string(),
+ name: z.string(),
+ }),
+ })
+ .merge(
+ oAuthClientSchema.pick({
+ clientId: true,
+ name: true,
+ developer: true,
+ website: true,
+ }),
+ );
diff --git a/apps/web/ui/modals/delete-oauth-app-modal.tsx b/apps/web/ui/modals/delete-oauth-app-modal.tsx
index f27c37f158..44e17d3b42 100644
--- a/apps/web/ui/modals/delete-oauth-app-modal.tsx
+++ b/apps/web/ui/modals/delete-oauth-app-modal.tsx
@@ -11,34 +11,51 @@ import {
import { toast } from "sonner";
import { mutate } from "swr";
-function DeleteAppModal({
- showDeleteAppModal,
- setShowDeleteAppModal,
- app,
-}: {
+interface DeleteAppModalProps {
showDeleteAppModal: boolean;
setShowDeleteAppModal: Dispatch<SetStateAction<boolean>>;
- app: OAuthAppProps;
-}) {
- const { id: workspaceId } = useWorkspace();
+ app: Pick<OAuthAppProps, "name" | "clientId">;
+ appType: "published" | "authorized";
+}
+
+function DeleteAppModal(props: DeleteAppModalProps) {
+ const { showDeleteAppModal, setShowDeleteAppModal, app, appType } = props;
+
const { isMobile } = useMediaQuery();
+ const { id: workspaceId } = useWorkspace();
const [deleting, setDeleting] = useState(false);
+ const action = useMemo(() => {
+ if (appType === "authorized") {
+ return {
+ url: `/api/oauth-apps/${app.clientId}/disconnect?workspaceId=${workspaceId}`,
+ mutate: `/api/oauth-apps/authorized?workspaceId=${workspaceId}`,
+ message: "Successfully disconnected the application!",
+ title: `Disconnect ${app.name}`,
+ description: `This will disconnect the application from your workspace. Are you sure you want to continue?`,
+ };
+ } else {
+ return {
+ url: `/api/oauth-apps/${app.clientId}?workspaceId=${workspaceId}`,
+ mutate: `/api/oauth-apps?workspaceId=${workspaceId}`,
+ message: "Successfully deleted the application!",
+ title: `Delete ${app.name}`,
+ description: `This will permanently delete the application and revoke all the access tokens associated with it. Are you sure you want to continue?`,
+ };
+ }
+ }, [app.clientId, appType, workspaceId]);
+
const deleteApp = useCallback(
async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
setDeleting(true);
- const response = await fetch(
- `/api/oauth-apps/${app.clientId}?workspaceId=${workspaceId}`,
- { method: "DELETE" },
- );
-
+ const response = await fetch(action.url, { method: "DELETE" });
const result = await response.json();
if (response.ok) {
- mutate(`/api/oauth-apps?workspaceId=${workspaceId}`);
- toast.success("Successfully deleted the application!");
+ mutate(action.mutate);
+ toast.success(action.message);
setShowDeleteAppModal(false);
} else {
toast.error(result.error.message);
@@ -53,11 +70,8 @@ function DeleteAppModal({
<Modal showModal={showDeleteAppModal} setShowModal={setShowDeleteAppModal}>
<div className="flex flex-col items-center justify-center space-y-3 border-b border-gray-200 px-4 py-4 pt-8 text-center sm:px-16">
<Logo />
- <h3 className="text-lg font-medium">Delete {app.name}</h3>
- <p className="text-sm text-gray-500">
- This will permanently the application and revoke all the access tokens
- associated with it. Are you sure you want to continue?
- </p>
+ <h3 className="text-lg font-medium">{action.title}</h3>
+ <p className="text-sm text-gray-500">{action.description}</p>
</div>
<form
onSubmit={deleteApp}
@@ -87,7 +101,13 @@ function DeleteAppModal({
);
}
-export function useDeleteAppModal({ app }: { app: OAuthAppProps }) {
+export function useDeleteAppModal({
+ app,
+ appType,
+}: {
+ app: Pick<OAuthAppProps, "name" | "clientId">;
+ appType: "published" | "authorized";
+}) {
const [showDeleteAppModal, setShowDeleteAppModal] = useState(false);
const DeleteAppModalCallback = useCallback(() => {
@@ -96,6 +116,7 @@ export function useDeleteAppModal({ app }: { app: OAuthAppProps }) {
showDeleteAppModal={showDeleteAppModal}
setShowDeleteAppModal={setShowDeleteAppModal}
app={app}
+ appType={appType}
/>
) : null;
}, [showDeleteAppModal, setShowDeleteAppModal]);