Files
Checkmate/client/src/Pages/Settings/SettingsEmail.jsx
T

378 lines
9.7 KiB
React
Raw Normal View History

2025-05-09 14:52:09 -07:00
import Box from "@mui/material/Box";
import Typography from "@mui/material/Typography";
2025-09-27 14:54:55 -07:00
import ConfigBox from "@/Components/v1/ConfigBox/index.jsx";
import TextInput from "@/Components/v1/Inputs/TextInput/index.jsx";
2025-05-09 14:52:09 -07:00
import Button from "@mui/material/Button";
import Stack from "@mui/material/Stack";
2025-06-23 10:02:06 +08:00
import { Switch } from "@mui/material";
2025-09-27 14:54:55 -07:00
import TextLink from "@/Components/v1/TextLink/index.jsx";
2025-05-09 14:52:09 -07:00
// Utils
import { useTheme } from "@emotion/react";
import { PropTypes } from "prop-types";
2025-06-24 14:30:14 +08:00
import { useState } from "react";
2025-05-09 14:52:09 -07:00
import { useTranslation } from "react-i18next";
2025-09-27 14:54:55 -07:00
import { PasswordEndAdornment } from "@/Components/v1/Inputs/TextInput/Adornments/index.jsx";
2026-02-09 23:37:07 +00:00
import { usePost } from "@/Hooks/UseApi";
import { useSelector } from "react-redux";
2026-01-12 23:57:39 +00:00
import { createToast } from "@/Utils/toastUtils.jsx";
import { Controller } from "react-hook-form";
2025-05-09 14:52:09 -07:00
const SettingsEmail = ({
isAdmin,
2025-05-09 14:52:09 -07:00
HEADER_SX,
2025-06-17 10:58:41 +08:00
isEmailPasswordSet,
emailPasswordHasBeenReset,
setEmailPasswordHasBeenReset,
control,
defaults,
formValues,
setValue,
2025-05-09 14:52:09 -07:00
}) => {
// Setup
2025-05-09 14:52:09 -07:00
const { t } = useTranslation();
const theme = useTheme();
2025-06-13 12:22:28 +05:30
// Destructure settings with default values
const {
systemEmailHost = "",
systemEmailPort = "",
systemEmailSecure = false,
systemEmailPool = false,
systemEmailUser = "",
systemEmailAddress = "",
systemEmailPassword = "",
systemEmailTLSServername = "",
systemEmailConnectionHost = "localhost",
systemEmailIgnoreTLS = false,
systemEmailRequireTLS = false,
systemEmailRejectUnauthorized = true,
} = formValues || {};
2025-05-09 14:52:09 -07:00
// Network
2026-02-09 23:37:07 +00:00
const { post: sendTestEmailFn, loading: isSending } = usePost();
const user = useSelector((state) => state.auth.user);
// Handlers
2025-05-09 14:52:09 -07:00
/**
* Handle sending test email with current form values
*/
const handleSendTestEmail = () => {
// Basic validation
2025-06-13 12:22:28 +05:30
if (
2026-02-09 23:41:21 +00:00
!systemEmailHost ||
!systemEmailPort ||
!systemEmailAddress ||
!systemEmailPassword
2025-06-13 12:22:28 +05:30
) {
createToast({
2025-07-01 15:25:45 +08:00
body: t("settingsPage.emailSettings.toastEmailRequiredFieldsError"),
2025-05-27 09:10:32 -07:00
variant: "error",
});
return;
}
2025-05-27 09:10:32 -07:00
2026-02-09 23:41:21 +00:00
// Send test email - only include optional fields if they have values
2026-02-09 23:37:07 +00:00
sendTestEmailFn("/settings/test-email", {
to: user.email,
2026-02-09 23:41:21 +00:00
systemEmailHost,
systemEmailPort,
systemEmailAddress,
systemEmailPassword: password || systemEmailPassword,
systemEmailSecure,
systemEmailPool,
systemEmailIgnoreTLS,
systemEmailRequireTLS,
systemEmailRejectUnauthorized,
...(systemEmailUser && { systemEmailUser }),
...(systemEmailTLSServername && { systemEmailTLSServername }),
...(systemEmailConnectionHost && { systemEmailConnectionHost }),
2026-02-09 23:37:07 +00:00
});
};
if (!isAdmin) {
return null;
}
2025-05-09 14:52:09 -07:00
return (
<ConfigBox>
<Box>
2025-05-27 09:10:32 -07:00
<Typography
component="h1"
variant="h2"
>
2025-06-24 14:30:14 +08:00
{t("settingsPage.emailSettings.title")}
</Typography>
<Typography sx={HEADER_SX}>
{t("settingsPage.emailSettings.description")}
2025-05-27 09:10:32 -07:00
</Typography>
2025-05-09 14:52:09 -07:00
</Box>
<Box>
<Stack gap={theme.spacing(10)}>
<Box>
<Controller
2025-05-09 14:52:09 -07:00
name="systemEmailHost"
control={control}
defaultValue={defaults.systemEmailHost}
render={({ field, fieldState }) => (
<TextInput
{...field}
label={t("settingsPage.emailSettings.labelHost")}
placeholder="smtp.gmail.com"
error={!!fieldState.error}
helperText={fieldState.error?.message}
/>
)}
2025-05-09 14:52:09 -07:00
/>
</Box>
<Box>
<Controller
2025-05-09 14:52:09 -07:00
name="systemEmailPort"
control={control}
defaultValue={defaults.systemEmailPort}
render={({ field, fieldState }) => (
<TextInput
{...field}
label={t("settingsPage.emailSettings.labelPort")}
placeholder="425"
type="number"
error={!!fieldState.error}
helperText={fieldState.error?.message}
/>
)}
2025-05-09 14:52:09 -07:00
/>
</Box>
<Box>
<Controller
2025-05-09 14:52:09 -07:00
name="systemEmailUser"
control={control}
defaultValue={defaults.systemEmailUser}
render={({ field, fieldState }) => (
<TextInput
{...field}
label={t("settingsPage.emailSettings.labelUser")}
placeholder={t("settingsPage.emailSettings.placeholderUser")}
error={!!fieldState.error}
helperText={fieldState.error?.message}
/>
)}
2025-05-09 14:52:09 -07:00
/>
</Box>
<Box>
<Controller
2025-05-09 14:52:09 -07:00
name="systemEmailAddress"
control={control}
defaultValue={defaults.systemEmailAddress}
render={({ field, fieldState }) => (
<TextInput
{...field}
label={t("settingsPage.emailSettings.labelAddress")}
placeholder="uptime@bluewavelabs.ca"
error={!!fieldState.error}
helperText={fieldState.error?.message}
/>
)}
2025-05-09 14:52:09 -07:00
/>
</Box>
2025-06-17 10:58:41 +08:00
{(isEmailPasswordSet === false || emailPasswordHasBeenReset === true) && (
2025-05-09 14:52:09 -07:00
<Box>
<Controller
2025-05-09 14:52:09 -07:00
name="systemEmailPassword"
control={control}
defaultValue={defaults.systemEmailPassword}
render={({ field, fieldState }) => (
<TextInput
{...field}
label={t("settingsPage.emailSettings.labelPassword")}
type="password"
placeholder="123 456 789 101112"
endAdornment={<PasswordEndAdornment />}
error={!!fieldState.error}
helperText={fieldState.error?.message}
/>
)}
2025-05-09 14:52:09 -07:00
/>
</Box>
)}
2025-06-17 10:58:41 +08:00
{isEmailPasswordSet === true && emailPasswordHasBeenReset === false && (
2025-05-09 14:52:09 -07:00
<Box>
2025-06-24 14:30:14 +08:00
<Typography>{t("settingsPage.emailSettings.labelPasswordSet")}</Typography>
2025-05-09 14:52:09 -07:00
<Button
onClick={() => {
2025-06-17 10:58:41 +08:00
setEmailPasswordHasBeenReset(true);
2025-05-09 14:52:09 -07:00
}}
variant="contained"
color="error"
sx={{ mt: theme.spacing(4) }}
>
{t("reset")}
</Button>
</Box>
)}
2025-06-11 14:42:51 +05:30
<Box>
<Controller
2025-06-11 14:42:51 +05:30
name="systemEmailTLSServername"
control={control}
defaultValue={defaults.systemEmailTLSServername}
render={({ field, fieldState }) => (
<TextInput
{...field}
label={t("settingsPage.emailSettings.labelTLSServername")}
placeholder="bluewavelabs.ca"
/>
)}
2025-06-11 14:42:51 +05:30
/>
</Box>
2025-06-13 20:40:01 +05:30
<Box>
<Controller
2025-06-13 20:40:01 +05:30
name="systemEmailConnectionHost"
control={control}
defaultValue={defaults.systemEmailConnectionHost}
render={({ field, fieldState }) => (
<TextInput
{...field}
label={t("settingsPage.emailSettings.labelConnectionHost")}
placeholder="bluewavelabs.ca"
error={!!fieldState.error}
helperText={fieldState.error?.message}
/>
)}
2025-06-13 20:40:01 +05:30
/>
</Box>
2025-06-11 14:42:51 +05:30
<Box
sx={{
display: "flex",
2025-06-13 12:22:28 +05:30
flexDirection: "column",
2025-06-11 14:42:51 +05:30
gap: theme.spacing(4),
}}
>
{[
[
2025-06-24 14:30:14 +08:00
"settingsPage.emailSettings.labelSecure",
"systemEmailSecure",
systemEmailSecure,
],
[
"settingsPage.emailSettings.labelPool",
"systemEmailPool",
systemEmailPool,
],
[
"settingsPage.emailSettings.labelIgnoreTLS",
"systemEmailIgnoreTLS",
systemEmailIgnoreTLS,
],
[
"settingsPage.emailSettings.labelRequireTLS",
"systemEmailRequireTLS",
systemEmailRequireTLS,
],
[
"settingsPage.emailSettings.labelRejectUnauthorized",
"systemEmailRejectUnauthorized",
systemEmailRejectUnauthorized,
],
].map(([labelKey, name, value]) => (
<Controller
key={name}
name={name}
control={control}
defaultValue={defaults[name]}
render={({ field }) => (
<Box
key={name}
sx={{
display: "flex",
alignItems: "center",
justifyContent: "space-between",
}}
>
<Typography>{t(labelKey)}</Typography>
<Switch
{...field}
checked={field.value}
/>
</Box>
)}
/>
))}
2025-06-23 10:02:06 +08:00
<TextLink
2025-06-24 14:30:14 +08:00
text={t("settingsPage.emailSettings.descriptionTransport")}
linkText={t("settingsPage.emailSettings.linkTransport")}
2025-06-23 10:02:06 +08:00
href="https://nodemailer.com/smtp"
target="_blank"
/>
<Box
component={"pre"}
sx={{
fontFamily: "monospace",
p: 2,
borderRadius: 1,
overflow: "auto",
}}
>
<code>
{JSON.stringify(
{
host: systemEmailHost,
port: systemEmailPort,
secure: systemEmailSecure,
auth: {
user: systemEmailUser || systemEmailAddress,
pass: "<your_password>",
},
name: systemEmailConnectionHost || "localhost",
pool: systemEmailPool,
tls: {
rejectUnauthorized: systemEmailRejectUnauthorized,
ignoreTLS: systemEmailIgnoreTLS,
requireTLS: systemEmailRequireTLS,
servername: systemEmailTLSServername,
},
},
null,
2
)}
</code>
</Box>
2025-06-11 14:42:51 +05:30
</Box>
2026-02-09 23:37:07 +00:00
<pre>
{JSON.stringify({
systemEmailHost: systemEmailHost,
systemEmailAddress: systemEmailAddress,
systemEmailPassword: systemEmailPassword,
2026-02-09 23:37:07 +00:00
})}
</pre>
<Box>
2025-06-14 11:01:47 +05:30
{systemEmailHost &&
systemEmailPort &&
systemEmailAddress &&
systemEmailPassword && (
<Button
variant="contained"
color="accent"
loading={isSending}
onClick={handleSendTestEmail}
>
2025-06-24 14:30:14 +08:00
{t("settingsPage.emailSettings.buttonSendTestEmail")}
2025-06-14 11:01:47 +05:30
</Button>
)}
</Box>
2025-05-09 14:52:09 -07:00
</Stack>
</Box>
</ConfigBox>
);
};
SettingsEmail.propTypes = {
isAdmin: PropTypes.bool,
2025-05-09 14:52:09 -07:00
HEADER_SX: PropTypes.object,
isPasswordSet: PropTypes.bool,
};
export default SettingsEmail;