Files
firegex-traffic-viewer/frontend/src/components/Header/index.tsx

146 lines
5.6 KiB
TypeScript
Raw Normal View History

2022-06-11 21:57:50 +02:00
import React, { useEffect, useState } from 'react';
2022-06-13 16:12:52 +02:00
import { ActionIcon, Badge, Button, Divider, Group, Image, Menu, Modal, Notification, Space, Switch, TextInput } from '@mantine/core';
2022-06-11 21:57:50 +02:00
import style from "./Header.module.scss";
2022-06-13 16:12:52 +02:00
import { changepassword, errorNotify, generalstats, logout, okNotify } from '../../js/utils';
import { ChangePassword, GeneralStats, update_freq } from '../../js/models';
2022-06-11 21:57:50 +02:00
import { BsPlusLg } from "react-icons/bs"
import { AiFillHome } from "react-icons/ai"
2022-06-12 12:09:50 +02:00
import { useLocation, useNavigate, useParams } from 'react-router-dom';
import AddNewRegex from '../AddNewRegex';
import AddNewService from '../AddNewService';
2022-06-13 16:12:52 +02:00
import { MdSettings } from 'react-icons/md';
import { FaLock } from 'react-icons/fa';
import { ImCross, ImExit } from 'react-icons/im';
import { useForm } from '@mantine/hooks';
2022-06-12 16:44:54 +02:00
2022-06-11 21:57:50 +02:00
function Header() {
2022-06-12 19:03:56 +02:00
const [generalStats, setGeneralStats] = useState<GeneralStats>({closed:0, regexes:0, services:0});
2022-06-11 21:57:50 +02:00
const location = useLocation()
const navigator = useNavigate()
2022-06-12 12:09:50 +02:00
const updateInfo = () => {
generalstats().then(res => {
setGeneralStats(res)
}).catch(
2022-06-12 16:44:54 +02:00
err => errorNotify("General Info Auto-Update failed!", err.toString())
)
2022-06-11 21:57:50 +02:00
}
2022-06-12 12:09:50 +02:00
useEffect(()=>{
updateInfo()
const updater = setInterval(updateInfo, update_freq)
return () => { clearInterval(updater) }
}, []);
2022-06-13 16:12:52 +02:00
const logout_action = () => {
logout().then(r => {
window.location.reload()
}).catch(r => {
errorNotify("Logout failed!",`Error: ${r}`)
})
}
const form = useForm({
initialValues: {
password:"",
expire:true
},
validationRules:{
password: (value) => value !== ""
}
})
const [loadingBtn, setLoadingBtn] = useState(false)
const [error, setError] = useState<null|string>(null)
const [changePasswordModal, setChangePasswordModal] = useState(false);
const submitRequest = async (values:ChangePassword) => {
setLoadingBtn(true)
await changepassword(values).then(res => {
if(!res){
okNotify("Password change done!","The password of the firewall has been changed!")
setChangePasswordModal(false)
form.reset()
}else{
setError(res)
}
}).catch( err => setError(err.toString()))
setLoadingBtn(false)
}
2022-06-12 12:09:50 +02:00
const {srv_id} = useParams()
2022-06-13 16:12:52 +02:00
2022-06-12 12:09:50 +02:00
const [open, setOpen] = useState(false);
const closeModal = () => {setOpen(false);}
2022-06-11 21:57:50 +02:00
return <div id="header-page" className={style.header}>
2022-06-12 22:28:16 +02:00
<div style={{ width: 240, marginLeft: 'auto', marginRight: 'auto', padding:"40px" }}>
<Image src="/header-logo.png" alt="Firegex logo" />
</div>
2022-06-11 21:57:50 +02:00
<div className="flex-spacer" />
<Badge color="green" size="lg" variant="filled">Services: {generalStats.services}</Badge>
<Badge style={{marginLeft:"10px"}} size="lg" color="yellow" variant="filled">Filtered Connections: {generalStats.closed}</Badge>
2022-06-12 19:03:56 +02:00
<Badge style={{marginLeft:"10px"}} size="lg" color="violet" variant="filled">Regexes: {generalStats.regexes}</Badge>
2022-06-11 21:57:50 +02:00
<div style={{marginLeft:"20px"}}></div>
2022-06-13 16:12:52 +02:00
<Menu>
<Menu.Label>Firewall Access</Menu.Label>
<Menu.Item icon={<ImExit size={14} />} onClick={logout_action}>Logout</Menu.Item>
<Divider />
<Menu.Item color="red" icon={<FaLock size={14} />} onClick={() => setChangePasswordModal(true)}>Change Password</Menu.Item>
</Menu>
<div style={{marginLeft:"20px"}}></div>
2022-06-11 21:57:50 +02:00
{ location.pathname !== "/"?
<ActionIcon color="teal" style={{marginRight:"10px"}}
size="xl" radius="md" variant="filled"
onClick={()=>navigator("/")}>
<AiFillHome size="25px" />
</ActionIcon>
:null}
2022-06-12 12:09:50 +02:00
<ActionIcon color="blue" onClick={()=>setOpen(true)} size="xl" radius="md" variant="filled"><BsPlusLg size="20px" /></ActionIcon>
{srv_id?
<Modal size="xl" title="Add a new regex filter" opened={open} onClose={closeModal} closeOnClickOutside={false} centered>
<AddNewRegex closePopup={closeModal} service={srv_id} />
</Modal>:
<Modal size="xl" title="Add a new service" opened={open} onClose={closeModal} closeOnClickOutside={false} centered>
<AddNewService closePopup={closeModal} />
</Modal>
}
2022-06-13 16:12:52 +02:00
<Modal size="xl" title="Change Firewall Password" opened={changePasswordModal} onClose={()=>setChangePasswordModal(false)} closeOnClickOutside={false} centered>
<form onSubmit={form.onSubmit(submitRequest)}>
<Space h="md" />
<TextInput
label="New Password"
placeholder="$3cr3t"
{...form.getInputProps('password')}
/>
<Space h="md" />
<Switch
label="Expire the login status to all connections"
{...form.getInputProps('expire', { type: 'checkbox' })}
/>
<Space h="md" />
<Group position="right" mt="md">
<Button loading={loadingBtn} type="submit">Change Password</Button>
</Group>
</form>
<Space h="xl" />
{error?<>
<Notification icon={<ImCross size={14} />} color="red" onClose={()=>{setError(null)}}>
Error: {error}
</Notification><Space h="md" /></>:null}
</Modal>
2022-06-11 21:57:50 +02:00
<div style={{marginLeft:"40px"}}></div>
</div>
}
export default Header;