From 4642dbdace6eb16eb96e4d58f5291dc22ad285f2 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Fri, 14 Jun 2024 22:39:02 +0530 Subject: [PATCH] feat: browser nav (input) --- src/components/molecules/BrowserNavBar.tsx | 123 +++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 src/components/molecules/BrowserNavBar.tsx diff --git a/src/components/molecules/BrowserNavBar.tsx b/src/components/molecules/BrowserNavBar.tsx new file mode 100644 index 00000000..e57e7c21 --- /dev/null +++ b/src/components/molecules/BrowserNavBar.tsx @@ -0,0 +1,123 @@ +import type { + FC, +} from 'react'; +import styled from 'styled-components'; + +import ReplayIcon from '@mui/icons-material/Replay'; +import ArrowBackIcon from '@mui/icons-material/ArrowBack'; +import ArrowForwardIcon from '@mui/icons-material/ArrowForward'; + +import { NavBarButton } from '../atoms/buttons/buttons'; +import { UrlForm } from './UrlForm'; +import { useCallback, useEffect, useState } from "react"; +import {useSocketStore} from "../../context/socket"; +import { getCurrentUrl } from "../../api/recording"; + +const StyledNavBar = styled.div<{ browserWidth: number }>` + display: flex; + padding: 5px; + background-color: #f6f6f6; + width: ${({ browserWidth }) => browserWidth}px; +`; + +interface NavBarProps { + browserWidth: number; + handleUrlChanged: (url: string) => void; +}; + +const BrowserNavBar: FC = ({ + browserWidth, + handleUrlChanged, +}) => { + + // context: + const { socket } = useSocketStore(); + + const [currentUrl, setCurrentUrl] = useState('https://'); + + const handleRefresh = useCallback(() : void => { + socket?.emit('input:refresh'); + }, [socket]); + + const handleGoTo = useCallback((address: string) : void => { + socket?.emit('input:url', address); + }, [socket]); + + const handleCurrentUrlChange = useCallback((url: string) => { + handleUrlChanged(url); + setCurrentUrl(url); + }, [handleUrlChanged, currentUrl]); + + useEffect(() => { + getCurrentUrl().then((response) => { + if (response) { + handleUrlChanged(response); + setCurrentUrl(response); + } + }).catch((error) => { + console.log("Fetching current url failed"); + }) + }, []); + + useEffect(() => { + if (socket) { + socket.on('urlChanged', handleCurrentUrlChange); + } + return () => { + if (socket) { + socket.off('urlChanged', handleCurrentUrlChange); + } + } + }, [socket, handleCurrentUrlChange]) + + const addAddress = (address: string) => { + if (socket) { + handleUrlChanged(address); + handleGoTo(address); + } + }; + + return ( + + { + socket?.emit('input:back'); + }} + disabled={false} + > + + + + { + socket?.emit('input:forward'); + }} + disabled={false} + > + + + + { + if (socket) { + handleRefresh() + } + }} + disabled={false} + > + + + + + + ); +} + +export default BrowserNavBar;