|
| 1 | +import { mutateSuffix } from "@/lib/swr/mutate"; |
| 2 | +import { |
| 3 | + Button, |
| 4 | + InfoTooltip, |
| 5 | + SimpleTooltipContent, |
| 6 | + useCopyToClipboard, |
| 7 | + useMediaQuery, |
| 8 | +} from "@dub/ui"; |
| 9 | +import { linkConstructor, TAB_ITEM_ANIMATION_SETTINGS } from "@dub/utils"; |
| 10 | +import { motion } from "framer-motion"; |
| 11 | +import { useMemo, useState } from "react"; |
| 12 | +import { useForm } from "react-hook-form"; |
| 13 | + |
| 14 | +interface Props { |
| 15 | + destinationDomain: string; |
| 16 | + shortLinkDomain: string; |
| 17 | +} |
| 18 | + |
| 19 | +interface FormData { |
| 20 | + url: string; |
| 21 | + key: string; |
| 22 | +} |
| 23 | + |
| 24 | +export function ReferralsEmbedCreateLink({ |
| 25 | + destinationDomain, |
| 26 | + shortLinkDomain, |
| 27 | +}: Props) { |
| 28 | + const { isMobile } = useMediaQuery(); |
| 29 | + const [, copyToClipboard] = useCopyToClipboard(); |
| 30 | + const [isSubmitting, setIsSubmitting] = useState(false); |
| 31 | + |
| 32 | + const { watch, register, handleSubmit } = useForm<FormData>(); |
| 33 | + |
| 34 | + const [key, url] = watch("key", "url"); |
| 35 | + |
| 36 | + const onSubmit = async (data: FormData) => { |
| 37 | + setIsSubmitting(true); |
| 38 | + |
| 39 | + try { |
| 40 | + const response = await fetch("/api/embed/referrals/links", { |
| 41 | + method: "POST", |
| 42 | + headers: { |
| 43 | + "Content-Type": "application/json", |
| 44 | + }, |
| 45 | + body: JSON.stringify({ |
| 46 | + ...data, |
| 47 | + url: linkConstructor({ |
| 48 | + domain: destinationDomain, |
| 49 | + key: data.url, |
| 50 | + }), |
| 51 | + }), |
| 52 | + }); |
| 53 | + |
| 54 | + const result = await response.json(); |
| 55 | + |
| 56 | + // TODO: |
| 57 | + // Display success or error message |
| 58 | + |
| 59 | + if (!response.ok) { |
| 60 | + const { error } = result; |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + await mutateSuffix("/links"); |
| 65 | + } finally { |
| 66 | + setIsSubmitting(false); |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + const saveDisabled = useMemo( |
| 71 | + () => Boolean(isSubmitting || !key || !url), |
| 72 | + [isSubmitting, key, url], |
| 73 | + ); |
| 74 | + |
| 75 | + return ( |
| 76 | + <motion.div |
| 77 | + className="border-border-subtle relative rounded-md border" |
| 78 | + {...TAB_ITEM_ANIMATION_SETTINGS} |
| 79 | + > |
| 80 | + <form |
| 81 | + onSubmit={handleSubmit(onSubmit)} |
| 82 | + className="max-h-[26rem] overflow-auto" |
| 83 | + > |
| 84 | + <div className="flex items-center justify-between border-b border-neutral-200 px-4 py-3"> |
| 85 | + <span className="text-base font-semibold">New link</span> |
| 86 | + <div className="flex items-center gap-x-2"> |
| 87 | + <Button |
| 88 | + text="Cancel" |
| 89 | + variant="secondary" |
| 90 | + type="button" |
| 91 | + className="h-9" |
| 92 | + /> |
| 93 | + <Button |
| 94 | + text="Create link" |
| 95 | + variant="primary" |
| 96 | + loading={isSubmitting} |
| 97 | + disabled={saveDisabled} |
| 98 | + className="h-9" |
| 99 | + /> |
| 100 | + </div> |
| 101 | + </div> |
| 102 | + |
| 103 | + <div className="space-y-6 p-6"> |
| 104 | + <div> |
| 105 | + <div className="flex items-center gap-2"> |
| 106 | + <label |
| 107 | + htmlFor="url" |
| 108 | + className="block text-sm font-medium text-neutral-700" |
| 109 | + > |
| 110 | + Destination URL |
| 111 | + </label> |
| 112 | + <InfoTooltip |
| 113 | + content={ |
| 114 | + <SimpleTooltipContent |
| 115 | + title="The URL your users will get redirected to when they visit your referral link." |
| 116 | + cta="Learn more." |
| 117 | + href="https://dub.co/help/article/how-to-create-link" |
| 118 | + /> |
| 119 | + } |
| 120 | + /> |
| 121 | + </div> |
| 122 | + <div className="mt-2 flex rounded-md"> |
| 123 | + <span className="inline-flex items-center rounded-l-md border border-r-0 border-neutral-300 bg-neutral-50 px-3 text-neutral-500 sm:text-sm"> |
| 124 | + {destinationDomain} |
| 125 | + </span> |
| 126 | + <input |
| 127 | + type="text" |
| 128 | + placeholder="about" |
| 129 | + className="block w-full rounded-r-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm" |
| 130 | + {...register("url", { required: false })} |
| 131 | + autoFocus={!isMobile} |
| 132 | + onPaste={(e: React.ClipboardEvent<HTMLInputElement>) => { |
| 133 | + e.preventDefault(); |
| 134 | + |
| 135 | + // if pasting in a URL, extract the pathname |
| 136 | + const text = e.clipboardData.getData("text/plain"); |
| 137 | + |
| 138 | + try { |
| 139 | + const url = new URL(text); |
| 140 | + e.currentTarget.value = url.pathname.slice(1); |
| 141 | + } catch (err) { |
| 142 | + e.currentTarget.value = text; |
| 143 | + } |
| 144 | + }} |
| 145 | + /> |
| 146 | + </div> |
| 147 | + </div> |
| 148 | + |
| 149 | + <div> |
| 150 | + <div className="flex items-center justify-between"> |
| 151 | + <div className="flex items-center gap-2"> |
| 152 | + <label |
| 153 | + htmlFor="short-link" |
| 154 | + className="block text-sm font-medium" |
| 155 | + > |
| 156 | + Short link |
| 157 | + </label> |
| 158 | + <InfoTooltip |
| 159 | + content={ |
| 160 | + <SimpleTooltipContent |
| 161 | + title="The URL that will be shared with your users. Keep it short and memorable!" |
| 162 | + cta="Learn more." |
| 163 | + href="https://dub.co/help/article/how-to-create-link" |
| 164 | + /> |
| 165 | + } |
| 166 | + /> |
| 167 | + </div> |
| 168 | + </div> |
| 169 | + <div className="mt-2 flex rounded-md"> |
| 170 | + <span className="inline-flex items-center rounded-l-md border border-r-0 border-neutral-300 bg-neutral-50 px-3 text-neutral-500 sm:text-sm"> |
| 171 | + {shortLinkDomain} |
| 172 | + </span> |
| 173 | + <input |
| 174 | + type="text" |
| 175 | + placeholder="another-link" |
| 176 | + className="block w-full rounded-r-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm" |
| 177 | + {...register("key", { required: true })} |
| 178 | + /> |
| 179 | + </div> |
| 180 | + </div> |
| 181 | + </div> |
| 182 | + </form> |
| 183 | + |
| 184 | + <div className="from-bg-default pointer-events-none absolute -bottom-px left-0 h-16 w-full rounded-b-lg bg-gradient-to-t sm:bottom-0" /> |
| 185 | + </motion.div> |
| 186 | + ); |
| 187 | +} |
0 commit comments