All files / src/components/map LayerList.tsx

0% Statements 0/317
100% Branches 1/1
100% Functions 1/1
0% Lines 0/317

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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
import type { MapLayer } from "@shared/proto/cline/map"
import { RemoveMapLayerRequest } from "@shared/proto/cline/map"
import React, { useState } from "react"
import { useMapContext } from "../../context/MapContext"
import { MapServiceClient } from "../../services/grpc-client"
 
interface LayerListProps {
	onZoomToLayer?: (layer: MapLayer) => void
	onVisibilityChange: (layerId: string, visible: boolean) => void
	visibleLayerIds: Set<string>
	mapStyle?: string
}
 
/**
 * LayerList component - displays and manages map layers
 * Features: visibility toggle, remove layer, zoom to extent
 */
const getLayerTypeIcon = (layerType?: string): string => {
	switch ((layerType || "").toLowerCase()) {
		case "point":
			return "●"
		case "line":
			return "〰"
		case "polygon":
			return "⬡"
		case "raster":
			return "▦"
		default:
			return "◈"
	}
}
 
export const LayerList: React.FC<LayerListProps> = ({
	onZoomToLayer,
	onVisibilityChange,
	visibleLayerIds,
	mapStyle = "dark",
}) => {
	const { layers } = useMapContext()
	const [collapsed, setCollapsed] = useState(false)
	const [confirmingClear, setConfirmingClear] = useState(false)
 
	const isDark = mapStyle === "dark"
	const bgColor = isDark ? "rgba(26, 26, 46, 0.95)" : "rgba(245, 245, 245, 0.95)"
	const textColor = isDark ? "#ffffff" : "#000000"
	const borderColor = isDark ? "rgba(255, 255, 255, 0.1)" : "rgba(0, 0, 0, 0.1)"
 
	// Separate workspace files from manual layers
	const workspaceLayers = layers.filter((l) => l.metadata?.source === "workspace")
 
	// Check if all workspace files are hidden or visible
	const allWorkspaceHidden = workspaceLayers.every((l) => !visibleLayerIds.has(l.id))
	const allWorkspaceVisible = workspaceLayers.every((l) => visibleLayerIds.has(l.id))
 
	const handleToggleVisibility = (layerId: string) => {
		const willBeVisible = !visibleLayerIds.has(layerId)
		onVisibilityChange(layerId, willBeVisible)
	}
 
	const handleShowAllWorkspace = () => {
		workspaceLayers.forEach((layer) => {
			onVisibilityChange(layer.id, true)
		})
	}
 
	const handleHideAllWorkspace = () => {
		workspaceLayers.forEach((layer) => {
			onVisibilityChange(layer.id, false)
		})
	}
 
	const handleRemoveLayer = async (layerId: string) => {
		try {
			// Remove from backend
			await MapServiceClient.removeMapLayer(RemoveMapLayerRequest.create({ layerId }))
		} catch (error) {
			console.error("Failed to remove layer:", error)
		}
	}
 
	const handleClearAll = async () => {
		try {
			await MapServiceClient.clearMapLayers({})
		} catch (error) {
			console.error("Failed to clear layers:", error)
		} finally {
			setConfirmingClear(false)
		}
	}
 
	const handleZoomTo = (layer: MapLayer) => {
		if (onZoomToLayer) {
			onZoomToLayer(layer)
		}
	}
 
	// Get layer color for visual indicator
	const getLayerColor = (layer: MapLayer): string => {
		return layer.style?.fillColor || layer.style?.color || "#0066CC"
	}
 
	if (layers.length === 0) {
		return null
	}
 
	return (
		<div
			style={{
				position: "absolute",
				top: "10px",
				left: "10px",
				background: bgColor,
				color: textColor,
				borderRadius: "4px",
				border: `1px solid ${borderColor}`,
				minWidth: "250px",
				maxWidth: "350px",
				maxHeight: "80vh",
				overflow: "hidden",
				display: "flex",
				flexDirection: "column",
				boxShadow: "0 2px 8px rgba(0, 0, 0, 0.3)",
				zIndex: 2,
			}}>
			{/* Header */}
			<div
				onClick={() => setCollapsed(!collapsed)}
				style={{
					padding: "12px",
					borderBottom: `1px solid ${borderColor}`,
					display: "flex",
					justifyContent: "space-between",
					alignItems: "center",
					cursor: "pointer",
				}}>
				<div style={{ display: "flex", alignItems: "center", gap: "8px" }}>
					<span style={{ fontSize: "16px" }}>📑</span>
					<span style={{ fontWeight: "bold", fontSize: "14px" }}>Layers ({layers.length})</span>
				</div>
				<button
					onClick={(e) => {
						e.stopPropagation()
						setCollapsed(!collapsed)
					}}
					style={{
						background: "transparent",
						border: "none",
						color: textColor,
						cursor: "pointer",
						fontSize: "18px",
						padding: "4px",
						display: "flex",
						alignItems: "center",
					}}
					title={collapsed ? "Expand" : "Collapse"}>
					{collapsed ? "▼" : "▲"}
				</button>
			</div>
 
			{!collapsed && (
				<>
					{/* Layer list */}
					<div
						style={{
							flex: 1,
							overflowY: "auto",
							padding: "8px",
						}}>
						{layers.map((layer) => {
							const isVisible = visibleLayerIds.has(layer.id)
							const layerColor = getLayerColor(layer)
 
							return (
								<div
									key={layer.id}
									style={{
										padding: "8px",
										marginBottom: "4px",
										background: isDark ? "rgba(255, 255, 255, 0.05)" : "rgba(0, 0, 0, 0.03)",
										borderRadius: "4px",
										border: `1px solid ${borderColor}`,
										opacity: isVisible ? 1 : 0.5,
									}}>
									{/* Layer name and color indicator */}
									<div style={{ display: "flex", alignItems: "center", gap: "6px", marginBottom: "8px" }}>
										<div
											style={{
												width: "14px",
												height: "14px",
												borderRadius: "2px",
												background: layerColor,
												border: "1px solid rgba(255, 255, 255, 0.3)",
												flexShrink: 0,
											}}
										/>
										<span
											style={{ fontSize: "11px", opacity: 0.7, flexShrink: 0 }}
											title={`Layer type: ${layer.layerType || "unknown"}`}>
											{getLayerTypeIcon(layer.layerType)}
										</span>
										{/* Workspace file badge */}
										{layer.metadata?.source === "workspace" && (
											<span style={{ fontSize: "12px" }} title="Workspace file">
												📁
											</span>
										)}
										<span
											style={{
												fontSize: "13px",
												fontWeight: "500",
												flex: 1,
												overflow: "hidden",
												textOverflow: "ellipsis",
												whiteSpace: "nowrap",
											}}>
											{layer.name || layer.id}
										</span>
									</div>
 
									{/* Layer metadata — skip internal system keys */}
									{layer.metadata &&
										(() => {
											const entries = Object.entries(layer.metadata).filter(
												([k]) => k !== "__operation" && k !== "source",
											)
											return entries.length > 0 ? (
												<div style={{ fontSize: "11px", opacity: 0.7, marginBottom: "8px" }}>
													{entries.slice(0, 4).map(([key, value]) => (
														<div key={key}>
															<b>{key}:</b> {value}
														</div>
													))}
													{entries.length > 4 && (
														<div style={{ opacity: 0.6 }}>+{entries.length - 4} more</div>
													)}
												</div>
											) : null
										})()}
 
									{/* Controls */}
									<div style={{ display: "flex", gap: "8px", alignItems: "center" }}>
										{/* Visibility toggle */}
										<label
											style={{
												display: "flex",
												alignItems: "center",
												gap: "4px",
												cursor: "pointer",
												fontSize: "12px",
											}}>
											<input
												checked={isVisible}
												onChange={() => handleToggleVisibility(layer.id)}
												style={{ cursor: "pointer" }}
												type="checkbox"
											/>
											<span>{isVisible ? "Visible" : "Hidden"}</span>
										</label>
 
										{/* Zoom to button */}
										<button
											onClick={() => handleZoomTo(layer)}
											style={{
												padding: "4px 8px",
												fontSize: "11px",
												background: isDark ? "rgba(255, 255, 255, 0.1)" : "rgba(0, 0, 0, 0.1)",
												border: `1px solid ${borderColor}`,
												borderRadius: "3px",
												color: textColor,
												cursor: "pointer",
											}}
											title="Zoom to layer extent">
											🔍
										</button>
 
										{/* Remove button */}
										<button
											onClick={() => handleRemoveLayer(layer.id)}
											style={{
												padding: "4px 8px",
												fontSize: "11px",
												background: "rgba(220, 53, 69, 0.1)",
												border: "1px solid rgba(220, 53, 69, 0.3)",
												borderRadius: "3px",
												color: "#dc3545",
												cursor: "pointer",
											}}
											title="Remove layer">
											✕
										</button>
									</div>
								</div>
							)
						})}
					</div>
 
					{/* Footer with workspace and clear all buttons */}
					<div
						style={{
							padding: "8px",
							borderTop: `1px solid ${borderColor}`,
							display: "flex",
							flexDirection: "column",
							gap: "8px",
						}}>
						{/* Workspace files control buttons */}
						{workspaceLayers.length > 0 && (
							<div style={{ display: "flex", gap: "8px" }}>
								<button
									disabled={allWorkspaceVisible}
									onClick={handleShowAllWorkspace}
									style={{
										flex: 1,
										padding: "8px",
										fontSize: "11px",
										background: isDark ? "rgba(255, 255, 255, 0.1)" : "rgba(0, 0, 0, 0.1)",
										border: `1px solid ${borderColor}`,
										borderRadius: "3px",
										color: textColor,
										cursor: allWorkspaceVisible ? "not-allowed" : "pointer",
										opacity: allWorkspaceVisible ? 0.5 : 1,
										fontWeight: "500",
									}}
									title="Show all workspace files">
									📁 Show All
								</button>
								<button
									disabled={allWorkspaceHidden}
									onClick={handleHideAllWorkspace}
									style={{
										flex: 1,
										padding: "8px",
										fontSize: "11px",
										background: isDark ? "rgba(255, 255, 255, 0.1)" : "rgba(0, 0, 0, 0.1)",
										border: `1px solid ${borderColor}`,
										borderRadius: "3px",
										color: textColor,
										cursor: allWorkspaceHidden ? "not-allowed" : "pointer",
										opacity: allWorkspaceHidden ? 0.5 : 1,
										fontWeight: "500",
									}}
									title="Hide all workspace files">
									📁 Hide All
								</button>
							</div>
						)}
 
						{/* Clear all layers — inline confirm (no window.confirm which is blocked in webviews) */}
						{layers.length > 1 &&
							(confirmingClear ? (
								<div style={{ display: "flex", gap: "6px", alignItems: "center" }}>
									<span style={{ fontSize: "11px", flex: 1, color: isDark ? "#ffa0a0" : "#cc0000" }}>
										Remove all {layers.length} layers?
									</span>
									<button
										onClick={handleClearAll}
										style={{
											padding: "4px 10px",
											fontSize: "11px",
											fontWeight: "600",
											background: "rgba(220, 53, 69, 0.8)",
											border: "none",
											borderRadius: "3px",
											color: "#fff",
											cursor: "pointer",
										}}>
										Yes
									</button>
									<button
										onClick={() => setConfirmingClear(false)}
										style={{
											padding: "4px 10px",
											fontSize: "11px",
											background: isDark ? "rgba(255,255,255,0.1)" : "rgba(0,0,0,0.1)",
											border: `1px solid ${borderColor}`,
											borderRadius: "3px",
											color: textColor,
											cursor: "pointer",
										}}>
										No
									</button>
								</div>
							) : (
								<button
									onClick={() => setConfirmingClear(true)}
									style={{
										width: "100%",
										padding: "8px",
										fontSize: "12px",
										background: "rgba(220, 53, 69, 0.1)",
										border: "1px solid rgba(220, 53, 69, 0.3)",
										borderRadius: "3px",
										color: "#dc3545",
										cursor: "pointer",
										fontWeight: "500",
									}}>
									Clear All Layers
								</button>
							))}
					</div>
				</>
			)}
		</div>
	)
}
 
export default LayerList