2022-06-11 21:57:50 +02:00
import { ActionIcon , Badge , Grid , Space , Title } 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-13 10:59:05 +02:00
import { errorNotify , okNotify , pauseservice , startservice , stopservice } from '../../js/utils' ;
2022-06-11 21:57:50 +02:00
//"status":"stop"/"wait"/"active"/"pause",
2022-06-12 14:40:48 +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-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! ` )
} 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-12 16:44:54 +02:00
setButtonLoading ( false )
}
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! ` )
} 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)! ` )
} 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-11 21:57:50 +02:00
return < >
< Grid className = { style . row } style = { { width : "100%" } } >
< Grid.Col span = { 4 } >
< div className = "center-flex-row" >
2022-06-12 12:09:50 +02:00
< 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 >
2022-06-11 21:57:50 +02:00
< Badge color = { status_color } size = "xl" radius = "md" > { service . internal_port } { "->" } { service . public_port } < / Badge >
< / div >
< / Grid.Col >
< Grid.Col className = "center-flex" span = { 8 } >
< div className = 'flex-spacer' > < / div >
< 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 >
< Space w = "xl" / > < Space w = "xl" / >
< 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 ) ?
< ActionIcon color = "yellow" loading = { buttonLoading }
2022-06-13 18:44:11 +02:00
onClick = { ( ) = > setStopModal ( true ) } size = "xl" radius = "md" variant = "filled"
2022-06-13 10:59:05 +02:00
disabled = { service . status === "stop" } >
< FaStop size = "20px" / >
< / ActionIcon > :
< ActionIcon color = "red" loading = { buttonLoading }
2022-06-12 16:44:54 +02:00
onClick = { pauseService } size = "xl" radius = "md" variant = "filled"
2022-06-13 10:59:05 +02:00
disabled = { service . status === "stop" } >
< FaPause size = "20px" / >
< / ActionIcon >
}
2022-06-11 21:57:50 +02:00
< Space w = "md" / >
2022-06-12 16:44:54 +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 >
2022-06-11 21:57:50 +02:00
< / div >
< Space w = "xl" / > < Space w = "xl" / >
{ onClick ? < MdOutlineArrowForwardIos onClick = { onClick } style = { { cursor : "pointer" } } size = "45px" / > : null }
< Space w = "xl" / >
< / Grid.Col >
< / Grid >
2022-06-12 16:44:54 +02:00
< YesNoModal
title = 'Are you sure to stop this service!'
description = { ` You are going to delete the service ' ${ service . id } ', causing the stopping of the firewall. This will cause the shutdown of your service ⚠️! ` }
onClose = { ( ) = > setStopModal ( false ) }
action = { stopService }
opened = { stopModal }
/ >
2022-06-11 21:57:50 +02:00
< hr style = { { width : "100%" } } / >
< / >
}
export default ServiceRow ;