The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
add domain selector
Kiran Kb6e7dba55701
diff --git a/apps/web/app/app.dub.co/(new-program)/[slug]/programs/new/form.tsx b/apps/web/app/app.dub.co/(new-program)/[slug]/programs/new/form.tsxindex 17c1f2577b..01e159f954 100644--- a/apps/web/app/app.dub.co/(new-program)/[slug]/programs/new/form.tsx+++ b/apps/web/app/app.dub.co/(new-program)/[slug]/programs/new/form.tsx@@ -2,15 +2,17 @@import { onboardProgramAction } from "@/lib/actions/partners/onboard-program";import { getLinkStructureOptions } from "@/lib/partners/get-link-structure-options";-import useDomains from "@/lib/swr/use-domains";import useWorkspace from "@/lib/swr/use-workspace";import { ProgramData } from "@/lib/types";+import { DomainSelector } from "@/ui/domains/domain-selector";import {Badge,Button,CircleCheckFill,FileUpload,+ InfoTooltip,Input,+ SimpleTooltipContent,useMediaQuery,} from "@dub/ui";import { cn } from "@dub/utils";@@ -26,7 +28,6 @@ export function Form() {const { isMobile } = useMediaQuery();const [isUploading, setIsUploading] = useState(false);const [hasSubmitted, setHasSubmitted] = useState(false);- const { activeWorkspaceDomains, loading } = useDomains();const { id: workspaceId, slug: workspaceSlug, mutate } = useWorkspace();const {@@ -163,27 +164,34 @@ export function Form() {</p></div>- <div>- <label className="block text-sm font-medium text-neutral-800">- Custom domain- </label>- {loading ? (- <div className="mt-2 h-10 w-full animate-pulse rounded-md bg-neutral-100" />- ) : (- <select- {...register("domain", { required: true })}- className="mt-2 block w-full rounded-md border border-neutral-300 bg-white py-2 pl-3 pr-10 text-sm text-neutral-900 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500"- >- {activeWorkspaceDomains?.map(({ slug }) => (- <option value={slug} key={slug}>- {slug}- </option>- ))}- </select>- )}+ <div className="space-y-2">+ <div className="flex items-center gap-x-2">+ <label className="block text-sm font-medium text-neutral-800">+ Custom domain+ </label>++ <InfoTooltip+ content={+ <SimpleTooltipContent+ title="A connected domain or sub-domain is required to create a program."+ cta="Learn more"+ href="https://dub.co/help/article/choosing-a-custom-domain"+ />+ }+ />+ </div>++ <DomainSelector+ selectedDomain={domain}+ setSelectedDomain={(domain) => setValue("domain", domain)}+ />++ <p className="text-xs font-normal text-neutral-500">+ Your custom domain dedicated to shortlink use on Dub+ </p></div>- <div>+ <div className="space-y-2"><label className="block text-sm font-medium text-neutral-800">Destination URL</label>@@ -191,8 +199,11 @@ export function Form() {{...register("url", { required: true })}type="url"placeholder="https://dub.co"- className={"mt-2 max-w-full"}+ className="max-w-full"/>+ <p className="text-xs font-normal text-neutral-500">+ Where you're sending people when they click the partner link above+ </p></div></div>diff --git a/apps/web/ui/domains/domain-selector.tsx b/apps/web/ui/domains/domain-selector.tsxnew file mode 100644index 0000000000..7b40de7b16--- /dev/null+++ b/apps/web/ui/domains/domain-selector.tsx@@ -0,0 +1,110 @@+import useDomains from "@/lib/swr/use-domains";+import { Button, Combobox } from "@dub/ui";+import { cn, OG_AVATAR_URL } from "@dub/utils";+import { useMemo, useState } from "react";+import { useAddEditDomainModal } from "../modals/add-edit-domain-modal";++interface DomainSelectorProps {+ selectedDomain: string;+ setSelectedDomain: (domain: string) => void;+}++export function DomainSelector({+ selectedDomain,+ setSelectedDomain,+}: DomainSelectorProps) {+ const [openPopover, setOpenPopover] = useState(false);+ const { allWorkspaceDomains, loading } = useDomains();++ const { AddEditDomainModal, setShowAddEditDomainModal } =+ useAddEditDomainModal({+ onSuccess: (domain) => {+ setSelectedDomain(domain.slug);+ },+ });++ const domainOptions = useMemo(() => {+ return allWorkspaceDomains?.map((domain) => ({+ value: domain.slug,+ label: domain.slug,+ icon: (+ <img+ src={domain.logo || `${OG_AVATAR_URL}${domain.id}`} // TODO: fix logo+ className="size-4 rounded-full"+ />+ ),+ }));+ }, [allWorkspaceDomains]);++ const selectedOption = useMemo(() => {+ if (!selectedDomain) {+ return null;+ }++ const domain = [...(allWorkspaceDomains || [])].find(+ (d) => d.slug === selectedDomain,+ );++ if (!domain) {+ return null;+ }++ return {+ value: domain.slug,+ label: domain.slug,+ icon: (+ <img+ src={domain.logo || `${OG_AVATAR_URL}${domain.id}`} // TODO: fix logo+ className="size-4 rounded-full"+ />+ ),+ };+ }, [allWorkspaceDomains, selectedDomain]);++ return (+ <>+ <AddEditDomainModal />+ <Combobox+ options={loading ? undefined : domainOptions}+ setSelected={(option) => {+ setSelectedDomain(option.value);+ }}+ selected={selectedOption}+ icon={loading ? null : selectedOption?.icon}+ caret={true}+ placeholder={loading ? "" : "Select domain"}+ searchPlaceholder="Search domain..."+ matchTriggerWidth+ open={openPopover}+ onOpenChange={setOpenPopover}+ buttonProps={{+ className: cn(+ "w-full justify-start border-neutral-300 px-3",+ "data-[state=open]:ring-1 data-[state=open]:ring-neutral-500 data-[state=open]:border-neutral-500",+ "focus:ring-1 focus:ring-neutral-500 focus:border-neutral-500 transition-none",+ ),+ }}+ emptyState={+ <div className="flex w-full flex-col items-center gap-2 py-4">+ No domains found+ <Button+ onClick={() => {+ setOpenPopover(false);+ setShowAddEditDomainModal(true);+ }}+ variant="primary"+ className="h-7 w-fit px-2"+ text="Add custom domain"+ />+ </div>+ }+ >+ {loading ? (+ <div className="my-0.5 h-4 animate-pulse rounded bg-neutral-200" />+ ) : (+ selectedOption?.label+ )}+ </Combobox>+ </>+ );+}diff --git a/apps/web/ui/modals/add-edit-domain-modal.tsx b/apps/web/ui/modals/add-edit-domain-modal.tsxindex f378d44ca9..343df7ee2b 100644--- a/apps/web/ui/modals/add-edit-domain-modal.tsx+++ b/apps/web/ui/modals/add-edit-domain-modal.tsx@@ -16,14 +16,16 @@ function AddEditDomainModal({showAddEditDomainModal,setShowAddEditDomainModal,props,+ onSuccess,}: {showAddEditDomainModal: boolean;setShowAddEditDomainModal: Dispatch<SetStateAction<boolean>>;props?: DomainProps;+ onSuccess?: (domain: DomainProps) => void;}) {return (<Modal- showModal={showAddEditDomainModal} // TODO change back to showAddEditDomainModal+ showModal={showAddEditDomainModal}setShowModal={setShowAddEditDomainModal}drawerRootProps={{ repositionInputs: false }}className="max-h-[90vh] max-w-lg"@@ -34,8 +36,9 @@ function AddEditDomainModal({<div className="flex-1 overflow-auto bg-neutral-50"><AddEditDomainFormprops={props}- onSuccess={() => {+ onSuccess={(domain) => {setShowAddEditDomainModal(false);+ onSuccess?.(domain);}}className="p-8"/>@@ -83,7 +86,12 @@ function AddDomainButton({export function useAddEditDomainModal({props,buttonProps,-}: { props?: DomainProps; buttonProps?: Partial<ButtonProps> } = {}) {+ onSuccess,+}: {+ props?: DomainProps;+ buttonProps?: Partial<ButtonProps>;+ onSuccess?: (domain: DomainProps) => void;+} = {}) {const [showAddEditDomainModal, setShowAddEditDomainModal] = useState(false);const AddEditDomainModalCallback = useCallback(() => {@@ -92,9 +100,10 @@ export function useAddEditDomainModal({showAddEditDomainModal={showAddEditDomainModal}setShowAddEditDomainModal={setShowAddEditDomainModal}props={props}+ onSuccess={onSuccess}/>);- }, [showAddEditDomainModal, setShowAddEditDomainModal]);+ }, [showAddEditDomainModal, setShowAddEditDomainModal, props, onSuccess]);const AddDomainButtonCallback = useCallback(() => {return (