2024-10-13 01:50:52 +02:00
import { ActionIcon , Badge , Box , Divider , Grid , Menu , Space , Title , Tooltip } from '@mantine/core' ;
2023-09-24 05:48:54 +02:00
import { useState } from 'react' ;
2022-07-07 21:08:02 +02:00
import { FaPlay , FaStop } from 'react-icons/fa' ;
2023-09-24 05:48:54 +02:00
import { nfregex , Service , serviceQueryKey } from '../utils' ;
2022-06-11 21:57:50 +02:00
import { MdOutlineArrowForwardIos } from "react-icons/md"
2022-07-21 10:20:54 +02:00
import YesNoModal from '../../YesNoModal' ;
2023-10-12 20:01:48 +02:00
import { errorNotify , isMediumScreen , 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' ;
2023-09-24 05:48:54 +02:00
import { useQueryClient } from '@tanstack/react-query' ;
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
2023-09-24 05:48:54 +02:00
const queryClient = useQueryClient ( )
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 )
2023-10-12 20:01:48 +02:00
const isMedium = isMediumScreen ( )
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! ` )
2023-09-24 05:48:54 +02:00
queryClient . invalidateQueries ( serviceQueryKey )
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! ` )
2023-09-24 05:48:54 +02:00
queryClient . invalidateQueries ( serviceQueryKey )
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! ` )
2023-09-24 05:48:54 +02:00
queryClient . invalidateQueries ( serviceQueryKey )
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 < >
2024-10-13 01:50:52 +02:00
< Box className = 'firegex__nfregex__rowbox' >
< Grid className = "firegex__nfregex__row" justify = "flex-end" style = { { width : "100%" } } >
< Grid.Col span = { { md :4 , xs : 12 } } >
2023-10-12 20:01:48 +02:00
2024-10-13 01:50:52 +02:00
< Box className = { "center-flex-row" } >
< Title className = "firegex__nfregex__name" >
2024-06-28 18:03:15 +02:00
{ service . name }
< / Title >
2024-10-13 01:50:52 +02:00
< Box className = "center-flex" style = { { gap : 6 } } >
< Badge color = { status_color } radius = "md" size = "lg" variant = "filled" > Status : < u > { service . status } < / u > < / Badge >
< Badge size = "lg" gradient = { { from : 'indigo' , to : 'cyan' } } variant = "gradient" radius = "md" >
: { service . port }
< / Badge >
< / Box >
{ isMedium ? null : < Space w = "xl" / > }
< / Box >
< / Grid.Col >
2022-06-15 01:23:54 +02:00
2024-10-13 01:50:52 +02:00
< Grid.Col className = { isMedium ? "center-flex" : "center-flex-row" } span = { { md :8 , xs : 12 } } >
< Box visibleFrom = 'md' className = 'flex-spacer' / >
< Space hiddenFrom = 'md' h = "md" / >
< Space hiddenFrom = 'md' w = "xl" / >
< Space hiddenFrom = 'md' w = "md" / >
< Box className = "center-flex-row" >
< Badge color = "yellow" radius = "sm" size = "md" variant = "filled" > Connections Blocked : { service . n_packets } < / Badge >
< Space h = "xs" / >
< Badge color = "violet" radius = "sm" size = "md" variant = "filled" > Regex : { service . n_regex } < / Badge >
< Space h = "xs" / >
< Badge color = { service . ip_int . match ( regex_ipv4 ) ? "cyan" : "pink" } radius = "sm" size = "md" variant = "filled" > { service . ip_int } on { service . proto } < / Badge >
< / Box >
{ isMedium ? < Box className = 'flex-spacer' / > : < Space h = "xl" / > }
< Box className = "center-flex" >
< MenuDropDownWithButton >
< Menu.Label > < b > Rename service < / b > < / Menu.Label >
< Menu.Item leftSection = { < BiRename size = { 18 } / > } onClick = { ( ) = > setRenameModal ( true ) } > Change service name < / Menu.Item >
< Divider / >
< Menu.Label > < b > Danger zone < / b > < / Menu.Label >
< Menu.Item color = "red" leftSection = { < BsTrashFill size = { 18 } / > } onClick = { ( ) = > setDeleteModal ( true ) } > Delete Service < / Menu.Item >
< / MenuDropDownWithButton >
< Space w = "md" / >
< Tooltip label = "Stop service" zIndex = { 0 } color = "red" opened = { tooltipStopOpened } >
< ActionIcon color = "red" loading = { buttonLoading }
onClick = { stopService } size = "xl" radius = "md" variant = "filled"
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 >
< Space w = "md" / >
< Tooltip label = "Start service" zIndex = { 0 } color = "teal" >
< 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 >
{ isMedium ? < Space w = "xl" / > : < Space w = "md" / > }
{ onClick ? < Box style = { { backgroundColor : "var(--secondary_color)" , borderRadius : "38%" , width : "35px" , height : "35px" , display : "flex" , justifyContent : "center" , alignItems : "center" , border : "#AAA 2px solid" } } >
< MdOutlineArrowForwardIos onClick = { onClick } style = { { cursor : "pointer" } } size = { 25 } / >
< / Box > : null }
{ isMedium ? < Space w = "xl" / > : null }
< / Box >
< / Grid.Col >
< / Grid >
< / Box >
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 ;