Skip to content
Snippets Groups Projects
Unverified Commit 798999d4 authored by GyDi's avatar GyDi Committed by GitHub
Browse files

fix: edit profile info

parent 0e68c5e8
No related branches found
No related tags found
No related merge requests found
import { mutate } from "swr"; import { mutate } from "swr";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { useLockFn, useSetState } from "ahooks"; import { useLockFn, useSetState } from "ahooks";
import { useTranslation } from "react-i18next";
import { import {
Button, Button,
Dialog, Dialog,
...@@ -25,6 +26,8 @@ interface Props { ...@@ -25,6 +26,8 @@ interface Props {
// remote / local file / merge / script // remote / local file / merge / script
const ProfileEdit = (props: Props) => { const ProfileEdit = (props: Props) => {
const { open, itemData, onClose } = props; const { open, itemData, onClose } = props;
const { t } = useTranslation();
const [form, setForm] = useSetState({ ...itemData }); const [form, setForm] = useSetState({ ...itemData });
const [option, setOption] = useSetState(itemData.option ?? {}); const [option, setOption] = useSetState(itemData.option ?? {});
const [showOpt, setShowOpt] = useState(!!itemData.option); const [showOpt, setShowOpt] = useState(!!itemData.option);
...@@ -33,7 +36,10 @@ const ProfileEdit = (props: Props) => { ...@@ -33,7 +36,10 @@ const ProfileEdit = (props: Props) => {
if (itemData) { if (itemData) {
setForm({ ...itemData }); setForm({ ...itemData });
setOption(itemData.option ?? {}); setOption(itemData.option ?? {});
setShowOpt(!!itemData.option?.user_agent); setShowOpt(
itemData.type === "remote" &&
(!!itemData.option?.user_agent || !!itemData.option?.update_interval)
);
} }
}, [itemData]); }, [itemData]);
...@@ -41,7 +47,10 @@ const ProfileEdit = (props: Props) => { ...@@ -41,7 +47,10 @@ const ProfileEdit = (props: Props) => {
try { try {
const { uid } = itemData; const { uid } = itemData;
const { name, desc, url } = form; const { name, desc, url } = form;
const option_ = itemData.type === "remote" ? option : undefined; const option_ =
itemData.type === "remote" || itemData.type === "local"
? option
: undefined;
if (itemData.type === "remote" && !url) { if (itemData.type === "remote" && !url) {
throw new Error("Remote URL should not be null"); throw new Error("Remote URL should not be null");
...@@ -65,11 +74,11 @@ const ProfileEdit = (props: Props) => { ...@@ -65,11 +74,11 @@ const ProfileEdit = (props: Props) => {
const type = const type =
form.type || form.type ||
(form.url ? "remote" : form.file?.endsWith("js") ? "script" : "local"); (form.url ? "remote" : form.file?.endsWith(".js") ? "script" : "local");
return ( return (
<Dialog open={open} onClose={onClose}> <Dialog open={open} onClose={onClose}>
<DialogTitle sx={{ pb: 0.5 }}>Edit Profile</DialogTitle> <DialogTitle sx={{ pb: 0.5 }}>{t("Edit Info")}</DialogTitle>
<DialogContent sx={{ width: 336, pb: 1 }}> <DialogContent sx={{ width: 336, pb: 1 }}>
<TextField <TextField
...@@ -100,7 +109,7 @@ const ProfileEdit = (props: Props) => { ...@@ -100,7 +109,7 @@ const ProfileEdit = (props: Props) => {
{type === "remote" && ( {type === "remote" && (
<TextField <TextField
{...textFieldProps} {...textFieldProps}
label="Subscription Url" label="Subscription URL"
value={form.url} value={form.url}
onChange={(e) => setForm({ url: e.target.value })} onChange={(e) => setForm({ url: e.target.value })}
onKeyDown={(e) => e.key === "Enter" && onUpdate()} onKeyDown={(e) => e.key === "Enter" && onUpdate()}
...@@ -108,26 +117,27 @@ const ProfileEdit = (props: Props) => { ...@@ -108,26 +117,27 @@ const ProfileEdit = (props: Props) => {
)} )}
{showOpt && ( {showOpt && (
<> <TextField
<TextField {...textFieldProps}
{...textFieldProps} label="User Agent"
label="User Agent" value={option.user_agent}
value={option.user_agent} placeholder="clash-verge/v1.0.0"
onChange={(e) => setOption({ user_agent: e.target.value })} onChange={(e) => setOption({ user_agent: e.target.value })}
onKeyDown={(e) => e.key === "Enter" && onUpdate()} onKeyDown={(e) => e.key === "Enter" && onUpdate()}
/> />
)}
<TextField
{...textFieldProps} {((type === "remote" && showOpt) || type === "local") && (
label="Update Interval (mins)" <TextField
value={option.update_interval} {...textFieldProps}
onChange={(e) => { label="Update Interval (mins)"
const str = e.target.value?.replace(/\D/, ""); value={option.update_interval}
setOption({ update_interval: str != null ? +str : str }); onChange={(e) => {
}} const str = e.target.value?.replace(/\D/, "");
onKeyDown={(e) => e.key === "Enter" && onUpdate()} setOption({ update_interval: str != null ? +str : str });
/> }}
</> onKeyDown={(e) => e.key === "Enter" && onUpdate()}
/>
)} )}
</DialogContent> </DialogContent>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment