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

159 lines
4.3 KiB
TypeScript
Raw Normal View History

2024-11-04 21:20:43 +05:30
import React, { useCallback, useEffect, useState } from "react";
2024-06-14 21:41:17 +05:30
import styled from "styled-components";
2025-01-09 20:11:56 +05:30
import BrowserNavBar from "./BrowserNavBar";
2024-06-14 21:41:17 +05:30
import { BrowserWindow } from "./BrowserWindow";
import { useBrowserDimensionsStore } from "../../context/browserDimensions";
2025-01-09 20:11:56 +05:30
import { BrowserTabs } from "./BrowserTabs";
2024-06-14 21:41:17 +05:30
import { useSocketStore } from "../../context/socket";
2024-11-04 21:20:43 +05:30
import {
getCurrentTabs,
} from "../../api/recording";
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 { socket } = useSocketStore();
2024-06-14 21:41:17 +05:30
2024-11-04 21:20:43 +05:30
const [tabs, setTabs] = useState<string[]>(["current"]);
2024-10-19 14:00:21 +05:30
const [tabIndex, setTabIndex] = React.useState(0);
const [showOutputData, setShowOutputData] = useState(false);
const { browserWidth } = useBrowserDimensionsStore();
2024-06-14 21:41:17 +05:30
2024-11-04 21:20:43 +05:30
const handleChangeIndex = useCallback(
(index: number) => {
setTabIndex(index);
},
[tabIndex]
);
2024-06-14 21:41:17 +05:30
2024-11-04 21:20:43 +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);
}
2024-06-14 21:41:17 +05:30
} else {
2024-11-04 21:20:43 +05:30
handleChangeIndex(tabIndex - 1);
2024-06-14 21:41:17 +05:30
}
2024-11-04 21:20:43 +05:30
// update client tabs
setTabs((prevState) => [
...prevState.slice(0, index),
...prevState.slice(index + 1),
]);
},
[tabs, socket, tabIndex]
);
2024-06-14 21:41:17 +05:30
const handleAddNewTab = useCallback(() => {
// Adds new tab by pressing the plus button
2024-11-04 21:20:43 +05:30
socket?.emit("addTab");
2024-06-14 21:41:17 +05:30
// Adds a new tab to the end of the tabs array and shifts focus
2024-11-04 21:20:43 +05:30
setTabs((prevState) => [...prevState, "new tab"]);
2024-06-14 21:41:17 +05:30
handleChangeIndex(tabs.length);
}, [socket, tabs]);
2024-11-04 21:20:43 +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
2024-11-04 21:20:43 +05:30
const handleTabChange = useCallback(
(index: number) => {
// page screencast and focus needs to be changed on backend
socket?.emit("changeTab", index);
},
[socket]
);
2024-06-14 21:41:17 +05:30
2025-04-29 00:30:50 +05:30
const handleUrlChanged = useCallback((url: string) => {
2024-06-14 21:41:17 +05:30
const parsedUrl = new URL(url);
if (parsedUrl.hostname) {
2024-11-04 21:20:43 +05:30
const host = parsedUrl.hostname
.match(/\b(?!www\.)[a-zA-Z0-9]+/g)
?.join(".");
2024-06-14 21:41:17 +05:30
if (host && host !== tabs[tabIndex]) {
setTabs((prevState) => [
...prevState.slice(0, tabIndex),
host,
2024-11-04 21:20:43 +05:30
...prevState.slice(tabIndex + 1),
]);
2024-06-14 21:41:17 +05:30
}
} else {
2024-11-04 21:20:43 +05:30
if (tabs[tabIndex] !== "new tab") {
2024-06-14 21:41:17 +05:30
setTabs((prevState) => [
...prevState.slice(0, tabIndex),
2024-11-04 21:20:43 +05:30
"new tab",
...prevState.slice(tabIndex + 1),
]);
2024-06-14 21:41:17 +05:30
}
}
2025-04-29 00:30:50 +05:30
}, [tabs, tabIndex]);
2024-06-14 21:41:17 +05:30
2024-11-04 21:20:43 +05:30
const tabHasBeenClosedHandler = useCallback(
(index: number) => {
handleCloseTab(index);
},
[handleCloseTab]
);
2024-06-14 21:41:17 +05:30
2024-10-19 14:00:21 +05:30
useEffect(() => {
if (socket) {
2024-11-04 21:20:43 +05:30
socket.on("newTab", handleNewTab);
socket.on("tabHasBeenClosed", tabHasBeenClosedHandler);
2024-10-19 14:00:21 +05:30
}
return () => {
if (socket) {
2024-11-04 21:20:43 +05:30
socket.off("newTab", handleNewTab);
socket.off("tabHasBeenClosed", tabHasBeenClosedHandler);
2024-10-19 14:00:21 +05:30
}
2024-11-04 21:20:43 +05:30
};
}, [socket, handleNewTab]);
2024-06-14 21:41:17 +05:30
useEffect(() => {
2024-11-04 21:20:43 +05:30
getCurrentTabs()
.then((response) => {
if (response) {
setTabs(response);
}
})
.catch((error) => {
console.log("Fetching current url failed");
});
2025-04-29 00:30:50 +05:30
}, []);
2024-06-14 21:41:17 +05:30
return (
<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
browserWidth={browserWidth}
2024-06-14 21:41:17 +05:30
handleUrlChanged={handleUrlChanged}
2025-01-09 17:13:54 +05:30
2024-06-14 21:41:17 +05:30
/>
2025-01-09 17:13:54 +05:30
<BrowserWindow />
2024-10-18 16:49:26 +05:30
</div>
2024-06-14 21:41:17 +05:30
);
2024-11-04 21:20:43 +05:30
};
2024-06-14 21:41:17 +05:30
const BrowserContentWrapper = styled.div``;