refactor: impl as struct methods

This commit is contained in:
GyDi
2022-01-05 02:00:59 +08:00
parent c29eadd9ee
commit 3550aa10ba
8 changed files with 290 additions and 201 deletions

View File

@@ -22,8 +22,7 @@ import {
} from "@mui/icons-material";
import { updateProxy } from "../services/api";
import { ApiType } from "../services/types";
import { getProfiles, setProfiles } from "../services/cmds";
import noop from "../utils/noop";
import { getProfiles, patchProfile } from "../services/cmds";
interface ItemProps {
proxy: ApiType.ProxyItem;
@@ -105,7 +104,7 @@ const ProxyGroup = ({ group }: Props) => {
profile.selected[index] = { name: group.name, now: name };
}
setProfiles(profiles.current!, profile).catch(console.error);
patchProfile(profiles.current!, profile).catch(console.error);
} catch {
setNow(oldValue);
// Todo

View File

@@ -3,8 +3,8 @@ import useSWR, { useSWRConfig } from "swr";
import { Box, Button, Grid, TextField, Typography } from "@mui/material";
import {
getProfiles,
putProfiles,
setProfiles,
selectProfile,
patchProfile,
importProfile,
} from "../services/cmds";
import { getProxies, updateProxy } from "../services/api";
@@ -52,7 +52,7 @@ const ProfilePage = () => {
name,
now,
}));
setProfiles(profiles.current!, profile).catch(console.error);
patchProfile(profiles.current!, profile).catch(console.error);
// update proxies cache
if (hasChange) mutate("getProxies", getProxies());
});
@@ -66,7 +66,7 @@ const ProfilePage = () => {
try {
await importProfile(url);
mutate("getProfiles", getProfiles());
if (!profiles.items?.length) putProfiles(0).catch(noop);
if (!profiles.items?.length) selectProfile(0).catch(noop);
notice.success("Successfully import profile.");
} catch {
notice.error("Failed to import profile.");
@@ -80,7 +80,7 @@ const ProfilePage = () => {
if (index === profiles.current || lockRef.current) return;
if (lockRef.current) return;
lockRef.current = true;
putProfiles(index)
selectProfile(index)
.then(() => {
mutate("getProfiles", { ...profiles, current: index }, true);
})

View File

@@ -1,6 +1,37 @@
import { invoke } from "@tauri-apps/api/tauri";
import { ApiType, CmdType } from "./types";
export async function getProfiles() {
return (await invoke<CmdType.ProfilesConfig>("get_profiles")) ?? {};
}
export async function syncProfiles() {
return invoke<void>("sync_profiles");
}
export async function importProfile(url: string) {
return invoke<void>("import_profile", { url });
}
export async function updateProfile(index: number) {
return invoke<void>("update_profile", { index });
}
export async function deleteProfile(index: number) {
return invoke<void>("delete_profile", { index });
}
export async function patchProfile(
index: number,
profile: CmdType.ProfileItem
) {
return invoke<void>("patch_profile", { index, profile });
}
export async function selectProfile(index: number) {
return invoke<void>("select_profile", { index });
}
export async function restartSidecar() {
return invoke<void>("restart_sidecar");
}
@@ -13,26 +44,6 @@ export async function patchClashConfig(payload: Partial<ApiType.ConfigData>) {
return invoke<void>("patch_clash_config", { payload });
}
export async function importProfile(url: string) {
return invoke<void>("import_profile", { url });
}
export async function updateProfile(index: number) {
return invoke<void>("update_profile", { index });
}
export async function getProfiles() {
return (await invoke<CmdType.ProfilesConfig>("get_profiles")) ?? {};
}
export async function setProfiles(index: number, profile: CmdType.ProfileItem) {
return invoke<void>("set_profiles", { index, profile });
}
export async function putProfiles(current: number) {
return invoke<void>("put_profiles", { current });
}
export async function setSysProxy(enable: boolean) {
return invoke<void>("set_sys_proxy", { enable });
}