fix: profile item loading state
This commit is contained in:
parent
2f6efbed63
commit
3cde019208
@ -1,7 +1,8 @@
|
|||||||
import dayjs from "dayjs";
|
import dayjs from "dayjs";
|
||||||
|
import { useState } from "react";
|
||||||
import { useLockFn } from "ahooks";
|
import { useLockFn } from "ahooks";
|
||||||
import { useSWRConfig } from "swr";
|
import { useSWRConfig } from "swr";
|
||||||
import { useEffect, useState } from "react";
|
import { useRecoilState } from "recoil";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import {
|
import {
|
||||||
alpha,
|
alpha,
|
||||||
@ -16,6 +17,7 @@ import {
|
|||||||
} from "@mui/material";
|
} from "@mui/material";
|
||||||
import { RefreshRounded } from "@mui/icons-material";
|
import { RefreshRounded } from "@mui/icons-material";
|
||||||
import { CmdType } from "../../services/types";
|
import { CmdType } from "../../services/types";
|
||||||
|
import { atomLoadingCache } from "../../services/states";
|
||||||
import { updateProfile, deleteProfile, viewProfile } from "../../services/cmds";
|
import { updateProfile, deleteProfile, viewProfile } from "../../services/cmds";
|
||||||
import parseTraffic from "../../utils/parse-traffic";
|
import parseTraffic from "../../utils/parse-traffic";
|
||||||
import ProfileEdit from "./profile-edit";
|
import ProfileEdit from "./profile-edit";
|
||||||
@ -37,9 +39,6 @@ const round = keyframes`
|
|||||||
to { transform: rotate(360deg); }
|
to { transform: rotate(360deg); }
|
||||||
`;
|
`;
|
||||||
|
|
||||||
// save the state of each item loading
|
|
||||||
const loadingCache: Record<string, boolean> = {};
|
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
selected: boolean;
|
selected: boolean;
|
||||||
itemData: CmdType.ProfileItem;
|
itemData: CmdType.ProfileItem;
|
||||||
@ -51,9 +50,9 @@ const ProfileItem = (props: Props) => {
|
|||||||
|
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const { mutate } = useSWRConfig();
|
const { mutate } = useSWRConfig();
|
||||||
const [loading, setLoading] = useState(loadingCache[itemData.uid] ?? false);
|
|
||||||
const [anchorEl, setAnchorEl] = useState<any>(null);
|
const [anchorEl, setAnchorEl] = useState<any>(null);
|
||||||
const [position, setPosition] = useState({ left: 0, top: 0 });
|
const [position, setPosition] = useState({ left: 0, top: 0 });
|
||||||
|
const [loadingCache, setLoadingCache] = useRecoilState(atomLoadingCache);
|
||||||
|
|
||||||
const { name = "Profile", extra, updated = 0 } = itemData;
|
const { name = "Profile", extra, updated = 0 } = itemData;
|
||||||
const { upload = 0, download = 0, total = 0 } = extra ?? {};
|
const { upload = 0, download = 0, total = 0 } = extra ?? {};
|
||||||
@ -68,9 +67,7 @@ const ProfileItem = (props: Props) => {
|
|||||||
const hasUrl = !!itemData.url;
|
const hasUrl = !!itemData.url;
|
||||||
const hasExtra = !!extra; // only subscription url has extra info
|
const hasExtra = !!extra; // only subscription url has extra info
|
||||||
|
|
||||||
useEffect(() => {
|
const loading = loadingCache[itemData.uid] ?? false;
|
||||||
loadingCache[itemData.uid] = loading;
|
|
||||||
}, [itemData, loading]);
|
|
||||||
|
|
||||||
const [editOpen, setEditOpen] = useState(false);
|
const [editOpen, setEditOpen] = useState(false);
|
||||||
const onEdit = () => {
|
const onEdit = () => {
|
||||||
@ -92,19 +89,19 @@ const ProfileItem = (props: Props) => {
|
|||||||
onSelect(true);
|
onSelect(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
const onUpdateWrapper = (withProxy: boolean) => async () => {
|
const onUpdate = useLockFn(async (withProxy: boolean) => {
|
||||||
setAnchorEl(null);
|
setAnchorEl(null);
|
||||||
if (loading) return;
|
setLoadingCache((cache) => ({ ...cache, [itemData.uid]: true }));
|
||||||
setLoading(true);
|
|
||||||
try {
|
try {
|
||||||
await updateProfile(itemData.uid, { with_proxy: withProxy });
|
await updateProfile(itemData.uid, { with_proxy: withProxy });
|
||||||
setLoading(false);
|
|
||||||
mutate("getProfiles");
|
mutate("getProfiles");
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
setLoading(false);
|
|
||||||
Notice.error(err?.message || err.toString());
|
Notice.error(err?.message || err.toString());
|
||||||
|
} finally {
|
||||||
|
setLoadingCache((cache) => ({ ...cache, [itemData.uid]: false }));
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
|
|
||||||
const onDelete = useLockFn(async () => {
|
const onDelete = useLockFn(async () => {
|
||||||
setAnchorEl(null);
|
setAnchorEl(null);
|
||||||
@ -127,8 +124,8 @@ const ProfileItem = (props: Props) => {
|
|||||||
{ label: "Select", handler: onForceSelect },
|
{ label: "Select", handler: onForceSelect },
|
||||||
{ label: "Edit", handler: onEdit },
|
{ label: "Edit", handler: onEdit },
|
||||||
{ label: "File", handler: onView },
|
{ label: "File", handler: onView },
|
||||||
{ label: "Update", handler: onUpdateWrapper(false) },
|
{ label: "Update", handler: () => onUpdate(false) },
|
||||||
{ label: "Update(Proxy)", handler: onUpdateWrapper(true) },
|
{ label: "Update(Proxy)", handler: () => onUpdate(true) },
|
||||||
{ label: "Delete", handler: onDelete },
|
{ label: "Delete", handler: onDelete },
|
||||||
];
|
];
|
||||||
const fileModeMenu = [
|
const fileModeMenu = [
|
||||||
@ -199,7 +196,7 @@ const ProfileItem = (props: Props) => {
|
|||||||
disabled={loading}
|
disabled={loading}
|
||||||
onClick={(e) => {
|
onClick={(e) => {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
onUpdateWrapper(false)();
|
onUpdate(false);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<RefreshRounded />
|
<RefreshRounded />
|
||||||
|
@ -10,3 +10,9 @@ export const atomLogData = atom<ApiType.LogItem[]>({
|
|||||||
key: "atomLogData",
|
key: "atomLogData",
|
||||||
default: [],
|
default: [],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// save the state of each profile item loading
|
||||||
|
export const atomLoadingCache = atom<Record<string, boolean>>({
|
||||||
|
key: "atomLoadingCache",
|
||||||
|
default: {},
|
||||||
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user