From 568189bfea9848fcaf5555157bf7309055f7e249 Mon Sep 17 00:00:00 2001 From: "plebeius.eth" Date: Sat, 16 Mar 2024 16:02:57 +0100 Subject: [PATCH] feat(home): user can connect to a sub with search bar --- src/views/home/home.tsx | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/views/home/home.tsx b/src/views/home/home.tsx index 4059e0de..bf14821d 100644 --- a/src/views/home/home.tsx +++ b/src/views/home/home.tsx @@ -1,6 +1,7 @@ +import { useRef } from 'react'; import styles from './home.module.css'; import useTheme from '../../hooks/use-theme'; -import { Link } from 'react-router-dom'; +import { Link, useNavigate } from 'react-router-dom'; import { Trans, useTranslation } from 'react-i18next'; import { useDefaultSubplebbitAddresses } from '../../hooks/use-default-subplebbits'; import { useEffect } from 'react'; @@ -44,12 +45,45 @@ const LanguageSettings = () => { ); }; +const isValidAddress = (address: string): boolean => { + if (address.includes('/') || address.includes('\\') || address.includes(' ')) { + return false; + } + return true; +}; + const SearchBar = () => { + const searchInputRef = useRef(null); const { t } = useTranslation(); + const navigate = useNavigate(); + + const handleSearchSubmit = () => { + const inputValue = searchInputRef.current?.value; + if (inputValue && isValidAddress(inputValue)) { + navigate(`/p/${inputValue}`); + } else { + alert('invalid address'); + } + }; + return (
- - + { + if (e.key === 'Enter') { + handleSearchSubmit(); + } + }} + /> +
); };