From f8ea0aeefc95285624534c8c8747de4c36f7db01 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Fri, 11 Oct 2024 05:51:14 +0530 Subject: [PATCH] feat: ensure manual & programmatic url works --- src/components/molecules/UrlForm.tsx | 46 +++++++++++++--------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/src/components/molecules/UrlForm.tsx b/src/components/molecules/UrlForm.tsx index a6681ab9..336fd7a7 100644 --- a/src/components/molecules/UrlForm.tsx +++ b/src/components/molecules/UrlForm.tsx @@ -1,7 +1,6 @@ -import { useState, useCallback, useEffect, } from 'react'; -import type { SyntheticEvent, } from 'react'; +import { useState, useEffect, useCallback } from 'react'; +import type { SyntheticEvent } from 'react'; import KeyboardArrowRightIcon from '@mui/icons-material/KeyboardArrowRight'; - import { NavBarForm, NavBarInput } from "../atoms/form"; import { UrlFormButton } from "../atoms/buttons/buttons"; import { useSocketStore } from '../../context/socket'; @@ -18,44 +17,41 @@ export const UrlForm = ({ handleRefresh, setCurrentAddress, }: Props) => { - // states: + // Internal address state, initially set to currentAddress. We need this else the input field will not update with recordingUrl const [address, setAddress] = useState(currentAddress); - // context: const { socket } = useSocketStore(); - const areSameAddresses = address === currentAddress; - const onChange = useCallback((event: SyntheticEvent): void => { setAddress((event.target as HTMLInputElement).value); - }, [address]); + }, []); const onSubmit = (event: SyntheticEvent): void => { event.preventDefault(); let url = address; - // add protocol if missing - if (!/^(?:f|ht)tps?\:\/\//.test(address)) { - url = "https://" + address; - setAddress(url); + // If no manual change, use the currentAddress prop + if (address === currentAddress) { + url = currentAddress; } - if (areSameAddresses) { - if (socket) { - handleRefresh(socket); - } - } else { - try { - // try the validity of url - new URL(url); - setCurrentAddress(url); - } catch (e) { - alert(`ERROR: ${url} is not a valid url!`); - } + // Add protocol if missing + if (!/^(?:f|ht)tps?\:\/\//.test(url)) { + url = "https://" + url; + setAddress(url); // Update the input field to reflect protocol addition + } + + try { + // Validate the URL + new URL(url); + setCurrentAddress(url); + } catch (e) { + alert(`ERROR: ${url} is not a valid url!`); } }; + // Sync internal state with currentAddress prop when it changes useEffect(() => { - setAddress(currentAddress) + setAddress(currentAddress); }, [currentAddress]); return (