From bf8d35c65d0a4e8b2bc058af57ea84fe2ceab20d Mon Sep 17 00:00:00 2001 From: Ciaran Morinan Date: Thu, 9 Jan 2025 20:16:15 +0000 Subject: [PATCH 1/4] add protocol dropdown to early access form website input --- .../waitlisted/early-access-modal.tsx | 16 ++-- .../src/pages/shared/url-input.tsx | 89 +++++++++++++++++++ 2 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 apps/hash-frontend/src/pages/shared/url-input.tsx diff --git a/apps/hash-frontend/src/pages/index.page/waitlisted/early-access-modal.tsx b/apps/hash-frontend/src/pages/index.page/waitlisted/early-access-modal.tsx index 9d535cbce4a..6ee43ff0292 100644 --- a/apps/hash-frontend/src/pages/index.page/waitlisted/early-access-modal.tsx +++ b/apps/hash-frontend/src/pages/index.page/waitlisted/early-access-modal.tsx @@ -9,6 +9,7 @@ import { Button } from "../../../shared/ui/button"; import { MenuItem } from "../../../shared/ui/menu-item"; import { Modal } from "../../../shared/ui/modal"; import { useAuthenticatedUser } from "../../shared/auth-info-context"; +import { UrlInput } from "../../shared/url-input"; type FormFieldMetadata = { label: string; @@ -93,7 +94,14 @@ const Input = ({ * - {options ? ( + {url ? ( + + ) : options ? ( ) : ( onChange(event.target.value)} placeholder={placeholder} diff --git a/apps/hash-frontend/src/pages/shared/url-input.tsx b/apps/hash-frontend/src/pages/shared/url-input.tsx new file mode 100644 index 00000000000..21362486bdd --- /dev/null +++ b/apps/hash-frontend/src/pages/shared/url-input.tsx @@ -0,0 +1,89 @@ +import { Select, TextField } from "@hashintel/design-system"; +import { Stack } from "@mui/material"; +import debounce from "lodash/debounce"; +import { useEffect, useState } from "react"; + +import { MenuItem } from "../../shared/ui/menu-item"; + +const protocols = ["https://", "http://"] as const; + +type Protocol = (typeof protocols)[number]; + +const protocolFromUrl = (url: string): Protocol => { + if (!url) { + return "https://"; + } + + try { + const urlObject = new URL(url); + if (!protocols.includes(urlObject.protocol as Protocol)) { + return "https://"; + } + + return urlObject.protocol as Protocol; + } catch { + return "https://"; + } +}; + +export const UrlInput = ({ + autoFocus, + onChange, + placeholder, + value, +}: { + autoFocus: boolean; + onChange: (value: string) => void; + placeholder: string; + value: string; +}) => { + const [protocol, setProtocol] = useState(() => { + return protocolFromUrl(value); + }); + const [rest, setRest] = useState(() => value.split("://").at(-1)); + + useEffect(() => { + setProtocol(protocolFromUrl(value)); + setRest(value.split("://").at(-1)); + }, [value]); + + const updateValue = debounce((fullUrl: string) => { + onChange(fullUrl); + }, 300); + + return ( + + + { + const newRest = event.target.value; + setRest(newRest); + updateValue(protocol + newRest); + }} + placeholder={placeholder} + sx={{ width: "100%" }} + type="text" + value={rest} + /> + + ); +}; From e59aa7eb38157424e0196e652d94c2bb5a013323 Mon Sep 17 00:00:00 2001 From: Ciaran Morinan Date: Fri, 10 Jan 2025 12:04:19 +0000 Subject: [PATCH 2/4] fix protocol/path setting in url input --- .../src/pages/shared/url-input.tsx | 50 +++++++------------ 1 file changed, 17 insertions(+), 33 deletions(-) diff --git a/apps/hash-frontend/src/pages/shared/url-input.tsx b/apps/hash-frontend/src/pages/shared/url-input.tsx index 21362486bdd..0339d86a175 100644 --- a/apps/hash-frontend/src/pages/shared/url-input.tsx +++ b/apps/hash-frontend/src/pages/shared/url-input.tsx @@ -1,29 +1,24 @@ import { Select, TextField } from "@hashintel/design-system"; import { Stack } from "@mui/material"; -import debounce from "lodash/debounce"; -import { useEffect, useState } from "react"; import { MenuItem } from "../../shared/ui/menu-item"; -const protocols = ["https://", "http://"] as const; +const protocols = ["https", "http"] as const; type Protocol = (typeof protocols)[number]; -const protocolFromUrl = (url: string): Protocol => { +const partsFromUrl = (url: string): { protocol: Protocol; rest: string } => { if (!url) { - return "https://"; + return { protocol: "https", rest: "" }; } - try { - const urlObject = new URL(url); - if (!protocols.includes(urlObject.protocol as Protocol)) { - return "https://"; - } + const [protocol, rest] = url.split("://"); - return urlObject.protocol as Protocol; - } catch { - return "https://"; + if (!protocol) { + return { protocol: "https", rest: "" }; } + + return { protocol: protocol as Protocol, rest: rest ?? "" }; }; export const UrlInput = ({ @@ -37,19 +32,7 @@ export const UrlInput = ({ placeholder: string; value: string; }) => { - const [protocol, setProtocol] = useState(() => { - return protocolFromUrl(value); - }); - const [rest, setRest] = useState(() => value.split("://").at(-1)); - - useEffect(() => { - setProtocol(protocolFromUrl(value)); - setRest(value.split("://").at(-1)); - }, [value]); - - const updateValue = debounce((fullUrl: string) => { - onChange(fullUrl); - }, 300); + const { protocol, rest } = partsFromUrl(value); return ( @@ -57,27 +40,28 @@ export const UrlInput = ({ value={protocol} onChange={(event) => { const newProtocol = event.target.value as Protocol; - setProtocol(newProtocol); - updateValue(newProtocol + rest); + onChange(`${newProtocol}://${rest}`); + }} + sx={{ + width: 110, + "& svg": { color: ({ palette }) => palette.gray[30] }, }} - sx={{ width: 100 }} > {protocols.map((option) => ( - {option} + {option}:// ))} { const newRest = event.target.value; - setRest(newRest); - updateValue(protocol + newRest); + onChange(`${protocol}://${newRest}`); }} placeholder={placeholder} sx={{ width: "100%" }} From e2c61352cd1a9a0b4f87af89469f170b73a1c97b Mon Sep 17 00:00:00 2001 From: Ciaran Morinan Date: Fri, 10 Jan 2025 12:29:35 +0000 Subject: [PATCH 3/4] remove gap between protocol and rest in url input --- .../src/pages/shared/url-input.tsx | 22 ++++++++++++++++--- .../inputs/mui-select-theme-options.tsx | 2 +- .../navigation/mui-menu-item-theme-options.ts | 3 +-- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/apps/hash-frontend/src/pages/shared/url-input.tsx b/apps/hash-frontend/src/pages/shared/url-input.tsx index 0339d86a175..98b6ba5c435 100644 --- a/apps/hash-frontend/src/pages/shared/url-input.tsx +++ b/apps/hash-frontend/src/pages/shared/url-input.tsx @@ -1,5 +1,5 @@ import { Select, TextField } from "@hashintel/design-system"; -import { Stack } from "@mui/material"; +import { outlinedInputClasses, Stack } from "@mui/material"; import { MenuItem } from "../../shared/ui/menu-item"; @@ -35,7 +35,7 @@ export const UrlInput = ({ const { protocol, rest } = partsFromUrl(value); return ( - +