diff --git a/virtweb_frontend/src/widgets/nwfilter/NWFilterDetails.tsx b/virtweb_frontend/src/widgets/nwfilter/NWFilterDetails.tsx index 49165e8..7b383c6 100644 --- a/virtweb_frontend/src/widgets/nwfilter/NWFilterDetails.tsx +++ b/virtweb_frontend/src/widgets/nwfilter/NWFilterDetails.tsx @@ -1,5 +1,17 @@ -import { ReactElement } from "react"; -import { NWFilter } from "../../api/NWFilterApi"; +import { Button } from "@mui/material"; +import React, { ReactElement } from "react"; +import { useNavigate } from "react-router-dom"; +import { + NWFilter, + NWFilterApi, + NWFilterIsBuiltin, +} from "../../api/NWFilterApi"; +import { useAlert } from "../../hooks/providers/AlertDialogProvider"; +import { useConfirm } from "../../hooks/providers/ConfirmDialogProvider"; +import { useSnackbar } from "../../hooks/providers/SnackbarProvider"; +import { AsyncWidget } from "../AsyncWidget"; +import { TabsWidget } from "../TabsWidget"; +import { XMLAsyncWidget } from "../XMLWidget"; interface DetailsProps { nwfilter: NWFilter; @@ -8,5 +20,127 @@ interface DetailsProps { } export function NWFilterDetails(p: DetailsProps): ReactElement { - return <>todo; + const [nwFiltersList, setNwFiltersList] = React.useState(); + + const load = async () => { + setNwFiltersList(await NWFilterApi.GetList()); + }; + + return ( + ( + + )} + /> + ); +} + +type InnerDetailsProps = DetailsProps & { nwFiltersList: NWFilter[] }; + +enum NetFilterTab { + General = 0, + Rules, + XML, + Danger, +} + +export function NetworkFilterDetailsInner( + p: InnerDetailsProps +): React.ReactElement { + const [currTab, setCurrTab] = React.useState(NetFilterTab.General); + + return ( + <> + 0, + }, + + { + label: "XML", + value: NetFilterTab.XML, + visible: !p.editable, + }, + { + label: "Danger zone", + value: NetFilterTab.Danger, + color: "red", + visible: !p.editable && !NWFilterIsBuiltin(p.nwfilter), + }, + ]} + /> + + {currTab === NetFilterTab.General && ( + + )} + {currTab === NetFilterTab.Rules && ( + + )} + {currTab === NetFilterTab.XML && } + {currTab === NetFilterTab.Danger && ( + + )} + + ); +} + +function NetworkFilterDetailsTabGeneral( + p: InnerDetailsProps +): React.ReactElement { + return <>; +} + +function NetworkFilterDetailsTabXML(p: InnerDetailsProps): React.ReactElement { + return ( + NWFilterApi.GetSingleXML(p.nwfilter.uuid!)} + /> + ); +} + +function NetworkFilterDetailsTabDanger( + p: InnerDetailsProps +): React.ReactElement { + const confirm = useConfirm(); + const snackbar = useSnackbar(); + const alert = useAlert(); + const navigate = useNavigate(); + + const requestDelete = async () => { + try { + if ( + !(await confirm( + "Do you really want to delete this network filter?", + `Delete network filter ${p.nwfilter.name}`, + "Delete" + )) + ) + return; + + await NWFilterApi.Delete(p.nwfilter); + + navigate("/nwfilter"); + snackbar("The network filter was successfully deleted!"); + } catch (e) { + console.error(e); + alert(`Failed to delete the network filter!\n${e}`); + } + }; + + return ( + + ); }