mirror of
https://github.com/NginxProxyManager/nginx-proxy-manager.git
synced 2025-10-03 19:30:10 +00:00
Streams modal
This commit is contained in:
@@ -31,6 +31,7 @@ interface Props {
|
||||
label?: string;
|
||||
required?: boolean;
|
||||
allowNew?: boolean;
|
||||
forHttp?: boolean; // the sslForced, http2Support, hstsEnabled, hstsSubdomains fields
|
||||
}
|
||||
export function SSLCertificateField({
|
||||
name = "certificateId",
|
||||
@@ -38,6 +39,7 @@ export function SSLCertificateField({
|
||||
id = "certificateId",
|
||||
required,
|
||||
allowNew,
|
||||
forHttp = true,
|
||||
}: Props) {
|
||||
const { isLoading, isError, error, data } = useCertificates();
|
||||
const { values, setFieldValue } = useFormikContext();
|
||||
@@ -55,7 +57,7 @@ export function SSLCertificateField({
|
||||
dnsProviderCredentials,
|
||||
propagationSeconds,
|
||||
} = v;
|
||||
if (!newValue?.value) {
|
||||
if (forHttp && !newValue?.value) {
|
||||
sslForced && setFieldValue("sslForced", false);
|
||||
http2Support && setFieldValue("http2Support", false);
|
||||
hstsEnabled && setFieldValue("hstsEnabled", false);
|
||||
@@ -94,7 +96,7 @@ export function SSLCertificateField({
|
||||
options?.unshift({
|
||||
value: 0,
|
||||
label: "None",
|
||||
subLabel: "This host will not use HTTPS",
|
||||
subLabel: forHttp ? "This host will not use HTTPS" : "No certificate assigned",
|
||||
icon: <IconShield size={14} className="text-red" />,
|
||||
});
|
||||
}
|
||||
|
@@ -1,9 +1,14 @@
|
||||
import cn from "classnames";
|
||||
import { Field, useFormikContext } from "formik";
|
||||
import { DNSProviderFields } from "src/components";
|
||||
import { DNSProviderFields, DomainNamesField } from "src/components";
|
||||
import { intl } from "src/locale";
|
||||
|
||||
export function SSLOptionsFields() {
|
||||
interface Props {
|
||||
forHttp?: boolean; // the sslForced, http2Support, hstsEnabled, hstsSubdomains fields
|
||||
forceDNSForNew?: boolean;
|
||||
requireDomainNames?: boolean; // used for streams
|
||||
}
|
||||
export function SSLOptionsFields({ forHttp = true, forceDNSForNew, requireDomainNames }: Props) {
|
||||
const { values, setFieldValue } = useFormikContext();
|
||||
const v: any = values || {};
|
||||
|
||||
@@ -12,6 +17,10 @@ export function SSLOptionsFields() {
|
||||
const { sslForced, http2Support, hstsEnabled, hstsSubdomains, meta } = v;
|
||||
const { dnsChallenge } = meta || {};
|
||||
|
||||
if (forceDNSForNew && newCertificate && !dnsChallenge) {
|
||||
setFieldValue("meta.dnsChallenge", true);
|
||||
}
|
||||
|
||||
const handleToggleChange = (e: any, fieldName: string) => {
|
||||
setFieldValue(fieldName, e.target.checked);
|
||||
if (fieldName === "meta.dnsChallenge" && !e.target.checked) {
|
||||
@@ -24,8 +33,8 @@ export function SSLOptionsFields() {
|
||||
const toggleClasses = "form-check-input";
|
||||
const toggleEnabled = cn(toggleClasses, "bg-cyan");
|
||||
|
||||
return (
|
||||
<>
|
||||
const getHttpOptions = () => (
|
||||
<div>
|
||||
<div className="row">
|
||||
<div className="col-6">
|
||||
<Field name="sslForced">
|
||||
@@ -102,6 +111,12 @@ export function SSLOptionsFields() {
|
||||
</Field>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
return (
|
||||
<div>
|
||||
{forHttp ? getHttpOptions() : null}
|
||||
{newCertificate ? (
|
||||
<>
|
||||
<Field name="meta.dnsChallenge">
|
||||
@@ -110,7 +125,8 @@ export function SSLOptionsFields() {
|
||||
<input
|
||||
className={dnsChallenge ? toggleEnabled : toggleClasses}
|
||||
type="checkbox"
|
||||
checked={!!dnsChallenge}
|
||||
checked={forceDNSForNew ? true : !!dnsChallenge}
|
||||
disabled={forceDNSForNew}
|
||||
onChange={(e) => handleToggleChange(e, field.name)}
|
||||
/>
|
||||
<span className="form-check-label">
|
||||
@@ -119,10 +135,10 @@ export function SSLOptionsFields() {
|
||||
</label>
|
||||
)}
|
||||
</Field>
|
||||
|
||||
{requireDomainNames ? <DomainNamesField /> : null}
|
||||
{dnsChallenge ? <DNSProviderFields /> : null}
|
||||
</>
|
||||
) : null}
|
||||
</>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
@@ -9,6 +9,7 @@ export * from "./useHealth";
|
||||
export * from "./useHostReport";
|
||||
export * from "./useProxyHosts";
|
||||
export * from "./useRedirectionHosts";
|
||||
export * from "./useStream";
|
||||
export * from "./useStreams";
|
||||
export * from "./useTheme";
|
||||
export * from "./useUser";
|
||||
|
54
frontend/src/hooks/useStream.ts
Normal file
54
frontend/src/hooks/useStream.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { createStream, getStream, type Stream, updateStream } from "src/api/backend";
|
||||
|
||||
const fetchStream = (id: number | "new") => {
|
||||
if (id === "new") {
|
||||
return Promise.resolve({
|
||||
id: 0,
|
||||
createdOn: "",
|
||||
modifiedOn: "",
|
||||
ownerUserId: 0,
|
||||
tcpForwarding: true,
|
||||
udpForwarding: false,
|
||||
meta: {},
|
||||
enabled: true,
|
||||
certificateId: 0,
|
||||
} as Stream);
|
||||
}
|
||||
return getStream(id, ["owner"]);
|
||||
};
|
||||
|
||||
const useStream = (id: number | "new", options = {}) => {
|
||||
return useQuery<Stream, Error>({
|
||||
queryKey: ["stream", id],
|
||||
queryFn: () => fetchStream(id),
|
||||
staleTime: 60 * 1000, // 1 minute
|
||||
...options,
|
||||
});
|
||||
};
|
||||
|
||||
const useSetStream = () => {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (values: Stream) => (values.id ? updateStream(values) : createStream(values)),
|
||||
onMutate: (values: Stream) => {
|
||||
if (!values.id) {
|
||||
return;
|
||||
}
|
||||
const previousObject = queryClient.getQueryData(["stream", values.id]);
|
||||
queryClient.setQueryData(["stream", values.id], (old: Stream) => ({
|
||||
...old,
|
||||
...values,
|
||||
}));
|
||||
return () => queryClient.setQueryData(["stream", values.id], previousObject);
|
||||
},
|
||||
onError: (_, __, rollback: any) => rollback(),
|
||||
onSuccess: async ({ id }: Stream) => {
|
||||
queryClient.invalidateQueries({ queryKey: ["stream", id] });
|
||||
queryClient.invalidateQueries({ queryKey: ["streams"] });
|
||||
queryClient.invalidateQueries({ queryKey: ["audit-logs"] });
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export { useStream, useSetStream };
|
@@ -130,6 +130,10 @@
|
||||
"setup.title": "Welcome!",
|
||||
"sign-in": "Sign in",
|
||||
"ssl-certificate": "SSL Certificate",
|
||||
"stream.forward-host": "Forward Host",
|
||||
"stream.forward-port": "Forward Port",
|
||||
"stream.incoming-port": "Incoming Port",
|
||||
"stream.new": "New Stream",
|
||||
"streams.actions-title": "Stream #{id}",
|
||||
"streams.add": "Add Stream",
|
||||
"streams.count": "{count} Streams",
|
||||
|
@@ -392,6 +392,18 @@
|
||||
"ssl-certificate": {
|
||||
"defaultMessage": "SSL Certificate"
|
||||
},
|
||||
"stream.forward-host": {
|
||||
"defaultMessage": "Forward Host"
|
||||
},
|
||||
"stream.forward-port": {
|
||||
"defaultMessage": "Forward Port"
|
||||
},
|
||||
"stream.incoming-port": {
|
||||
"defaultMessage": "Incoming Port"
|
||||
},
|
||||
"stream.new": {
|
||||
"defaultMessage": "New Stream"
|
||||
},
|
||||
"streams.actions-title": {
|
||||
"defaultMessage": "Stream #{id}"
|
||||
},
|
||||
|
292
frontend/src/modals/StreamModal.tsx
Normal file
292
frontend/src/modals/StreamModal.tsx
Normal file
@@ -0,0 +1,292 @@
|
||||
import { Field, Form, Formik } from "formik";
|
||||
import { useState } from "react";
|
||||
import { Alert } from "react-bootstrap";
|
||||
import Modal from "react-bootstrap/Modal";
|
||||
import { Button, Loading, SSLCertificateField, SSLOptionsFields } from "src/components";
|
||||
import { useSetStream, useStream } from "src/hooks";
|
||||
import { intl } from "src/locale";
|
||||
import { validateNumber, validateString } from "src/modules/Validations";
|
||||
import { showSuccess } from "src/notifications";
|
||||
|
||||
interface Props {
|
||||
id: number | "new";
|
||||
onClose: () => void;
|
||||
}
|
||||
export function StreamModal({ id, onClose }: Props) {
|
||||
const { data, isLoading, error } = useStream(id);
|
||||
const { mutate: setStream } = useSetStream();
|
||||
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,
|
||||
};
|
||||
|
||||
setStream(payload, {
|
||||
onError: (err: any) => setErrorMsg(err.message),
|
||||
onSuccess: () => {
|
||||
showSuccess(intl.formatMessage({ id: "notification.stream-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={
|
||||
{
|
||||
incomingPort: data?.incomingPort,
|
||||
forwardingHost: data?.forwardingHost,
|
||||
forwardingPort: data?.forwardingPort,
|
||||
tcpForwarding: data?.tcpForwarding,
|
||||
udpForwarding: data?.udpForwarding,
|
||||
certificateId: data?.certificateId,
|
||||
meta: data?.meta || {},
|
||||
} as any
|
||||
}
|
||||
onSubmit={onSubmit}
|
||||
>
|
||||
{({ setFieldValue }: any) => (
|
||||
<Form>
|
||||
<Modal.Header closeButton>
|
||||
<Modal.Title>
|
||||
{intl.formatMessage({ id: data?.id ? "stream.edit" : "stream.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>
|
||||
</ul>
|
||||
</div>
|
||||
<div className="card-body">
|
||||
<div className="tab-content">
|
||||
<div className="tab-pane active show" id="tab-details" role="tabpanel">
|
||||
<Field name="incomingPort" validate={validateNumber(1, 65535)}>
|
||||
{({ field, form }: any) => (
|
||||
<div className="mb-3">
|
||||
<label className="form-label" htmlFor="incomingPort">
|
||||
{intl.formatMessage({ id: "stream.incoming-port" })}
|
||||
</label>
|
||||
<input
|
||||
id="incomingPort"
|
||||
type="number"
|
||||
min={1}
|
||||
max={65535}
|
||||
className={`form-control ${form.errors.incomingPort && form.touched.incomingPort ? "is-invalid" : ""}`}
|
||||
required
|
||||
placeholder="eg: 8080"
|
||||
{...field}
|
||||
/>
|
||||
{form.errors.incomingPort ? (
|
||||
<div className="invalid-feedback">
|
||||
{form.errors.incomingPort &&
|
||||
form.touched.incomingPort
|
||||
? form.errors.incomingPort
|
||||
: null}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
)}
|
||||
</Field>
|
||||
<div className="row">
|
||||
<div className="col-md-8">
|
||||
<Field name="forwardingHost" validate={validateString(1, 255)}>
|
||||
{({ field, form }: any) => (
|
||||
<div className="mb-3">
|
||||
<label
|
||||
className="form-label"
|
||||
htmlFor="forwardingHost"
|
||||
>
|
||||
{intl.formatMessage({
|
||||
id: "stream.forward-host",
|
||||
})}
|
||||
</label>
|
||||
<input
|
||||
id="forwardingHost"
|
||||
type="text"
|
||||
className={`form-control ${form.errors.forwardingHost && form.touched.forwardingHost ? "is-invalid" : ""}`}
|
||||
required
|
||||
placeholder="example.com or 10.0.0.1 or 2001:db8:3333:4444:5555:6666:7777:8888"
|
||||
{...field}
|
||||
/>
|
||||
{form.errors.forwardingHost ? (
|
||||
<div className="invalid-feedback">
|
||||
{form.errors.forwardingHost &&
|
||||
form.touched.forwardingHost
|
||||
? form.errors.forwardingHost
|
||||
: null}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
)}
|
||||
</Field>
|
||||
</div>
|
||||
<div className="col-md-4">
|
||||
<Field
|
||||
name="forwardingPort"
|
||||
validate={validateNumber(1, 65535)}
|
||||
>
|
||||
{({ field, form }: any) => (
|
||||
<div className="mb-3">
|
||||
<label
|
||||
className="form-label"
|
||||
htmlFor="forwardingPort"
|
||||
>
|
||||
{intl.formatMessage({
|
||||
id: "stream.forward-port",
|
||||
})}
|
||||
</label>
|
||||
<input
|
||||
id="forwardingPort"
|
||||
type="number"
|
||||
min={1}
|
||||
max={65535}
|
||||
className={`form-control ${form.errors.forwardingPort && form.touched.forwardingPort ? "is-invalid" : ""}`}
|
||||
required
|
||||
placeholder="eg: 8081"
|
||||
{...field}
|
||||
/>
|
||||
{form.errors.forwardingPort ? (
|
||||
<div className="invalid-feedback">
|
||||
{form.errors.forwardingPort &&
|
||||
form.touched.forwardingPort
|
||||
? form.errors.forwardingPort
|
||||
: null}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
)}
|
||||
</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">
|
||||
{intl.formatMessage({
|
||||
id: "streams.tcp",
|
||||
})}
|
||||
</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">
|
||||
{intl.formatMessage({
|
||||
id: "streams.udp",
|
||||
})}
|
||||
</span>
|
||||
</label>
|
||||
)}
|
||||
</Field>
|
||||
</div>
|
||||
</div>
|
||||
<div className="tab-pane" id="tab-ssl" role="tabpanel">
|
||||
<SSLCertificateField
|
||||
name="certificateId"
|
||||
label="ssl-certificate"
|
||||
allowNew
|
||||
forHttp={false}
|
||||
/>
|
||||
<SSLOptionsFields forHttp={false} forceDNSForNew requireDomainNames />
|
||||
</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>
|
||||
);
|
||||
}
|
@@ -4,4 +4,5 @@ export * from "./DeleteConfirmModal";
|
||||
export * from "./EventDetailsModal";
|
||||
export * from "./PermissionsModal";
|
||||
export * from "./SetPasswordModal";
|
||||
export * from "./StreamModal";
|
||||
export * from "./UserModal";
|
||||
|
@@ -85,18 +85,18 @@ const validateDomain = (allowWildcards = false) => {
|
||||
const validateDomains = (allowWildcards = false, maxDomains?: number) => {
|
||||
const vDom = validateDomain(allowWildcards);
|
||||
|
||||
return (value: string[]): string | undefined => {
|
||||
if (!value.length) {
|
||||
return (value?: string[]): string | undefined => {
|
||||
if (!value?.length) {
|
||||
return intl.formatMessage({ id: "error.required" });
|
||||
}
|
||||
|
||||
// Deny if the list of domains is hit
|
||||
if (maxDomains && value.length >= maxDomains) {
|
||||
if (maxDomains && value?.length >= maxDomains) {
|
||||
return intl.formatMessage({ id: "error.max-domains" }, { max: maxDomains });
|
||||
}
|
||||
|
||||
// validate each domain
|
||||
for (let i = 0; i < value.length; i++) {
|
||||
for (let i = 0; i < value?.length; i++) {
|
||||
if (!vDom(value[i])) {
|
||||
return intl.formatMessage({ id: "error.invalid-domain" }, { domain: value[i] });
|
||||
}
|
||||
|
@@ -5,17 +5,24 @@ import { intl } from "src/locale";
|
||||
interface Props {
|
||||
tableInstance: ReactTable<any>;
|
||||
onNew?: () => void;
|
||||
isFiltered?: boolean;
|
||||
}
|
||||
export default function Empty({ tableInstance, onNew }: 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: "dead-hosts.empty" })}</h2>
|
||||
<p className="text-muted">{intl.formatMessage({ id: "empty-subtitle" })}</p>
|
||||
<Button className="btn-red my-3" onClick={onNew}>
|
||||
{intl.formatMessage({ id: "dead-hosts.add" })}
|
||||
</Button>
|
||||
{isFiltered ? (
|
||||
<h2>{intl.formatMessage({ id: "empty-search" })}</h2>
|
||||
) : (
|
||||
<>
|
||||
<h2>{intl.formatMessage({ id: "dead-hosts.empty" })}</h2>
|
||||
<p className="text-muted">{intl.formatMessage({ id: "empty-subtitle" })}</p>
|
||||
<Button className="btn-red my-3" onClick={onNew}>
|
||||
{intl.formatMessage({ id: "dead-hosts.add" })}
|
||||
</Button>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
|
@@ -9,13 +9,14 @@ import Empty from "./Empty";
|
||||
|
||||
interface Props {
|
||||
data: DeadHost[];
|
||||
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, onEdit, onDelete, onDisableToggle, onNew }: Props) {
|
||||
export default function Table({ data, isFetching, onEdit, onDelete, onDisableToggle, onNew, isFiltered }: Props) {
|
||||
const columnHelper = createColumnHelper<DeadHost>();
|
||||
const columns = useMemo(
|
||||
() => [
|
||||
@@ -133,6 +134,9 @@ export default function Table({ data, isFetching, onEdit, onDelete, onDisableTog
|
||||
});
|
||||
|
||||
return (
|
||||
<TableLayout tableInstance={tableInstance} emptyState={<Empty tableInstance={tableInstance} onNew={onNew} />} />
|
||||
<TableLayout
|
||||
tableInstance={tableInstance}
|
||||
emptyState={<Empty tableInstance={tableInstance} onNew={onNew} isFiltered={isFiltered} />}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
@@ -81,6 +81,7 @@ export default function TableWrapper() {
|
||||
</div>
|
||||
<Table
|
||||
data={filtered ?? data ?? []}
|
||||
isFiltered={!!search}
|
||||
isFetching={isFetching}
|
||||
onEdit={(id: number) => setEditId(id)}
|
||||
onDelete={(id: number) => setDeleteId(id)}
|
||||
|
@@ -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: "streams.empty" })}</h2>
|
||||
<p className="text-muted">{intl.formatMessage({ id: "empty-subtitle" })}</p>
|
||||
<Button className="btn-blue my-3">{intl.formatMessage({ id: "streams.add" })}</Button>
|
||||
{isFiltered ? (
|
||||
<h2>{intl.formatMessage({ id: "empty-search" })}</h2>
|
||||
) : (
|
||||
<>
|
||||
<h2>{intl.formatMessage({ id: "streams.empty" })}</h2>
|
||||
<p className="text-muted">{intl.formatMessage({ id: "empty-subtitle" })}</p>
|
||||
<Button className="btn-blue my-3" onClick={onNew}>
|
||||
{intl.formatMessage({ id: "streams.add" })}
|
||||
</Button>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
|
@@ -2,16 +2,21 @@ import { IconDotsVertical, IconEdit, IconPower, IconTrash } from "@tabler/icons-
|
||||
import { createColumnHelper, getCoreRowModel, useReactTable } from "@tanstack/react-table";
|
||||
import { useMemo } from "react";
|
||||
import type { Stream } from "src/api/backend";
|
||||
import { CertificateFormatter, DomainsFormatter, GravatarFormatter, StatusFormatter } from "src/components";
|
||||
import { CertificateFormatter, GravatarFormatter, StatusFormatter, ValueWithDateFormatter } from "src/components";
|
||||
import { TableLayout } from "src/components/Table/TableLayout";
|
||||
import { intl } from "src/locale";
|
||||
import Empty from "./Empty";
|
||||
|
||||
interface Props {
|
||||
data: Stream[];
|
||||
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, isFiltered, onEdit, onDelete, onDisableToggle, onNew }: Props) {
|
||||
const columnHelper = createColumnHelper<Stream>();
|
||||
const columns = useMemo(
|
||||
() => [
|
||||
@@ -30,8 +35,7 @@ export default function Table({ data, isFetching }: Props) {
|
||||
header: intl.formatMessage({ id: "column.incoming-port" }),
|
||||
cell: (info: any) => {
|
||||
const value = info.getValue();
|
||||
// Bit of a hack to reuse the DomainsFormatter component
|
||||
return <DomainsFormatter domains={[value.incomingPort]} createdOn={value.createdOn} />;
|
||||
return <ValueWithDateFormatter value={value.incomingPort} createdOn={value.createdOn} />;
|
||||
},
|
||||
}),
|
||||
columnHelper.accessor((row: any) => row, {
|
||||
@@ -99,16 +103,37 @@ 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" })}
|
||||
</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>
|
||||
@@ -121,7 +146,7 @@ export default function Table({ data, isFetching }: Props) {
|
||||
},
|
||||
}),
|
||||
],
|
||||
[columnHelper],
|
||||
[columnHelper, onEdit, onDisableToggle, onDelete],
|
||||
);
|
||||
|
||||
const tableInstance = useReactTable<Stream>({
|
||||
@@ -135,5 +160,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} />}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
@@ -1,11 +1,19 @@
|
||||
import { IconSearch } from "@tabler/icons-react";
|
||||
import { useQueryClient } from "@tanstack/react-query";
|
||||
import { useState } from "react";
|
||||
import Alert from "react-bootstrap/Alert";
|
||||
import { Button, LoadingPage } from "src/components";
|
||||
import { useStreams } from "src/hooks";
|
||||
import { intl } from "src/locale";
|
||||
import { DeleteConfirmModal, StreamModal } from "src/modals";
|
||||
import { showSuccess } from "src/notifications";
|
||||
import Table from "./Table";
|
||||
|
||||
export default function TableWrapper() {
|
||||
const queryClient = useQueryClient();
|
||||
const [search, setSearch] = useState("");
|
||||
const [editId, setEditId] = useState(0 as number | "new");
|
||||
const [deleteId, setDeleteId] = useState(0);
|
||||
const { isFetching, isLoading, isError, error, data } = useStreams(["owner", "certificate"]);
|
||||
|
||||
if (isLoading) {
|
||||
@@ -16,6 +24,29 @@ export default function TableWrapper() {
|
||||
return <Alert variant="danger">{error?.message || "Unknown error"}</Alert>;
|
||||
}
|
||||
|
||||
const handleDelete = async () => {
|
||||
// await deleteDeadHost(deleteId);
|
||||
showSuccess(intl.formatMessage({ id: "notification.host-deleted" }));
|
||||
};
|
||||
|
||||
const handleDisableToggle = async (id: number, enabled: boolean) => {
|
||||
// await toggleDeadHost(id, enabled);
|
||||
queryClient.invalidateQueries({ queryKey: ["dead-hosts"] });
|
||||
queryClient.invalidateQueries({ queryKey: ["dead-host", id] });
|
||||
showSuccess(intl.formatMessage({ id: enabled ? "notification.host-enabled" : "notification.host-disabled" }));
|
||||
};
|
||||
|
||||
let filtered = null;
|
||||
if (search && data) {
|
||||
filtered = data?.filter((_item) => {
|
||||
return true;
|
||||
// return item.domainNames.some((domain: string) => domain.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-blue" />
|
||||
@@ -27,25 +58,46 @@ 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-blue">
|
||||
{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-blue" onClick={() => setEditId("new")}>
|
||||
{intl.formatMessage({ id: "streams.add" })}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<Table data={data ?? []} isFetching={isFetching} />
|
||||
<Table
|
||||
data={filtered ?? data ?? []}
|
||||
isFetching={isFetching}
|
||||
onEdit={(id: number) => setEditId(id)}
|
||||
onDelete={(id: number) => setDeleteId(id)}
|
||||
onDisableToggle={handleDisableToggle}
|
||||
onNew={() => setEditId("new")}
|
||||
/>
|
||||
{editId ? <StreamModal id={editId} onClose={() => setEditId(0)} /> : null}
|
||||
{deleteId ? (
|
||||
<DeleteConfirmModal
|
||||
title={intl.formatMessage({ id: "stream.delete.title" })}
|
||||
onConfirm={handleDelete}
|
||||
onClose={() => setDeleteId(0)}
|
||||
invalidations={[["streams"], ["stream", deleteId]]}
|
||||
>
|
||||
{intl.formatMessage({ id: "stream.delete.content" })}
|
||||
</DeleteConfirmModal>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
@@ -8,9 +8,6 @@ interface Props {
|
||||
isFiltered?: boolean;
|
||||
}
|
||||
export default function Empty({ tableInstance, onNewUser, isFiltered }: Props) {
|
||||
if (isFiltered) {
|
||||
}
|
||||
|
||||
return (
|
||||
<tr>
|
||||
<td colSpan={tableInstance.getVisibleFlatColumns().length}>
|
||||
|
Reference in New Issue
Block a user