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.

Move webhooks into modal

Tim Wilsonffbd4e78dcf2
diff --git a/apps/web/ui/modals/link-builder/constants.ts b/apps/web/ui/modals/link-builder/constants.ts
index 6f8f65ae3f..0eb8967673 100644
--- a/apps/web/ui/modals/link-builder/constants.ts
+++ b/apps/web/ui/modals/link-builder/constants.ts
@@ -1,4 +1,3 @@
-import { ExpandedLinkProps } from "@/lib/types";
import {
CircleHalfDottedClock,
Crosshairs3,
@@ -6,10 +5,10 @@ import {
Icon,
Incognito,
InputPassword,
+ Webhook,
WindowSearch,
} from "@dub/ui/icons";
import { Settings } from "lucide-react";
-import { UseFormSetValue } from "react-hook-form";
import { LinkFormData } from ".";
import { getABTestingLabel } from "./ab-testing-modal";
import { getExpirationLabel } from "./expiration-modal";
@@ -26,7 +25,6 @@ type MoreItem = {
type: string;
badgeLabel?: (data: LinkFormData) => string;
enabled?: (data: LinkFormData) => boolean;
- remove?: (setValue: UseFormSetValue<ExpandedLinkProps>) => void;
add?: boolean;
};
@@ -61,12 +59,17 @@ export const MORE_ITEMS: MoreItem[] = [
learnMoreUrl: "https://dub.co/help/article/link-expiration",
shortcutKey: "e",
enabled: (data) => Boolean(data.expiresAt),
- remove: (setValue) => {
- setValue("expiresAt", null, { shouldDirty: true });
- setValue("expiredUrl", null, { shouldDirty: true });
- },
type: "modal",
},
+ {
+ key: "webhookIds",
+ icon: Webhook,
+ label: "Webhooks",
+ shortcutKey: "w",
+ enabled: (data) => Boolean(data.webhookIds?.length),
+ type: "modal",
+ add: false,
+ },
{
key: "advanced",
icon: Settings,
diff --git a/apps/web/ui/modals/link-builder/index.tsx b/apps/web/ui/modals/link-builder/index.tsx
index c8542c5e46..0a49d72e03 100644
--- a/apps/web/ui/modals/link-builder/index.tsx
+++ b/apps/web/ui/modals/link-builder/index.tsx
@@ -68,7 +68,6 @@ import { useTargetingModal } from "./targeting-modal";
import { useMetatags } from "./use-metatags";
import { useUTMModal } from "./utm-modal";
import { UTMTemplatesButton } from "./utm-templates-button";
-import { WebhookSelect } from "./webhook-select";
export const LinkModalContext = createContext<{
workspaceId?: string;
@@ -524,7 +523,6 @@ function LinkBuilderInner({
<ABTestingButton />
<PasswordButton />
</div>
- <WebhookSelect />
<MoreDropdown />
</div>
{homepageDemo ? (
diff --git a/apps/web/ui/modals/link-builder/more-dropdown.tsx b/apps/web/ui/modals/link-builder/more-dropdown.tsx
index 5aee89b311..7fc7112c74 100644
--- a/apps/web/ui/modals/link-builder/more-dropdown.tsx
+++ b/apps/web/ui/modals/link-builder/more-dropdown.tsx
@@ -17,6 +17,7 @@ import { MOBILE_MORE_ITEMS, MORE_ITEMS } from "./constants";
import { useExpirationModal } from "./expiration-modal";
import { usePasswordModal } from "./password-modal";
import { useTargetingModal } from "./targeting-modal";
+import { useWebhooksModal } from "./webhooks-modal";
export function MoreDropdown() {
const { isMobile } = useMediaQuery();
@@ -30,17 +31,19 @@ export function MoreDropdown() {
return [...(isMobile ? MOBILE_MORE_ITEMS : []), ...MORE_ITEMS];
}, [data, isMobile]);
+ const { ABTestingModal, setShowABTestingModal } = useABTestingModal();
const { PasswordModal, setShowPasswordModal } = usePasswordModal();
const { TargetingModal, setShowTargetingModal } = useTargetingModal();
const { ExpirationModal, setShowExpirationModal } = useExpirationModal();
- const { ABTestingModal, setShowABTestingModal } = useABTestingModal();
+ const { WebhooksModal, setShowWebhooksModal } = useWebhooksModal();
const { AdvancedModal, setShowAdvancedModal } = useAdvancedModal();
const modalCallbacks = {
+ tests: setShowABTestingModal,
password: setShowPasswordModal,
targeting: setShowTargetingModal,
expiresAt: setShowExpirationModal,
- tests: setShowABTestingModal,
+ webhookIds: setShowWebhooksModal,
advanced: setShowAdvancedModal,
};
@@ -60,11 +63,12 @@ export function MoreDropdown() {
return (
<>
+ <ABTestingModal />
<PasswordModal />
<TargetingModal />
<ExpirationModal />
+ <WebhooksModal />
<AdvancedModal />
- <ABTestingModal />
<Popover
align="start"
content={
diff --git a/apps/web/ui/modals/link-builder/webhook-select.tsx b/apps/web/ui/modals/link-builder/webhook-select.tsx
deleted file mode 100644
index 9c076f2bb5..0000000000
--- a/apps/web/ui/modals/link-builder/webhook-select.tsx
+++ /dev/null
@@ -1,97 +0,0 @@
-import useWebhooks from "@/lib/swr/use-webhooks";
-import useWorkspace from "@/lib/swr/use-workspace";
-import { Button, Combobox, useKeyboardShortcut, Webhook } from "@dub/ui";
-import { cn } from "@dub/utils";
-import { useMemo, useState } from "react";
-import { useFormContext } from "react-hook-form";
-import { LinkFormData } from ".";
-
-export function WebhookSelect() {
- const [isOpen, setIsOpen] = useState(false);
- const { watch, setValue } = useFormContext<LinkFormData>();
- const { webhooks: availableWebhooks } = useWebhooks();
- useKeyboardShortcut("w", () => setIsOpen(true), { modal: true });
-
- const webhookIds = watch("webhookIds") as string[];
-
- const options = useMemo(
- () =>
- availableWebhooks?.map((webhook) => ({
- label: webhook.name,
- value: webhook.id,
- icon: <Webhook className="size-3.5" />,
- })),
- [availableWebhooks],
- );
-
- const selectedWebhooks = useMemo(
- () =>
- webhookIds
- .map((id) => options?.find(({ value }) => value === id)!)
- .filter(Boolean),
-
- [webhookIds, options],
- );
-
- const hasSelectedWebhooks = selectedWebhooks.length > 0;
-
- return (
- <Combobox
- multiple
- selected={selectedWebhooks || []}
- setSelected={(webhooks) => {
- const selectedIds = webhooks.map(({ value }) => value);
- setValue("webhookIds", selectedIds, { shouldDirty: true });
- }}
- options={options}
- icon={
- <Webhook
- className={cn(
- "size-3.5 flex-none",
- hasSelectedWebhooks && "text-blue-500",
- )}
- />
- }
- side="top"
- placeholder="Webhooks"
- searchPlaceholder="Search webhooks..."
- shortcutHint="W"
- buttonProps={{
- className:
- "h-9 px-2.5 w-fit font-medium text-neutral-700 max-w-48 min-w-0",
- }}
- open={isOpen}
- onOpenChange={setIsOpen}
- emptyState={<NoWebhooksFound />}
- >
- {selectedWebhooks.length > 0
- ? selectedWebhooks.length === 1
- ? selectedWebhooks[0].label
- : `${selectedWebhooks.length} Webhooks`
- : "Webhooks"}
- </Combobox>
- );
-}
-
-const NoWebhooksFound = () => {
- const { slug } = useWorkspace();
-
- return (
- <div className="flex h-full flex-col items-center justify-center gap-2 px-2 py-4 text-center text-sm">
- <div className="flex items-center justify-center rounded-2xl border border-neutral-200 bg-neutral-50 p-3">
- <Webhook className="size-6 text-neutral-700" />
- </div>
- <p className="mt-2 font-medium text-neutral-950">No webhooks found</p>
- <p className="mx-auto mt-1 w-full max-w-[180px] text-neutral-700">
- Add a webhook to receive a click event when someone clicks your link.
- </p>
- <div>
- <Button
- className="mt-1 h-8"
- onClick={() => window.open(`/${slug}/settings/webhooks`, "_blank")}
- text="Add webhook"
- />
- </div>
- </div>
- );
-};
diff --git a/apps/web/ui/modals/link-builder/webhooks-modal.tsx b/apps/web/ui/modals/link-builder/webhooks-modal.tsx
new file mode 100644
index 0000000000..1e665f2fed
--- /dev/null
+++ b/apps/web/ui/modals/link-builder/webhooks-modal.tsx
@@ -0,0 +1,265 @@
+import useWebhooks from "@/lib/swr/use-webhooks";
+import useWorkspace from "@/lib/swr/use-workspace";
+import {
+ Button,
+ Combobox,
+ Modal,
+ Tooltip,
+ useKeyboardShortcut,
+ Webhook,
+} from "@dub/ui";
+import { cn } from "@dub/utils";
+import { ChevronsUpDown } from "lucide-react";
+import {
+ Dispatch,
+ SetStateAction,
+ useCallback,
+ useMemo,
+ useState,
+} from "react";
+import { useForm, useFormContext } from "react-hook-form";
+import { LinkFormData } from ".";
+
+function WebhooksModal({
+ showWebhooksModal,
+ setShowWebhooksModal,
+}: {
+ showWebhooksModal: boolean;
+ setShowWebhooksModal: Dispatch<SetStateAction<boolean>>;
+}) {
+ return (
+ <Modal
+ showModal={showWebhooksModal}
+ setShowModal={setShowWebhooksModal}
+ className="sm:max-w-md"
+ >
+ <WebhooksModalInner setShowWebhooksModal={setShowWebhooksModal} />
+ </Modal>
+ );
+}
+
+function WebhooksModalInner({
+ setShowWebhooksModal,
+}: {
+ setShowWebhooksModal: Dispatch<SetStateAction<boolean>>;
+}) {
+ const {
+ watch: watchParent,
+ getValues: getValuesParent,
+ setValue: setValueParent,
+ } = useFormContext<LinkFormData>();
+
+ const {
+ watch,
+ setValue,
+ reset,
+ formState: { isDirty },
+ handleSubmit,
+ } = useForm<Pick<LinkFormData, "webhookIds">>({
+ values: {
+ webhookIds: getValuesParent("webhookIds"),
+ },
+ });
+
+ const parentWebhookIds = watchParent("webhookIds");
+ const webhookIds = watch("webhookIds");
+
+ return (
+ <form
+ className="px-5 py-4"
+ onSubmit={(e) => {
+ e.stopPropagation();
+ handleSubmit((data) => {
+ setValueParent("webhookIds", data.webhookIds, { shouldDirty: true });
+ setShowWebhooksModal(false);
+ })(e);
+ }}
+ >
+ <div className="flex items-center justify-between">
+ <div className="flex items-center gap-2">
+ <h3 className="text-lg font-medium">Link Webhooks</h3>
+ </div>
+ <div className="max-md:hidden">
+ <Tooltip
+ content={
+ <div className="px-2 py-1 text-xs text-neutral-700">
+ Press{" "}
+ <strong className="font-medium text-neutral-950">W</strong> to
+ open this quickly
+ </div>
+ }
+ side="right"
+ >
+ <kbd className="flex size-6 cursor-default items-center justify-center rounded-md border border-neutral-200 font-sans text-xs text-neutral-950">
+ W
+ </kbd>
+ </Tooltip>
+ </div>
+ </div>
+
+ <div className="mt-6">
+ <span className="mb-1 block text-sm font-normal text-neutral-500">
+ Webhooks
+ </span>
+ <WebhookSelect
+ webhookIds={webhookIds as string[]}
+ onChange={(webhookIds) =>
+ setValue("webhookIds", webhookIds, { shouldDirty: true })
+ }
+ />
+ </div>
+
+ <div className="mt-6 flex items-center justify-between">
+ <div>
+ {Boolean(parentWebhookIds?.length) && (
+ <button
+ type="button"
+ className="text-xs font-medium text-neutral-700 transition-colors hover:text-neutral-950"
+ onClick={() => {
+ setValueParent("webhookIds", [], { shouldDirty: true });
+ setShowWebhooksModal(false);
+ }}
+ >
+ Remove webhooks
+ </button>
+ )}
+ </div>
+ <div className="flex items-center gap-2">
+ <Button
+ type="button"
+ variant="secondary"
+ text="Cancel"
+ className="h-9 w-fit"
+ onClick={() => {
+ reset();
+ setShowWebhooksModal(false);
+ }}
+ />
+ <Button
+ type="submit"
+ variant="primary"
+ text="Save webhooks"
+ className="h-9 w-fit"
+ disabled={!isDirty}
+ />
+ </div>
+ </div>
+ </form>
+ );
+}
+
+export function useWebhooksModal() {
+ const [showWebhooksModal, setShowWebhooksModal] = useState(false);
+
+ const WebhooksModalCallback = useCallback(() => {
+ return (
+ <WebhooksModal
+ showWebhooksModal={showWebhooksModal}
+ setShowWebhooksModal={setShowWebhooksModal}
+ />
+ );
+ }, [showWebhooksModal, setShowWebhooksModal]);
+
+ return useMemo(
+ () => ({
+ setShowWebhooksModal,
+ WebhooksModal: WebhooksModalCallback,
+ }),
+ [setShowWebhooksModal, WebhooksModalCallback],
+ );
+}
+
+export function WebhookSelect({
+ webhookIds,
+ onChange,
+}: {
+ webhookIds: string[];
+ onChange: (webhookIds: string[]) => void;
+}) {
+ const [isOpen, setIsOpen] = useState(false);
+ const { webhooks: availableWebhooks } = useWebhooks();
+ useKeyboardShortcut("w", () => setIsOpen(true), { modal: true });
+
+ const options = useMemo(
+ () =>
+ availableWebhooks?.map((webhook) => ({
+ label: webhook.name,
+ value: webhook.id,
+ icon: <Webhook className="size-3.5" />,
+ })),
+ [availableWebhooks],
+ );
+
+ const selectedWebhooks = useMemo(
+ () =>
+ webhookIds
+ .map((id) => options?.find(({ value }) => value === id)!)
+ .filter(Boolean),
+
+ [webhookIds, options],
+ );
+
+ const hasSelectedWebhooks = selectedWebhooks.length > 0;
+
+ return (
+ <Combobox
+ multiple
+ selected={selectedWebhooks || []}
+ setSelected={(webhooks) => {
+ const selectedIds = webhooks.map(({ value }) => value);
+ onChange(selectedIds);
+ }}
+ options={options}
+ icon={
+ <Webhook
+ className={cn(
+ "size-3.5 flex-none",
+ hasSelectedWebhooks && "text-blue-500",
+ )}
+ />
+ }
+ placeholder="Webhooks"
+ searchPlaceholder="Search webhooks..."
+ buttonProps={{
+ className:
+ "h-10 px-2.5 w-full bg-white font-normal border-neutral-200 bg-white",
+ }}
+ matchTriggerWidth
+ caret={
+ <ChevronsUpDown className="ml-2 size-4 shrink-0 text-neutral-400" />
+ }
+ open={isOpen}
+ onOpenChange={setIsOpen}
+ emptyState={<NoWebhooksFound />}
+ >
+ {selectedWebhooks.length > 0
+ ? selectedWebhooks.length === 1
+ ? selectedWebhooks[0].label
+ : `${selectedWebhooks.length} Webhooks`
+ : "Webhooks"}
+ </Combobox>
+ );
+}
+
+const NoWebhooksFound = () => {
+ const { slug } = useWorkspace();
+
+ return (
+ <div className="flex h-full flex-col items-center justify-center gap-2 px-2 py-4 text-center text-sm">
+ <div className="flex items-center justify-center rounded-2xl border border-neutral-200 bg-neutral-50 p-3">
+ <Webhook className="size-6 text-neutral-700" />
+ </div>
+ <p className="mt-2 font-medium text-neutral-950">No webhooks found</p>
+ <p className="mx-auto mt-1 w-full max-w-[180px] text-neutral-700">
+ Add a webhook to receive a click event when someone clicks your link.
+ </p>
+ <div>
+ <Button
+ className="mt-1 h-8"
+ onClick={() => window.open(`/${slug}/settings/webhooks`, "_blank")}
+ text="Add webhook"
+ />
+ </div>
+ </div>
+ );
+};