The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
reuse the link selector
Kiran Kc7d47ee811ee
diff --git a/apps/web/lib/integrations/segment/components/configure-webhook.tsx b/apps/web/lib/integrations/segment/components/configure-webhook.tsxindex 88eb4fe047..98898d3f71 100644--- a/apps/web/lib/integrations/segment/components/configure-webhook.tsx+++ b/apps/web/lib/integrations/segment/components/configure-webhook.tsx@@ -1,27 +1,20 @@"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 { WebhookProps } from "@/lib/types";import {LINK_LEVEL_WEBHOOK_TRIGGERS,WEBHOOK_TRIGGER_DESCRIPTIONS,WORKSPACE_LEVEL_WEBHOOK_TRIGGERS,} from "@/lib/webhook/constants";import { Link } from "@/ui/shared/icons";-import { Button, Checkbox, Combobox, LinkLogo } from "@dub/ui";-import {- cn,- fetcher,- getApexDomain,- linkConstructor,- truncate,-} from "@dub/utils";-import { FormEvent, useEffect, useMemo, useState } from "react";+import { LinksSelector } from "@/ui/webhooks/link-selector";+import { Button, Checkbox } from "@dub/ui";+import { fetcher } from "@dub/utils";+import { FormEvent, useEffect, useState } from "react";import { toast } from "sonner";import useSWR, { mutate } from "swr";-import { useDebounce } from "use-debounce";import { SegmentSettingsProps } from "./types";export function ConfigureWebhook({@@ -114,7 +107,7 @@ export function ConfigureWebhook({</span></label><div className="mt-3 flex flex-col gap-2">- {WORKSPACE_LEVEL_WEBHOOK_TRIGGERS.map((trigger) => (+ {WORKSPACE_LEVEL_WEBHOOK_TRIGGERS.slice(-2).map((trigger) => (<div key={trigger} className="group flex gap-2"><Checkboxvalue={trigger}@@ -219,104 +212,3 @@ export function ConfigureWebhook({</form>);}--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.tsxindex ca2fe82365..175a4a4672 100644--- a/apps/web/ui/webhooks/add-edit-webhook-form.tsx+++ b/apps/web/ui/webhooks/add-edit-webhook-form.tsx@@ -1,28 +1,20 @@"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, NewWebhook, WebhookProps } from "@/lib/types";+import { NewWebhook, WebhookProps } from "@/lib/types";import {LINK_LEVEL_WEBHOOK_TRIGGERS,WEBHOOK_TRIGGER_DESCRIPTIONS,WORKSPACE_LEVEL_WEBHOOK_TRIGGERS,} from "@/lib/webhook/constants";-import {- Button,- Checkbox,- Combobox,- CopyButton,- InfoTooltip,- LinkLogo,-} from "@dub/ui";-import { cn, getApexDomain, linkConstructor, truncate } from "@dub/utils";+import { Button, Checkbox, CopyButton, InfoTooltip } from "@dub/ui";+import { cn } from "@dub/utils";import { redirect, useRouter } from "next/navigation";import { FormEvent, useMemo, useState } from "react";import { toast } from "sonner";import { mutate } from "swr";-import { useDebounce } from "use-debounce";+import { LinksSelector } from "./link-selector";const defaultValues: NewWebhook = {name: "",@@ -292,104 +284,3 @@ export default function AddEditWebhookForm({</>);}--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/link-selector.tsx b/apps/web/ui/webhooks/link-selector.tsxnew file mode 100644index 0000000000..effb10e564--- /dev/null+++ b/apps/web/ui/webhooks/link-selector.tsx@@ -0,0 +1,109 @@+"use client";++import useLinks from "@/lib/swr/use-links";+import { LinkProps } from "@/lib/types";+import { Combobox, LinkLogo } from "@dub/ui";+import { cn, getApexDomain, linkConstructor, truncate } from "@dub/utils";+import { useMemo, useState } from "react";+import { useDebounce } from "use-debounce";++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,+ },+});++export 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>+ );+}