diff --git a/src/pages/_layout.tsx b/src/pages/_layout.tsx index a65b8d4b..c5fd12ac 100644 --- a/src/pages/_layout.tsx +++ b/src/pages/_layout.tsx @@ -50,11 +50,11 @@ const handleNoticeMessage = ( switch (status) { case "import_sub_url::ok": mutate("getProfiles"); - navigate("/profile", { state: { current: msg } }); + navigate("/", { state: { activateProfile: msg } }); showNotice("success", t("Import Subscription Successful")); + window.dispatchEvent(new CustomEvent('activate-profile', { detail: msg })); break; case "import_sub_url::error": - navigate("/profile"); showNotice("error", msg); break; case "set_config::error": diff --git a/src/pages/home.tsx b/src/pages/home.tsx index e19adb21..4d8597c2 100644 --- a/src/pages/home.tsx +++ b/src/pages/home.tsx @@ -1,5 +1,5 @@ -import React, { useRef, useMemo, useCallback, useState } from "react"; -import { useNavigate } from "react-router-dom"; +import React, {useRef, useMemo, useCallback, useState, useEffect} from "react"; +import { useLocation, useNavigate } from "react-router-dom"; import { useLockFn } from "ahooks"; import { useTranslation } from "react-i18next"; import { toast } from "sonner"; @@ -27,7 +27,7 @@ import { AlertTriangle, Loader2, Globe, - Send, + Send, ExternalLink, RefreshCw, } from "lucide-react"; import { useVerge } from "@/hooks/use-verge"; import { useSystemState } from "@/hooks/use-system-state"; @@ -37,14 +37,17 @@ import { ProxySelectors } from "@/components/home/proxy-selectors"; import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; import { closeAllConnections } from "@/services/api"; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip"; +import { updateProfile } from "@/services/cmds"; const MinimalHomePage: React.FC = () => { const { t } = useTranslation(); const navigate = useNavigate(); const [isToggling, setIsToggling] = useState(false); + const [isUpdating, setIsUpdating] = useState(false); const { profiles, patchProfiles, activateSelected, mutateProfiles } = useProfiles(); const viewerRef = useRef(null); + const [uidToActivate, setUidToActivate] = useState(null); const profileItems = useMemo(() => { const items = @@ -56,7 +59,7 @@ const MinimalHomePage: React.FC = () => { const currentProfile = useMemo(() => { return profileItems.find(p => p.uid === profiles?.current); }, [profileItems, profiles?.current]); - console.log(currentProfile); + console.log("Current profile", currentProfile); const currentProfileName = currentProfile?.name || profiles?.current; const activateProfile = useCallback( @@ -76,6 +79,28 @@ const MinimalHomePage: React.FC = () => { [patchProfiles, activateSelected, mutateProfiles, t], ); + useEffect(() => { + const handleActivationEvent = (event: Event) => { + const customEvent = event as CustomEvent; + const profileId = customEvent.detail; + if (profileId) { + setUidToActivate(profileId); + } + }; + + window.addEventListener('activate-profile', handleActivationEvent); + return () => { + window.removeEventListener('activate-profile', handleActivationEvent); + }; + }, []); + + useEffect(() => { + if (uidToActivate && profileItems.some(p => p.uid === uidToActivate)) { + activateProfile(uidToActivate, false); + setUidToActivate(null); + } + }, [uidToActivate, profileItems, activateProfile]); + const handleProfileChange = useLockFn(async (uid: string) => { if (profiles?.current === uid) return; await activateProfile(uid, true); @@ -128,6 +153,20 @@ const MinimalHomePage: React.FC = () => { } }); + const handleUpdateProfile = useLockFn(async () => { + if (!currentProfile?.uid || currentProfile.type !== 'remote') return; + setIsUpdating(true); + try { + await updateProfile(currentProfile.uid); + toast.success(t("Profile Updated Successfully")); + mutateProfiles(); // Обновляем данные в UI + } catch (err: any) { + toast.error(t("Failed to update profile"), { description: err.message }); + } finally { + setIsUpdating(false); + } + }); + const navMenuItems = [ { label: "Profiles", path: "/profile" }, { label: "Settings", path: "/settings" }, @@ -141,42 +180,69 @@ const MinimalHomePage: React.FC = () => {
- - {profileItems.length > 0 && ( -
- - - - - - {t("Profiles")} - - {profileItems.map((p) => ( - handleProfileChange(p.uid)} - > - {p.name} - {profiles?.current === p.uid && ( - - )} - - ))} - - viewerRef.current?.create()}> - - {t("Add Profile")} - - - +
+
+ {profileItems.length > 0 && ( +
+ + + + + + {t("Profiles")} + + {profileItems.map((p) => ( + handleProfileChange(p.uid)} + > + {p.name} + {profiles?.current === p.uid && ( + + )} + + ))} + + viewerRef.current?.create()}> + + {t("Add Profile")} + + + +
+ )} + {currentProfile?.type === 'remote' && ( + + + + + + +

{t("Update")}

+
+
+
+ )}
- )} +
@@ -204,9 +270,24 @@ const MinimalHomePage: React.FC = () => {