2024-06-28 18:03:15 +02:00
import { Grid , Text , Title , Badge , Space , ActionIcon , Tooltip , Box } from '@mantine/core' ;
2023-09-24 05:48:54 +02:00
import { useState } from 'react' ;
2022-07-21 21:17:06 +02:00
import { RegexFilter } from '../../js/models' ;
import { b64decode , errorNotify , getapiobject , okNotify } from '../../js/utils' ;
2022-06-12 14:40:48 +02:00
import { BsTrashFill } from "react-icons/bs"
2022-07-21 21:17:06 +02:00
import YesNoModal from '../YesNoModal' ;
2022-06-30 15:58:03 +02:00
import { FaPause , FaPlay } from 'react-icons/fa' ;
2024-06-28 18:03:15 +02:00
import { useClipboard } from '@mantine/hooks' ;
2022-06-12 12:09:50 +02:00
function RegexView ( { regexInfo } : { regexInfo :RegexFilter } ) {
2022-06-12 14:40:48 +02:00
2022-06-12 16:44:54 +02:00
const mode_string = regexInfo . mode === "C" ? "C -> S" :
regexInfo . mode === "S" ? "S -> C" :
regexInfo . mode === "B" ? "S <-> C" : "🤔"
2022-06-12 14:40:48 +02:00
2022-07-01 02:29:28 +02:00
let regex_expr = b64decode ( regexInfo . regex ) ;
2022-06-12 14:40:48 +02:00
2022-06-12 16:44:54 +02:00
const [ deleteModal , setDeleteModal ] = useState ( false ) ;
2022-06-30 15:58:03 +02:00
const [ deleteTooltipOpened , setDeleteTooltipOpened ] = useState ( false ) ;
const [ statusTooltipOpened , setStatusTooltipOpened ] = useState ( false ) ;
2024-06-28 18:03:15 +02:00
const clipboard = useClipboard ( { timeout : 500 } ) ;
2022-06-12 16:44:54 +02:00
2022-06-13 10:59:05 +02:00
const deleteRegex = ( ) = > {
2022-07-21 21:17:06 +02:00
getapiobject ( ) . regexdelete ( regexInfo . id ) . then ( res = > {
2022-06-13 10:59:05 +02:00
if ( ! res ) {
okNotify ( ` Regex ${ regex_expr } deleted successfully! ` , ` Regex ' ${ regex_expr } ' ID: ${ regexInfo . id } has been deleted! ` )
} else {
errorNotify ( ` Regex ${ regex_expr } deleation failed! ` , ` Error: ${ res } ` )
}
} ) . catch ( err = > errorNotify ( ` Regex ${ regex_expr } deleation failed! ` , ` Error: ${ err } ` ) )
}
2022-06-30 15:58:03 +02:00
const changeRegexStatus = ( ) = > {
2022-07-21 21:17:06 +02:00
( regexInfo . active ? getapiobject ( ) . regexdisable :getapiobject ( ) . regexenable ) ( regexInfo . id ) . then ( res = > {
2022-06-30 15:58:03 +02:00
if ( ! res ) {
okNotify ( ` Regex ${ regex_expr } ${ regexInfo . active ? "deactivated" : "activated" } successfully! ` , ` Regex ' ${ regex_expr } ' ID: ${ regexInfo . id } has been ${ regexInfo . active ? "deactivated" : "activated" } ! ` )
} else {
errorNotify ( ` Regex ${ regex_expr } ${ regexInfo . active ? "deactivation" : "activation" } failed! ` , ` Error: ${ res } ` )
}
} ) . catch ( err = > errorNotify ( ` Regex ${ regex_expr } ${ regexInfo . active ? "deactivation" : "activation" } failed! ` , ` Error: ${ err } ` ) )
}
2024-10-13 01:50:52 +02:00
return < Box className = "firegex__regexview__box" >
2022-06-12 14:40:48 +02:00
< Grid >
2022-07-21 12:39:30 +02:00
< Grid.Col span = { 2 } className = "center-flex" >
< Title order = { 4 } > Regex : < / Title >
2022-06-12 14:40:48 +02:00
< / Grid.Col >
< Grid.Col span = { 8 } >
2024-10-13 01:50:52 +02:00
< Box className = "firegex__regexview__outer_regex_text" >
< Text className = "firegex__regexview__regex_text" onClick = { ( ) = > {
2024-06-28 18:03:15 +02:00
clipboard . copy ( regex_expr )
okNotify ( "Regex copied to clipboard!" , ` The regex ' ${ regex_expr } ' has been copied to the clipboard! ` )
} } > { regex_expr } < / Text >
< / Box >
2022-06-12 14:40:48 +02:00
< / Grid.Col >
2022-06-30 15:58:03 +02:00
< Grid.Col span = { 2 } className = 'center-flex' >
< Space w = "xs" / >
2023-06-05 00:55:38 +02:00
< Tooltip label = { regexInfo . active ? "Deactivate" : "Activate" } zIndex = { 0 } color = { regexInfo . active ? "orange" : "teal" } opened = { statusTooltipOpened } >
2022-06-30 15:58:03 +02:00
< ActionIcon color = { regexInfo . active ? "orange" : "teal" } onClick = { changeRegexStatus } size = "xl" radius = "md" variant = "filled"
onFocus = { ( ) = > setStatusTooltipOpened ( false ) } onBlur = { ( ) = > setStatusTooltipOpened ( false ) }
onMouseEnter = { ( ) = > setStatusTooltipOpened ( true ) } onMouseLeave = { ( ) = > setStatusTooltipOpened ( false ) }
> { regexInfo . active ? < FaPause size = "20px" / > : < FaPlay size = "20px" / > } < / ActionIcon >
< / Tooltip >
< Space w = "xs" / >
2023-06-05 00:55:38 +02:00
< Tooltip label = "Delete regex" zIndex = { 0 } color = "red" opened = { deleteTooltipOpened } >
2022-06-16 02:25:41 +02:00
< ActionIcon color = "red" onClick = { ( ) = > setDeleteModal ( true ) } size = "xl" radius = "md" variant = "filled"
2022-06-30 15:58:03 +02:00
onFocus = { ( ) = > setDeleteTooltipOpened ( false ) } onBlur = { ( ) = > setDeleteTooltipOpened ( false ) }
onMouseEnter = { ( ) = > setDeleteTooltipOpened ( true ) } onMouseLeave = { ( ) = > setDeleteTooltipOpened ( false ) }
2022-06-16 02:25:41 +02:00
> < BsTrashFill size = { 22 } / > < / ActionIcon >
2022-06-14 22:43:48 +02:00
< / Tooltip >
2022-06-30 15:58:03 +02:00
2022-06-14 22:43:48 +02:00
< / Grid.Col >
2022-07-21 12:53:27 +02:00
< Grid.Col className = 'center-flex' span = { 12 } >
2024-10-13 01:50:52 +02:00
< Box className = 'center-flex-row' >
2022-07-21 12:53:27 +02:00
< Space h = "md" / >
2024-10-13 01:50:52 +02:00
< Box className = 'center-flex' >
2022-07-21 12:53:27 +02:00
< Badge size = "md" color = "cyan" variant = "filled" > Service : { regexInfo . service_id } < / Badge >
< Space w = "xs" / >
< Badge size = "md" color = { regexInfo . active ? "lime" : "red" } variant = "filled" > { regexInfo . active ? "ACTIVE" : "DISABLED" } < / Badge >
< Space w = "xs" / >
< Badge size = "md" color = "gray" variant = "filled" > ID : { regexInfo . id } < / Badge >
2024-10-13 01:50:52 +02:00
< / Box >
< / Box >
< Box className = 'flex-spacer' / >
< Box className = 'center-flex-row' >
2022-06-22 01:31:46 +02:00
< Badge size = "md" color = { regexInfo . is_case_sensitive ? "grape" : "pink" } variant = "filled" > Case : { regexInfo . is_case_sensitive ? "SENSIIVE" : "INSENSITIVE" } < / Badge >
2022-06-17 20:48:27 +02:00
< Space h = "xs" / >
2022-06-12 16:44:54 +02:00
< Badge size = "md" color = "yellow" variant = "filled" > Packets filtered : { regexInfo . n_packets } < / Badge >
< Space h = "xs" / >
< Badge size = "md" color = "blue" variant = "filled" > Mode : { mode_string } < / Badge >
2024-10-13 01:50:52 +02:00
< / Box >
2022-06-12 16:44:54 +02:00
< / Grid.Col >
2022-06-12 14:40:48 +02:00
< / Grid >
2022-06-12 16:44:54 +02:00
< YesNoModal
title = 'Are you sure to delete this regex?'
2022-06-26 17:53:57 +02:00
description = { ` You are going to delete the regex ' ${ regex_expr } '. ` }
2022-06-12 16:44:54 +02:00
onClose = { ( ) = > setDeleteModal ( false ) }
2022-06-13 10:59:05 +02:00
action = { deleteRegex }
2022-06-12 16:44:54 +02:00
opened = { deleteModal }
/ >
2022-06-12 12:09:50 +02:00
2024-10-13 01:50:52 +02:00
< / Box >
2022-06-12 12:09:50 +02:00
}
export default RegexView ;