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

251 lines
8.5 KiB
TypeScript
Raw Normal View History

2024-09-25 20:27:56 +05:30
import React, { useState, useContext } from 'react';
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-08 21:08:20 +05:30
import { IconButton, Menu, MenuItem, Typography, Avatar, Chip, Button, Modal, Tabs, Tab, Box } from "@mui/material";
2024-12-08 20:32:41 +05:30
import { AccountCircle, Logout, Clear, YouTube, X } 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-10-21 01:13:06 +05:30
export const NavBar: React.FC<NavBarProps> = ({ recordingName, isRecording }) => {
2024-10-21 01:12:27 +05:30
const { notify, browserId, setBrowserId, recordingUrl } = 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-10-28 19:07:08 +05:30
2024-10-28 19:06:43 +05:30
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
2024-12-08 21:08:20 +05:30
const currentVersion = "0.0.3"; // Dynamically fetch from package.json
const [open, setOpen] = useState(false);
const [latestVersion, setLatestVersion] = useState(null);
const [tab, setTab] = useState(0);
const fetchLatestVersion = async () => {
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
setLatestVersion(version);
} catch (error) {
console.error("Failed to fetch latest version:", error);
setLatestVersion(null); // Handle errors gracefully
}
};
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 = (newValue: any) => {
2024-12-08 21:08:20 +05:30
setTab(newValue);
};
2024-10-11 04:57:48 +05:30
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-10-28 19:06:43 +05:30
const handleMenuClose = () => {
setAnchorEl(null);
};
2024-06-24 22:34:54 +05:30
2024-09-25 20:27:56 +05:30
const logout = async () => {
dispatch({ type: 'LOGOUT' });
window.localStorage.removeItem('user');
2024-11-01 08:25:33 +05:30
const { data } = await axios.get(`${apiUrl}/auth/logout`);
2024-09-25 20:27:56 +05:30
notify('success', data.message);
navigate('/login');
};
2024-09-10 02:49:30 +05:30
const goToMainMenu = async () => {
2024-06-24 22:34:54 +05:30
if (browserId) {
await stopRecording(browserId);
notify('warning', 'Current Recording was terminated');
setBrowserId(null);
}
2024-09-10 02:52:07 +05:30
navigate('/');
2024-06-24 22:34:54 +05:30
};
return (
<NavBarWrapper>
2024-09-30 16:05:50 +05:30
<div style={{
display: 'flex',
justifyContent: 'flex-start',
}}>
2024-11-03 21:18:17 +05:30
<img src={MaxunLogo} width={45} height={40} style={{ borderRadius: '5px', margin: '5px 0px 5px 15px' }} />
2024-12-06 05:00:46 +05:30
<div style={{ padding: '11px' }}><ProjectName>Maxun</ProjectName></div>
2024-12-06 04:57:09 +05:30
<Chip
label={`v${packageJson.version}`}
color="primary"
variant="outlined"
2024-12-06 05:00:46 +05:30
sx={{ marginTop: '10px' }}
2024-12-06 04:57:09 +05:30
/>
</div>
2024-09-30 16:05:26 +05:30
{
2024-10-28 19:06:43 +05:30
user ? (
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'flex-end' }}>
{!isRecording ? (
<>
<Button variant="contained" onClick={handleUpdateOpen}>
2024-12-08 21:08:20 +05:30
Check for Updates
</Button>
<Modal open={open} onClose={handleUpdateClose}>
2024-12-08 21:08:20 +05:30
<Box
sx={{
position: "absolute",
top: "50%",
left: "50%",
transform: "translate(-50%, -50%)",
width: 400,
bgcolor: "background.paper",
boxShadow: 24,
p: 4,
borderRadius: 2,
}}
>
{latestVersion === null ? (
<Typography>Checking for updates...</Typography>
) : currentVersion === latestVersion ? (
<Typography variant="h6" textAlign="center">
🎉 You're up to date!
</Typography>
) : (
<>
<Typography variant="h6" textAlign="center">
A new version is available: {latestVersion}
</Typography>
<Tabs
value={tab}
onChange={handleUpdateTabChange}
2024-12-08 21:08:20 +05:30
sx={{ marginTop: 2, marginBottom: 2 }}
centered
>
<Tab label="Manual" />
<Tab label="Docker Compose" />
</Tabs>
{tab === 0 && (
<Box>
<Typography variant="h6">Manual Upgrade</Typography>
<Typography component="pre" sx={{ bgcolor: "#f5f5f5", p: 2, borderRadius: 1 }}>
git pull origin main
<br />
npm install
<br />
npm run start
</Typography>
</Box>
)}
{tab === 1 && (
<Box>
<Typography variant="h6">Docker Compose Upgrade</Typography>
<Typography component="pre" sx={{ bgcolor: "#f5f5f5", p: 2, borderRadius: 1 }}>
docker pull getmaxun/maxun:latest
<br />
docker-compose up -d
</Typography>
</Box>
)}
</>
)}
</Box>
</Modal>
2024-11-05 23:05:50 +05:30
<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>
2024-10-28 19:06:43 +05:30
<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',
}}
2024-12-08 20:37:48 +05:30
PaperProps={{sx: {width: '180px'}}}
2024-10-28 19:06:43 +05:30
>
<MenuItem onClick={() => { handleMenuClose(); logout(); }}>
<Logout sx={{ marginRight: '5px' }} /> Logout
</MenuItem>
2024-12-08 20:42:24 +05:30
<MenuItem onClick={() => {
window.open('https://discord.gg/5GbPjBUkws', '_blank');
}}>
<DiscordIcon sx={{ marginRight: '5px' }} /> Discord
</MenuItem>
2024-12-08 20:30:11 +05:30
<MenuItem onClick={() => {
2024-12-08 20:55:24 +05:30
window.open('https://www.youtube.com/@MaxunOSS/videos?ref=app', '_blank');
2024-12-08 20:29:53 +05:30
}}>
2024-12-08 20:26:34 +05:30
<YouTube sx={{ marginRight: '5px' }} /> YouTube
</MenuItem>
2024-12-08 20:32:41 +05:30
<MenuItem onClick={() => {
2024-12-08 20:55:24 +05:30
window.open('https://x.com/maxun_io?ref=app', '_blank');
2024-12-08 20:32:41 +05:30
}}>
<X sx={{ marginRight: '5px' }} /> Twiiter (X)
</MenuItem>
2024-10-28 19:06:43 +05:30
</Menu>
</>
) : (
<>
<IconButton onClick={goToMainMenu} sx={{
borderRadius: '5px',
padding: '8px',
background: 'red',
color: 'white',
marginRight: '10px',
'&:hover': { color: 'white', backgroundColor: 'red' }
}}>
<Clear sx={{ marginRight: '5px' }} />
Discard
</IconButton>
<SaveRecording fileName={recordingName} />
</>
)}
</div>
2024-09-30 16:00:26 +05:30
) : ""
}
2024-06-24 22:34:54 +05:30
</NavBarWrapper>
);
};
const NavBarWrapper = styled.div`
grid-area: navbar;
2024-10-10 21:14:51 +05:30
background-color: white;
2024-06-24 22:34:54 +05:30
padding:5px;
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;
`;