|
| 1 | +/** |
| 2 | + * UntrackedStatus - Shows untracked files count with interactive tooltip |
| 3 | + */ |
| 4 | + |
| 5 | +import React, { useState, useEffect, useRef } from "react"; |
| 6 | +import styled from "@emotion/styled"; |
| 7 | +import { keyframes } from "@emotion/react"; |
| 8 | + |
| 9 | +interface UntrackedStatusProps { |
| 10 | + workspaceId: string; |
| 11 | + workspacePath: string; |
| 12 | + refreshTrigger?: number; |
| 13 | +} |
| 14 | + |
| 15 | +const Container = styled.div` |
| 16 | + position: relative; |
| 17 | + display: inline-block; |
| 18 | +`; |
| 19 | + |
| 20 | +const Badge = styled.div<{ hasUntracked: boolean }>` |
| 21 | + padding: 4px 10px; |
| 22 | + border-radius: 3px; |
| 23 | + font-weight: 500; |
| 24 | + font-size: 11px; |
| 25 | + background: ${(props) => (props.hasUntracked ? "#3e2a00" : "transparent")}; |
| 26 | + border: 1px solid ${(props) => (props.hasUntracked ? "#806000" : "transparent")}; |
| 27 | + color: ${(props) => (props.hasUntracked ? "#ffb347" : "#888")}; |
| 28 | + white-space: nowrap; |
| 29 | + cursor: ${(props) => (props.hasUntracked ? "pointer" : "default")}; |
| 30 | + transition: all 0.2s ease; |
| 31 | +
|
| 32 | + &:hover { |
| 33 | + ${(props) => |
| 34 | + props.hasUntracked && |
| 35 | + ` |
| 36 | + background: #4a3200; |
| 37 | + border-color: #a07000; |
| 38 | + `} |
| 39 | + } |
| 40 | +`; |
| 41 | + |
| 42 | +const fadeIn = keyframes` |
| 43 | + from { |
| 44 | + opacity: 0; |
| 45 | + transform: translateY(-4px); |
| 46 | + } |
| 47 | + to { |
| 48 | + opacity: 1; |
| 49 | + transform: translateY(0); |
| 50 | + } |
| 51 | +`; |
| 52 | + |
| 53 | +const Tooltip = styled.div` |
| 54 | + position: absolute; |
| 55 | + top: calc(100% + 8px); |
| 56 | + right: 0; |
| 57 | + background: #2d2d30; |
| 58 | + border: 1px solid #454545; |
| 59 | + border-radius: 4px; |
| 60 | + padding: 8px; |
| 61 | + min-width: 200px; |
| 62 | + max-width: 400px; |
| 63 | + z-index: 1000; |
| 64 | + box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3); |
| 65 | + animation: ${fadeIn} 0.15s ease; |
| 66 | +`; |
| 67 | + |
| 68 | +const TooltipHeader = styled.div` |
| 69 | + font-size: 11px; |
| 70 | + font-weight: 600; |
| 71 | + color: #ccc; |
| 72 | + margin-bottom: 8px; |
| 73 | + padding-bottom: 6px; |
| 74 | + border-bottom: 1px solid #3e3e42; |
| 75 | +`; |
| 76 | + |
| 77 | +const FileList = styled.div` |
| 78 | + max-height: 200px; |
| 79 | + overflow-y: auto; |
| 80 | + margin-bottom: 8px; |
| 81 | +`; |
| 82 | + |
| 83 | +const FileItem = styled.div` |
| 84 | + font-size: 11px; |
| 85 | + color: #aaa; |
| 86 | + padding: 3px 4px; |
| 87 | + font-family: var(--font-monospace); |
| 88 | + white-space: nowrap; |
| 89 | + overflow: hidden; |
| 90 | + text-overflow: ellipsis; |
| 91 | +
|
| 92 | + &:hover { |
| 93 | + background: #37373d; |
| 94 | + } |
| 95 | +`; |
| 96 | + |
| 97 | +const TrackButton = styled.button` |
| 98 | + width: 100%; |
| 99 | + padding: 4px 8px; |
| 100 | + background: transparent; |
| 101 | + color: #888; |
| 102 | + border: 1px solid #444; |
| 103 | + border-radius: 3px; |
| 104 | + font-size: 11px; |
| 105 | + cursor: pointer; |
| 106 | + transition: all 0.2s ease; |
| 107 | + font-family: var(--font-primary); |
| 108 | +
|
| 109 | + &:hover { |
| 110 | + background: rgba(255, 255, 255, 0.05); |
| 111 | + color: #ccc; |
| 112 | + border-color: #666; |
| 113 | + } |
| 114 | +
|
| 115 | + &:active { |
| 116 | + background: rgba(255, 255, 255, 0.1); |
| 117 | + } |
| 118 | +
|
| 119 | + &:disabled { |
| 120 | + color: #555; |
| 121 | + border-color: #333; |
| 122 | + cursor: not-allowed; |
| 123 | + background: transparent; |
| 124 | + } |
| 125 | +`; |
| 126 | + |
| 127 | +export const UntrackedStatus: React.FC<UntrackedStatusProps> = ({ |
| 128 | + workspaceId, |
| 129 | + workspacePath, |
| 130 | + refreshTrigger, |
| 131 | +}) => { |
| 132 | + const [untrackedFiles, setUntrackedFiles] = useState<string[]>([]); |
| 133 | + const [isLoading, setIsLoading] = useState(false); |
| 134 | + const [showTooltip, setShowTooltip] = useState(false); |
| 135 | + const [isTracking, setIsTracking] = useState(false); |
| 136 | + const containerRef = useRef<HTMLDivElement>(null); |
| 137 | + const hasLoadedOnce = useRef(false); |
| 138 | + |
| 139 | + // Load untracked files |
| 140 | + useEffect(() => { |
| 141 | + let cancelled = false; |
| 142 | + |
| 143 | + const loadUntracked = async () => { |
| 144 | + // Only show loading on first load ever, not on subsequent refreshes |
| 145 | + if (!hasLoadedOnce.current) { |
| 146 | + setIsLoading(true); |
| 147 | + } |
| 148 | + |
| 149 | + try { |
| 150 | + const result = await window.api.workspace.executeBash( |
| 151 | + workspaceId, |
| 152 | + "git ls-files --others --exclude-standard", |
| 153 | + { timeout_secs: 5 } |
| 154 | + ); |
| 155 | + |
| 156 | + if (cancelled) return; |
| 157 | + |
| 158 | + if (result.success) { |
| 159 | + const files = (result.data.output ?? "") |
| 160 | + .split("\n") |
| 161 | + .map((f) => f.trim()) |
| 162 | + .filter(Boolean); |
| 163 | + setUntrackedFiles(files); |
| 164 | + } |
| 165 | + |
| 166 | + hasLoadedOnce.current = true; |
| 167 | + } catch (err) { |
| 168 | + console.error("Failed to load untracked files:", err); |
| 169 | + } finally { |
| 170 | + setIsLoading(false); |
| 171 | + } |
| 172 | + }; |
| 173 | + |
| 174 | + void loadUntracked(); |
| 175 | + |
| 176 | + return () => { |
| 177 | + cancelled = true; |
| 178 | + }; |
| 179 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 180 | + }, [workspaceId, workspacePath, refreshTrigger]); |
| 181 | + |
| 182 | + // Close tooltip when clicking outside |
| 183 | + useEffect(() => { |
| 184 | + if (!showTooltip) return; |
| 185 | + |
| 186 | + const handleClickOutside = (e: MouseEvent) => { |
| 187 | + if (containerRef.current && !containerRef.current.contains(e.target as Node)) { |
| 188 | + setShowTooltip(false); |
| 189 | + } |
| 190 | + }; |
| 191 | + |
| 192 | + document.addEventListener("mousedown", handleClickOutside); |
| 193 | + return () => document.removeEventListener("mousedown", handleClickOutside); |
| 194 | + }, [showTooltip]); |
| 195 | + |
| 196 | + const handleTrackAll = async () => { |
| 197 | + if (untrackedFiles.length === 0 || isTracking) return; |
| 198 | + |
| 199 | + setIsTracking(true); |
| 200 | + try { |
| 201 | + // Use git add with -- to treat all arguments as file paths |
| 202 | + // Escape single quotes by replacing ' with '\'' for safe shell quoting |
| 203 | + const escapedFiles = untrackedFiles.map((f) => `'${f.replace(/'/g, "'\\''")}'`).join(" "); |
| 204 | + const result = await window.api.workspace.executeBash( |
| 205 | + workspaceId, |
| 206 | + `git add -- ${escapedFiles}`, |
| 207 | + { timeout_secs: 10 } |
| 208 | + ); |
| 209 | + |
| 210 | + if (result.success) { |
| 211 | + setUntrackedFiles([]); |
| 212 | + setShowTooltip(false); |
| 213 | + } else { |
| 214 | + console.error("Failed to track files:", result.error); |
| 215 | + } |
| 216 | + } catch (err) { |
| 217 | + console.error("Failed to track files:", err); |
| 218 | + } finally { |
| 219 | + setIsTracking(false); |
| 220 | + } |
| 221 | + }; |
| 222 | + |
| 223 | + const count = untrackedFiles.length; |
| 224 | + const hasUntracked = count > 0; |
| 225 | + |
| 226 | + return ( |
| 227 | + <Container ref={containerRef}> |
| 228 | + <Badge |
| 229 | + hasUntracked={hasUntracked} |
| 230 | + onClick={() => hasUntracked && setShowTooltip(!showTooltip)} |
| 231 | + > |
| 232 | + {isLoading ? "..." : `${count} Untracked`} |
| 233 | + </Badge> |
| 234 | + |
| 235 | + {showTooltip && hasUntracked && ( |
| 236 | + <Tooltip> |
| 237 | + <TooltipHeader>Untracked Files ({count})</TooltipHeader> |
| 238 | + <FileList> |
| 239 | + {untrackedFiles.map((file) => ( |
| 240 | + <FileItem key={file}>{file}</FileItem> |
| 241 | + ))} |
| 242 | + </FileList> |
| 243 | + <TrackButton onClick={() => void handleTrackAll()} disabled={isTracking}> |
| 244 | + {isTracking ? "Tracking..." : "Track All"} |
| 245 | + </TrackButton> |
| 246 | + </Tooltip> |
| 247 | + )} |
| 248 | + </Container> |
| 249 | + ); |
| 250 | +}; |
0 commit comments