The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
feat: add tooltip button
flooorianflo80f0430412dc
diff --git a/apps/web/ui/modals/add-edit-link-modal/index.tsx b/apps/web/ui/modals/add-edit-link-modal/index.tsxindex 26d164d00f..dd17626753 100644--- a/apps/web/ui/modals/add-edit-link-modal/index.tsx+++ b/apps/web/ui/modals/add-edit-link-modal/index.tsx@@ -59,7 +59,7 @@ import Preview from "./preview";import TagsSection from "./tags-section";import UTMSection from "./utm-section";import { useCompletion } from "ai/react";-import SimplePieChart from "@/ui/charts/simple-pie-chart";+import { ButtonWithTooltip } from "./tooltip-button";function AddEditLinkModal({showAddEditLinkModal,@@ -78,12 +78,18 @@ function AddEditLinkModal({const { slug } = params;const router = useRouter();const pathname = usePathname();- const { id: workspaceId, aiUsage, aiLimit, nextPlan } = useWorkspace();+ const {+ id: workspaceId,+ aiUsage: tempAiUsage,+ aiLimit,+ nextPlan,+ } = useWorkspace();const [keyError, setKeyError] = useState<string | null>(null);const [urlError, setUrlError] = useState<string | null>(null);const [generatingRandomKey, setGeneratingRandomKey] = useState(false);const [saving, setSaving] = useState(false);+ const [aiUsage, setAiUsage] = useState(tempAiUsage);const {allActiveDomains: domains,@@ -154,6 +160,7 @@ function AddEditLinkModal({},onFinish: (_, completion) => {setGeneratedKeys((prev) => [...prev, completion]);+ setAiUsage(aiUsage ? aiUsage + 1 : 1);},});@@ -529,99 +536,49 @@ function AddEditLinkModal({</button>) : (<div className="flex items-center">- <Tooltip content="Generate a random key">- <button- className="flex h-6 w-6 items-center justify-center rounded-md text-gray-500 transition-colors duration-75 hover:bg-gray-100 active:bg-gray-200 disabled:cursor-not-allowed"- onClick={generateRandomKey}- disabled={generatingRandomKey || generatingAIKey}- type="button"- >- {generatingRandomKey ? (- <LoadingCircle />- ) : (- <Random className="h-3 w-3" />- )}- </button>- </Tooltip>- <Tooltip- content={- !url ? (- "Please enter a destination URL to generate AI Short Link."- ) : aiUsage && aiLimit ? (- aiUsage + (generatedKeys.length - 1) < aiLimit ? (- <div className="flex items-center gap-4 px-4 py-2">- <div>- <span className="block max-w-xs text-center text-sm text-gray-700">- Create a short link using AI- </span>- {aiUsage && aiLimit && (- <span className="text-xs text-gray-500">- {aiLimit -- aiUsage -- (generatedKeys.length - 1)}- /{aiLimit} left- </span>- )}- </div>- {aiUsage && aiLimit && (- <SimplePieChart- data={[- {- name: "Used",- value:- aiUsage + (generatedKeys.length - 1),- color: "text-gray-200",- },- {- name: "Remaining",- value:- aiLimit -- aiUsage -- (generatedKeys.length - 1),- color: "text-black",- },- ]}- />- )}- </div>- ) : (- <TooltipContent- title="You've reached your AI usage limit. Upgrade to Pro to get more AI credits."- cta={`Upgrade to ${nextPlan.name}`}- onClick={() => {- queryParams({- set: {- upgrade: nextPlan.name.toLowerCase(),- },- });- }}- />- )- ) : (- "Create a short link using AI"- )+ <ButtonWithTooltip+ tooltip={{+ content: "Generate a random key",+ }}+ onClick={generateRandomKey}+ disabled={generatingRandomKey || generatingAIKey}+ >+ {generatingRandomKey ? (+ <LoadingCircle />+ ) : (+ <Random className="h-3 w-3" />+ )}+ </ButtonWithTooltip>+ <ButtonWithTooltip+ onClick={generateAIKey}+ disabled={+ generatingAIKey ||+ (aiLimit && aiUsage && aiUsage >= aiLimit) ||+ !url}+ tooltip={{+ ai:+ url && aiLimit && aiUsage+ ? {+ data: {+ limit: aiLimit,+ usage: aiUsage,+ },+ title: "AI Usage",+ nextPlan: nextPlan,+ }+ : undefined,+ content: url+ ? "Generate AI key"+ : "Enter a URL first",+ }}>- <button- className="ml-2 flex h-6 w-6 items-center justify-center rounded-md text-gray-500 transition-colors duration-75 hover:bg-gray-100 active:bg-gray-200 disabled:cursor-not-allowed"- onClick={generateAIKey}- disabled={- generatingAIKey ||- generatingRandomKey ||- !url ||- (aiUsage && aiLimit- ? aiUsage + (generatedKeys.length - 1) >= aiLimit- : false)- }- type="button"- >- {generatingAIKey ? (- <LoadingCircle />- ) : (- <Magic className="h-4 w-4" />- )}- </button>- </Tooltip>+ {generatingAIKey ? (+ <LoadingCircle />+ ) : (+ <Magic className="h-4 w-4" />+ )}+ </ButtonWithTooltip></div>)}</div>diff --git a/apps/web/ui/modals/add-edit-link-modal/og-section.tsx b/apps/web/ui/modals/add-edit-link-modal/og-section.tsxindex 0346763e1a..b139ceaca9 100644--- a/apps/web/ui/modals/add-edit-link-modal/og-section.tsx+++ b/apps/web/ui/modals/add-edit-link-modal/og-section.tsx@@ -13,7 +13,7 @@ import {Unsplash,useRouterStuff,} from "@dub/ui";-import { TooltipContent } from "@dub/ui/src/tooltip";+import { Tooltip, TooltipContent } from "@dub/ui/src/tooltip";import { FADE_IN_ANIMATION_SETTINGS, HOME_DOMAIN } from "@dub/utils";import { motion } from "framer-motion";import { Link2 } from "lucide-react";@@ -22,6 +22,8 @@ import TextareaAutosize from "react-textarea-autosize";import UnsplashSearch from "./unsplash-search";import { useCompletion } from "ai/react";import { toast } from "sonner";+import SimplePieChart from "@/ui/charts/simple-pie-chart";+import TooltipButton, { ButtonWithTooltip } from "./tooltip-button";export default function OGSection({props,@@ -34,10 +36,9 @@ export default function OGSection({setData: Dispatch<SetStateAction<LinkProps>>;generatingMetatags: boolean;}) {- const { id: workspaceId, plan } = useWorkspace();- const { queryParams } = useRouterStuff();+ const { id: workspaceId, plan, aiUsage, aiLimit, nextPlan } = useWorkspace();- const { title, description, image, proxy } = data;+ const { title, description, image, proxy, url } = data;const {completion: completionTitle,@@ -160,9 +161,10 @@ export default function OGSection({<div className="flex items-center justify-between"><p className="block text-sm font-medium text-gray-700">Image</p><div className="flex items-center justify-between">- <button- className="mr-1 flex h-6 w-6 items-center justify-center rounded-md transition-colors duration-75 hover:bg-gray-100 active:bg-gray-200"- type="button"+ <ButtonWithTooltip+ tooltip={{+ content: "Paste a URL to an image.",+ }}onClick={() => {const image = window.prompt("Paste a URL to an image.",@@ -174,7 +176,7 @@ export default function OGSection({}}><Link2 className="h-4 w-4 text-gray-500" />- </button>+ </ButtonWithTooltip><Popovercontent={<UnsplashSearch@@ -185,12 +187,14 @@ export default function OGSection({openPopover={openPopover}setOpenPopover={handleSet}>- <div+ <ButtonWithTooltiponClick={handleSet}- className="flex h-6 w-6 cursor-pointer items-center justify-center rounded-md transition-colors duration-75 hover:bg-gray-100 active:bg-gray-200"+ tooltip={{+ content: "Find high-resolution photos on Unsplash.",+ }}><Unsplash className="h-3 w-3 text-gray-500" />- </div>+ </ButtonWithTooltip></Popover></div></div>@@ -297,18 +301,32 @@ export default function OGSection({<p className="text-sm text-gray-500">{title?.length || 0}/120</p>- <button- className="flex h-6 w-6 items-center justify-center rounded-md text-gray-500 transition-colors duration-75 hover:bg-gray-100 active:bg-gray-200 disabled:cursor-not-allowed"+ <ButtonWithTooltiponClick={generateTitle}disabled={generatingTitle}- type="button"+ tooltip={{+ ai:+ aiLimit && aiUsage+ ? {+ data: {+ limit: aiLimit,+ usage: aiUsage,+ },+ title: "Create a title using AI",+ nextPlan: nextPlan,+ }+ : undefined,+ content: !title+ ? "Please enter a title to generate one using AI."+ : "Create an optimized title using AI.",+ }}>{generatingTitle ? (<LoadingCircle />) : (<Magic className="h-4 w-4" />)}- </button>+ </ButtonWithTooltip></div></div><div className="relative mt-1 flex rounded-md shadow-sm">@@ -342,18 +360,32 @@ export default function OGSection({<p className="text-sm text-gray-500">{description?.length || 0}/240</p>- <button- className="flex h-6 w-6 items-center justify-center rounded-md text-gray-500 transition-colors duration-75 hover:bg-gray-100 active:bg-gray-200 disabled:cursor-not-allowed"+ <ButtonWithTooltiponClick={generateDescription}disabled={generatingDescription}- type="button"+ tooltip={{+ ai:+ aiLimit && aiUsage+ ? {+ data: {+ limit: aiLimit,+ usage: aiUsage,+ },+ title: "Create a description using AI",+ nextPlan: nextPlan,+ }+ : undefined,+ content: !title+ ? "Please enter a description to generate one using AI."+ : "Create an optimized description using AI.",+ }}>- {generatingDescription ? (+ {generatingTitle ? (<LoadingCircle />) : (<Magic className="h-4 w-4" />)}- </button>+ </ButtonWithTooltip></div></div><div className="relative mt-1 flex rounded-md shadow-sm">diff --git a/apps/web/ui/modals/add-edit-link-modal/tooltip-button.tsx b/apps/web/ui/modals/add-edit-link-modal/tooltip-button.tsxnew file mode 100644index 0000000000..cc4c815307--- /dev/null+++ b/apps/web/ui/modals/add-edit-link-modal/tooltip-button.tsx@@ -0,0 +1,98 @@+"use client";++import SimplePieChart from "@/ui/charts/simple-pie-chart";+import { TooltipContent, useRouterStuff } from "@dub/ui";+import { Tooltip } from "@dub/ui/src/tooltip";+import { cn } from "@dub/utils";++// HOC to wrap a button with a tooltip and make it reusable+export default function TooltipButton() {+ return function WrappedTooltipButton({+ children,+ tooltip: { content, cta, ai },+ ...props+ }: {+ children: React.ReactNode;+ tooltip: {+ content: string;+ cta?: string;+ ai?: {+ title: string;+ data: {+ usage: number;+ limit: number;+ };+ nextPlan: {+ name: string;+ };+ };+ };+ [key: string]: any;+ }) {+ const { queryParams } = useRouterStuff();++ return (+ <Tooltip+ content={+ ai ? (+ ai.data.usage < ai.data.limit ? (+ <div className="flex items-center gap-4 px-4 py-2">+ <div>+ <span className="block max-w-xs text-center text-sm text-gray-700">+ {ai.title}+ </span>+ <span className="text-xs text-gray-500">+ {ai.data.limit - ai.data.usage} / {ai.data.limit} left+ </span>+ </div>+ <SimplePieChart+ data={[+ {+ name: "Used",+ value: ai.data.usage,+ color: "text-gray-200",+ },+ {+ name: "Remaining",+ value: ai.data.limit - ai.data.usage,+ color: "text-black",+ },+ ]}+ />+ </div>+ ) : (+ <TooltipContent+ title="You've reached your AI usage limit. Upgrade to Pro to get more AI credits."+ cta={`Upgrade to ${ai.nextPlan.name}`}+ onClick={() => {+ queryParams({+ set: {+ upgrade: ai.nextPlan.name.toLowerCase(),+ },+ });+ }}+ />+ )+ ) : cta ? (+ <TooltipContent title={content} cta={cta} />+ ) : (+ content+ )+ }+ >+ <button+ {...props}+ disabled={props.disabled || (ai && ai?.data.usage >= ai?.data.limit)}+ className={cn(+ "flex h-6 w-6 items-center justify-center rounded-md text-gray-500 transition-colors duration-75 hover:bg-gray-100 active:bg-gray-200 disabled:cursor-not-allowed",+ props.className,+ )}+ >+ {children}+ </button>+ </Tooltip>+ );+ };+}++export const ButtonWithTooltip = TooltipButton();