feat: refactor commands and support update profile

This commit is contained in:
GyDi
2021-12-18 02:01:02 +08:00
parent c1bcfc6785
commit 98378e6261
9 changed files with 280 additions and 229 deletions

View File

@@ -28,10 +28,11 @@ interface Props {
selected: boolean;
itemData: ProfileItem;
onClick: () => void;
onUpdate: () => void;
}
const ProfileItemComp: React.FC<Props> = (props) => {
const { selected, itemData, onClick } = props;
const { selected, itemData, onClick, onUpdate } = props;
const { name = "Profile", extra } = itemData;
const { upload = 0, download = 0, total = 0 } = extra ?? {};
@@ -87,6 +88,7 @@ const ProfileItemComp: React.FC<Props> = (props) => {
color="inherit"
onClick={(e) => {
e.stopPropagation();
onUpdate();
}}
>
<MenuRounded />

View File

@@ -1,7 +1,12 @@
import { useState } from "react";
import useSWR, { useSWRConfig } from "swr";
import { Box, Button, Grid, TextField, Typography } from "@mui/material";
import { getProfiles, importProfile, putProfiles } from "../services/command";
import {
getProfiles,
importProfile,
putProfiles,
updateProfile,
} from "../services/command";
import ProfileItemComp from "../components/profile-item";
import useNotice from "../utils/use-notice";
@@ -33,6 +38,16 @@ const RulesPage = () => {
});
};
const onUpdateProfile = (index: number) => {
updateProfile(index)
.then(() => {
mutate("getProfiles");
})
.catch((err) => {
console.error(err);
});
};
return (
<Box sx={{ width: 0.9, maxWidth: "850px", mx: "auto", mb: 2 }}>
<Typography variant="h4" component="h1" sx={{ py: 2, mb: 1 }}>
@@ -66,6 +81,7 @@ const RulesPage = () => {
selected={profiles.current === idx}
itemData={item}
onClick={() => onProfileChange(idx)}
onUpdate={() => onUpdateProfile(idx)}
/>
</Grid>
))}

View File

@@ -1,7 +1,7 @@
import { invoke } from "@tauri-apps/api/tauri";
export async function restartSidecar() {
return invoke<void>("restart_sidebar");
return invoke<void>("restart_sidecar");
}
export interface ClashInfo {
@@ -15,7 +15,11 @@ export async function getClashInfo() {
}
export async function importProfile(url: string) {
return invoke<string>("import_profile", { url });
return invoke<void>("import_profile", { url });
}
export async function updateProfile(index: number) {
return invoke<void>("update_profile", { index });
}
export interface ProfileItem {