2022-06-15 19:44:41 +02:00
import React , { useState } from 'react' ;
2022-06-15 01:23:54 +02:00
import { ActionIcon , Badge , Button , Divider , Group , Image , Menu , Modal , Notification , Space , Switch , TextInput , Tooltip , FloatingTooltip , MediaQuery } from '@mantine/core' ;
2022-06-11 21:57:50 +02:00
import style from "./Header.module.scss" ;
2022-06-15 19:44:41 +02:00
import { changepassword , errorNotify , eventUpdateName , generalstats , logout , okNotify } from '../../js/utils' ;
import { ChangePassword , GeneralStats } 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 { FaLock } from 'react-icons/fa' ;
import { ImCross , ImExit } from 'react-icons/im' ;
2022-06-15 19:44:41 +02:00
import { useForm , useWindowEvent } 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-15 16:34:55 +02:00
const updateInfo = ( ) = > {
generalstats ( ) . then ( res = > {
setGeneralStats ( res )
} ) . catch (
err = > errorNotify ( "General Info Auto-Update failed!" , err . toString ( ) )
)
2022-06-11 21:57:50 +02:00
}
2022-06-15 19:44:41 +02:00
useWindowEvent ( eventUpdateName , updateInfo )
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 ( )
const [ open , setOpen ] = useState ( false ) ;
2022-06-15 19:44:41 +02:00
const closeModal = ( ) = > { setOpen ( false ) ; }
2022-06-11 21:57:50 +02:00
return < div id = "header-page" className = { style . header } >
2022-06-15 01:52:33 +02:00
< FloatingTooltip zIndex = { 0 } label = "Home" transition = "pop" transitionDuration = { 200 } openDelay = { 1000 } transitionTimingFunction = "ease" color = "dark" position = "right" >
2022-06-14 22:43:48 +02:00
< div style = { { width : 240 , marginLeft : 'auto' , marginRight : 'auto' , padding : "40px" , cursor : 'pointer' } } >
< Image src = "/header-logo.png" alt = "Firegex logo" onClick = { ( ) = > navigator ( "/" ) } / >
< / div >
< / FloatingTooltip >
2022-06-11 21:57:50 +02:00
< div className = "flex-spacer" / >
2022-06-15 01:23:54 +02:00
< MediaQuery largerThan = "md" styles = { { display : 'none' } } >
< div >
< Badge color = "green" size = "lg" variant = "filled" > Services : { generalStats . services } < / Badge >
< Space h = "xs" / >
< Badge size = "lg" color = "yellow" variant = "filled" > Filtered Connections : { generalStats . closed } < / Badge >
< Space h = "xs" / >
< Badge size = "lg" color = "violet" variant = "filled" > Regexes : { generalStats . regexes } < / Badge >
< / div >
< / MediaQuery >
< MediaQuery smallerThan = "md" styles = { { display : 'none' } } > < div >
< div className = "center-flex" >
< Badge color = "green" size = "lg" variant = "filled" > Services : { generalStats . services } < / Badge >
< Space w = "xs" / >
< Badge size = "lg" color = "yellow" variant = "filled" > Filtered Connections : { generalStats . closed } < / Badge >
< Space w = "xs" / >
< Badge size = "lg" color = "violet" variant = "filled" > Regexes : { generalStats . regexes } < / Badge >
< / div >
< / div > < / MediaQuery >
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 !== "/" ?
2022-06-15 01:52:33 +02:00
< Tooltip zIndex = { 0 } label = "Home" position = 'left' transition = "pop" transitionDuration = { 200 } openDelay = { 500 } transitionTimingFunction = "ease" color = "teal" >
2022-06-11 21:57:50 +02:00
< ActionIcon color = "teal" style = { { marginRight : "10px" } }
size = "xl" radius = "md" variant = "filled"
onClick = { ( ) = > navigator ( "/" ) } >
< AiFillHome size = "25px" / >
< / ActionIcon >
2022-06-14 22:43:48 +02:00
< / Tooltip >
2022-06-11 21:57:50 +02:00
: null }
2022-06-15 01:23:54 +02:00
{ srv_id ?
2022-06-15 01:52:33 +02:00
< Tooltip label = "Add a new regex" zIndex = { 0 } position = 'left' transition = "pop" transitionDuration = { 200 } openDelay = { 500 } transitionTimingFunction = "ease" color = "blue" >
2022-06-14 22:43:48 +02:00
< ActionIcon color = "blue" onClick = { ( ) = > setOpen ( true ) } size = "xl" radius = "md" variant = "filled" > < BsPlusLg size = "20px" / > < / ActionIcon >
< / Tooltip >
2022-06-15 01:52:33 +02:00
: < Tooltip label = "Add a new service" zIndex = { 0 } position = 'left' transition = "pop" transitionDuration = { 200 } openDelay = { 500 } transitionTimingFunction = "ease" color = "blue" >
2022-06-14 22:43:48 +02:00
< ActionIcon color = "blue" onClick = { ( ) = > setOpen ( true ) } size = "xl" radius = "md" variant = "filled" > < BsPlusLg size = "20px" / > < / ActionIcon >
< / Tooltip >
}
2022-06-12 12:09:50 +02:00
{ srv_id ?
2022-06-15 01:23:54 +02:00
< AddNewRegex opened = { open } onClose = { closeModal } service = { srv_id } / > :
< AddNewService opened = { open } onClose = { closeModal } / >
2022-06-12 12:09:50 +02:00
}
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 ;