2022-06-30 15:58:03 +02:00
import { ActionIcon , Badge , Divider , Grid , MediaQuery , Menu , Space , Title , Tooltip } from '@mantine/core' ;
2022-06-12 16:44:54 +02:00
import React , { useState } from 'react' ;
2022-07-07 21:08:02 +02:00
import { FaPlay , FaStop } from 'react-icons/fa' ;
2022-07-21 21:17:06 +02:00
import { nfregex , Service } from '../utils' ;
2022-06-11 21:57:50 +02:00
import { MdOutlineArrowForwardIos } from "react-icons/md"
2022-07-08 13:13:30 +02:00
import style from "./index.module.scss" ;
2022-07-21 10:20:54 +02:00
import YesNoModal from '../../YesNoModal' ;
2022-07-21 21:17:06 +02:00
import { errorNotify , okNotify , regex_ipv4 } from '../../../js/utils' ;
2022-07-07 21:08:02 +02:00
import { BsTrashFill } from 'react-icons/bs' ;
2022-06-30 15:58:03 +02:00
import { BiRename } from 'react-icons/bi'
2022-07-08 13:13:30 +02:00
import RenameForm from './RenameForm' ;
2023-06-14 21:49:15 +02:00
import { MenuDropDownWithButton } from '../../MainLayout' ;
2022-06-11 21:57:50 +02:00
2022-06-30 15:58:03 +02:00
function ServiceRow ( { service , onClick } : { service :Service , onClick ? : ( ) = > void } ) {
2022-06-11 21:57:50 +02:00
let status_color = "gray" ;
switch ( service . status ) {
case "stop" : status_color = "red" ; break ;
case "active" : status_color = "teal" ; break ;
}
2022-06-12 16:44:54 +02:00
const [ buttonLoading , setButtonLoading ] = useState ( false )
2022-06-16 02:25:41 +02:00
const [ tooltipStopOpened , setTooltipStopOpened ] = useState ( false ) ;
2022-06-30 15:58:03 +02:00
const [ deleteModal , setDeleteModal ] = useState ( false )
2022-07-08 13:13:30 +02:00
const [ renameModal , setRenameModal ] = 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-07-21 10:20:54 +02:00
await nfregex . servicestop ( service . service_id ) . then ( res = > {
2022-06-13 10:59:05 +02:00
if ( ! res ) {
2022-07-07 22:36:56 +02:00
okNotify ( ` Service ${ service . name } stopped successfully! ` , ` The service on ${ service . port } has been stopped! ` )
2022-06-13 10:59:05 +02:00
} else {
2022-07-07 21:08:02 +02:00
errorNotify ( ` An error as occurred during the stopping of the service ${ service . port } ` , ` Error: ${ res } ` )
2022-06-13 10:59:05 +02:00
}
} ) . catch ( err = > {
2022-07-07 21:08:02 +02:00
errorNotify ( ` An error as occurred during the stopping of the service ${ service . port } ` , ` Error: ${ err } ` )
2022-06-13 10:59:05 +02:00
} )
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-07-21 10:20:54 +02:00
await nfregex . servicestart ( service . service_id ) . then ( res = > {
2022-06-13 10:59:05 +02:00
if ( ! res ) {
2022-07-07 22:36:56 +02:00
okNotify ( ` Service ${ service . name } started successfully! ` , ` The service on ${ service . port } has been started! ` )
2022-06-13 10:59:05 +02:00
} else {
2022-07-07 21:08:02 +02:00
errorNotify ( ` An error as occurred during the starting of the service ${ service . port } ` , ` Error: ${ res } ` )
2022-06-13 10:59:05 +02:00
}
} ) . catch ( err = > {
2022-07-07 21:08:02 +02:00
errorNotify ( ` An error as occurred during the starting of the service ${ service . port } ` , ` Error: ${ err } ` )
2022-06-13 10:59:05 +02:00
} )
2022-06-12 16:44:54 +02:00
setButtonLoading ( false )
}
2022-06-30 15:58:03 +02:00
const deleteService = ( ) = > {
2022-07-21 10:20:54 +02:00
nfregex . servicedelete ( service . service_id ) . then ( res = > {
2022-06-30 15:58:03 +02:00
if ( ! res ) {
2022-07-07 22:36:56 +02:00
okNotify ( "Service delete complete!" , ` The service ${ service . name } has been deleted! ` )
2022-06-30 15:58:03 +02:00
} else
errorNotify ( "An error occurred while deleting a service" , ` Error: ${ res } ` )
} ) . catch ( err = > {
errorNotify ( "An error occurred while deleting a service" , ` Error: ${ err } ` )
} )
}
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" >
2022-07-07 21:08:02 +02:00
< div className = "center-flex" > < Title className = { style . name } > { service . name } < / Title > < Badge size = "xl" gradient = { { from : 'indigo' , to : 'cyan' } } variant = "gradient" > : { service . port } < / Badge > < / div >
2022-07-07 23:34:39 +02:00
< Badge color = { status_color } radius = "sm" size = "lg" variant = "filled" > Status : < u > { service . status } < / u > < / Badge >
2022-06-15 01:23:54 +02:00
< / div >
< / div > < / MediaQuery >
< MediaQuery largerThan = "md" styles = { { display : 'none' } } > < div >
< div className = "center-flex" >
2022-07-07 21:08:02 +02:00
< div className = "center-flex" > < Title className = { style . name } > { service . name } < / Title > < Badge size = "xl" gradient = { { from : 'indigo' , to : 'cyan' } } variant = "gradient" > : { service . port } < / Badge > < / div >
2022-07-07 23:34:39 +02:00
< Badge style = { { marginLeft : "20px" } } color = { status_color } radius = "sm" size = "lg" variant = "filled" > Status : < u > { service . status } < / u > < / Badge >
2022-06-15 01:23:54 +02:00
< Space w = "xl" / >
< / 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" >
2022-07-10 15:05:56 +02:00
< Badge color = "yellow" radius = "sm" size = "md" variant = "filled" > Connections Blocked : { service . n_packets } < / Badge >
< Space h = "xs" / >
2022-07-07 23:34:39 +02:00
< Badge color = "violet" radius = "sm" size = "md" variant = "filled" > Regex : { service . n_regex } < / Badge >
2022-07-10 15:05:56 +02:00
< Space h = "xs" / >
2022-07-19 15:32:37 +02:00
< Badge color = { service . ip_int . match ( regex_ipv4 ) ? "cyan" : "pink" } radius = "sm" size = "md" variant = "filled" > { service . ip_int } on { service . proto } < / Badge >
2022-06-11 21:57:50 +02:00
< / 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" >
2023-06-14 21:49:15 +02:00
< MenuDropDownWithButton >
2022-06-30 15:58:03 +02:00
< Menu.Label > < b > Rename service < / b > < / Menu.Label >
2022-07-08 13:13:30 +02:00
< Menu.Item icon = { < BiRename size = { 18 } / > } onClick = { ( ) = > setRenameModal ( true ) } > Change service name < / Menu.Item >
2022-06-30 15:58:03 +02:00
< Divider / >
2022-06-30 19:21:14 +02:00
< Menu.Label > < b > Danger zone < / b > < / Menu.Label >
2022-06-30 15:58:03 +02:00
< Menu.Item color = "red" icon = { < BsTrashFill size = { 18 } / > } onClick = { ( ) = > setDeleteModal ( true ) } > Delete Service < / Menu.Item >
2023-06-14 21:49:15 +02:00
< / MenuDropDownWithButton >
2022-07-07 21:08:02 +02:00
< Space w = "md" / >
2023-06-05 00:55:38 +02:00
< Tooltip label = "Stop service" zIndex = { 0 } color = "red" opened = { tooltipStopOpened } >
2022-07-07 21:08:02 +02:00
< ActionIcon color = "red" loading = { buttonLoading }
2022-07-07 22:36:56 +02:00
onClick = { stopService } size = "xl" radius = "md" variant = "filled"
2022-07-07 21:08:02 +02:00
disabled = { service . status === "stop" }
aria - describedby = "tooltip-stop-id"
onFocus = { ( ) = > setTooltipStopOpened ( false ) } onBlur = { ( ) = > setTooltipStopOpened ( false ) }
onMouseEnter = { ( ) = > setTooltipStopOpened ( true ) } onMouseLeave = { ( ) = > setTooltipStopOpened ( false ) } >
< FaStop size = "20px" / >
< / ActionIcon >
< / Tooltip >
2022-06-11 21:57:50 +02:00
< Space w = "md" / >
2023-06-05 00:55:38 +02:00
< Tooltip label = "Start service" zIndex = { 0 } 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 >
< hr style = { { width : "100%" } } / >
2022-06-30 15:58:03 +02:00
< YesNoModal
title = 'Are you sure to delete this service?'
2022-07-07 21:08:02 +02:00
description = { ` You are going to delete the service ' ${ service . port } ', causing the stopping of the firewall and deleting all the regex associated. This will cause the shutdown of your service! ⚠️ ` }
2022-06-30 15:58:03 +02:00
onClose = { ( ) = > setDeleteModal ( false ) }
action = { deleteService }
opened = { deleteModal }
/ >
2022-07-08 13:13:30 +02:00
< RenameForm
onClose = { ( ) = > setRenameModal ( false ) }
opened = { renameModal }
service = { service }
/ >
2022-06-11 21:57:50 +02:00
< / >
}
export default ServiceRow ;