Skip to content

Commit f10aef7

Browse files
committed
Create link error message and bug fix
Changed the error message from "Invalid key" to "Enter a valid short link". Found a bug that the destination URL allowed spaces, and then those links went to 404 pages. So now it shows an error if the user tries to create a short link with spaces in the destination url.
1 parent 7d509a9 commit f10aef7

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

apps/web/ui/links/destination-url-input.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ export const DestinationUrlInput = forwardRef<
9797
{...(formContext && {
9898
onChange: (e) => {
9999
const url = e.target.value;
100-
101100
formContext.setValue("url", url);
102101
const parentParams = getParamsFromURL(url);
103102

apps/web/ui/links/link-builder/controls/link-builder-destination-url-input.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { useAvailableDomains } from "../../use-available-domains";
1717
*/
1818
export const LinkBuilderDestinationUrlInput = memo(
1919
forwardRef<HTMLInputElement>((_, ref) => {
20-
const { control, setValue, clearErrors } = useFormContext<LinkFormData>();
20+
const { control, setValue, clearErrors, setError } = useFormContext<LinkFormData>();
2121
0;
2222

2323
const { errors } = useFormState({ control, name: ["url"] });
@@ -42,8 +42,13 @@ export const LinkBuilderDestinationUrlInput = memo(
4242
value={field.value}
4343
domains={domains}
4444
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
45-
clearErrors("url");
46-
field.onChange(e.target.value);
45+
const value = e.target.value;
46+
if (/\s/.test(value)) {
47+
setError("url", { message: "Enter a valid URL" });
48+
} else {
49+
clearErrors("url");
50+
field.onChange(value);
51+
}
4752
}}
4853
required={key !== "_root"}
4954
error={errors.url?.message || undefined}

apps/web/ui/partners/add-partner-link-modal.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const AddPartnerLinkModal = ({
4040
const [isSubmitting, setIsSubmitting] = useState(false);
4141
const [errorMessage, setErrorMessage] = useState<string | null>(null);
4242

43-
const { register, handleSubmit, watch } = useForm<FormData>({
43+
const { register, handleSubmit, watch, setError, clearErrors, formState } = useForm<FormData>({
4444
defaultValues: {
4545
url: "",
4646
key: "",
@@ -91,7 +91,11 @@ const AddPartnerLinkModal = ({
9191
copyToClipboard(data.shortLink);
9292
} catch (error) {
9393
setErrorMessage(
94-
error instanceof Error ? error.message : "Failed to create link.",
94+
error instanceof Error && error.message === "Invalid key."
95+
? "Enter a valid short link"
96+
: error instanceof Error
97+
? error.message
98+
: "Failed to create link."
9599
);
96100
} finally {
97101
setIsSubmitting(false);
@@ -188,9 +192,14 @@ const AddPartnerLinkModal = ({
188192
<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">
189193
{destinationDomain}
190194
</span>
191-
192195
<input
193-
{...register("url", { required: false })}
196+
{...register("url", {
197+
required: false,
198+
validate: value =>
199+
value && /\s/.test(value)
200+
? "Enter a valid URL"
201+
: true
202+
})}
194203
type="text"
195204
id="url"
196205
placeholder="(optional)"
@@ -209,6 +218,11 @@ const AddPartnerLinkModal = ({
209218
}}
210219
/>
211220
</div>
221+
{formState.errors.url && (
222+
<span className="mt-1 block text-sm text-red-600 dark:text-red-400">
223+
{formState.errors.url.message}
224+
</span>
225+
)}
212226
</div>
213227
</div>
214228
</div>

0 commit comments

Comments
 (0)