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.

Show a disabled with a tooltip that says "this is managed by XY integration".

Kiran Kf539a41d766b
diff --git a/apps/web/app/api/webhooks/[webhookId]/route.ts b/apps/web/app/api/webhooks/[webhookId]/route.ts
index 55debe8d33..6c1037a31a 100644
--- a/apps/web/app/api/webhooks/[webhookId]/route.ts
+++ b/apps/web/app/api/webhooks/[webhookId]/route.ts
@@ -29,6 +29,7 @@ export const GET = withWorkspace(
triggers: true,
disabledAt: true,
links: true,
+ installationId: true,
},
});
@@ -136,6 +137,7 @@ export const PATCH = withWorkspace(
secret: true,
triggers: true,
disabledAt: true,
+ installationId: true,
links: {
select: {
linkId: true,
diff --git a/apps/web/app/api/webhooks/route.ts b/apps/web/app/api/webhooks/route.ts
index cf984f1f68..c0c826cae7 100644
--- a/apps/web/app/api/webhooks/route.ts
+++ b/apps/web/app/api/webhooks/route.ts
@@ -27,6 +27,7 @@ export const GET = withWorkspace(
disabledAt: true,
links: true,
receiver: true,
+ installationId: true,
},
orderBy: {
updatedAt: "desc",
diff --git a/apps/web/lib/swr/use-webhook.ts b/apps/web/lib/swr/use-webhook.ts
new file mode 100644
index 0000000000..385e60a924
--- /dev/null
+++ b/apps/web/lib/swr/use-webhook.ts
@@ -0,0 +1,30 @@
+import { WebhookProps } from "@/lib/types";
+import { fetcher } from "@dub/utils";
+import { useParams } from "next/navigation";
+import useSWR from "swr";
+import useWorkspace from "./use-workspace";
+
+export default function useWebhook() {
+ const { id: workspaceId } = useWorkspace();
+ const { webhookId } = useParams();
+
+ const {
+ data: webhook,
+ error,
+ mutate,
+ } = useSWR<WebhookProps>(
+ workspaceId &&
+ webhookId &&
+ `/api/webhooks/${webhookId}?workspaceId=${workspaceId}`,
+ fetcher,
+ {
+ dedupingInterval: 60000,
+ },
+ );
+
+ return {
+ webhook,
+ mutate,
+ isLoading: workspaceId && webhookId && !webhook && !error,
+ };
+}
diff --git a/apps/web/lib/webhook/create-webhook.ts b/apps/web/lib/webhook/create-webhook.ts
index 0c602d6d89..a3c6681099 100644
--- a/apps/web/lib/webhook/create-webhook.ts
+++ b/apps/web/lib/webhook/create-webhook.ts
@@ -51,6 +51,7 @@ export async function createWebhook({
triggers: true,
links: true,
disabledAt: true,
+ installationId: true,
},
});
diff --git a/apps/web/lib/zod/schemas/webhooks.ts b/apps/web/lib/zod/schemas/webhooks.ts
index e39451f161..8e3c2a4210 100644
--- a/apps/web/lib/zod/schemas/webhooks.ts
+++ b/apps/web/lib/zod/schemas/webhooks.ts
@@ -7,8 +7,9 @@ export const webhookSchema = z.object({
url: z.string(),
secret: z.string(),
triggers: z.array(z.enum(WEBHOOK_TRIGGERS)),
- disabledAt: z.date().nullable().optional(),
+ disabledAt: z.date().nullable(),
linkIds: z.array(z.string()).optional(),
+ installationId: z.string().nullable(),
});
export const createWebhookSchema = z.object({
diff --git a/apps/web/ui/webhooks/add-edit-webhook-form.tsx b/apps/web/ui/webhooks/add-edit-webhook-form.tsx
index 175a4a4672..6a29693384 100644
--- a/apps/web/ui/webhooks/add-edit-webhook-form.tsx
+++ b/apps/web/ui/webhooks/add-edit-webhook-form.tsx
@@ -100,8 +100,18 @@ export default function AddEditWebhookForm({
};
const { name, url, secret, triggers, linkIds = [] } = data;
+
const buttonDisabled = !name || !url || !triggers.length || saving;
- const canManageWebhook = !permissionsError;
+
+ const updateDisabled =
+ webhook?.installationId !== null || permissionsError !== false;
+
+ const disabledTooltip = webhook?.installationId
+ ? `This webhook is managed by ${webhook.name} integration.`
+ : permissionsError
+ ? permissionsError
+ : undefined;
+
const enableLinkSelection = LINK_LEVEL_WEBHOOK_TRIGGERS.some((trigger) =>
triggers.includes(trigger),
);
@@ -121,7 +131,7 @@ export default function AddEditWebhookForm({
className={cn(
"block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm",
{
- "cursor-not-allowed bg-gray-50": !canManageWebhook,
+ "cursor-not-allowed bg-gray-50": updateDisabled,
},
)}
required
@@ -130,7 +140,7 @@ export default function AddEditWebhookForm({
autoFocus
autoComplete="off"
placeholder="Webhook name"
- disabled={!canManageWebhook}
+ disabled={updateDisabled}
/>
</div>
</div>
@@ -144,7 +154,7 @@ export default function AddEditWebhookForm({
className={cn(
"block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm",
{
- "cursor-not-allowed bg-gray-50": !canManageWebhook,
+ "cursor-not-allowed bg-gray-50": updateDisabled,
},
)}
required
@@ -152,7 +162,7 @@ export default function AddEditWebhookForm({
onChange={(e) => setData({ ...data, url: e.target.value })}
autoComplete="off"
placeholder="Webhook URL"
- disabled={!canManageWebhook}
+ disabled={updateDisabled}
/>
</div>
</div>
@@ -190,7 +200,7 @@ export default function AddEditWebhookForm({
value={trigger}
id={trigger}
checked={triggers.includes(trigger)}
- disabled={!canManageWebhook}
+ disabled={updateDisabled}
onCheckedChange={(checked) => {
setData({
...data,
@@ -230,7 +240,7 @@ export default function AddEditWebhookForm({
value={trigger}
id={trigger}
checked={triggers.includes(trigger)}
- disabled={!canManageWebhook}
+ disabled={updateDisabled}
onCheckedChange={(checked) => {
setData({
...data,
@@ -264,7 +274,7 @@ export default function AddEditWebhookForm({
linkIds: ids,
})
}
- disabled={!canManageWebhook}
+ disabled={updateDisabled}
/>
</div>
</div>
@@ -273,11 +283,11 @@ export default function AddEditWebhookForm({
<Button
text={webhook ? "Save changes" : "Create webhook"}
- disabled={buttonDisabled}
+ disabled={buttonDisabled || updateDisabled}
loading={saving}
type="submit"
- {...(permissionsError && {
- disabledTooltip: permissionsError,
+ {...(disabledTooltip && {
+ disabledTooltip,
})}
/>
</form>
diff --git a/apps/web/ui/webhooks/webhook-header.tsx b/apps/web/ui/webhooks/webhook-header.tsx
index a4b19fda3f..b6c574e9cf 100644
--- a/apps/web/ui/webhooks/webhook-header.tsx
+++ b/apps/web/ui/webhooks/webhook-header.tsx
@@ -2,8 +2,8 @@
import { enableOrDisableWebhook } from "@/lib/actions/enable-disable-webhook";
import { clientAccessCheck } from "@/lib/api/tokens/permissions";
+import useWebhook from "@/lib/swr/use-webhook";
import useWorkspace from "@/lib/swr/use-workspace";
-import { WebhookProps } from "@/lib/types";
import { ThreeDots } from "@/ui/shared/icons";
import {
Button,
@@ -15,37 +15,26 @@ import {
TokenAvatar,
useCopyToClipboard,
} from "@dub/ui";
-import { fetcher } from "@dub/utils";
import { ChevronLeft, CircleX, Send, Trash } from "lucide-react";
import { useAction } from "next-safe-action/hooks";
import Link from "next/link";
import { notFound, useRouter, useSelectedLayoutSegment } from "next/navigation";
import { useState } from "react";
import { toast } from "sonner";
-import useSWR from "swr";
import { useDeleteWebhookModal } from "../modals/delete-webhook-modal";
import { useSendTestWebhookModal } from "../modals/send-test-webhook-modal";
import { WebhookStatus } from "./webhook-status";
export default function WebhookHeader({ webhookId }: { webhookId: string }) {
const router = useRouter();
+ const { webhook, isLoading, mutate } = useWebhook();
const { slug, id: workspaceId, role } = useWorkspace();
const [copiedWebhookId, copyToClipboard] = useCopyToClipboard();
+ const [openPopover, setOpenPopover] = useState(false);
const selectedLayoutSegment = useSelectedLayoutSegment();
const page = selectedLayoutSegment === null ? "" : selectedLayoutSegment;
- const [openPopover, setOpenPopover] = useState(false);
-
- const {
- data: webhook,
- isLoading,
- mutate,
- } = useSWR<WebhookProps>(
- `/api/webhooks/${webhookId}?workspaceId=${workspaceId}`,
- fetcher,
- );
-
const { DeleteWebhookModal, setDeleteWebhookModal } = useDeleteWebhookModal({
webhook,
});
@@ -83,6 +72,14 @@ export default function WebhookHeader({ webhookId }: { webhookId: string }) {
});
};
+ const disabled =
+ webhook?.installationId !== null || permissionsError !== false;
+ const disabledTooltip = webhook?.installationId
+ ? `This webhook is managed by ${webhook.name} integration.`
+ : permissionsError
+ ? permissionsError
+ : undefined;
+
return (
<>
<MaxWidthWrapper className="grid max-w-screen-lg gap-8">
@@ -165,8 +162,8 @@ export default function WebhookHeader({ webhookId }: { webhookId: string }) {
setOpenPopover(false);
}}
- disabled={!!permissionsError}
- disabledTooltip={permissionsError}
+ disabled={disabled}
+ disabledTooltip={disabledTooltip}
loading={isExecuting}
/>
@@ -192,8 +189,8 @@ export default function WebhookHeader({ webhookId }: { webhookId: string }) {
onClick={() => {
setDeleteWebhookModal(true);
}}
- disabled={!!permissionsError}
- disabledTooltip={permissionsError}
+ disabled={disabled}
+ disabledTooltip={disabledTooltip}
/>
</div>
}