From 798999d490d31aed2856aa4fb0d0068b38eb58ff Mon Sep 17 00:00:00 2001 From: GyDi Date: Fri, 6 May 2022 12:46:27 +0800 Subject: [PATCH] fix: edit profile info --- src/components/profile/profile-edit.tsx | 58 +++++++++++++++---------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/src/components/profile/profile-edit.tsx b/src/components/profile/profile-edit.tsx index c3f8abd..83cd530 100644 --- a/src/components/profile/profile-edit.tsx +++ b/src/components/profile/profile-edit.tsx @@ -1,6 +1,7 @@ import { mutate } from "swr"; import { useEffect, useState } from "react"; import { useLockFn, useSetState } from "ahooks"; +import { useTranslation } from "react-i18next"; import { Button, Dialog, @@ -25,6 +26,8 @@ interface Props { // remote / local file / merge / script const ProfileEdit = (props: Props) => { const { open, itemData, onClose } = props; + + const { t } = useTranslation(); const [form, setForm] = useSetState({ ...itemData }); const [option, setOption] = useSetState(itemData.option ?? {}); const [showOpt, setShowOpt] = useState(!!itemData.option); @@ -33,7 +36,10 @@ const ProfileEdit = (props: Props) => { if (itemData) { setForm({ ...itemData }); setOption(itemData.option ?? {}); - setShowOpt(!!itemData.option?.user_agent); + setShowOpt( + itemData.type === "remote" && + (!!itemData.option?.user_agent || !!itemData.option?.update_interval) + ); } }, [itemData]); @@ -41,7 +47,10 @@ const ProfileEdit = (props: Props) => { try { const { uid } = itemData; 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) { throw new Error("Remote URL should not be null"); @@ -65,11 +74,11 @@ const ProfileEdit = (props: Props) => { const type = form.type || - (form.url ? "remote" : form.file?.endsWith("js") ? "script" : "local"); + (form.url ? "remote" : form.file?.endsWith(".js") ? "script" : "local"); return ( - Edit Profile + {t("Edit Info")} { {type === "remote" && ( setForm({ url: e.target.value })} onKeyDown={(e) => e.key === "Enter" && onUpdate()} @@ -108,26 +117,27 @@ const ProfileEdit = (props: Props) => { )} {showOpt && ( - <> - setOption({ user_agent: e.target.value })} - onKeyDown={(e) => e.key === "Enter" && onUpdate()} - /> + setOption({ user_agent: e.target.value })} + onKeyDown={(e) => e.key === "Enter" && onUpdate()} + /> + )} - { - const str = e.target.value?.replace(/\D/, ""); - setOption({ update_interval: str != null ? +str : str }); - }} - onKeyDown={(e) => e.key === "Enter" && onUpdate()} - /> - + {((type === "remote" && showOpt) || type === "local") && ( + { + const str = e.target.value?.replace(/\D/, ""); + setOption({ update_interval: str != null ? +str : str }); + }} + onKeyDown={(e) => e.key === "Enter" && onUpdate()} + /> )}