Files
parcer/src/components/molecules/NavBar.tsx

415 lines
11 KiB
TypeScript
Raw Normal View History

import { useTranslation } from "react-i18next"; // Import useTranslation hook
2024-12-09 00:54:39 +05:30
import React, { useState, useContext, useEffect } from 'react';
2024-09-25 20:27:56 +05:30
import axios from 'axios';
2024-06-24 22:34:54 +05:30
import styled from "styled-components";
import { stopRecording } from "../../api/recording";
import { useGlobalInfoStore } from "../../context/globalInfo";
2024-12-09 00:54:39 +05:30
import { IconButton, Menu, MenuItem, Typography, Chip, Button, Modal, Tabs, Tab, Box, Snackbar } from "@mui/material";
import { AccountCircle, Logout, Clear, YouTube, X, Update, Close,Language } from "@mui/icons-material";
2024-10-21 01:12:06 +05:30
import { useNavigate } from 'react-router-dom';
2024-09-25 20:27:56 +05:30
import { AuthContext } from '../../context/auth';
2024-10-28 19:06:43 +05:30
import { SaveRecording } from '../molecules/SaveRecording';
2024-10-29 08:44:50 +05:30
import DiscordIcon from '../atoms/DiscordIcon';
2024-11-01 08:25:33 +05:30
import { apiUrl } from '../../apiConfig';
2024-11-03 21:18:17 +05:30
import MaxunLogo from "../../assets/maxunlogo.png";
2024-12-06 04:56:31 +05:30
import packageJson from "../../../package.json"
2024-06-24 22:34:54 +05:30
interface NavBarProps {
recordingName: string;
isRecording: boolean;
}
2024-12-07 22:20:17 +05:30
export const NavBar: React.FC<NavBarProps> = ({
recordingName,
isRecording,
}) => {
const { notify, browserId, setBrowserId } = useGlobalInfoStore();
2024-09-25 20:27:56 +05:30
const { state, dispatch } = useContext(AuthContext);
const { user } = state;
2024-10-28 19:06:43 +05:30
const navigate = useNavigate();
2024-12-07 22:20:17 +05:30
const { t, i18n } = useTranslation(); // Get translation function and i18n methods
2024-10-28 19:07:08 +05:30
2024-10-28 19:06:43 +05:30
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
2024-12-07 22:20:17 +05:30
const [langAnchorEl, setLangAnchorEl] = useState<null | HTMLElement>(null);
2024-10-11 04:57:48 +05:30
const currentVersion = packageJson.version;
2024-12-08 21:08:20 +05:30
const [open, setOpen] = useState(false);
2024-12-09 00:54:39 +05:30
const [latestVersion, setLatestVersion] = useState<string | null>(null);
2024-12-08 21:08:20 +05:30
const [tab, setTab] = useState(0);
2024-12-09 00:54:39 +05:30
const [isUpdateAvailable, setIsUpdateAvailable] = useState(false);
2024-12-08 21:08:20 +05:30
2024-12-09 00:54:39 +05:30
const fetchLatestVersion = async (): Promise<string | null> => {
2024-12-08 21:08:20 +05:30
try {
const response = await fetch("https://api.github.com/repos/getmaxun/maxun/releases/latest");
const data = await response.json();
const version = data.tag_name.replace(/^v/, ""); // Remove 'v' prefix
2024-12-09 00:54:39 +05:30
return version;
2024-12-08 21:08:20 +05:30
} catch (error) {
console.error("Failed to fetch latest version:", error);
2024-12-09 00:54:39 +05:30
return null; // Handle errors gracefully
2024-12-08 21:08:20 +05:30
}
};
const handleUpdateOpen = () => {
2024-12-08 21:08:20 +05:30
setOpen(true);
fetchLatestVersion();
};
const handleUpdateClose = () => {
2024-12-08 21:08:20 +05:30
setOpen(false);
setTab(0); // Reset tab to the first tab
};
const handleUpdateTabChange = (event: React.SyntheticEvent, newValue: number) => {
2024-12-08 21:08:20 +05:30
setTab(newValue);
};
2024-10-28 19:06:43 +05:30
const handleMenuOpen = (event: React.MouseEvent<HTMLElement>) => {
setAnchorEl(event.currentTarget);
};
2024-10-11 04:48:25 +05:30
2024-12-07 22:20:17 +05:30
const handleLangMenuOpen = (event: React.MouseEvent<HTMLElement>) => {
setLangAnchorEl(event.currentTarget);
};
2024-10-28 19:06:43 +05:30
const handleMenuClose = () => {
setAnchorEl(null);
2024-12-07 22:20:17 +05:30
setLangAnchorEl(null);
2024-10-28 19:06:43 +05:30
};
2024-06-24 22:34:54 +05:30
2024-09-25 20:27:56 +05:30
const logout = async () => {
2024-12-07 22:20:17 +05:30
dispatch({ type: "LOGOUT" });
window.localStorage.removeItem("user");
2024-11-01 08:25:33 +05:30
const { data } = await axios.get(`${apiUrl}/auth/logout`);
2024-12-07 22:20:17 +05:30
notify("success", data.message);
navigate("/login");
2024-09-25 20:27:56 +05:30
};
2024-09-10 02:49:30 +05:30
const goToMainMenu = async () => {
2024-06-24 22:34:54 +05:30
if (browserId) {
await stopRecording(browserId);
2024-12-07 22:20:17 +05:30
notify("warning", "Current Recording was terminated");
2024-06-24 22:34:54 +05:30
setBrowserId(null);
}
2024-12-07 22:20:17 +05:30
navigate("/");
};
const changeLanguage = (lang: string) => {
i18n.changeLanguage(lang); // Change language dynamically
localStorage.setItem("language", lang); // Persist language to localStorage
2024-06-24 22:34:54 +05:30
};
2024-12-09 00:54:39 +05:30
useEffect(() => {
const checkForUpdates = async () => {
const latestVersion = await fetchLatestVersion();
setLatestVersion(latestVersion); // Set the latest version state
if (latestVersion && latestVersion !== currentVersion) {
setIsUpdateAvailable(true); // Show a notification or highlight the "Upgrade" button
}
};
checkForUpdates();
}, []);
2024-06-24 22:34:54 +05:30
return (
2024-06-24 22:34:54 +05:30
<NavBarWrapper>
2024-12-07 22:20:17 +05:30
<div
style={{
display: "flex",
justifyContent: "flex-start",
}}
>
<img
src={MaxunLogo}
width={45}
height={40}
style={{ borderRadius: "5px", margin: "5px 0px 5px 15px" }}
/>
<div style={{ padding: "11px" }}>
<ProjectName>Maxun</ProjectName>
</div>
<Chip
label="beta"
color="primary"
variant="outlined"
sx={{ marginTop: "10px" }}
/>
2024-09-30 16:05:50 +05:30
</div>
2024-12-07 22:20:17 +05:30
{user ? (
<div
style={{
display: "flex",
alignItems: "center",
justifyContent: "flex-end",
}}
>
{!isRecording ? (
<>
<IconButton
component="a"
href="https://discord.gg/5GbPjBUkws"
target="_blank"
rel="noopener noreferrer"
sx={{
display: "flex",
alignItems: "center",
borderRadius: "5px",
padding: "8px",
marginRight: "30px",
}}
>
<DiscordIcon sx={{ marginRight: "5px" }} />
</IconButton>
<iframe
src="https://ghbtns.com/github-btn.html?user=getmaxun&repo=maxun&type=star&count=true&size=large"
frameBorder="0"
scrolling="0"
width="170"
height="30"
title="GitHub"
></iframe>
<IconButton
onClick={handleMenuOpen}
sx={{
display: "flex",
alignItems: "center",
borderRadius: "5px",
padding: "8px",
marginRight: "10px",
"&:hover": { backgroundColor: "white", color: "#ff00c3" },
}}
>
<AccountCircle sx={{ marginRight: "5px" }} />
<Typography variant="body1">{user.email}</Typography>
</IconButton>
<Menu
anchorEl={anchorEl}
open={Boolean(anchorEl)}
onClose={handleMenuClose}
anchorOrigin={{
vertical: "bottom",
horizontal: "right",
}}
transformOrigin={{
vertical: "top",
horizontal: "right",
}}
>
<MenuItem
onClick={() => {
handleMenuClose();
logout();
2024-11-05 23:05:50 +05:30
}}
2024-10-29 08:44:50 +05:30
>
2024-12-07 22:20:17 +05:30
<Logout sx={{ marginRight: "5px" }} /> {t("logout")}
</MenuItem>
</Menu>
2024-12-10 21:45:09 +05:30
2024-12-07 22:20:17 +05:30
</>
) : (
<>
<IconButton
onClick={goToMainMenu}
sx={{
borderRadius: "5px",
padding: "8px",
background: "red",
color: "white",
marginRight: "10px",
"&:hover": { color: "white", backgroundColor: "red" },
}}
>
<Clear sx={{ marginRight: "5px" }} />
{t("discard")}
</IconButton>
<SaveRecording fileName={recordingName} />
</>
)}
<IconButton
onClick={handleLangMenuOpen}
sx={{
display: "flex",
alignItems: "center",
borderRadius: "5px",
padding: "8px",
marginRight: "10px",
}}
>
2024-12-10 21:45:09 +05:30
<Typography variant="body1">
<Language />
</Typography>
2024-12-07 22:20:17 +05:30
</IconButton>
<Menu
anchorEl={langAnchorEl}
open={Boolean(langAnchorEl)}
onClose={handleMenuClose}
anchorOrigin={{
vertical: "bottom",
horizontal: "right",
}}
transformOrigin={{
vertical: "top",
horizontal: "right",
}}
>
<MenuItem
onClick={() => {
changeLanguage("en");
handleMenuClose();
}}
>
English
</MenuItem>
<MenuItem
onClick={() => {
changeLanguage("es");
handleMenuClose();
}}
>
Español
</MenuItem>
<MenuItem
onClick={() => {
changeLanguage("ja");
handleMenuClose();
}}
>
</MenuItem>
<MenuItem
onClick={() => {
changeLanguage("ar");
handleMenuClose();
}}
>
العربية
</MenuItem>
<MenuItem
onClick={() => {
changeLanguage("zh");
handleMenuClose();
}}
>
</MenuItem>
2024-12-10 20:40:59 +05:30
<MenuItem
onClick={() => {
changeLanguage("de");
handleMenuClose();
}}
>
German
</MenuItem>
2024-12-07 22:20:17 +05:30
</Menu>
</div>
) : (
<><IconButton
onClick={handleLangMenuOpen}
sx={{
display: "flex",
alignItems: "center",
borderRadius: "5px",
padding: "8px",
marginRight: "10px",
}}
>
<Typography variant="body1">{t("language")}</Typography>
</IconButton>
<Menu
anchorEl={langAnchorEl}
open={Boolean(langAnchorEl)}
onClose={handleMenuClose}
anchorOrigin={{
vertical: "bottom",
horizontal: "right",
}}
transformOrigin={{
vertical: "top",
horizontal: "right",
}}
>
<MenuItem
onClick={() => {
changeLanguage("en");
handleMenuClose();
}}
>
English
</MenuItem>
<MenuItem
onClick={() => {
changeLanguage("es");
handleMenuClose();
}}
>
Español
</MenuItem>
<MenuItem
onClick={() => {
changeLanguage("ja");
handleMenuClose();
}}
>
</MenuItem>
<MenuItem
onClick={() => {
changeLanguage("ar");
handleMenuClose();
}}
>
العربية
</MenuItem>
<MenuItem
onClick={() => {
changeLanguage("zh");
handleMenuClose();
}}
>
</MenuItem>
</Menu></>
)}
2024-06-24 22:34:54 +05:30
</NavBarWrapper>
2024-06-24 22:34:54 +05:30
);
};
const NavBarWrapper = styled.div`
grid-area: navbar;
2024-10-10 21:14:51 +05:30
background-color: white;
2024-12-07 22:20:17 +05:30
padding: 5px;
2024-06-24 22:34:54 +05:30
display: flex;
justify-content: space-between;
2024-10-23 07:12:33 +05:30
border-bottom: 1px solid #e0e0e0;
2024-06-24 22:34:54 +05:30
`;
const ProjectName = styled.b`
2024-10-10 21:14:51 +05:30
color: #3f4853;
2024-06-24 22:34:54 +05:30
font-size: 1.3em;
`;