Skip to content

Commit

Permalink
fix: incorporated the url class
Browse files Browse the repository at this point in the history
  • Loading branch information
devamitranjan committed Sep 24, 2024
1 parent 2da2552 commit 3efa0fb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
6 changes: 2 additions & 4 deletions frontend/src/components/Menu/MenuDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
DialogProps,
MenuItem,
} from "@mui/material";
import { isAbsoluteUrl } from "next/dist/shared/lib/utils";
import { useEffect, FC } from "react";
import { useForm, Controller } from "react-hook-form";
import { useTranslation } from "react-i18next";
Expand All @@ -25,8 +24,8 @@ import { ContentContainer } from "@/app-components/dialogs/layouts/ContentContai
import { ContentItem } from "@/app-components/dialogs/layouts/ContentItem";
import { Input } from "@/app-components/inputs/Input";
import { ToggleableInput } from "@/app-components/inputs/ToggleableInput";
import { URL_REGEX } from "@/constants";
import { IMenuItem, IMenuItemAttributes, MenuType } from "@/types/menu.types";
import { isAbsoluteUrl } from "@/utils/URL";

export type MenuDialogProps = DialogProps & {
open: boolean;
Expand Down Expand Up @@ -67,8 +66,7 @@ export const MenuDialog: FC<MenuDialogProps> = ({
url: {
required: t("message.url_is_invalid"),
validate: (value: string = "") =>
(isAbsoluteUrl(value) && URL_REGEX.test(value)) ||
t("message.url_is_invalid"),
isAbsoluteUrl(value) || t("message.url_is_invalid"),
},
payload: {},
};
Expand Down
3 changes: 0 additions & 3 deletions frontend/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,3 @@ export const DATE_TIME_FORMAT = {

export const USER_DEFAULT_PICTURE =
"https://avatars.dicebear.com/v2/identicon/6659e07058af581e68e33d05.svg";

export const URL_REGEX =
/^(https?:\/\/)?((([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,})(:[0-9]{1,5})?(\/[^\s]*)?)$/i;
16 changes: 15 additions & 1 deletion frontend/src/utils/URL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,25 @@ export const getFromQuery = ({

export const buildURL = (baseUrl: string, relativePath: string): string => {
try {

const url = new URL(relativePath, baseUrl);

return url.toString();
} catch {
throw new Error(`Invalid base URL: ${baseUrl}`);
}
};

export const isAbsoluteUrl = (value: string = ""): boolean => {
try {
const url = new URL(value);
const hostnameParts = url.hostname.split(".");

return (
(url.protocol === "http:" || url.protocol === "https:") &&
hostnameParts.length > 1 &&
hostnameParts[hostnameParts.length - 1].length > 1
);
} catch (error) {
return false;
}
};
7 changes: 5 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3efa0fb

Please sign in to comment.