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.

Add `LinkCommentsInput` component

Tim Wilsone279bb913c7d
diff --git a/apps/web/ui/links/link-builder/controls/link-comments-input.tsx b/apps/web/ui/links/link-builder/controls/link-comments-input.tsx
new file mode 100644
index 0000000000..9ba327350d
--- /dev/null
+++ b/apps/web/ui/links/link-builder/controls/link-comments-input.tsx
@@ -0,0 +1,50 @@
+import { InfoTooltip, SimpleTooltipContent, useEnterSubmit } from "@dub/ui";
+import { memo } from "react";
+import { Controller, useFormContext } from "react-hook-form";
+import TextareaAutosize from "react-textarea-autosize";
+import { LinkFormData } from "../link-builder-provider";
+
+export const LinkCommentsInput = memo(() => {
+ const { handleKeyDown } = useEnterSubmit();
+ const { control } = useFormContext<LinkFormData>();
+
+ return (
+ <div>
+ <div className="flex items-center gap-2">
+ <label
+ htmlFor="comments"
+ className="block text-sm font-medium text-neutral-700"
+ >
+ Comments
+ </label>
+ <InfoTooltip
+ content={
+ <SimpleTooltipContent
+ title="Use comments to add context to your short links – for you and your team."
+ cta="Learn more."
+ href="https://dub.co/help/article/link-comments"
+ />
+ }
+ />
+ </div>
+ <Controller
+ name="comments"
+ control={control}
+ render={({ field }) => (
+ <TextareaAutosize
+ id="comments"
+ name="comments"
+ minRows={3}
+ className="mt-2 block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
+ placeholder="Add comments"
+ value={field.value ?? ""}
+ onChange={(e) => field.onChange(e.target.value)}
+ onKeyDown={handleKeyDown}
+ />
+ )}
+ />
+ </div>
+ );
+});
+
+LinkCommentsInput.displayName = "LinkCommentsInput";
diff --git a/apps/web/ui/modals/link-builder/index.tsx b/apps/web/ui/modals/link-builder/index.tsx
index c91c9858c9..58190952ce 100644
--- a/apps/web/ui/modals/link-builder/index.tsx
+++ b/apps/web/ui/modals/link-builder/index.tsx
@@ -4,6 +4,7 @@ import useWorkspace from "@/lib/swr/use-workspace";
import { ExpandedLinkProps } from "@/lib/types";
import { FolderDropdown } from "@/ui/folders/folder-dropdown";
import { DestinationUrlInput } from "@/ui/links/destination-url-input";
+import { LinkCommentsInput } from "@/ui/links/link-builder/controls/link-comments-input";
import {
LinkBuilderProps,
LinkBuilderProvider,
@@ -17,12 +18,9 @@ import {
ArrowTurnLeft,
Button,
ButtonProps,
- InfoTooltip,
LinkLogo,
Modal,
- SimpleTooltipContent,
TooltipContent,
- useEnterSubmit,
useKeyboardShortcut,
useRouterStuff,
} from "@dub/ui";
@@ -45,8 +43,7 @@ import {
useRef,
useState,
} from "react";
-import { Controller, useFormContext } from "react-hook-form";
-import TextareaAutosize from "react-textarea-autosize";
+import { Controller, useFormContext, useWatch } from "react-hook-form";
import { useDebounce } from "use-debounce";
import { ConversionTrackingToggle } from "./conversion-tracking-toggle";
import { DraftControls, DraftControlsHandle } from "./draft-controls";
@@ -105,23 +102,16 @@ function LinkBuilderInner({
const {
control,
- watch,
handleSubmit,
setValue,
clearErrors,
formState: { isDirty, isSubmitting, isSubmitSuccessful, errors },
} = useFormContext<LinkFormData>();
- const formRef = useRef<HTMLFormElement>(null);
- const { handleKeyDown } = useEnterSubmit(formRef);
-
- const [url, domain, key, title, description] = watch([
- "url",
- "domain",
- "key",
- "title",
- "description",
- ]);
+ const [url, domain, key, title, description] = useWatch({
+ control,
+ name: ["url", "domain", "key", "title", "description"],
+ });
const [debouncedUrl] = useDebounce(getUrlWithoutUTMParams(url), 500);
@@ -208,7 +198,7 @@ function LinkBuilderInner({
draftControlsRef.current?.onClose();
}}
>
- <form ref={formRef} onSubmit={handleSubmit(onSubmit)}>
+ <form onSubmit={handleSubmit(onSubmit)}>
<div className="flex flex-col items-start gap-2 px-6 py-4 md:flex-row md:items-center md:justify-between">
{flags?.linkFolders && (
<div className="flex items-center gap-2">
@@ -335,41 +325,7 @@ function LinkBuilderInner({
<TagSelect />
- <div>
- <div className="flex items-center gap-2">
- <label
- htmlFor="comments"
- className="block text-sm font-medium text-neutral-700"
- >
- Comments
- </label>
- <InfoTooltip
- content={
- <SimpleTooltipContent
- title="Use comments to add context to your short links – for you and your team."
- cta="Learn more."
- href="https://dub.co/help/article/link-comments"
- />
- }
- />
- </div>
- <Controller
- name="comments"
- control={control}
- render={({ field }) => (
- <TextareaAutosize
- id="comments"
- name="comments"
- minRows={3}
- className="mt-2 block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
- placeholder="Add comments"
- value={field.value ?? ""}
- onChange={(e) => field.onChange(e.target.value)}
- onKeyDown={handleKeyDown}
- />
- )}
- />
- </div>
+ <LinkCommentsInput />
<ConversionTrackingToggle />
diff --git a/apps/web/ui/modals/link-builder/use-metatags.ts b/apps/web/ui/modals/link-builder/use-metatags.ts
index 41281a256a..797f61dbe4 100644
--- a/apps/web/ui/modals/link-builder/use-metatags.ts
+++ b/apps/web/ui/modals/link-builder/use-metatags.ts
@@ -1,7 +1,7 @@
import { useLinkBuilderContext } from "@/ui/links/link-builder/link-builder-provider";
import { getUrlWithoutUTMParams, truncate } from "@dub/utils";
import { useEffect } from "react";
-import { useFormContext } from "react-hook-form";
+import { useFormContext, useWatch } from "react-hook-form";
import { useDebounce } from "use-debounce";
import { LinkFormData } from ".";
@@ -12,15 +12,11 @@ export function useMetatags({
initial: boolean;
enabled: boolean;
}) {
- const { watch, setValue } = useFormContext<LinkFormData>();
- const [url, password, proxy, title, description, image] = watch([
- "url",
- "password",
- "proxy",
- "title",
- "description",
- "image",
- ]);
+ const { control, setValue } = useFormContext<LinkFormData>();
+ const [url, password, proxy, title, description, image] = useWatch({
+ control,
+ name: ["url", "password", "proxy", "title", "description", "image"],
+ });
const [debouncedUrl] = useDebounce(getUrlWithoutUTMParams(url), 500);
const { generatingMetatags, setGeneratingMetatags } = useLinkBuilderContext();
diff --git a/packages/ui/src/hooks/use-enter-submit.ts b/packages/ui/src/hooks/use-enter-submit.ts
index 70fe55e558..b50f1164c5 100644
--- a/packages/ui/src/hooks/use-enter-submit.ts
+++ b/packages/ui/src/hooks/use-enter-submit.ts
@@ -1,17 +1,25 @@
-import { KeyboardEvent } from "react";
+import { KeyboardEvent, useCallback } from "react";
-export function useEnterSubmit(formRef: React.RefObject<HTMLFormElement>) {
- const handleKeyDown = (event: KeyboardEvent<HTMLTextAreaElement>) => {
- // Check if CMD/CTRL + Enter is pressed
- if (event.key === "Enter" && event.metaKey) {
- event.preventDefault(); // Prevent the default behavior of 'Enter' key
+export function useEnterSubmit(formRef?: React.RefObject<HTMLFormElement>) {
+ const handleKeyDown = useCallback(
+ (event: KeyboardEvent<HTMLTextAreaElement>) => {
+ // Check if CMD/CTRL + Enter is pressed
+ if (event.key === "Enter" && event.metaKey) {
+ event.preventDefault(); // Prevent the default behavior of 'Enter' key
- // Check if formRef is currently pointing to a form and if so, submit it
- if (formRef.current) {
- formRef.current.requestSubmit();
+ // Check if formRef is currently pointing to a form and if so, submit it
+ if (formRef?.current) {
+ formRef.current.requestSubmit();
+ return;
+ }
+
+ // Try determining the form from the event target
+ const form = (event.target as HTMLTextAreaElement).form;
+ form?.requestSubmit();
}
- }
- };
+ },
+ [],
+ );
return { handleKeyDown };
}