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.

update webhook link selector

Kiran K5a863ca90a64
diff --git a/apps/web/lib/integrations/segment/components/settings.tsx b/apps/web/lib/integrations/segment/components/settings.tsx
index e36e1c9226..d53e3a31a5 100644
--- a/apps/web/lib/integrations/segment/components/settings.tsx
+++ b/apps/web/lib/integrations/segment/components/settings.tsx
@@ -1,109 +1,17 @@
"use client";
-import useWorkspace from "@/lib/swr/use-workspace";
-import { InstalledIntegrationInfoProps } from "@/lib/types";
-import { Button, Webhook } from "@dub/ui";
-import { useAction } from "next-safe-action/hooks";
-import { useState } from "react";
-import { toast } from "sonner";
-import { installSegmentAction } from "../install";
-
-type SegmentSettingsProps = InstalledIntegrationInfoProps & {
- credentials: {
- writeKey?: string;
- };
-};
+import { SegmentSettingsProps } from "./types";
+import { UpdateWriteKey } from "./update-write-key";
+import WebhookLinkSelector from "./webhook-link-selector";
export const SegmentSettings = ({
installed,
credentials,
}: SegmentSettingsProps) => {
- const { id: workspaceId } = useWorkspace();
- const [writeKey, setWriteKey] = useState(credentials?.writeKey);
-
- const { executeAsync, isExecuting } = useAction(installSegmentAction, {
- async onSuccess() {
- toast.success("Segment integration enabled successfully.");
- },
- onError({ error }) {
- toast.error(error.serverError || "Failed to enable Segment integration.");
- },
- });
-
- const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
- e.preventDefault();
-
- if (!workspaceId) {
- return;
- }
-
- if (!writeKey) {
- toast.error("Write key is required.");
- return;
- }
-
- await executeAsync({
- workspaceId,
- writeKey,
- });
- };
-
return (
- <div className="w-full rounded-lg border border-gray-200 bg-white">
- <div className="flex items-center gap-x-2 border-b border-gray-200 px-6 py-4">
- <Webhook className="size-4" />
- <p className="text-sm font-medium text-gray-700">
- Send events to Segment
- </p>
- </div>
-
- <div className="p-6">
- <p className="text-sm leading-normal text-gray-600">
- To send click events to Segment, you need to add your Segment write
- key below.{" "}
- <a
- href="https://segment.com/docs/connections/find-writekey/"
- target="_blank"
- rel="noopener noreferrer"
- className="text-gray-500 underline underline-offset-4 hover:text-gray-700"
- >
- Learn about
- </a>{" "}
- how to locate your write key.
- </p>
-
- <form className="mt-4 flex items-end gap-2" onSubmit={onSubmit}>
- <div className="flex-1">
- <label htmlFor="writeKey" className="flex items-center space-x-2">
- <h2 className="text-sm font-medium text-gray-900">
- Segment write key
- </h2>
- </label>
- <div className="relative mt-2 rounded-md shadow-sm">
- <input
- className="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"
- placeholder="Enter your write key"
- required
- type="text"
- autoComplete="off"
- name="writeKey"
- value={writeKey}
- onChange={(e) => setWriteKey(e.target.value)}
- readOnly={installed?.id ? true : false}
- />
- </div>
- </div>
-
- <Button
- type="submit"
- variant="primary"
- text="Enable Segment"
- className="w-fit"
- loading={isExecuting}
- disabled={installed?.id ? true : false || !writeKey}
- />
- </form>
- </div>
- </div>
+ <>
+ <UpdateWriteKey installed={installed} credentials={credentials} />
+ {installed && <WebhookLinkSelector credentials={credentials} />}
+ </>
);
};
diff --git a/apps/web/lib/integrations/segment/components/types.ts b/apps/web/lib/integrations/segment/components/types.ts
new file mode 100644
index 0000000000..80e42d9acd
--- /dev/null
+++ b/apps/web/lib/integrations/segment/components/types.ts
@@ -0,0 +1,9 @@
+export interface SegmentSettingsProps {
+ installed: {
+ id: string;
+ };
+ credentials: {
+ writeKey?: string;
+ webhookId?: string;
+ };
+}
diff --git a/apps/web/lib/integrations/segment/components/update-write-key.tsx b/apps/web/lib/integrations/segment/components/update-write-key.tsx
new file mode 100644
index 0000000000..f9abdcb799
--- /dev/null
+++ b/apps/web/lib/integrations/segment/components/update-write-key.tsx
@@ -0,0 +1,99 @@
+"use client";
+
+import useWorkspace from "@/lib/swr/use-workspace";
+import { Lock } from "@/ui/shared/icons";
+import { Button } from "@dub/ui";
+import { useAction } from "next-safe-action/hooks";
+import { useState } from "react";
+import { toast } from "sonner";
+import { installSegmentAction } from "../install";
+import { SegmentSettingsProps } from "./types";
+
+export const UpdateWriteKey = ({
+ installed,
+ credentials,
+}: SegmentSettingsProps) => {
+ const { id: workspaceId } = useWorkspace();
+ const [writeKey, setWriteKey] = useState(credentials?.writeKey);
+
+ const { executeAsync, isExecuting } = useAction(installSegmentAction, {
+ async onSuccess() {
+ toast.success("Segment integration enabled successfully.");
+ },
+ onError({ error }) {
+ toast.error(error.serverError || "Failed to enable Segment integration.");
+ },
+ });
+
+ const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
+ e.preventDefault();
+
+ if (!workspaceId) {
+ return;
+ }
+
+ if (!writeKey) {
+ toast.error("Write key is required.");
+ return;
+ }
+
+ await executeAsync({
+ workspaceId,
+ writeKey,
+ });
+ };
+
+ return (
+ <div className="w-full rounded-lg border border-gray-200 bg-white">
+ <div className="flex items-center gap-x-2 border-b border-gray-200 px-6 py-4">
+ <Lock className="size-4" />
+ <p className="text-sm font-medium text-gray-700">
+ Configure Segment write key
+ </p>
+ </div>
+
+ <div className="p-4">
+ <p className="text-sm leading-normal text-gray-600">
+ To send click events to Segment, you need to add your Segment write
+ key below.{" "}
+ <a
+ href="https://segment.com/docs/connections/find-writekey/"
+ target="_blank"
+ rel="noopener noreferrer"
+ className="text-gray-500 underline underline-offset-4 hover:text-gray-700"
+ >
+ Learn about
+ </a>{" "}
+ how to locate your write key.
+ </p>
+
+ <form className="mt-4 flex items-end gap-2" onSubmit={onSubmit}>
+ <div className="flex-1">
+ <div className="relative rounded-md shadow-sm">
+ <input
+ className="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"
+ placeholder="Enter your write key"
+ required
+ type="text"
+ autoComplete="off"
+ name="writeKey"
+ value={writeKey}
+ onChange={(e) => setWriteKey(e.target.value)}
+ readOnly={installed?.id ? true : false}
+ />
+ </div>
+ </div>
+
+ <Button
+ type="submit"
+ variant="primary"
+ text="Enable Segment"
+ className="w-fit"
+ loading={isExecuting}
+ disabled={installed?.id ? true : false || !writeKey}
+ />
+ </form>
+ </div>
+ </div>
+ );
+};
diff --git a/apps/web/lib/integrations/segment/components/webhook-link-selector.tsx b/apps/web/lib/integrations/segment/components/webhook-link-selector.tsx
new file mode 100644
index 0000000000..dde0432cb4
--- /dev/null
+++ b/apps/web/lib/integrations/segment/components/webhook-link-selector.tsx
@@ -0,0 +1,228 @@
+"use client";
+
+import { clientAccessCheck } from "@/lib/api/tokens/permissions";
+import useLinks from "@/lib/swr/use-links";
+import useWorkspace from "@/lib/swr/use-workspace";
+import { LinkProps, WebhookProps } from "@/lib/types";
+import { Link } from "@/ui/shared/icons";
+import { Button, Combobox, LinkLogo } from "@dub/ui";
+import {
+ cn,
+ fetcher,
+ getApexDomain,
+ linkConstructor,
+ truncate,
+} from "@dub/utils";
+import { FormEvent, useMemo, useState } from "react";
+import { toast } from "sonner";
+import useSWR, { mutate } from "swr";
+import { useDebounce } from "use-debounce";
+import { SegmentSettingsProps } from "./types";
+
+export default function WebhookLinkSelector({
+ credentials,
+}: {
+ credentials: SegmentSettingsProps["credentials"];
+}) {
+ const webhookId = credentials?.webhookId;
+
+ const [saving, setSaving] = useState(false);
+ const { id: workspaceId, role } = useWorkspace();
+ const [data, setData] = useState<Pick<WebhookProps, "linkIds">>();
+
+ // if (!flags?.webhooks) {
+ // redirect(`/${workspaceSlug}`);
+ // }
+
+ const { data: webhook } = useSWR<WebhookProps>(
+ `/api/webhooks/${webhookId}?workspaceId=${workspaceId}`,
+ fetcher,
+ );
+
+ const { error: permissionsError } = clientAccessCheck({
+ action: "webhooks.write",
+ role,
+ });
+
+ // Save the form data
+ const onSubmit = async (e: FormEvent) => {
+ e.preventDefault();
+
+ setSaving(true);
+
+ const response = await fetch(
+ `/api/webhooks/${webhookId}?workspaceId=${workspaceId}`,
+ {
+ method: "PATCH",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify(data),
+ },
+ );
+
+ setSaving(false);
+
+ const result = await response.json();
+
+ if (!response.ok) {
+ toast.error(result.error.message);
+ return;
+ }
+
+ mutate(`/api/webhooks/${webhookId}?workspaceId=${workspaceId}`, result);
+ toast.success("Webhook preferences saved!");
+ };
+
+ const canManageWebhook = !permissionsError;
+ const linkIds = data?.linkIds || [];
+
+ return (
+ <div className="w-full rounded-lg border border-gray-200 bg-white">
+ <div className="flex items-center gap-x-2 border-b border-gray-200 px-6 py-4">
+ <Link className="size-4" />
+ <p className="text-sm font-medium text-gray-700">
+ Configure links we should send events for
+ </p>
+ </div>
+
+ <div className="p-4">
+ <p className="text-sm leading-normal text-gray-600">
+ Choose the links from the list below to track click events. We'll send
+ a <strong>click event</strong> to Segment each time a user clicks on
+ one of the links you select.
+ </p>
+
+ <form
+ onSubmit={onSubmit}
+ className="mt-4 flex flex-col items-center gap-2 sm:flex-row"
+ >
+ <div className="w-full flex-grow">
+ <LinksSelector
+ selectedLinkIds={linkIds}
+ setSelectedLinkIds={(ids) =>
+ setData({
+ ...data,
+ linkIds: ids,
+ })
+ }
+ disabled={!canManageWebhook}
+ />
+ </div>
+
+ <Button
+ text="Save changes"
+ loading={saving}
+ type="submit"
+ className="w-full sm:w-fit"
+ {...(permissionsError && {
+ disabledTooltip: permissionsError,
+ })}
+ disabled={!canManageWebhook}
+ />
+ </form>
+ </div>
+ </div>
+ );
+}
+
+const getLinkOption = (link: LinkProps) => ({
+ value: link.id,
+ label: linkConstructor({ ...link, pretty: true }),
+ icon: (
+ <LinkLogo
+ apexDomain={getApexDomain(link.url)}
+ className="h-4 w-4 sm:h-4 sm:w-4"
+ />
+ ),
+ meta: {
+ url: link.url,
+ },
+});
+
+function LinksSelector({
+ selectedLinkIds,
+ setSelectedLinkIds,
+ disabled,
+}: {
+ selectedLinkIds: string[];
+ setSelectedLinkIds: (ids: string[]) => void;
+ disabled?: boolean;
+}) {
+ const [search, setSearch] = useState("");
+ const [debouncedSearch] = useDebounce(search, 500);
+
+ const { links } = useLinks(
+ {
+ search: debouncedSearch,
+ },
+ {
+ keepPreviousData: false,
+ },
+ );
+
+ const { links: selectedLinks } = useLinks({
+ linkIds: selectedLinkIds,
+ });
+
+ const options = useMemo(
+ () => links?.map((link) => getLinkOption(link)),
+ [links],
+ );
+
+ const selectedOptions = useMemo(
+ () =>
+ selectedLinkIds
+ .map((id) =>
+ [...(links || []), ...(selectedLinks || [])].find((l) => l.id === id),
+ )
+ .filter(Boolean)
+ .map((l) => getLinkOption(l as LinkProps)),
+ [selectedLinkIds, links, selectedLinks],
+ );
+
+ return (
+ <Combobox
+ multiple
+ caret
+ matchTriggerWidth
+ side="top" // Since this control is near the bottom of the page, prefer top to avoid jumping
+ options={options}
+ selected={selectedOptions ?? []}
+ setSelected={(selected) => {
+ setSelectedLinkIds(selected.map(({ value: id }) => id));
+ }}
+ shouldFilter={false}
+ onSearchChange={setSearch}
+ buttonProps={{
+ disabled,
+ className: cn(
+ "h-auto py-1.5 px-2.5 w-full max-w-full text-gray-700 border-gray-300 items-start",
+ ),
+ }}
+ >
+ {selectedLinkIds.length === 0 ? (
+ <div className="py-0.5">Select links...</div>
+ ) : selectedLinks && selectedOptions ? (
+ <div className="flex flex-wrap gap-2">
+ {selectedOptions.slice(0, 10).map((option) => (
+ <span
+ key={option.value}
+ className="animate-fade-in flex min-w-0 items-center gap-1 rounded-md bg-gray-100 px-1.5 py-1 text-xs text-gray-600"
+ >
+ <LinkLogo
+ apexDomain={getApexDomain(option.meta.url)}
+ className="size-3 shrink-0 sm:size-3"
+ />
+ <span className="min-w-0 truncate">
+ {truncate(option.label, 32)}
+ </span>
+ </span>
+ ))}
+ </div>
+ ) : (
+ <div className="my-0.5 h-5 w-1/3 animate-pulse rounded bg-gray-200" />
+ )}
+ </Combobox>
+ );
+}
diff --git a/apps/web/ui/webhooks/add-edit-webhook-form.tsx b/apps/web/ui/webhooks/add-edit-webhook-form.tsx
index 1ccd18eaf5..ca2fe82365 100644
--- a/apps/web/ui/webhooks/add-edit-webhook-form.tsx
+++ b/apps/web/ui/webhooks/add-edit-webhook-form.tsx
@@ -81,8 +81,6 @@ export default function AddEditWebhookForm({
e.preventDefault();
setSaving(true);
- console.log(data);
-
const response = await fetch(endpoint.url, {
method: endpoint.method,
headers: {