浏览代码

Redirection hosts ui

Jamie Curnow 2 月之前
父节点
当前提交
e36c1b99a5

+ 2 - 2
frontend/src/api/backend/getRedirectionHost.ts

@@ -1,8 +1,8 @@
 import * as api from "./base";
 import type { HostExpansion } from "./expansions";
-import type { ProxyHost } from "./models";
+import type { RedirectionHost } from "./models";
 
-export async function getRedirectionHost(id: number, expand?: HostExpansion[], params = {}): Promise<ProxyHost> {
+export async function getRedirectionHost(id: number, expand?: HostExpansion[], params = {}): Promise<RedirectionHost> {
 	return await api.get({
 		url: `/nginx/redirection-hosts/${id}`,
 		params: {

+ 9 - 1
frontend/src/components/Table/Formatter/EventFormatter.tsx

@@ -1,4 +1,4 @@
-import { IconBoltOff, IconDisc, IconUser } from "@tabler/icons-react";
+import { IconArrowsCross, IconBolt, IconBoltOff, IconDisc, IconUser } from "@tabler/icons-react";
 import type { AuditLog } from "src/api/backend";
 import { DateTimeFormat, intl } from "src/locale";
 
@@ -10,6 +10,8 @@ const getEventValue = (event: AuditLog) => {
 	switch (event.objectType) {
 		case "user":
 			return event.meta?.name;
+		case "proxy-host":
+		case "redirection-host":
 		case "dead-host":
 			return event.meta?.domainNames?.join(", ") || "N/A";
 		case "stream":
@@ -37,6 +39,12 @@ const getIcon = (row: AuditLog) => {
 		case "user":
 			ico = <IconUser size={16} className={c} />;
 			break;
+		case "proxy-host":
+			ico = <IconBolt size={16} className={c} />;
+			break;
+		case "redirection-host":
+			ico = <IconArrowsCross size={16} className={c} />;
+			break;
 		case "dead-host":
 			ico = <IconBoltOff size={16} className={c} />;
 			break;

+ 1 - 0
frontend/src/hooks/index.ts

@@ -8,6 +8,7 @@ export * from "./useDnsProviders";
 export * from "./useHealth";
 export * from "./useHostReport";
 export * from "./useProxyHosts";
+export * from "./useRedirectionHost";
 export * from "./useRedirectionHosts";
 export * from "./useStream";
 export * from "./useStreams";

+ 69 - 0
frontend/src/hooks/useRedirectionHost.ts

@@ -0,0 +1,69 @@
+import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
+import {
+	createRedirectionHost,
+	getRedirectionHost,
+	type RedirectionHost,
+	updateRedirectionHost,
+} from "src/api/backend";
+
+const fetchRedirectionHost = (id: number | "new") => {
+	if (id === "new") {
+		return Promise.resolve({
+			id: 0,
+			createdOn: "",
+			modifiedOn: "",
+			ownerUserId: 0,
+			domainNames: [],
+			forwardDomainName: "",
+			preservePath: false,
+			certificateId: 0,
+			sslForced: false,
+			advancedConfig: "",
+			meta: {},
+			http2Support: false,
+			forwardScheme: "auto",
+			forwardHttpCode: 301,
+			blockExploits: false,
+			enabled: true,
+			hstsEnabled: false,
+			hstsSubdomains: false,
+		} as RedirectionHost);
+	}
+	return getRedirectionHost(id, ["owner"]);
+};
+
+const useRedirectionHost = (id: number | "new", options = {}) => {
+	return useQuery<RedirectionHost, Error>({
+		queryKey: ["redirection-host", id],
+		queryFn: () => fetchRedirectionHost(id),
+		staleTime: 60 * 1000, // 1 minute
+		...options,
+	});
+};
+
+const useSetRedirectionHost = () => {
+	const queryClient = useQueryClient();
+	return useMutation({
+		mutationFn: (values: RedirectionHost) =>
+			values.id ? updateRedirectionHost(values) : createRedirectionHost(values),
+		onMutate: (values: RedirectionHost) => {
+			if (!values.id) {
+				return;
+			}
+			const previousObject = queryClient.getQueryData(["redirection-host", values.id]);
+			queryClient.setQueryData(["redirection-host", values.id], (old: RedirectionHost) => ({
+				...old,
+				...values,
+			}));
+			return () => queryClient.setQueryData(["redirection-host", values.id], previousObject);
+		},
+		onError: (_, __, rollback: any) => rollback(),
+		onSuccess: async ({ id }: RedirectionHost) => {
+			queryClient.invalidateQueries({ queryKey: ["redirection-host", id] });
+			queryClient.invalidateQueries({ queryKey: ["redirection-hosts"] });
+			queryClient.invalidateQueries({ queryKey: ["audit-logs"] });
+		},
+	});
+};
+
+export { useRedirectionHost, useSetRedirectionHost };

+ 12 - 0
frontend/src/locale/lang/en.json

@@ -72,17 +72,25 @@
   "error.passwords-must-match": "Passwords must match",
   "error.required": "This is required",
   "event.created-dead-host": "Created 404 Host",
+  "event.created-redirection-host": "Created Redirection Host",
   "event.created-stream": "Created Stream",
   "event.created-user": "Created User",
   "event.deleted-dead-host": "Deleted 404 Host",
   "event.deleted-stream": "Deleted Stream",
   "event.deleted-user": "Deleted User",
   "event.disabled-dead-host": "Disabled 404 Host",
+  "event.disabled-redirection-host": "Disabled Redirection Host",
   "event.disabled-stream": "Disabled Stream",
   "event.enabled-dead-host": "Enabled 404 Host",
+  "event.enabled-redirection-host": "Enabled Redirection Host",
   "event.enabled-stream": "Enabled Stream",
+  "event.updated-redirection-host": "Updated Redirection Host",
   "event.updated-user": "Updated User",
   "footer.github-fork": "Fork me on Github",
+  "host.flags.block-exploits": "Block Common Exploits",
+  "host.flags.preserve-path": "Preserve Path",
+  "host.flags.protocols": "Protocols",
+  "host.flags.title": "Options",
   "hosts.title": "Hosts",
   "http-only": "HTTP Only",
   "lets-encrypt": "Let's Encrypt",
@@ -99,6 +107,7 @@
   "notification.host-deleted": "Host has been deleted",
   "notification.host-disabled": "Host has been disabled",
   "notification.host-enabled": "Host has been enabled",
+  "notification.redirection-host-saved": "Redirection Host has been saved",
   "notification.stream-deleted": "Stream has been deleted",
   "notification.stream-disabled": "Stream has been disabled",
   "notification.stream-enabled": "Stream has been enabled",
@@ -124,6 +133,9 @@
   "proxy-hosts.count": "{count} Proxy Hosts",
   "proxy-hosts.empty": "There are no Proxy Hosts",
   "proxy-hosts.title": "Proxy Hosts",
+  "redirect-host.forward-domain": "Forward Domain",
+  "redirect-host.forward-scheme": "Scheme",
+  "redirection-host.new": "New Redirection Host",
   "redirection-hosts.actions-title": "Redirection Host #{id}",
   "redirection-hosts.add": "Add Redirection Host",
   "redirection-hosts.count": "{count} Redirection Hosts",

+ 36 - 0
frontend/src/locale/src/en.json

@@ -215,6 +215,18 @@
 	"event.created-dead-host": {
 		"defaultMessage": "Created 404 Host"
 	},
+	"event.created-redirection-host": {
+		"defaultMessage": "Created Redirection Host"
+	},
+	"event.updated-redirection-host": {
+		"defaultMessage": "Updated Redirection Host"
+	},
+	"event.enabled-redirection-host": {
+		"defaultMessage": "Enabled Redirection Host"
+	},
+	"event.disabled-redirection-host": {
+		"defaultMessage": "Disabled Redirection Host"
+	},
 	"event.created-stream": {
 		"defaultMessage": "Created Stream"
 	},
@@ -251,6 +263,18 @@
 	"empty-subtitle": {
 		"defaultMessage": "Why don't you create one?"
 	},
+	"host.flags.title": {
+		"defaultMessage": "Options"
+	},
+	"host.flags.block-exploits": {
+		"defaultMessage": "Block Common Exploits"
+	},
+	"host.flags.preserve-path": {
+		"defaultMessage": "Preserve Path"
+	},
+	"host.flags.protocols": {
+		"defaultMessage": "Protocols"
+	},
 	"hosts.title": {
 		"defaultMessage": "Hosts"
 	},
@@ -299,6 +323,9 @@
 	"notification.host-enabled": {
 		"defaultMessage": "Host has been enabled"
 	},
+	"notification.redirection-host-saved": {
+		"defaultMessage": "Redirection Host has been saved"
+	},
 	"notification.stream-deleted": {
 		"defaultMessage": "Stream has been deleted"
 	},
@@ -374,6 +401,12 @@
 	"proxy-hosts.title": {
 		"defaultMessage": "Proxy Hosts"
 	},
+	"redirect-host.forward-scheme": {
+		"defaultMessage": "Scheme"
+	},
+	"redirect-host.forward-domain": {
+		"defaultMessage": "Forward Domain"
+	},
 	"redirection-hosts.actions-title": {
 		"defaultMessage": "Redirection Host #{id}"
 	},
@@ -386,6 +419,9 @@
 	"redirection-hosts.empty": {
 		"defaultMessage": "There are no Redirection Hosts"
 	},
+	"redirection-host.new": {
+		"defaultMessage": "New Redirection Host"
+	},
 	"redirection-hosts.title": {
 		"defaultMessage": "Redirection Hosts"
 	},

+ 304 - 0
frontend/src/modals/RedirectionHostModal.tsx

@@ -0,0 +1,304 @@
+import { IconSettings } from "@tabler/icons-react";
+import { Field, Form, Formik } from "formik";
+import { useState } from "react";
+import { Alert } from "react-bootstrap";
+import Modal from "react-bootstrap/Modal";
+import {
+	Button,
+	DomainNamesField,
+	Loading,
+	NginxConfigField,
+	SSLCertificateField,
+	SSLOptionsFields,
+} from "src/components";
+import { useRedirectionHost, useSetRedirectionHost } from "src/hooks";
+import { intl } from "src/locale";
+import { validateString } from "src/modules/Validations";
+import { showSuccess } from "src/notifications";
+
+interface Props {
+	id: number | "new";
+	onClose: () => void;
+}
+export function RedirectionHostModal({ id, onClose }: Props) {
+	const { data, isLoading, error } = useRedirectionHost(id);
+	const { mutate: setRedirectionHost } = useSetRedirectionHost();
+	const [errorMsg, setErrorMsg] = useState<string | null>(null);
+	const [isSubmitting, setIsSubmitting] = useState(false);
+
+	const onSubmit = async (values: any, { setSubmitting }: any) => {
+		if (isSubmitting) return;
+		setIsSubmitting(true);
+		setErrorMsg(null);
+
+		const { ...payload } = {
+			id: id === "new" ? undefined : id,
+			...values,
+		};
+
+		setRedirectionHost(payload, {
+			onError: (err: any) => setErrorMsg(err.message),
+			onSuccess: () => {
+				showSuccess(intl.formatMessage({ id: "notification.redirection-host-saved" }));
+				onClose();
+			},
+			onSettled: () => {
+				setIsSubmitting(false);
+				setSubmitting(false);
+			},
+		});
+	};
+
+	return (
+		<Modal show onHide={onClose} animation={false}>
+			{!isLoading && error && (
+				<Alert variant="danger" className="m-3">
+					{error?.message || "Unknown error"}
+				</Alert>
+			)}
+			{isLoading && <Loading noLogo />}
+			{!isLoading && data && (
+				<Formik
+					initialValues={
+						{
+							// Details tab
+							domainNames: data?.domainNames || [],
+							forwardDomainName: data?.forwardDomainName || "",
+							forwardScheme: data?.forwardScheme || "auto",
+							forwardHttpCode: data?.forwardHttpCode || 301,
+							preservePath: data?.preservePath || false,
+							blockExploits: data?.blockExploits || false,
+							// SSL tab
+							certificateId: data?.certificateId || 0,
+							sslForced: data?.sslForced || false,
+							http2Support: data?.http2Support || false,
+							hstsEnabled: data?.hstsEnabled || false,
+							hstsSubdomains: data?.hstsSubdomains || false,
+							// Advanced tab
+							advancedConfig: data?.advancedConfig || "",
+							meta: data?.meta || {},
+						} as any
+					}
+					onSubmit={onSubmit}
+				>
+					{() => (
+						<Form>
+							<Modal.Header closeButton>
+								<Modal.Title>
+									{intl.formatMessage({
+										id: data?.id ? "redirection-host.edit" : "redirection-host.new",
+									})}
+								</Modal.Title>
+							</Modal.Header>
+							<Modal.Body className="p-0">
+								<Alert variant="danger" show={!!errorMsg} onClose={() => setErrorMsg(null)} dismissible>
+									{errorMsg}
+								</Alert>
+
+								<div className="card m-0 border-0">
+									<div className="card-header">
+										<ul className="nav nav-tabs card-header-tabs" data-bs-toggle="tabs">
+											<li className="nav-item" role="presentation">
+												<a
+													href="#tab-details"
+													className="nav-link active"
+													data-bs-toggle="tab"
+													aria-selected="true"
+													role="tab"
+												>
+													{intl.formatMessage({ id: "column.details" })}
+												</a>
+											</li>
+											<li className="nav-item" role="presentation">
+												<a
+													href="#tab-ssl"
+													className="nav-link"
+													data-bs-toggle="tab"
+													aria-selected="false"
+													tabIndex={-1}
+													role="tab"
+												>
+													{intl.formatMessage({ id: "column.ssl" })}
+												</a>
+											</li>
+											<li className="nav-item ms-auto" role="presentation">
+												<a
+													href="#tab-advanced"
+													className="nav-link"
+													title="Settings"
+													data-bs-toggle="tab"
+													aria-selected="false"
+													tabIndex={-1}
+													role="tab"
+												>
+													<IconSettings size={20} />
+												</a>
+											</li>
+										</ul>
+									</div>
+									<div className="card-body">
+										<div className="tab-content">
+											<div className="tab-pane active show" id="tab-details" role="tabpanel">
+												<DomainNamesField isWildcardPermitted />
+												<div className="row">
+													<div className="col-md-4">
+														<Field name="forwardScheme">
+															{({ field, form }: any) => (
+																<div className="mb-3">
+																	<label
+																		className="form-label"
+																		htmlFor="forwardScheme"
+																	>
+																		{intl.formatMessage({
+																			id: "redirect-host.forward-scheme",
+																		})}
+																	</label>
+																	<select
+																		id="forwardScheme"
+																		className={`form-control ${form.errors.forwardScheme && form.touched.forwardScheme ? "is-invalid" : ""}`}
+																		required
+																		{...field}
+																	>
+																		<option value="$scheme">Auto</option>
+																		<option value="http">http</option>
+																		<option value="https">https</option>
+																	</select>
+																	{form.errors.forwardScheme ? (
+																		<div className="invalid-feedback">
+																			{form.errors.forwardScheme &&
+																			form.touched.forwardScheme
+																				? form.errors.forwardScheme
+																				: null}
+																		</div>
+																	) : null}
+																</div>
+															)}
+														</Field>
+													</div>
+													<div className="col-md-8">
+														<Field
+															name="forwardDomainName"
+															validate={validateString(1, 255)}
+														>
+															{({ field, form }: any) => (
+																<div className="mb-3">
+																	<label
+																		className="form-label"
+																		htmlFor="forwardDomainName"
+																	>
+																		{intl.formatMessage({
+																			id: "redirect-host.forward-domain",
+																		})}
+																	</label>
+																	<input
+																		id="forwardDomainName"
+																		type="text"
+																		className={`form-control ${form.errors.forwardDomainName && form.touched.forwardDomainName ? "is-invalid" : ""}`}
+																		required
+																		placeholder="example.com"
+																		{...field}
+																	/>
+																	{form.errors.forwardDomainName ? (
+																		<div className="invalid-feedback">
+																			{form.errors.forwardDomainName &&
+																			form.touched.forwardDomainName
+																				? form.errors.forwardDomainName
+																				: null}
+																		</div>
+																	) : null}
+																</div>
+															)}
+														</Field>
+													</div>
+												</div>
+												<div className="my-3">
+													<h4 className="py-2">
+														{intl.formatMessage({ id: "host.flags.title" })}
+													</h4>
+													<div className="divide-y">
+														<div>
+															<label className="row" htmlFor="preservePath">
+																<span className="col">
+																	{intl.formatMessage({
+																		id: "host.flags.preserve-path",
+																	})}
+																</span>
+																<span className="col-auto">
+																	<Field name="preservePath" type="checkbox">
+																		{({ field }: any) => (
+																			<label className="form-check form-check-single form-switch">
+																				<input
+																					{...field}
+																					id="preservePath"
+																					className="form-check-input"
+																					type="checkbox"
+																				/>
+																			</label>
+																		)}
+																	</Field>
+																</span>
+															</label>
+														</div>
+														<div>
+															<label className="row" htmlFor="blockExploits">
+																<span className="col">
+																	{intl.formatMessage({
+																		id: "host.flags.block-exploits",
+																	})}
+																</span>
+																<span className="col-auto">
+																	<Field name="blockExploits" type="checkbox">
+																		{({ field }: any) => (
+																			<label className="form-check form-check-single form-switch">
+																				<input
+																					{...field}
+																					id="blockExploits"
+																					className="form-check-input"
+																					type="checkbox"
+																				/>
+																			</label>
+																		)}
+																	</Field>
+																</span>
+															</label>
+														</div>
+													</div>
+												</div>
+											</div>
+											<div className="tab-pane" id="tab-ssl" role="tabpanel">
+												<SSLCertificateField
+													name="certificateId"
+													label="ssl-certificate"
+													allowNew
+												/>
+												<SSLOptionsFields />
+											</div>
+											<div className="tab-pane" id="tab-advanced" role="tabpanel">
+												<NginxConfigField />
+											</div>
+										</div>
+									</div>
+								</div>
+							</Modal.Body>
+							<Modal.Footer>
+								<Button data-bs-dismiss="modal" onClick={onClose} disabled={isSubmitting}>
+									{intl.formatMessage({ id: "cancel" })}
+								</Button>
+								<Button
+									type="submit"
+									actionType="primary"
+									className="ms-auto"
+									data-bs-dismiss="modal"
+									isLoading={isSubmitting}
+									disabled={isSubmitting}
+								>
+									{intl.formatMessage({ id: "save" })}
+								</Button>
+							</Modal.Footer>
+						</Form>
+					)}
+				</Formik>
+			)}
+		</Modal>
+	);
+}

+ 68 - 38
frontend/src/modals/StreamModal.tsx

@@ -205,54 +205,84 @@ export function StreamModal({ id, onClose }: Props) {
 														</Field>
 													</div>
 												</div>
-												<div className="mb-3">
-													<div className="form-label">Protocols</div>
-													<Field name="tcpForwarding">
-														{({ field }: any) => (
-															<label className="form-check form-switch">
-																<input
-																	className="form-check-input"
-																	type="checkbox"
-																	name={field.name}
-																	checked={field.value}
-																	onChange={(e: any) => {
-																		setFieldValue(field.name, e.target.checked);
-																		if (!e.target.checked) {
-																			setFieldValue("udpForwarding", true);
-																		}
-																	}}
-																/>
-																<span className="form-check-label">
+												<div className="my-3">
+													<h3 className="py-2">
+														{intl.formatMessage({ id: "host.flags.protocols" })}
+													</h3>
+													<div className="divide-y">
+														<div>
+															<label className="row" htmlFor="tcpForwarding">
+																<span className="col">
 																	{intl.formatMessage({
 																		id: "streams.tcp",
 																	})}
 																</span>
+																<span className="col-auto">
+																	<Field name="tcpForwarding" type="checkbox">
+																		{({ field }: any) => (
+																			<label className="form-check form-check-single form-switch">
+																				<input
+																					id="tcpForwarding"
+																					className="form-check-input"
+																					type="checkbox"
+																					name={field.name}
+																					checked={field.value}
+																					onChange={(e: any) => {
+																						setFieldValue(
+																							field.name,
+																							e.target.checked,
+																						);
+																						if (!e.target.checked) {
+																							setFieldValue(
+																								"udpForwarding",
+																								true,
+																							);
+																						}
+																					}}
+																				/>
+																			</label>
+																		)}
+																	</Field>
+																</span>
 															</label>
-														)}
-													</Field>
-													<Field name="udpForwarding">
-														{({ field }: any) => (
-															<label className="form-check form-switch">
-																<input
-																	className="form-check-input"
-																	type="checkbox"
-																	name={field.name}
-																	checked={field.value}
-																	onChange={(e: any) => {
-																		setFieldValue(field.name, e.target.checked);
-																		if (!e.target.checked) {
-																			setFieldValue("tcpForwarding", true);
-																		}
-																	}}
-																/>
-																<span className="form-check-label">
+														</div>
+														<div>
+															<label className="row" htmlFor="udpForwarding">
+																<span className="col">
 																	{intl.formatMessage({
 																		id: "streams.udp",
 																	})}
 																</span>
+																<span className="col-auto">
+																	<Field name="udpForwarding" type="checkbox">
+																		{({ field }: any) => (
+																			<label className="form-check form-check-single form-switch">
+																				<input
+																					id="udpForwarding"
+																					className="form-check-input"
+																					type="checkbox"
+																					name={field.name}
+																					checked={field.value}
+																					onChange={(e: any) => {
+																						setFieldValue(
+																							field.name,
+																							e.target.checked,
+																						);
+																						if (!e.target.checked) {
+																							setFieldValue(
+																								"tcpForwarding",
+																								true,
+																							);
+																						}
+																					}}
+																				/>
+																			</label>
+																		)}
+																	</Field>
+																</span>
 															</label>
-														)}
-													</Field>
+														</div>
+													</div>
 												</div>
 											</div>
 											<div className="tab-pane" id="tab-ssl" role="tabpanel">

+ 1 - 1
frontend/src/modals/UserModal.tsx

@@ -167,7 +167,7 @@ export function UserModal({ userId, onClose }: Props) {
 								</div>
 								{currentUser && data && currentUser?.id !== data?.id ? (
 									<div className="my-3">
-										<h3 className="py-2">{intl.formatMessage({ id: "user.flags.title" })}</h3>
+										<h4 className="py-2">{intl.formatMessage({ id: "user.flags.title" })}</h4>
 										<div className="divide-y">
 											<div>
 												<label className="row" htmlFor="isAdmin">

+ 1 - 0
frontend/src/modals/index.ts

@@ -3,6 +3,7 @@ export * from "./DeadHostModal";
 export * from "./DeleteConfirmModal";
 export * from "./EventDetailsModal";
 export * from "./PermissionsModal";
+export * from "./RedirectionHostModal";
 export * from "./SetPasswordModal";
 export * from "./StreamModal";
 export * from "./UserModal";

+ 0 - 5
frontend/src/pages/Dashboard/index.tsx

@@ -122,14 +122,9 @@ const Dashboard = () => {
 			<pre>
 				<code>{`Todo:
 
-- Users: permissions modal and trigger after adding user
-- modal dialgs for everything
-- Tables
 - check mobile
-- fix bad jwt not refreshing entire page
 - add help docs for host types
 - REDO SCREENSHOTS in docs folder
-- Remove letsEncryptEmail field from new certificate requests, use current user email server side
 
 More for api, then implement here:
 - Properly implement refresh tokens

+ 14 - 4
frontend/src/pages/Nginx/RedirectionHosts/Empty.tsx

@@ -4,15 +4,25 @@ import { intl } from "src/locale";
 
 interface Props {
 	tableInstance: ReactTable<any>;
+	onNew?: () => void;
+	isFiltered?: boolean;
 }
-export default function Empty({ tableInstance }: Props) {
+export default function Empty({ tableInstance, onNew, isFiltered }: Props) {
 	return (
 		<tr>
 			<td colSpan={tableInstance.getVisibleFlatColumns().length}>
 				<div className="text-center my-4">
-					<h2>{intl.formatMessage({ id: "redirection-hosts.empty" })}</h2>
-					<p className="text-muted">{intl.formatMessage({ id: "empty-subtitle" })}</p>
-					<Button className="btn-yellow my-3">{intl.formatMessage({ id: "redirection-hosts.add" })}</Button>
+					{isFiltered ? (
+						<h2>{intl.formatMessage({ id: "empty-search" })}</h2>
+					) : (
+						<>
+							<h2>{intl.formatMessage({ id: "redirection-hosts.empty" })}</h2>
+							<p className="text-muted">{intl.formatMessage({ id: "empty-subtitle" })}</p>
+							<Button className="btn-yellow my-3" onClick={onNew}>
+								{intl.formatMessage({ id: "redirection-hosts.add" })}
+							</Button>
+						</>
+					)}
 				</div>
 			</td>
 		</tr>

+ 40 - 7
frontend/src/pages/Nginx/RedirectionHosts/Table.tsx

@@ -9,9 +9,14 @@ import Empty from "./Empty";
 
 interface Props {
 	data: RedirectionHost[];
+	isFiltered?: boolean;
 	isFetching?: boolean;
+	onEdit?: (id: number) => void;
+	onDelete?: (id: number) => void;
+	onDisableToggle?: (id: number, enabled: boolean) => void;
+	onNew?: () => void;
 }
-export default function Table({ data, isFetching }: Props) {
+export default function Table({ data, isFetching, onEdit, onDelete, onDisableToggle, onNew, isFiltered }: Props) {
 	const columnHelper = createColumnHelper<RedirectionHost>();
 	const columns = useMemo(
 		() => [
@@ -90,16 +95,39 @@ export default function Table({ data, isFetching }: Props) {
 										{ id: info.row.original.id },
 									)}
 								</span>
-								<a className="dropdown-item" href="#">
+								<a
+									className="dropdown-item"
+									href="#"
+									onClick={(e) => {
+										e.preventDefault();
+										onEdit?.(info.row.original.id);
+									}}
+								>
 									<IconEdit size={16} />
 									{intl.formatMessage({ id: "action.edit" })}
 								</a>
-								<a className="dropdown-item" href="#">
+								<a
+									className="dropdown-item"
+									href="#"
+									onClick={(e) => {
+										e.preventDefault();
+										onDisableToggle?.(info.row.original.id, !info.row.original.enabled);
+									}}
+								>
 									<IconPower size={16} />
-									{intl.formatMessage({ id: "action.disable" })}
+									{intl.formatMessage({
+										id: info.row.original.enabled ? "action.disable" : "action.enable",
+									})}
 								</a>
 								<div className="dropdown-divider" />
-								<a className="dropdown-item" href="#">
+								<a
+									className="dropdown-item"
+									href="#"
+									onClick={(e) => {
+										e.preventDefault();
+										onDelete?.(info.row.original.id);
+									}}
+								>
 									<IconTrash size={16} />
 									{intl.formatMessage({ id: "action.delete" })}
 								</a>
@@ -112,7 +140,7 @@ export default function Table({ data, isFetching }: Props) {
 				},
 			}),
 		],
-		[columnHelper],
+		[columnHelper, onEdit, onDisableToggle, onDelete],
 	);
 
 	const tableInstance = useReactTable<RedirectionHost>({
@@ -126,5 +154,10 @@ export default function Table({ data, isFetching }: Props) {
 		enableSortingRemoval: false,
 	});
 
-	return <TableLayout tableInstance={tableInstance} emptyState={<Empty tableInstance={tableInstance} />} />;
+	return (
+		<TableLayout
+			tableInstance={tableInstance}
+			emptyState={<Empty tableInstance={tableInstance} onNew={onNew} isFiltered={isFiltered} />}
+		/>
+	);
 }

+ 69 - 13
frontend/src/pages/Nginx/RedirectionHosts/TableWrapper.tsx

@@ -1,11 +1,20 @@
 import { IconSearch } from "@tabler/icons-react";
+import { useQueryClient } from "@tanstack/react-query";
+import { useState } from "react";
 import Alert from "react-bootstrap/Alert";
+import { deleteRedirectionHost, toggleRedirectionHost } from "src/api/backend";
 import { Button, LoadingPage } from "src/components";
 import { useRedirectionHosts } from "src/hooks";
 import { intl } from "src/locale";
+import { DeleteConfirmModal, RedirectionHostModal } from "src/modals";
+import { showSuccess } from "src/notifications";
 import Table from "./Table";
 
 export default function TableWrapper() {
+	const queryClient = useQueryClient();
+	const [search, setSearch] = useState("");
+	const [deleteId, setDeleteId] = useState(0);
+	const [editId, setEditId] = useState(0 as number | "new");
 	const { isFetching, isLoading, isError, error, data } = useRedirectionHosts(["owner", "certificate"]);
 
 	if (isLoading) {
@@ -16,6 +25,31 @@ export default function TableWrapper() {
 		return <Alert variant="danger">{error?.message || "Unknown error"}</Alert>;
 	}
 
+	const handleDelete = async () => {
+		await deleteRedirectionHost(deleteId);
+		showSuccess(intl.formatMessage({ id: "notification.host-deleted" }));
+	};
+
+	const handleDisableToggle = async (id: number, enabled: boolean) => {
+		await toggleRedirectionHost(id, enabled);
+		queryClient.invalidateQueries({ queryKey: ["redirection-hosts"] });
+		queryClient.invalidateQueries({ queryKey: ["redirection-host", id] });
+		showSuccess(intl.formatMessage({ id: enabled ? "notification.host-enabled" : "notification.host-disabled" }));
+	};
+
+	let filtered = null;
+	if (search && data) {
+		filtered = data?.filter((item) => {
+			return (
+				item.domainNames.some((domain: string) => domain.toLowerCase().includes(search)) ||
+				item.forwardDomainName.toLowerCase().includes(search)
+			);
+		});
+	} else if (search !== "") {
+		// this can happen if someone deletes the last item while searching
+		setSearch("");
+	}
+
 	return (
 		<div className="card mt-4">
 			<div className="card-status-top bg-yellow" />
@@ -27,25 +61,47 @@ export default function TableWrapper() {
 						</div>
 						<div className="col-md-auto col-sm-12">
 							<div className="ms-auto d-flex flex-wrap btn-list">
-								<div className="input-group input-group-flat w-auto">
-									<span className="input-group-text input-group-text-sm">
-										<IconSearch size={16} />
-									</span>
-									<input
-										id="advanced-table-search"
-										type="text"
-										className="form-control form-control-sm"
-										autoComplete="off"
-									/>
-								</div>
-								<Button size="sm" className="btn-yellow">
+								{data?.length ? (
+									<div className="input-group input-group-flat w-auto">
+										<span className="input-group-text input-group-text-sm">
+											<IconSearch size={16} />
+										</span>
+										<input
+											id="advanced-table-search"
+											type="text"
+											className="form-control form-control-sm"
+											autoComplete="off"
+											onChange={(e: any) => setSearch(e.target.value.toLowerCase().trim())}
+										/>
+									</div>
+								) : null}
+								<Button size="sm" className="btn-yellow" onClick={() => setEditId("new")}>
 									{intl.formatMessage({ id: "redirection-hosts.add" })}
 								</Button>
 							</div>
 						</div>
 					</div>
 				</div>
-				<Table data={data ?? []} isFetching={isFetching} />
+				<Table
+					data={filtered ?? data ?? []}
+					isFiltered={!!search}
+					isFetching={isFetching}
+					onEdit={(id: number) => setEditId(id)}
+					onDelete={(id: number) => setDeleteId(id)}
+					onDisableToggle={handleDisableToggle}
+					onNew={() => setEditId("new")}
+				/>
+				{editId ? <RedirectionHostModal id={editId} onClose={() => setEditId(0)} /> : null}
+				{deleteId ? (
+					<DeleteConfirmModal
+						title={intl.formatMessage({ id: "redirection-host.delete.title" })}
+						onConfirm={handleDelete}
+						onClose={() => setDeleteId(0)}
+						invalidations={[["redirection-hosts"], ["redirection-host", deleteId]]}
+					>
+						{intl.formatMessage({ id: "redirection-host.delete.content" })}
+					</DeleteConfirmModal>
+				) : null}
 			</div>
 		</div>
 	);