fix: incorporated the url class

This commit is contained in:
Amit Ranjan 2024-09-24 12:00:20 +05:30
parent 2da2552c3b
commit 3efa0fb60f
4 changed files with 22 additions and 10 deletions

View File

@ -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";
@ -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;
@ -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: {},
};

View File

@ -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;

View File

@ -29,7 +29,6 @@ export const getFromQuery = ({
export const buildURL = (baseUrl: string, relativePath: string): string => {
try {
const url = new URL(relativePath, baseUrl);
return url.toString();
@ -37,3 +36,18 @@ export const buildURL = (baseUrl: string, relativePath: string): string => {
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
package-lock.json generated
View File

@ -7,6 +7,7 @@
"": {
"name": "hexabot",
"version": "2.0.0",
"license": "AGPL-3.0-only",
"workspaces": [
"frontend",
"widget"
@ -19,7 +20,8 @@
},
"frontend": {
"name": "hexabot-ui",
"version": "0.1.0",
"version": "2.0.0",
"license": "AGPL-3.0-only",
"dependencies": {
"@chatscope/chat-ui-kit-react": "^2.0.3",
"@chatscope/chat-ui-kit-styles": "^1.4.0",
@ -9802,7 +9804,8 @@
},
"widget": {
"name": "hexabot-widget",
"version": "0.0.0",
"version": "2.0.0",
"license": "AGPL-3.0-only",
"dependencies": {
"@types/emoji-js": "^3.5.2",
"autolinker": "^4.0.0",