feat: added scroll top button for agent and rule pages

This commit is contained in:
huzibaca 2024-11-22 09:22:44 +08:00
parent 37c2599754
commit 8873526619
3 changed files with 117 additions and 32 deletions

View File

@ -0,0 +1,35 @@
import { IconButton, Fade } from "@mui/material";
import KeyboardArrowUpIcon from "@mui/icons-material/KeyboardArrowUp";
interface Props {
onClick: () => void;
show: boolean;
}
export const ScrollTopButton = ({ onClick, show }: Props) => {
return (
<Fade in={show}>
<IconButton
onClick={onClick}
sx={{
position: "absolute",
bottom: "20px",
right: "20px",
backgroundColor: (theme) =>
theme.palette.mode === "dark"
? "rgba(255,255,255,0.1)"
: "rgba(0,0,0,0.1)",
"&:hover": {
backgroundColor: (theme) =>
theme.palette.mode === "dark"
? "rgba(255,255,255,0.2)"
: "rgba(0,0,0,0.2)",
},
visibility: show ? "visible" : "hidden",
}}
>
<KeyboardArrowUpIcon />
</IconButton>
</Fade>
);
};

View File

@ -1,4 +1,4 @@
import { useRef } from "react"; import { useRef, useState, useEffect } from "react";
import { useLockFn } from "ahooks"; import { useLockFn } from "ahooks";
import { Virtuoso, type VirtuosoHandle } from "react-virtuoso"; import { Virtuoso, type VirtuosoHandle } from "react-virtuoso";
import { import {
@ -15,6 +15,7 @@ import { useRenderList } from "./use-render-list";
import { ProxyRender } from "./proxy-render"; import { ProxyRender } from "./proxy-render";
import delayManager from "@/services/delay"; import delayManager from "@/services/delay";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { ScrollTopButton } from "../layout/scroll-top-button";
interface Props { interface Props {
mode: string; mode: string;
@ -32,6 +33,22 @@ export const ProxyGroups = (props: Props) => {
const virtuosoRef = useRef<VirtuosoHandle>(null); const virtuosoRef = useRef<VirtuosoHandle>(null);
const [showScrollTop, setShowScrollTop] = useState(false);
// 添加滚动处理函数
const handleScroll = (e: any) => {
const scrollTop = e.target.scrollTop;
setShowScrollTop(scrollTop > 100);
};
// 滚动到顶部
const scrollToTop = () => {
virtuosoRef.current?.scrollTo?.({
top: 0,
behavior: "smooth",
});
};
// 切换分组的节点代理 // 切换分组的节点代理
const handleChangeProxy = useLockFn( const handleChangeProxy = useLockFn(
async (group: IProxyGroupItem, proxy: IProxyItem) => { async (group: IProxyGroupItem, proxy: IProxyItem) => {
@ -57,7 +74,7 @@ export const ProxyGroups = (props: Props) => {
if (!current.selected) current.selected = []; if (!current.selected) current.selected = [];
const index = current.selected.findIndex( const index = current.selected.findIndex(
(item) => item.name === group.name (item) => item.name === group.name,
); );
if (index < 0) { if (index < 0) {
@ -66,14 +83,14 @@ export const ProxyGroups = (props: Props) => {
current.selected[index] = { name, now: proxy.name }; current.selected[index] = { name, now: proxy.name };
} }
await patchCurrent({ selected: current.selected }); await patchCurrent({ selected: current.selected });
} },
); );
// 测全部延迟 // 测全部延迟
const handleCheckAll = useLockFn(async (groupName: string) => { const handleCheckAll = useLockFn(async (groupName: string) => {
const proxies = renderList const proxies = renderList
.filter( .filter(
(e) => e.group?.name === groupName && (e.type === 2 || e.type === 4) (e) => e.group?.name === groupName && (e.type === 2 || e.type === 4),
) )
.flatMap((e) => e.proxyCol || e.proxy!) .flatMap((e) => e.proxyCol || e.proxy!)
.filter(Boolean); .filter(Boolean);
@ -82,7 +99,7 @@ export const ProxyGroups = (props: Props) => {
if (providers.size) { if (providers.size) {
Promise.allSettled( Promise.allSettled(
[...providers].map((p) => providerHealthCheck(p)) [...providers].map((p) => providerHealthCheck(p)),
).then(() => onProxies()); ).then(() => onProxies());
} }
@ -105,7 +122,7 @@ export const ProxyGroups = (props: Props) => {
(e) => (e) =>
e.group?.name === name && e.group?.name === name &&
((e.type === 2 && e.proxy?.name === now) || ((e.type === 2 && e.proxy?.name === now) ||
(e.type === 4 && e.proxyCol?.some((p) => p.name === now))) (e.type === 4 && e.proxyCol?.some((p) => p.name === now))),
); );
if (index >= 0) { if (index >= 0) {
@ -122,22 +139,33 @@ export const ProxyGroups = (props: Props) => {
} }
return ( return (
<Virtuoso <div style={{ position: "relative", height: "100%" }}>
ref={virtuosoRef} <Virtuoso
style={{ height: "calc(100% - 16px)" }} ref={virtuosoRef}
totalCount={renderList.length} style={{ height: "calc(100% - 16px)" }}
increaseViewportBy={256} totalCount={renderList.length}
itemContent={(index) => ( increaseViewportBy={256}
<ProxyRender scrollerRef={(ref) => {
key={renderList[index].key} if (ref) {
item={renderList[index]} ref.addEventListener("scroll", handleScroll);
indent={mode === "rule" || mode === "script"} }
onLocation={handleLocation} }}
onCheckAll={handleCheckAll} itemContent={(index) => (
onHeadState={onHeadState} <>
onChangeProxy={handleChangeProxy} <ProxyRender
/> key={renderList[index].key}
)} item={renderList[index]}
/> indent={mode === "rule" || mode === "script"}
onLocation={handleLocation}
onCheckAll={handleCheckAll}
onHeadState={onHeadState}
onChangeProxy={handleChangeProxy}
/>
</>
)}
/>
<ScrollTopButton show={showScrollTop} onClick={scrollToTop} />
</div>
); );
}; };

View File

@ -1,7 +1,7 @@
import useSWR from "swr"; import useSWR from "swr";
import { useState, useMemo } from "react"; import { useState, useMemo, useRef } from "react";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { Virtuoso } from "react-virtuoso"; import { Virtuoso, VirtuosoHandle } from "react-virtuoso";
import { Box } from "@mui/material"; import { Box } from "@mui/material";
import { getRules } from "@/services/api"; import { getRules } from "@/services/api";
import { BaseEmpty, BasePage } from "@/components/base"; import { BaseEmpty, BasePage } from "@/components/base";
@ -9,6 +9,7 @@ import RuleItem from "@/components/rule/rule-item";
import { ProviderButton } from "@/components/rule/provider-button"; import { ProviderButton } from "@/components/rule/provider-button";
import { BaseSearchBox } from "@/components/base/base-search-box"; import { BaseSearchBox } from "@/components/base/base-search-box";
import { useTheme } from "@mui/material/styles"; import { useTheme } from "@mui/material/styles";
import { ScrollTopButton } from "@/components/layout/scroll-top-button";
const RulesPage = () => { const RulesPage = () => {
const { t } = useTranslation(); const { t } = useTranslation();
@ -16,11 +17,24 @@ const RulesPage = () => {
const theme = useTheme(); const theme = useTheme();
const isDark = theme.palette.mode === "dark"; const isDark = theme.palette.mode === "dark";
const [match, setMatch] = useState(() => (_: string) => true); const [match, setMatch] = useState(() => (_: string) => true);
const virtuosoRef = useRef<VirtuosoHandle>(null);
const [showScrollTop, setShowScrollTop] = useState(false);
const rules = useMemo(() => { const rules = useMemo(() => {
return data.filter((item) => match(item.payload)); return data.filter((item) => match(item.payload));
}, [data, match]); }, [data, match]);
const scrollToTop = () => {
virtuosoRef.current?.scrollTo({
top: 0,
behavior: "smooth",
});
};
const handleScroll = (e: any) => {
setShowScrollTop(e.target.scrollTop > 100);
};
return ( return (
<BasePage <BasePage
full full
@ -51,16 +65,24 @@ const RulesPage = () => {
margin: "10px", margin: "10px",
borderRadius: "8px", borderRadius: "8px",
bgcolor: isDark ? "#282a36" : "#ffffff", bgcolor: isDark ? "#282a36" : "#ffffff",
position: "relative",
}} }}
> >
{rules.length > 0 ? ( {rules.length > 0 ? (
<Virtuoso <>
data={rules} <Virtuoso
itemContent={(index, item) => ( ref={virtuosoRef}
<RuleItem index={index + 1} value={item} /> data={rules}
)} itemContent={(index, item) => (
followOutput={"smooth"} <RuleItem index={index + 1} value={item} />
/> )}
followOutput={"smooth"}
scrollerRef={(ref) => {
if (ref) ref.addEventListener("scroll", handleScroll);
}}
/>
<ScrollTopButton onClick={scrollToTop} show={showScrollTop} />
</>
) : ( ) : (
<BaseEmpty /> <BaseEmpty />
)} )}