2022-06-15 01:23:54 +02:00
import { ActionIcon , Badge , Grid , MediaQuery , Space , Title , Tooltip } from '@mantine/core' ;
2022-06-12 16:44:54 +02:00
import React , { useState } from 'react' ;
2022-06-12 12:09:50 +02:00
import { FaPause , FaPlay , FaStop } from 'react-icons/fa' ;
2022-06-11 21:57:50 +02:00
import { Service } from '../../js/models' ;
import { MdOutlineArrowForwardIos } from "react-icons/md"
import style from "./ServiceRow.module.scss" ;
2022-06-12 16:44:54 +02:00
import YesNoModal from '../YesNoModal' ;
2022-06-15 19:44:41 +02:00
import { errorNotify , fireUpdateRequest , okNotify , pauseservice , startservice , stopservice } from '../../js/utils' ;
2022-06-11 21:57:50 +02:00
//"status":"stop"/"wait"/"active"/"pause",
2022-06-15 01:23:54 +02:00
function ServiceRow ( { service , onClick , additional_buttons } : { service :Service , onClick ? : ( ) = > void , additional_buttons? :any } ) {
2022-06-11 21:57:50 +02:00
let status_color = "gray" ;
switch ( service . status ) {
case "stop" : status_color = "red" ; break ;
case "wait" : status_color = "yellow" ; break ;
case "active" : status_color = "teal" ; break ;
case "pause" : status_color = "cyan" ; break ;
}
2022-06-12 16:44:54 +02:00
const [ stopModal , setStopModal ] = useState ( false ) ;
const [ buttonLoading , setButtonLoading ] = useState ( false )
2022-06-16 02:25:41 +02:00
const [ tooltipStopOpened , setTooltipStopOpened ] = useState ( false ) ;
2022-06-12 16:44:54 +02:00
2022-06-13 10:59:05 +02:00
const stopService = async ( ) = > {
2022-06-12 16:44:54 +02:00
setButtonLoading ( true )
2022-06-13 10:59:05 +02:00
await stopservice ( service . id ) . then ( res = > {
if ( ! res ) {
okNotify ( ` Service ${ service . id } stopped successfully! ` , ` The service ${ service . name } has been stopped! ` )
2022-06-15 19:44:41 +02:00
fireUpdateRequest ( ) ;
2022-06-13 10:59:05 +02:00
} else {
errorNotify ( ` An error as occurred during the stopping of the service ${ service . id } ` , ` Error: ${ res } ` )
}
} ) . catch ( err = > {
errorNotify ( ` An error as occurred during the stopping of the service ${ service . id } ` , ` Error: ${ err } ` )
} )
2022-06-15 16:18:19 +02:00
setButtonLoading ( false ) ;
2022-06-12 16:44:54 +02:00
}
2022-06-13 10:59:05 +02:00
const startService = async ( ) = > {
2022-06-12 16:44:54 +02:00
setButtonLoading ( true )
2022-06-13 10:59:05 +02:00
await startservice ( service . id ) . then ( res = > {
if ( ! res ) {
okNotify ( ` Service ${ service . id } started successfully! ` , ` The service ${ service . name } has been started! ` )
2022-06-15 19:44:41 +02:00
fireUpdateRequest ( ) ;
2022-06-13 10:59:05 +02:00
} else {
errorNotify ( ` An error as occurred during the starting of the service ${ service . id } ` , ` Error: ${ res } ` )
}
} ) . catch ( err = > {
errorNotify ( ` An error as occurred during the starting of the service ${ service . id } ` , ` Error: ${ err } ` )
} )
2022-06-12 16:44:54 +02:00
setButtonLoading ( false )
}
2022-06-13 10:59:05 +02:00
const pauseService = async ( ) = > {
2022-06-12 16:44:54 +02:00
setButtonLoading ( true )
2022-06-13 10:59:05 +02:00
await pauseservice ( service . id ) . then ( res = > {
if ( ! res ) {
okNotify ( ` Service ${ service . id } paused successfully! ` , ` The service ${ service . name } has been paused (Transparent mode)! ` )
2022-06-15 19:44:41 +02:00
fireUpdateRequest ( ) ;
2022-06-13 10:59:05 +02:00
} else {
errorNotify ( ` An error as occurred during the pausing of the service ${ service . id } ` , ` Error: ${ res } ` )
}
} ) . catch ( err = > {
errorNotify ( ` An error as occurred during the pausing of the service ${ service . id } ` , ` Error: ${ err } ` )
} )
2022-06-12 16:44:54 +02:00
setButtonLoading ( false )
2022-06-15 16:34:55 +02:00
2022-06-12 16:44:54 +02:00
}
2022-06-11 21:57:50 +02:00
return < >
2022-06-15 01:23:54 +02:00
< Grid className = { style . row } justify = "flex-end" style = { { width : "100%" } } >
< Grid.Col md = { 4 } xs = { 12 } >
< MediaQuery smallerThan = "md" styles = { { display : 'none' } } > < div >
< div className = "center-flex-row" >
< div className = "center-flex" > < Title className = { style . name } > { service . name } < / Title > < Badge size = "xl" gradient = { { from : 'indigo' , to : 'cyan' } } variant = "gradient" > : { service . public_port } < / Badge > < / div >
< Badge color = { status_color } size = "xl" radius = "md" > { service . internal_port } { "->" } { service . public_port } < / Badge >
< / div >
< / div > < / MediaQuery >
< MediaQuery largerThan = "md" styles = { { display : 'none' } } > < div >
< div className = "center-flex" >
< div className = "center-flex" > < Title className = { style . name } > { service . name } < / Title > < Badge size = "xl" gradient = { { from : 'indigo' , to : 'cyan' } } variant = "gradient" > : { service . public_port } < / Badge > < / div >
< Space w = "xl" / >
< Badge color = { status_color } size = "xl" radius = "md" > { service . internal_port } { "->" } { service . public_port } < / Badge >
< / div >
< / div > < / MediaQuery >
< MediaQuery largerThan = "md" styles = { { display : 'none' } } >
< Space h = "xl" / >
< / MediaQuery >
2022-06-11 21:57:50 +02:00
< / Grid.Col >
2022-06-15 01:23:54 +02:00
< Grid.Col className = "center-flex" md = { 8 } xs = { 12 } >
< MediaQuery smallerThan = "md" styles = { { display : 'none' } } >
< div className = 'flex-spacer' / >
< / MediaQuery >
< MediaQuery largerThan = "md" styles = { { display : 'none' } } >
< > < Space w = "xl" / > < Space w = "xl" / > < / >
< / MediaQuery >
2022-06-11 21:57:50 +02:00
< div className = "center-flex-row" >
< Badge style = { { marginBottom : "20px" } } color = { status_color } radius = "sm" size = "xl" variant = "filled" > Status : < u > { service . status } < / u > < / Badge >
< Badge style = { { marginBottom : "8px" } } color = "violet" radius = "sm" size = "lg" variant = "filled" > Regex : { service . n_regex } < / Badge >
< Badge color = "yellow" radius = "sm" size = "lg" variant = "filled" > Connections Blocked : { service . n_packets } < / Badge >
< / div >
2022-06-15 01:23:54 +02:00
< MediaQuery largerThan = "md" styles = { { display : 'none' } } >
< div className = 'flex-spacer' / >
< / MediaQuery >
< MediaQuery smallerThan = "md" styles = { { display : 'none' } } >
< > < Space w = "xl" / > < Space w = "xl" / > < / >
< / MediaQuery >
2022-06-11 21:57:50 +02:00
< div className = "center-flex" >
2022-06-12 14:40:48 +02:00
{ additional_buttons }
2022-06-13 10:59:05 +02:00
{ [ "pause" , "wait" ] . includes ( service . status ) ?
2022-06-14 22:43:48 +02:00
2022-06-16 02:25:41 +02:00
< Tooltip label = "Stop service" zIndex = { 0 } transition = "pop" transitionDuration = { 200 } /*openDelay={500}*/ transitionTimingFunction = "ease" color = "orange" opened = { tooltipStopOpened } tooltipId = "tooltip-stop-id" >
2022-06-14 22:43:48 +02:00
< ActionIcon color = "yellow" loading = { buttonLoading }
onClick = { ( ) = > setStopModal ( true ) } size = "xl" radius = "md" variant = "filled"
2022-06-16 02:25:41 +02:00
disabled = { service . status === "stop" }
aria - describedby = "tooltip-stop-id"
onFocus = { ( ) = > setTooltipStopOpened ( false ) } onBlur = { ( ) = > setTooltipStopOpened ( false ) }
onMouseEnter = { ( ) = > setTooltipStopOpened ( true ) } onMouseLeave = { ( ) = > setTooltipStopOpened ( false ) } >
2022-06-14 22:43:48 +02:00
< FaStop size = "20px" / >
< / ActionIcon >
< / Tooltip > :
2022-06-18 00:18:50 +02:00
< Tooltip label = { service . status === "stop" ? "Start in pause mode" : "Pause service" } zIndex = { 0 } transition = "pop" transitionDuration = { 200 } /*openDelay={500}*/ transitionTimingFunction = "ease" color = { service . status === "stop" ? "cyan" : "red" } >
< ActionIcon color = { service . status === "stop" ? "cyan" : "red" } loading = { buttonLoading }
2022-06-14 22:43:48 +02:00
onClick = { pauseService } size = "xl" radius = "md" variant = "filled"
2022-06-18 00:18:50 +02:00
/*disabled={service.status === "stop"}*/ >
2022-06-14 22:43:48 +02:00
< FaPause size = "20px" / >
< / ActionIcon >
< / Tooltip >
2022-06-13 10:59:05 +02:00
}
2022-06-11 21:57:50 +02:00
< Space w = "md" / >
2022-06-16 02:25:41 +02:00
< Tooltip label = "Start service" transition = "pop" zIndex = { 0 } transitionDuration = { 200 } /*openDelay={500}*/ transitionTimingFunction = "ease" color = "teal" >
2022-06-14 22:43:48 +02:00
< ActionIcon color = "teal" size = "xl" radius = "md" onClick = { startService } loading = { buttonLoading }
variant = "filled" disabled = { ! [ "stop" , "pause" ] . includes ( service . status ) ? true : false } >
< FaPlay size = "20px" / >
< / ActionIcon >
< / Tooltip >
2022-06-11 21:57:50 +02:00
< / div >
< Space w = "xl" / > < Space w = "xl" / >
2022-06-15 01:23:54 +02:00
{ onClick ? < div >
< MdOutlineArrowForwardIos onClick = { onClick } style = { { cursor : "pointer" } } size = { 45 } / >
< Space w = "xl" / >
< / div > : null }
< MediaQuery largerThan = "md" styles = { { display : 'none' } } >
< > < Space w = "xl" / > < Space w = "xl" / > < / >
< / MediaQuery >
2022-06-11 21:57:50 +02:00
< / Grid.Col >
< / Grid >
2022-06-12 16:44:54 +02:00
< YesNoModal
2022-06-14 22:43:48 +02:00
title = 'Are you sure to stop this service?'
2022-06-16 02:25:41 +02:00
description = { ` You are going to delete the service ' ${ service . id } ', causing the firewall to stop. This will cause the shutdown of your service! ⚠️ ` }
2022-06-15 19:44:41 +02:00
onClose = { ( ) = > { setStopModal ( false ) ; } }
2022-06-12 16:44:54 +02:00
action = { stopService }
opened = { stopModal }
/ >
2022-06-11 21:57:50 +02:00
< hr style = { { width : "100%" } } / >
< / >
}
export default ServiceRow ;