Files
parcer/src/components/organisms/BrowserContent.tsx

140 lines
4.2 KiB
TypeScript
Raw Normal View History

2024-06-14 21:41:17 +05:30
import React, { useCallback, useEffect, useState } from 'react';
import styled from "styled-components";
import BrowserNavBar from "../molecules/BrowserNavBar";
import { BrowserWindow } from "./BrowserWindow";
import { useBrowserDimensionsStore } from "../../context/browserDimensions";
import { BrowserTabs } from "../molecules/BrowserTabs";
import { useSocketStore } from "../../context/socket";
import { getCurrentTabs, getCurrentUrl, interpretCurrentRecording } from "../../api/recording";
2024-10-11 10:20:24 +05:30
import { Box } from '@mui/material';
2024-10-18 22:28:06 +05:30
import { InterpretationLog } from "../molecules/InterpretationLog";
2024-06-14 21:41:17 +05:30
2024-10-11 07:36:14 +05:30
// TODO: Tab !show currentUrl after recordingUrl global state
2024-06-14 21:41:17 +05:30
export const BrowserContent = () => {
2024-10-19 14:00:21 +05:30
const { width } = useBrowserDimensionsStore();
const { socket } = useSocketStore();
2024-06-14 21:41:17 +05:30
2024-10-19 14:00:21 +05:30
const [tabs, setTabs] = useState<string[]>(['current']);
const [tabIndex, setTabIndex] = React.useState(0);
const [showOutputData, setShowOutputData] = useState(false);
2024-06-14 21:41:17 +05:30
2024-10-19 14:00:21 +05:30
const handleChangeIndex = useCallback((index: number) => {
setTabIndex(index);
}, [tabIndex])
2024-06-14 21:41:17 +05:30
const handleCloseTab = useCallback((index: number) => {
// the tab needs to be closed on the backend
socket?.emit('closeTab', {
index,
isCurrent: tabIndex === index,
});
// change the current index as current tab gets closed
if (tabIndex === index) {
if (tabs.length > index + 1) {
handleChangeIndex(index);
} else {
handleChangeIndex(index - 1);
}
} else {
handleChangeIndex(tabIndex - 1);
}
// update client tabs
setTabs((prevState) => [
...prevState.slice(0, index),
...prevState.slice(index + 1)
])
}, [tabs, socket, tabIndex]);
const handleAddNewTab = useCallback(() => {
// Adds new tab by pressing the plus button
socket?.emit('addTab');
// Adds a new tab to the end of the tabs array and shifts focus
setTabs((prevState) => [...prevState, 'new tab']);
handleChangeIndex(tabs.length);
}, [socket, tabs]);
2024-10-19 14:00:21 +05:30
const handleNewTab = useCallback((tab: string) => {
// Adds a new tab to the end of the tabs array and shifts focus
setTabs((prevState) => [...prevState, tab]);
// changes focus on the new tab - same happens in the remote browser
handleChangeIndex(tabs.length);
handleTabChange(tabs.length);
}, [tabs]);
2024-06-14 21:41:17 +05:30
const handleTabChange = useCallback((index: number) => {
// page screencast and focus needs to be changed on backend
2024-10-19 14:00:21 +05:30
socket?.emit('changeTab', index);
2024-06-14 21:41:17 +05:30
}, [socket]);
const handleUrlChanged = (url: string) => {
const parsedUrl = new URL(url);
if (parsedUrl.hostname) {
const host = parsedUrl.hostname.match(/\b(?!www\.)[a-zA-Z0-9]+/g)?.join('.')
if (host && host !== tabs[tabIndex]) {
setTabs((prevState) => [
...prevState.slice(0, tabIndex),
host,
...prevState.slice(tabIndex + 1)
])
}
} else {
if (tabs[tabIndex] !== 'new tab') {
setTabs((prevState) => [
...prevState.slice(0, tabIndex),
'new tab',
...prevState.slice(tabIndex + 1)
])
}
}
};
const tabHasBeenClosedHandler = useCallback((index: number) => {
handleCloseTab(index);
}, [handleCloseTab])
2024-10-19 14:00:21 +05:30
useEffect(() => {
if (socket) {
socket.on('newTab', handleNewTab);
socket.on('tabHasBeenClosed', tabHasBeenClosedHandler);
}
return () => {
if (socket) {
socket.off('newTab', handleNewTab);
socket.off('tabHasBeenClosed', tabHasBeenClosedHandler);
}
}
}, [socket, handleNewTab])
2024-06-14 21:41:17 +05:30
useEffect(() => {
getCurrentTabs().then((response) => {
if (response) {
setTabs(response);
}
}).catch((error) => {
console.log("Fetching current url failed");
})
}, [])
return (
2024-10-19 04:48:26 +05:30
<div id="browser">
2024-06-14 21:41:17 +05:30
<BrowserTabs
tabs={tabs}
handleTabChange={handleTabChange}
handleAddNewTab={handleAddNewTab}
handleCloseTab={handleCloseTab}
handleChangeIndex={handleChangeIndex}
tabIndex={tabIndex}
/>
<BrowserNavBar
// todo: use width from browser dimension once fixed
2024-10-18 22:28:06 +05:30
browserWidth={1150}
2024-06-14 21:41:17 +05:30
handleUrlChanged={handleUrlChanged}
/>
2024-10-19 14:00:21 +05:30
<BrowserWindow />
2024-10-18 16:49:26 +05:30
</div>
2024-06-14 21:41:17 +05:30
);
}
const BrowserContentWrapper = styled.div`
2024-07-25 00:10:42 +05:30
`;