Files
firegex-traffic-viewer/frontend/src/pages/NFRegex/index.tsx

88 lines
4.6 KiB
TypeScript
Raw Normal View History

import { ActionIcon, Badge, Box, LoadingOverlay, Space, ThemeIcon, Title, Tooltip } from '@mantine/core';
2023-09-24 05:48:54 +02:00
import { useEffect, useState } from 'react';
import { BsPlusLg, BsRegex } from "react-icons/bs";
2022-07-21 10:20:54 +02:00
import { useNavigate, useParams } from 'react-router-dom';
import ServiceRow from '../../components/NFRegex/ServiceRow';
2023-09-24 05:48:54 +02:00
import { nfregexServiceQuery } from '../../components/NFRegex/utils';
2023-10-12 20:01:48 +02:00
import { errorNotify, getErrorMessage, isMediumScreen } from '../../js/utils';
2025-02-18 17:36:15 +01:00
import AddEditService from '../../components/NFRegex/AddEditService';
import AddNewRegex from '../../components/AddNewRegex';
2023-09-24 17:05:55 +02:00
import { useQueryClient } from '@tanstack/react-query';
import { TbReload } from 'react-icons/tb';
import { FaFilter } from 'react-icons/fa';
import { FaServer } from "react-icons/fa6";
import { VscRegex } from "react-icons/vsc";
2022-07-21 10:20:54 +02:00
function NFRegex({ children }: { children: any }) {
const navigator = useNavigate()
const [open, setOpen] = useState(false);
const {srv} = useParams()
2023-09-24 17:05:55 +02:00
const queryClient = useQueryClient()
2023-10-12 20:01:48 +02:00
const isMedium = isMediumScreen()
2023-09-24 05:48:54 +02:00
const services = nfregexServiceQuery()
useEffect(()=> {
2023-09-24 17:05:55 +02:00
if(services.isError)
2023-09-24 05:48:54 +02:00
errorNotify("NFRegex Update failed!", getErrorMessage(services.error))
},[services.isError])
2022-07-21 10:20:54 +02:00
const closeModal = () => {setOpen(false);}
2022-07-21 12:39:30 +02:00
return <>
<Space h="sm" />
2024-10-13 01:50:52 +02:00
<Box className={isMedium?'center-flex':'center-flex-row'}>
<Title order={5} className="center-flex"><ThemeIcon radius="md" size="md" variant='filled' color='grape' ><BsRegex size={20} /></ThemeIcon><Space w="xs" />Netfilter Regex</Title>
2024-10-13 01:50:52 +02:00
{isMedium?<Box className='flex-spacer' />:<Space h="sm" />}
<Box className='center-flex' >
2025-02-25 23:53:04 +01:00
{isMedium?"General stats:":null}
<Space w="xs" />
<Badge size="md" radius="sm" color="green" variant="filled"><FaServer style={{ marginBottom: -1, marginRight: 4}} />Services: {services.isLoading?0:services.data?.length}</Badge>
2023-10-12 20:01:48 +02:00
<Space w="xs" />
<Badge color="yellow" radius="sm" size="md" variant="filled"><FaFilter style={{ marginBottom: -2, marginRight: 4}} />{services.isLoading?0:services.data?.reduce((acc, s)=> acc+=s.n_packets, 0)}</Badge>
2023-10-12 20:01:48 +02:00
<Space w="xs" />
<Badge size="md" radius="sm" color="violet" variant="filled"><VscRegex style={{ marginBottom: -2, marginRight: 4}} />{services.isLoading?0:services.data?.reduce((acc, s)=> acc+=s.n_regex, 0)}</Badge>
2023-10-12 20:01:48 +02:00
<Space w="xs" />
2024-10-13 01:50:52 +02:00
</Box>
2023-10-12 20:01:48 +02:00
{isMedium?null:<Space h="md" />}
2024-10-13 01:50:52 +02:00
<Box className='center-flex' >
2023-10-12 20:01:48 +02:00
{ srv?
2025-02-28 21:14:09 +01:00
<Tooltip label="Add a new regex" position='bottom' color="blue">
<ActionIcon color="blue" onClick={()=>setOpen(true)} size="lg" radius="md" variant="filled"><BsPlusLg size={18} /></ActionIcon>
2023-10-12 20:01:48 +02:00
</Tooltip>
2025-02-28 21:14:09 +01:00
: <Tooltip label="Add a new service" position='bottom' color="blue">
<ActionIcon color="blue" onClick={()=>setOpen(true)} size="lg" radius="md" variant="filled"><BsPlusLg size={18} /></ActionIcon>
2023-10-12 20:01:48 +02:00
</Tooltip>
}
<Space w="xs" />
2025-02-28 21:14:09 +01:00
<Tooltip label="Refresh" position='bottom' color="indigo">
2023-10-12 20:01:48 +02:00
<ActionIcon color="indigo" onClick={()=>queryClient.invalidateQueries(["nfregex"])} size="lg" radius="md" variant="filled"
2025-02-28 21:14:09 +01:00
loading={services.isFetching}><TbReload size={18} /></ActionIcon>
2023-10-12 20:01:48 +02:00
</Tooltip>
2024-10-13 01:50:52 +02:00
</Box>
</Box>
2023-10-12 20:01:48 +02:00
<Space h="md" />
2024-10-13 01:50:52 +02:00
<Box className="center-flex-row" style={{gap: 20}}>
2022-07-21 12:39:30 +02:00
{srv?null:<>
2023-09-24 05:48:54 +02:00
<LoadingOverlay visible={services.isLoading} />
{(services.data && services.data?.length > 0)?services.data.map( srv => <ServiceRow service={srv} key={srv.service_id} onClick={()=>{
2022-07-21 10:20:54 +02:00
navigator("/nfregex/"+srv.service_id)
2024-10-13 01:50:52 +02:00
}} />):<><Space h="xl"/> <Title className='center-flex' style={{textAlign:"center"}} order={3}>No services found! Add one clicking the "+" buttons</Title>
<Box className='center-flex'>
2025-02-28 21:14:09 +01:00
<Tooltip label="Add a new service" color="blue">
<ActionIcon color="blue" onClick={()=>setOpen(true)} size="xl" radius="md" variant="filled"><BsPlusLg size="20px" /></ActionIcon>
2022-07-21 10:20:54 +02:00
</Tooltip>
2024-10-13 01:50:52 +02:00
</Box>
2022-07-21 10:20:54 +02:00
</>}
</>}
2024-10-13 01:50:52 +02:00
</Box>
2022-07-21 12:39:30 +02:00
{srv?children:null}
{srv?
<AddNewRegex opened={open} onClose={closeModal} service={srv} />:
2025-02-18 17:36:15 +01:00
<AddEditService opened={open} onClose={closeModal} />
}
2022-07-21 12:39:30 +02:00
</>
2022-07-21 10:20:54 +02:00
}
export default NFRegex;