fix: resolve deprecated warnings in console

This commit is contained in:
wonfen 2025-02-18 07:10:28 +08:00
parent 31ddccd3e1
commit 3b4013a1b0
5 changed files with 109 additions and 80 deletions

View File

@ -1,6 +1,6 @@
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, useRef, useState } from "react"; import { ChangeEvent, useEffect, useRef, useState, useMemo } 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";
@ -22,7 +22,20 @@ type SearchProps = {
onSearch: (match: (content: string) => boolean, state: SearchState) => void; onSearch: (match: (content: string) => boolean, state: SearchState) => void;
}; };
export const BaseSearchBox = styled((props: SearchProps) => { const StyledTextField = styled(TextField)(({ theme }) => ({
"& .MuiInputBase-root": {
background: theme.palette.mode === "light" ? "#fff" : undefined,
paddingRight: "4px",
},
"& .MuiInputBase-root svg[aria-label='active'] path": {
fill: theme.palette.primary.light,
},
"& .MuiInputBase-root svg[aria-label='inactive'] path": {
fill: "#A7A7A7",
},
}));
export const BaseSearchBox = (props: 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 [matchCase, setMatchCase] = useState(props.matchCase ?? false);
@ -43,58 +56,58 @@ export const BaseSearchBox = styled((props: SearchProps) => {
inheritViewBox: true, inheritViewBox: true,
}; };
useEffect(() => { const createMatcher = useMemo(() => {
if (!inputRef.current) return; return (searchText: string) => {
onChange({
target: inputRef.current,
} as ChangeEvent<HTMLInputElement>);
}, [matchCase, matchWholeWord, useRegularExpression]);
const onChange = (e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
props.onSearch(
(content) => doSearch([content], e.target?.value ?? "").length > 0,
{
text: e.target?.value ?? "",
matchCase,
matchWholeWord,
useRegularExpression,
},
);
};
const doSearch = (searchList: string[], searchItem: string) => {
setErrorMessage("");
return searchList.filter((item) => {
try { try {
let searchItemCopy = searchItem; return (content: string) => {
if (!matchCase) { if (!searchText) return true;
item = item.toLowerCase();
searchItemCopy = searchItemCopy.toLowerCase(); let item = !matchCase ? content.toLowerCase() : content;
} let searchItem = !matchCase ? searchText.toLowerCase() : searchText;
if (matchWholeWord) {
const regex = new RegExp(`\\b${searchItemCopy}\\b`);
if (useRegularExpression) { if (useRegularExpression) {
const regexWithOptions = new RegExp(searchItemCopy); return new RegExp(searchItem).test(item);
return regexWithOptions.test(item) && regex.test(item);
} else {
return regex.test(item);
} }
} else if (useRegularExpression) {
const regex = new RegExp(searchItemCopy); if (matchWholeWord) {
return regex.test(item); return new RegExp(`\\b${searchItem}\\b`).test(item);
} else { }
return item.includes(searchItemCopy);
} return item.includes(searchItem);
};
} catch (err) { } catch (err) {
setErrorMessage(`${err}`); setErrorMessage(`${err}`);
return () => true;
} }
};
}, [matchCase, matchWholeWord, useRegularExpression]);
useEffect(() => {
if (!inputRef.current) return;
const value = inputRef.current.value;
setErrorMessage("");
props.onSearch(createMatcher(value), {
text: value,
matchCase,
matchWholeWord,
useRegularExpression,
});
}, [matchCase, matchWholeWord, useRegularExpression, createMatcher]);
const onChange = (e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
const value = e.target?.value ?? "";
setErrorMessage("");
props.onSearch(createMatcher(value), {
text: value,
matchCase,
matchWholeWord,
useRegularExpression,
}); });
}; };
return ( return (
<Tooltip title={errorMessage} placement="bottom-start"> <Tooltip title={errorMessage} placement="bottom-start">
<TextField <StyledTextField
autoComplete="new-password" autoComplete="new-password"
inputRef={inputRef} inputRef={inputRef}
hiddenLabel hiddenLabel
@ -115,9 +128,7 @@ export const BaseSearchBox = styled((props: SearchProps) => {
component={matchCaseIcon} component={matchCaseIcon}
{...iconStyle} {...iconStyle}
aria-label={matchCase ? "active" : "inactive"} aria-label={matchCase ? "active" : "inactive"}
onClick={() => { onClick={() => setMatchCase(!matchCase)}
setMatchCase(!matchCase);
}}
/> />
</div> </div>
</Tooltip> </Tooltip>
@ -127,9 +138,7 @@ export const BaseSearchBox = styled((props: SearchProps) => {
component={matchWholeWordIcon} component={matchWholeWordIcon}
{...iconStyle} {...iconStyle}
aria-label={matchWholeWord ? "active" : "inactive"} aria-label={matchWholeWord ? "active" : "inactive"}
onClick={() => { onClick={() => setMatchWholeWord(!matchWholeWord)}
setMatchWholeWord(!matchWholeWord);
}}
/> />
</div> </div>
</Tooltip> </Tooltip>
@ -139,28 +148,16 @@ export const BaseSearchBox = styled((props: SearchProps) => {
component={useRegularExpressionIcon} component={useRegularExpressionIcon}
aria-label={useRegularExpression ? "active" : "inactive"} aria-label={useRegularExpression ? "active" : "inactive"}
{...iconStyle} {...iconStyle}
onClick={() => { onClick={() =>
setUseRegularExpression(!useRegularExpression); setUseRegularExpression(!useRegularExpression)
}} }
/>{" "} />{" "}
</div> </div>
</Tooltip> </Tooltip>
</Box> </Box>
), ),
}} }}
{...props}
/> />
</Tooltip> </Tooltip>
); );
})(({ theme }) => ({ };
"& .MuiInputBase-root": {
background: theme.palette.mode === "light" ? "#fff" : undefined,
"padding-right": "4px",
},
"& .MuiInputBase-root svg[aria-label='active'] path": {
fill: theme.palette.primary.light,
},
"& .MuiInputBase-root svg[aria-label='inactive'] path": {
fill: "#A7A7A7",
},
}));

