feat(settings page): add loading state (#1157)
Some checks failed
Alpha Build / alpha (macos-latest, aarch64-apple-darwin) (push) Has been cancelled
Alpha Build / alpha (macos-latest, x86_64-apple-darwin) (push) Has been cancelled
Alpha Build / alpha (windows-latest, aarch64-pc-windows-msvc) (push) Has been cancelled
Alpha Build / alpha (windows-latest, i686-pc-windows-msvc) (push) Has been cancelled
Alpha Build / alpha (windows-latest, x86_64-pc-windows-msvc) (push) Has been cancelled
Alpha Build / alpha-for-linux (ubuntu-latest, aarch64-unknown-linux-gnu) (push) Has been cancelled
Alpha Build / alpha-for-linux (ubuntu-latest, armv7-unknown-linux-gnueabihf) (push) Has been cancelled
Alpha Build / alpha-for-linux (ubuntu-latest, i686-unknown-linux-gnu) (push) Has been cancelled
Alpha Build / alpha-for-linux (ubuntu-latest, x86_64-unknown-linux-gnu) (push) Has been cancelled
Alpha Build / alpha-for-fixed-webview2 (arm64, windows-latest, aarch64-pc-windows-msvc) (push) Has been cancelled
Alpha Build / alpha-for-fixed-webview2 (x64, windows-latest, x86_64-pc-windows-msvc) (push) Has been cancelled
Alpha Build / alpha-for-fixed-webview2 (x86, windows-latest, i686-pc-windows-msvc) (push) Has been cancelled
Alpha Build / Update tag (push) Has been cancelled

* feat(settings page): add loading state

* fix: type
This commit is contained in:
Eric Huang 2024-06-09 06:26:07 +08:00 committed by GitHub
parent 97bd51cf5f
commit ad35ed96c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 29 additions and 8 deletions

View File

@ -1,4 +1,4 @@
import React, { ReactNode } from "react";
import React, { ReactNode, useState } from "react";
import {
Box,
List,
@ -8,13 +8,15 @@ import {
ListSubheader,
} from "@mui/material";
import { ChevronRightRounded } from "@mui/icons-material";
import CircularProgress from "@mui/material/CircularProgress";
import isAsyncFunction from "@/utils/is-async-function";
interface ItemProps {
label: ReactNode;
extra?: ReactNode;
children?: ReactNode;
secondary?: ReactNode;
onClick?: () => void;
onClick?: () => void | Promise<any>;
}
export const SettingItem: React.FC<ItemProps> = (props) => {
@ -28,11 +30,27 @@ export const SettingItem: React.FC<ItemProps> = (props) => {
</Box>
);
const [isLoading, setIsLoading] = useState(false);
const handleClick = () => {
if (onClick) {
if (isAsyncFunction(onClick)) {
setIsLoading(true);
onClick()!.finally(() => setIsLoading(false));
} else {
onClick();
}
}
};
return clickable ? (
<ListItem disablePadding>
<ListItemButton onClick={onClick}>
<ListItemButton onClick={handleClick} disabled={isLoading}>
<ListItemText primary={primary} secondary={secondary} />
{isLoading ? (
<CircularProgress color="inherit" size={20} />
) : (
<ChevronRightRounded />
)}
</ListItemButton>
</ListItem>
) : (

View File

@ -51,14 +51,14 @@ const SettingClash = ({ onError }: Props) => {
const onChangeVerge = (patch: Partial<IVergeConfig>) => {
mutateVerge({ ...verge, ...patch }, false);
};
const onUpdateGeo = useLockFn(async () => {
const onUpdateGeo = async () => {
try {
await updateGeoData();
Notice.success(t("GeoData Updated"));
} catch (err: any) {
Notice.error(err?.response.data.message || err.toString());
}
});
};
return (
<SettingList title={t("Clash Setting")}>

View File

@ -55,7 +55,7 @@ const SettingVerge = ({ onError }: Props) => {
mutateVerge({ ...verge, ...patch }, false);
};
const onCheckUpdate = useLockFn(async () => {
const onCheckUpdate = async () => {
try {
const info = await checkUpdate();
if (!info?.shouldUpdate) {
@ -66,7 +66,7 @@ const SettingVerge = ({ onError }: Props) => {
} catch (err: any) {
Notice.error(err.message || err.toString());
}
});
};
return (
<SettingList title={t("Verge Setting")}>

View File

@ -0,0 +1,3 @@
export default function isAsyncFunction(fn: Function): boolean {
return fn.constructor.name === "AsyncFunction";
}