Skip to content

Commit

Permalink
refactor: reuse url function (#1768)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeharding authored Dec 10, 2024
1 parent 3fa81bd commit d1c9ef4
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions src/features/post/new/PostEditorRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,10 @@ export default function PostEditorRoot({
errorMessage = "Please add a title to your post.";
} else if (title.length < 3) {
errorMessage = "Post title must contain at least three characters.";
} else if (postType === "link" && (!url || !validUrl(url))) {
} else if (
postType === "link" &&
(!url || !isValidUrl(url, { allowRelative: false }))
) {
errorMessage =
"Please add a valid URL to your post (start with https://).";
} else if (postType === "photo" && !photoUrl) {
Expand Down Expand Up @@ -527,13 +530,3 @@ export default function PostEditorRoot({
</>
);
}

function validUrl(url: string): boolean {
try {
new URL(url);
} catch (_) {
return false;
}

return true;
}

0 comments on commit d1c9ef4

Please sign in to comment.