View File

@ -100,7 +100,7 @@ export const ProxyRender = (props: RenderProps) => {
<ListItemText <ListItemText
primary={<StyledPrimary>{group.name}</StyledPrimary>} primary={<StyledPrimary>{group.name}</StyledPrimary>}
secondary={ secondary={
<ListItemTextChild <Box
sx={{ sx={{
overflow: "hidden", overflow: "hidden",
display: "flex", display: "flex",
@ -108,16 +108,19 @@ export const ProxyRender = (props: RenderProps) => {
pt: "2px", pt: "2px",
}} }}
> >
<Box sx={{ marginTop: "2px" }}> <Box component="span" sx={{ marginTop: "2px" }}>
<StyledTypeBox>{group.type}</StyledTypeBox> <StyledTypeBox>{group.type}</StyledTypeBox>
<StyledSubtitle sx={{ color: "text.secondary" }}> <StyledSubtitle sx={{ color: "text.secondary" }}>
{group.now} {group.now}
</StyledSubtitle> </StyledSubtitle>
</Box> </Box>
</ListItemTextChild> </Box>
} }
secondaryTypographyProps={{ slotProps={{
sx: { display: "flex", alignItems: "center", color: "#ccc" }, secondary: {
component: "div",
sx: { display: "flex", alignItems: "center", color: "#ccc" },
},
}} }}
/> />
{headState?.open ? <ExpandLessRounded /> : <ExpandMoreRounded />} {headState?.open ? <ExpandLessRounded /> : <ExpandMoreRounded />}
@ -219,11 +222,7 @@ const StyledSubtitle = styled("span")`
white-space: nowrap; white-space: nowrap;
`; `;
const ListItemTextChild = styled("span")` const StyledTypeBox = styled(Box)(({ theme }) => ({
display: block;
`;
const StyledTypeBox = styled(ListItemTextChild)(({ theme }) => ({
display: "inline-block", display: "inline-block",
border: "1px solid #ccc", border: "1px solid #ccc",
borderColor: alpha(theme.palette.primary.main, 0.5), borderColor: alpha(theme.palette.primary.main, 0.5),

View File

@ -182,7 +182,11 @@ const SettingVerge = ({ onError }: Props) => {
> >
<Select size="small" sx={{ width: 140, "> div": { py: "7.5px" } }}> <Select size="small" sx={{ width: 140, "> div": { py: "7.5px" } }}>
{routers.map((page: { label: string; path: string }) => { {routers.map((page: { label: string; path: string }) => {
return <MenuItem value={page.path}>{t(page.label)}</MenuItem>; return (
<MenuItem key={page.path} value={page.path}>
{t(page.label)}
</MenuItem>
);
})} })}
</Select> </Select>
</GuardState> </GuardState>

View File

@ -1,4 +1,4 @@
import { useMemo, useRef, useState } from "react"; import { useMemo, useRef, useState, useCallback } from "react";
import { useLockFn } from "ahooks"; import { useLockFn } from "ahooks";
import { Box, Button, IconButton, MenuItem } from "@mui/material"; import { Box, Button, IconButton, MenuItem } from "@mui/material";
import { Virtuoso } from "react-virtuoso"; import { Virtuoso } from "react-virtuoso";
@ -15,7 +15,10 @@ import {
ConnectionDetailRef, ConnectionDetailRef,
} from "@/components/connection/connection-detail"; } from "@/components/connection/connection-detail";
import parseTraffic from "@/utils/parse-traffic"; import parseTraffic from "@/utils/parse-traffic";
import { BaseSearchBox } from "@/components/base/base-search-box"; import {
BaseSearchBox,
type SearchState,
} from "@/components/base/base-search-box";
import { BaseStyledSelect } from "@/components/base/base-styled-select"; import { BaseStyledSelect } from "@/components/base/base-styled-select";
import useSWRSubscription from "swr/subscription"; import useSWRSubscription from "swr/subscription";
import { createSockette } from "@/utils/websocket"; import { createSockette } from "@/utils/websocket";
@ -135,6 +138,10 @@ const ConnectionsPage = () => {
const detailRef = useRef<ConnectionDetailRef>(null!); const detailRef = useRef<ConnectionDetailRef>(null!);
const handleSearch = useCallback((match: (content: string) => boolean) => {
setMatch(() => match);
}, []);
return ( return (
<BasePage <BasePage
full full
@ -211,7 +218,7 @@ const ConnectionsPage = () => {
))} ))}
</BaseStyledSelect> </BaseStyledSelect>
)} )}
<BaseSearchBox onSearch={(match) => setMatch(() => match)} /> <BaseSearchBox onSearch={handleSearch} />
</Box> </Box>
{filterConn.length === 0 ? ( {filterConn.length === 0 ? (

View File

@ -1,4 +1,11 @@
import { Box, ButtonGroup, Grid, IconButton } from "@mui/material"; import {
Box,
ButtonGroup,
Grid,
IconButton,
Select,
MenuItem,
} from "@mui/material";
import { useLockFn } from "ahooks"; import { useLockFn } from "ahooks";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { BasePage, Notice } from "@/components/base"; import { BasePage, Notice } from "@/components/base";
@ -31,6 +38,12 @@ const SettingPage = () => {
const mode = useThemeMode(); const mode = useThemeMode();
const isDark = mode === "light" ? false : true; const isDark = mode === "light" ? false : true;
const routers = [
{ label: "Manual", path: "manual" },
{ label: "TG Channel", path: "telegram" },
{ label: "Github Repo", path: "github" },
];
return ( return (
<BasePage <BasePage
title={t("Settings")} title={t("Settings")}
@ -95,6 +108,15 @@ const SettingPage = () => {
</Box> </Box>
</Grid> </Grid>
</Grid> </Grid>
<Select size="small" sx={{ width: 140, "> div": { py: "7.5px" } }}>
{routers.map((page: { label: string; path: string }) => {
return (
<MenuItem key={page.path} value={page.path}>
{t(page.label)}
</MenuItem>
);
})}
</Select>
</BasePage> </BasePage>
); );
}; };