import { X, Minus, Maximize, SlidersHorizontal, Globe, Eye, EyeOff } from "lucide-react"; import { WindowMinimise, WindowToggleMaximise, Quit } from "../../wailsjs/runtime/runtime"; import { Menubar, MenubarContent, MenubarMenu, MenubarItem, MenubarTrigger, MenubarLabel, MenubarSeparator } from "@/components/ui/menubar"; import { fetchCurrentIPInfo } from "@/lib/api"; import type { CurrentIPInfo } from "@/types/api"; import { openExternal } from "@/lib/utils"; import { useEffect, useRef, useState } from "react"; const IP_INFO_REFRESH_INTERVAL_MS = 30000; const SPOTIFY_BLOCKED_COUNTRY_CODES = new Set([ "AF", "IO", "CF", "CN", "CU", "ER", "IR", "MM", "KP", "RU", "SO", "SS", "SD", "SY", "TM", "YE", ]); export function TitleBar() { const [currentIPInfo, setCurrentIPInfo] = useState(null); const [isLoadingCurrentIPInfo, setIsLoadingCurrentIPInfo] = useState(false); const [currentIPInfoError, setCurrentIPInfoError] = useState(""); const [showIPAddress, setShowIPAddress] = useState(false); const currentIPInfoRef = useRef(null); useEffect(() => { currentIPInfoRef.current = currentIPInfo; }, [currentIPInfo]); const loadCurrentIPInfo = async (options?: { silent?: boolean; }) => { const silent = options?.silent ?? false; if (!silent) { setIsLoadingCurrentIPInfo(true); setCurrentIPInfoError(""); } try { const info = await fetchCurrentIPInfo(); setCurrentIPInfo(info); setCurrentIPInfoError(""); } catch (error) { if (!silent || !currentIPInfoRef.current) { setCurrentIPInfo(null); setCurrentIPInfoError(error instanceof Error ? error.message : "Unable to detect IP"); } } finally { if (!silent) { setIsLoadingCurrentIPInfo(false); } } }; useEffect(() => { void loadCurrentIPInfo(); }, []); useEffect(() => { const intervalId = window.setInterval(() => { void loadCurrentIPInfo({ silent: true }); }, IP_INFO_REFRESH_INTERVAL_MS); const handleFocus = () => { if (document.visibilityState === "hidden") { return; } void loadCurrentIPInfo({ silent: true }); }; window.addEventListener("focus", handleFocus); document.addEventListener("visibilitychange", handleFocus); return () => { window.clearInterval(intervalId); window.removeEventListener("focus", handleFocus); document.removeEventListener("visibilitychange", handleFocus); }; }, []); const handleMinimize = () => { WindowMinimise(); }; const handleMaximize = () => { WindowToggleMaximise(); }; const handleClose = () => { Quit(); }; const detectedCountryCode = currentIPInfo?.country_code?.toUpperCase() || ""; const detectedFlagPath = detectedCountryCode ? `/assets/flags/${detectedCountryCode.toLowerCase()}.svg` : ""; const isSpotifyBlockedCountry = detectedCountryCode !== "" && SPOTIFY_BLOCKED_COUNTRY_CODES.has(detectedCountryCode); return (<>
openExternal("https://afkarxyz.qzz.io")} className="gap-2"> Website
Network {isSpotifyBlockedCountry && ( (Blocked by Spotify) )}
{detectedFlagPath ? ({detectedCountryCode}) : ()} {isLoadingCurrentIPInfo ? "Detecting..." : currentIPInfo ? showIPAddress ? `${currentIPInfo.ip} - ${currentIPInfo.country}${detectedCountryCode ? ` (${detectedCountryCode})` : ""}` : `${currentIPInfo.country}${detectedCountryCode ? ` (${detectedCountryCode})` : ""}` : "Unavailable"}
{currentIPInfo && !isLoadingCurrentIPInfo && ()}
{!isLoadingCurrentIPInfo && !currentIPInfo && currentIPInfoError && (
IP detection unavailable
)}
); }