All files / src/components/browser BrowserSettingsMenu.tsx

0% Statements 0/171
0% Branches 0/1
0% Functions 0/1
0% Lines 0/171

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218                                                                                                                                                                                                                                                                                                                                                                                                                                                   
import { EmptyRequest, StringRequest } from "@shared/proto/cline/common"
import { VSCodeButton } from "@vscode/webview-ui-toolkit/react"
import { useEffect, useRef, useState } from "react"
import { useExtensionState } from "@/context/ExtensionStateContext"
import { BrowserServiceClient, UiServiceClient } from "../../services/grpc-client"
 
interface ConnectionInfo {
	isConnected: boolean
	isRemote: boolean
	host?: string
}
 
export const BrowserSettingsMenu = () => {
	const { browserSettings, navigateToSettings } = useExtensionState()
	const containerRef = useRef<HTMLDivElement>(null)
	const [showInfoPopover, setShowInfoPopover] = useState(false)
	const [connectionInfo, setConnectionInfo] = useState<ConnectionInfo>({
		isConnected: false,
		isRemote: !!browserSettings.remoteBrowserEnabled,
		host: browserSettings.remoteBrowserHost,
	})
	const popoverRef = useRef<HTMLDivElement>(null)
 
	// Get actual connection info from the browser session using gRPC
	useEffect(() => {
		// Function to fetch connection info
		;(async () => {
			try {
				console.log("[DEBUG] SENDING BROWSER CONNECTION INFO REQUEST")
				const info = await BrowserServiceClient.getBrowserConnectionInfo(EmptyRequest.create({}))
				console.log("[DEBUG] GOT BROWSER REPLY:", info, typeof info)
				setConnectionInfo({
					isConnected: info.isConnected,
					isRemote: info.isRemote,
					host: info.host,
				})
			} catch (error) {
				console.error("Error fetching browser connection info:", error)
			}
		})()
 
		// No need for message event listeners anymore!
	}, [browserSettings.remoteBrowserHost, browserSettings.remoteBrowserEnabled])
 
	// Close popover when clicking outside
	useEffect(() => {
		const handleClickOutside = (event: MouseEvent) => {
			if (
				popoverRef.current &&
				!popoverRef.current.contains(event.target as Node) &&
				!event.composedPath().some((el) => (el as HTMLElement).classList?.contains("browser-info-icon"))
			) {
				setShowInfoPopover(false)
			}
		}
 
		if (showInfoPopover) {
			document.addEventListener("mousedown", handleClickOutside)
		}
		return () => {
			document.removeEventListener("mousedown", handleClickOutside)
		}
	}, [showInfoPopover])
 
	const openBrowserSettings = () => {
		// First open the settings panel using direct navigation
		navigateToSettings()
 
		// After a short delay, send a message to scroll to browser settings
		setTimeout(async () => {
			try {
				await UiServiceClient.scrollToSettings(StringRequest.create({ value: "browser" }))
			} catch (error) {
				console.error("Error scrolling to browser settings:", error)
			}
		}, 300) // Give the settings panel time to open
	}
 
	const toggleInfoPopover = () => {
		setShowInfoPopover(!showInfoPopover)
 
		// Request updated connection info when opening the popover using gRPC
		if (!showInfoPopover) {
			const fetchConnectionInfo = async () => {
				try {
					const info = await BrowserServiceClient.getBrowserConnectionInfo(EmptyRequest.create({}))
					setConnectionInfo({
						isConnected: info.isConnected,
						isRemote: info.isRemote,
						host: info.host,
					})
				} catch (error) {
					console.error("Error fetching browser connection info:", error)
				}
			}
 
			fetchConnectionInfo()
		}
	}
 
	// Determine icon based on connection state
	const getIconClass = () => {
		if (connectionInfo.isRemote) {
			return "codicon-remote"
		} else {
			return connectionInfo.isConnected ? "codicon-vm-running" : "codicon-info"
		}
	}
 
	// Determine icon color based on connection state
	const getIconColor = () => {
		if (connectionInfo.isRemote) {
			return connectionInfo.isConnected ? "var(--vscode-charts-blue)" : "var(--vscode-foreground)"
		} else if (connectionInfo.isConnected) {
			return "var(--vscode-charts-green)"
		} else {
			return "var(--vscode-foreground)"
		}
	}
 
	// Check connection status every second to keep icon in sync using gRPC
	useEffect(() => {
		// Function to fetch connection info
		const fetchConnectionInfo = async () => {
			try {
				const info = await BrowserServiceClient.getBrowserConnectionInfo(EmptyRequest.create({}))
				setConnectionInfo({
					isConnected: info.isConnected,
					isRemote: info.isRemote,
					host: info.host,
				})
			} catch (error) {
				console.error("Error fetching browser connection info:", error)
			}
		}
 
		// Request connection info immediately
		fetchConnectionInfo()
 
		// Set up interval to refresh every second
		const intervalId = setInterval(fetchConnectionInfo, 1000)
 
		return () => clearInterval(intervalId)
	}, [])
 
	return (
		<div ref={containerRef} style={{ position: "relative", marginTop: "-1px", display: "flex" }}>
			<VSCodeButton
				appearance="icon"
				className="browser-info-icon"
				onClick={toggleInfoPopover}
				style={{ marginRight: "4px" }}
				title="Browser connection info">
				<i
					className={`codicon ${getIconClass()}`}
					style={{
						fontSize: "14.5px",
						color: getIconColor(),
					}}
				/>
			</VSCodeButton>
 
			{showInfoPopover && (
				// InfoPopover - Dropdown container with connection details
				<div
					className="absolute top-[30px] right-0 z-[100] w-[60dvw] max-w-[250px] rounded p-2.5 shadow-lg"
					ref={popoverRef}
					style={{
						backgroundColor: "var(--vscode-editorWidget-background)",
						border: "1px solid var(--vscode-widget-border)",
						boxShadow: "0 2px 8px rgba(0, 0, 0, 0.15)",
					}}>
					<h4 style={{ margin: "0 0 8px 0" }}>Browser Connection</h4>
					{/* InfoRow - Status row container */}
					<div className="flex flex-wrap whitespace-nowrap mb-1">
						{/* InfoLabel - Fixed-width label */}
						<div className="flex-none w-[90px] font-medium">Status:</div>
						{/* InfoValue - Flexible value container */}
						<div
							className="flex-1 break-words"
							style={{
								color: connectionInfo.isConnected
									? "var(--vscode-charts-green)"
									: "var(--vscode-errorForeground)",
							}}>
							{connectionInfo.isConnected ? "Connected" : "Disconnected"}
						</div>
					</div>
					{connectionInfo.isConnected && (
						// InfoRow - Type row container
						<div className="flex flex-wrap whitespace-nowrap mb-1">
							{/* InfoLabel - Fixed-width label */}
							<div className="flex-none w-[90px] font-medium">Type:</div>
							{/* InfoValue - Flexible value container */}
							<div className="flex-1 break-words">{connectionInfo.isRemote ? "Remote" : "Local"}</div>
						</div>
					)}
					{connectionInfo.isConnected && connectionInfo.isRemote && connectionInfo.host && (
						// InfoRow - Remote host row container
						<div className="flex flex-wrap whitespace-nowrap mb-1">
							{/* InfoLabel - Fixed-width label */}
							<div className="flex-none w-[90px] font-medium">Remote Host:</div>
							{/* InfoValue - Flexible value container */}
							<div className="flex-1 break-words">{connectionInfo.host}</div>
						</div>
					)}
				</div>
			)}
 
			<VSCodeButton appearance="icon" onClick={openBrowserSettings}>
				<i className="codicon codicon-settings-gear" style={{ fontSize: "14.5px" }} />
			</VSCodeButton>
		</div>
	)
}
 
export default BrowserSettingsMenu