refactor: base components
This commit is contained in:
@@ -31,22 +31,23 @@ export interface DialogRef {
|
|||||||
close: () => void;
|
close: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const BaseDialog: React.FC<Props> = (props) => {
|
export const BaseDialog: React.FC<Props> = ({
|
||||||
const {
|
open,
|
||||||
open,
|
title,
|
||||||
title,
|
children,
|
||||||
children,
|
okBtn,
|
||||||
okBtn,
|
cancelBtn,
|
||||||
cancelBtn,
|
contentSx,
|
||||||
contentSx,
|
disableCancel,
|
||||||
disableCancel,
|
disableOk,
|
||||||
disableOk,
|
disableFooter,
|
||||||
disableFooter,
|
loading,
|
||||||
loading,
|
onOk,
|
||||||
} = props;
|
onCancel,
|
||||||
|
onClose,
|
||||||
|
}) => {
|
||||||
return (
|
return (
|
||||||
<Dialog open={open} onClose={props.onClose}>
|
<Dialog open={open} onClose={onClose}>
|
||||||
<DialogTitle>{title}</DialogTitle>
|
<DialogTitle>{title}</DialogTitle>
|
||||||
|
|
||||||
<DialogContent sx={contentSx}>{children}</DialogContent>
|
<DialogContent sx={contentSx}>{children}</DialogContent>
|
||||||
@@ -54,16 +55,12 @@ export const BaseDialog: React.FC<Props> = (props) => {
|
|||||||
{!disableFooter && (
|
{!disableFooter && (
|
||||||
<DialogActions>
|
<DialogActions>
|
||||||
{!disableCancel && (
|
{!disableCancel && (
|
||||||
<Button variant="outlined" onClick={props.onCancel}>
|
<Button variant="outlined" onClick={onCancel}>
|
||||||
{cancelBtn}
|
{cancelBtn}
|
||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
{!disableOk && (
|
{!disableOk && (
|
||||||
<LoadingButton
|
<LoadingButton loading={loading} variant="contained" onClick={onOk}>
|
||||||
loading={loading}
|
|
||||||
variant="contained"
|
|
||||||
onClick={props.onOk}
|
|
||||||
>
|
|
||||||
{okBtn}
|
{okBtn}
|
||||||
</LoadingButton>
|
</LoadingButton>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -20,10 +20,8 @@ interface Props {
|
|||||||
children?: ReactNode;
|
children?: ReactNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const BaseErrorBoundary = (props: Props) => {
|
export const BaseErrorBoundary = ({ children }: Props) => {
|
||||||
return (
|
return (
|
||||||
<ErrorBoundary FallbackComponent={ErrorFallback}>
|
<ErrorBoundary FallbackComponent={ErrorFallback}>{children}</ErrorBoundary>
|
||||||
{props.children}
|
|
||||||
</ErrorBoundary>
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -9,30 +9,36 @@ type Props = {
|
|||||||
children?: React.ReactNode;
|
children?: React.ReactNode;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const BaseFieldset: React.FC<Props> = (props: Props) => {
|
export const BaseFieldset: React.FC<Props> = ({
|
||||||
|
label,
|
||||||
|
fontSize,
|
||||||
|
width,
|
||||||
|
padding,
|
||||||
|
children,
|
||||||
|
}: Props) => {
|
||||||
const Fieldset = styled(Box)<{ component?: string }>(() => ({
|
const Fieldset = styled(Box)<{ component?: string }>(() => ({
|
||||||
position: "relative",
|
position: "relative",
|
||||||
border: "1px solid #bbb",
|
border: "1px solid #bbb",
|
||||||
borderRadius: "5px",
|
borderRadius: "5px",
|
||||||
width: props.width ?? "auto",
|
width: width ?? "auto",
|
||||||
padding: props.padding ?? "15px",
|
padding: padding ?? "15px",
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const Label = styled("legend")(({ theme }) => ({
|
const Label = styled("legend")(({ theme }) => ({
|
||||||
position: "absolute",
|
position: "absolute",
|
||||||
top: "-10px",
|
top: "-10px",
|
||||||
left: props.padding ?? "15px",
|
left: padding ?? "15px",
|
||||||
backgroundColor: theme.palette.background.paper,
|
backgroundColor: theme.palette.background.paper,
|
||||||
backgroundImage:
|
backgroundImage:
|
||||||
"linear-gradient(rgba(255, 255, 255, 0.16), rgba(255, 255, 255, 0.16))",
|
"linear-gradient(rgba(255, 255, 255, 0.16), rgba(255, 255, 255, 0.16))",
|
||||||
color: theme.palette.text.primary,
|
color: theme.palette.text.primary,
|
||||||
fontSize: props.fontSize ?? "1em",
|
fontSize: fontSize ?? "1em",
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Fieldset component="fieldset">
|
<Fieldset component="fieldset">
|
||||||
<Label>{props.label}</Label>
|
<Label>{label}</Label>
|
||||||
{props.children}
|
{children}
|
||||||
</Fieldset>
|
</Fieldset>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,6 +1,13 @@
|
|||||||
import { Box, SvgIcon, TextField, styled } from "@mui/material";
|
import { Box, SvgIcon, TextField, styled } from "@mui/material";
|
||||||
import Tooltip from "@mui/material/Tooltip";
|
import Tooltip from "@mui/material/Tooltip";
|
||||||
import { ChangeEvent, useEffect, useMemo, useRef, useState } from "react";
|
import {
|
||||||
|
ChangeEvent,
|
||||||
|
useCallback,
|
||||||
|
useEffect,
|
||||||
|
useMemo,
|
||||||
|
useRef,
|
||||||
|
useState,
|
||||||
|
} from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
import matchCaseIcon from "@/assets/image/component/match_case.svg?react";
|
import matchCaseIcon from "@/assets/image/component/match_case.svg?react";
|
||||||
@@ -35,15 +42,20 @@ const StyledTextField = styled(TextField)(({ theme }) => ({
|
|||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
export const BaseSearchBox = (props: SearchProps) => {
|
export const BaseSearchBox = ({
|
||||||
|
placeholder,
|
||||||
|
matchCase: defaultMatchCase = false,
|
||||||
|
matchWholeWord: defaultMatchWholeWord = false,
|
||||||
|
useRegularExpression: defaultUseRegularExpression = false,
|
||||||
|
onSearch,
|
||||||
|
}: SearchProps) => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const inputRef = useRef<HTMLInputElement>(null);
|
const inputRef = useRef<HTMLInputElement>(null);
|
||||||
const [matchCase, setMatchCase] = useState(props.matchCase ?? false);
|
const onSearchRef = useRef(onSearch);
|
||||||
const [matchWholeWord, setMatchWholeWord] = useState(
|
const [matchCase, setMatchCase] = useState(defaultMatchCase);
|
||||||
props.matchWholeWord ?? false,
|
const [matchWholeWord, setMatchWholeWord] = useState(defaultMatchWholeWord);
|
||||||
);
|
|
||||||
const [useRegularExpression, setUseRegularExpression] = useState(
|
const [useRegularExpression, setUseRegularExpression] = useState(
|
||||||
props.useRegularExpression ?? false,
|
defaultUseRegularExpression,
|
||||||
);
|
);
|
||||||
const [errorMessage, setErrorMessage] = useState("");
|
const [errorMessage, setErrorMessage] = useState("");
|
||||||
|
|
||||||
@@ -56,8 +68,8 @@ export const BaseSearchBox = (props: SearchProps) => {
|
|||||||
inheritViewBox: true,
|
inheritViewBox: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
// 验证正则表达式的辅助函数
|
// Helper that verifies whether a pattern is a valid regular expression
|
||||||
const validateRegex = (pattern: string) => {
|
const validateRegex = useCallback((pattern: string) => {
|
||||||
if (!pattern) return true;
|
if (!pattern) return true;
|
||||||
try {
|
try {
|
||||||
new RegExp(pattern);
|
new RegExp(pattern);
|
||||||
@@ -66,46 +78,48 @@ export const BaseSearchBox = (props: SearchProps) => {
|
|||||||
console.warn("[BaseSearchBox] validateRegex error:", e);
|
console.warn("[BaseSearchBox] validateRegex error:", e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
}, []);
|
||||||
|
|
||||||
const createMatcher = useMemo(() => {
|
const createMatcher = useMemo(() => {
|
||||||
return (searchText: string) => {
|
return (searchText: string) => {
|
||||||
try {
|
if (useRegularExpression && searchText) {
|
||||||
// 当启用正则表达式验证是否合规
|
const isValid = validateRegex(searchText);
|
||||||
if (useRegularExpression && searchText) {
|
if (!isValid) {
|
||||||
const isValid = validateRegex(searchText);
|
// Invalid regex should result in no match
|
||||||
if (!isValid) {
|
return () => false;
|
||||||
throw new Error(t("Invalid regular expression"));
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return (content: string) => {
|
||||||
|
if (!searchText) {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (content: string) => {
|
const item = !matchCase ? content.toLowerCase() : content;
|
||||||
if (!searchText) return true;
|
const searchItem = !matchCase ? searchText.toLowerCase() : searchText;
|
||||||
|
|
||||||
const item = !matchCase ? content.toLowerCase() : content;
|
if (useRegularExpression) {
|
||||||
const searchItem = !matchCase ? searchText.toLowerCase() : searchText;
|
return new RegExp(searchItem).test(item);
|
||||||
|
}
|
||||||
|
|
||||||
if (useRegularExpression) {
|
if (matchWholeWord) {
|
||||||
return new RegExp(searchItem).test(item);
|
return new RegExp(`\\b${searchItem}\\b`).test(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (matchWholeWord) {
|
return item.includes(searchItem);
|
||||||
return new RegExp(`\\b${searchItem}\\b`).test(item);
|
};
|
||||||
}
|
|
||||||
|
|
||||||
return item.includes(searchItem);
|
|
||||||
};
|
|
||||||
} catch (err) {
|
|
||||||
setErrorMessage(err instanceof Error ? err.message : `${err}`);
|
|
||||||
return () => false; // 无效正则规则 不匹配值
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}, [matchCase, matchWholeWord, useRegularExpression, t]);
|
}, [matchCase, matchWholeWord, useRegularExpression, validateRegex]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
onSearchRef.current = onSearch;
|
||||||
|
}, [onSearch]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!inputRef.current) return;
|
if (!inputRef.current) return;
|
||||||
const value = inputRef.current.value;
|
const value = inputRef.current.value;
|
||||||
props.onSearch(createMatcher(value), {
|
const matcher = createMatcher(value);
|
||||||
|
onSearchRef.current(matcher, {
|
||||||
text: value,
|
text: value,
|
||||||
matchCase,
|
matchCase,
|
||||||
matchWholeWord,
|
matchWholeWord,
|
||||||
@@ -117,7 +131,7 @@ export const BaseSearchBox = (props: SearchProps) => {
|
|||||||
const value = e.target?.value ?? "";
|
const value = e.target?.value ?? "";
|
||||||
setErrorMessage("");
|
setErrorMessage("");
|
||||||
|
|
||||||
// 验证正则表达式
|
// Validate regex input eagerly
|
||||||
if (useRegularExpression && value) {
|
if (useRegularExpression && value) {
|
||||||
const isValid = validateRegex(value);
|
const isValid = validateRegex(value);
|
||||||
if (!isValid) {
|
if (!isValid) {
|
||||||
@@ -125,7 +139,8 @@ export const BaseSearchBox = (props: SearchProps) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
props.onSearch(createMatcher(value), {
|
const matcher = createMatcher(value);
|
||||||
|
onSearchRef.current(matcher, {
|
||||||
text: value,
|
text: value,
|
||||||
matchCase,
|
matchCase,
|
||||||
matchWholeWord,
|
matchWholeWord,
|
||||||
@@ -133,6 +148,21 @@ export const BaseSearchBox = (props: SearchProps) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleToggleUseRegularExpression = () => {
|
||||||
|
setUseRegularExpression((prev) => {
|
||||||
|
const next = !prev;
|
||||||
|
if (!next) {
|
||||||
|
setErrorMessage("");
|
||||||
|
} else {
|
||||||
|
const value = inputRef.current?.value ?? "";
|
||||||
|
if (value && !validateRegex(value)) {
|
||||||
|
setErrorMessage(t("Invalid regular expression"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return next;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Tooltip title={errorMessage || ""} placement="bottom-start">
|
<Tooltip title={errorMessage || ""} placement="bottom-start">
|
||||||
<StyledTextField
|
<StyledTextField
|
||||||
@@ -143,7 +173,7 @@ export const BaseSearchBox = (props: SearchProps) => {
|
|||||||
size="small"
|
size="small"
|
||||||
variant="outlined"
|
variant="outlined"
|
||||||
spellCheck="false"
|
spellCheck="false"
|
||||||
placeholder={props.placeholder ?? t("Filter conditions")}
|
placeholder={placeholder ?? t("Filter conditions")}
|
||||||
sx={{ input: { py: 0.65, px: 1.25 } }}
|
sx={{ input: { py: 0.65, px: 1.25 } }}
|
||||||
onChange={onChange}
|
onChange={onChange}
|
||||||
error={!!errorMessage}
|
error={!!errorMessage}
|
||||||
@@ -158,7 +188,7 @@ export const BaseSearchBox = (props: SearchProps) => {
|
|||||||
component={matchCaseIcon}
|
component={matchCaseIcon}
|
||||||
{...iconStyle}
|
{...iconStyle}
|
||||||
aria-label={matchCase ? "active" : "inactive"}
|
aria-label={matchCase ? "active" : "inactive"}
|
||||||
onClick={() => setMatchCase(!matchCase)}
|
onClick={() => setMatchCase((prev) => !prev)}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</Tooltip>
|
</Tooltip>
|
||||||
@@ -168,7 +198,7 @@ export const BaseSearchBox = (props: SearchProps) => {
|
|||||||
component={matchWholeWordIcon}
|
component={matchWholeWordIcon}
|
||||||
{...iconStyle}
|
{...iconStyle}
|
||||||
aria-label={matchWholeWord ? "active" : "inactive"}
|
aria-label={matchWholeWord ? "active" : "inactive"}
|
||||||
onClick={() => setMatchWholeWord(!matchWholeWord)}
|
onClick={() => setMatchWholeWord((prev) => !prev)}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</Tooltip>
|
</Tooltip>
|
||||||
@@ -178,10 +208,8 @@ export const BaseSearchBox = (props: SearchProps) => {
|
|||||||
component={useRegularExpressionIcon}
|
component={useRegularExpressionIcon}
|
||||||
aria-label={useRegularExpression ? "active" : "inactive"}
|
aria-label={useRegularExpression ? "active" : "inactive"}
|
||||||
{...iconStyle}
|
{...iconStyle}
|
||||||
onClick={() =>
|
onClick={handleToggleUseRegularExpression}
|
||||||
setUseRegularExpression(!useRegularExpression)
|
/>
|
||||||
}
|
|
||||||
/>{" "}
|
|
||||||
</div>
|
</div>
|
||||||
</Tooltip>
|
</Tooltip>
|
||||||
</Box>
|
</Box>
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { useCallback } from "react";
|
|||||||
|
|
||||||
import { installService, restartCore } from "@/services/cmds";
|
import { installService, restartCore } from "@/services/cmds";
|
||||||
import { showNotice } from "@/services/noticeService";
|
import { showNotice } from "@/services/noticeService";
|
||||||
|
|
||||||
import { useSystemState } from "./use-system-state";
|
import { useSystemState } from "./use-system-state";
|
||||||
|
|
||||||
const executeWithErrorHandling = async (
|
const executeWithErrorHandling = async (
|
||||||
@@ -34,6 +35,8 @@ export const useServiceInstaller = () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
await executeWithErrorHandling(() => restartCore(), "Restarting Core...");
|
await executeWithErrorHandling(() => restartCore(), "Restarting Core...");
|
||||||
|
await mutateRunningMode();
|
||||||
|
await mutateServiceOk();
|
||||||
}, [mutateRunningMode, mutateServiceOk]);
|
}, [mutateRunningMode, mutateServiceOk]);
|
||||||
return { installServiceAndRestartCore };
|
return { installServiceAndRestartCore };
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -37,6 +37,8 @@ export const useServiceUninstaller = () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
await executeWithErrorHandling(() => restartCore(), "Restarting Core...");
|
await executeWithErrorHandling(() => restartCore(), "Restarting Core...");
|
||||||
|
await mutateRunningMode();
|
||||||
|
await mutateServiceOk();
|
||||||
}, [mutateRunningMode, mutateServiceOk]);
|
}, [mutateRunningMode, mutateServiceOk]);
|
||||||
|
|
||||||
return { uninstallServiceAndRestartCore };
|
return { uninstallServiceAndRestartCore };
|
||||||
|
|||||||
Reference in New Issue
Block a user