/** * IssueMapper — issue intelligence. * * One continuous workspace rather than two cards side by side: * * HEADER → ISSUE COMMAND (left) │ GENERATED PLAN (right) * * and the plan itself reads as the transformation it performs: * * ISSUE → MODULES → SYMBOLS → DEPENDENCIES → PLAN * * Every value shown comes from `POST /api/v1/issues/map` unchanged — complexity, * confidence, affected components, target files, steps and citations are all * backend fields. Nothing here derives or invents a metric. * * Typography rule: uppercase monospace is reserved for labels, paths, metadata * and step numbers. Issue titles and generated instructions keep readable casing. */ import React, { useState, useEffect, useRef } from 'react'; import { apiUrl, extractErrorMessage } from '../../lib/api'; import { AlertTriangle, ArrowRight, Loader2, ShieldCheck, ShieldAlert } from 'lucide-react'; import { FilePath } from '../ui/FilePath'; interface Step { step_number: number; description: string; files_to_modify: string[]; } interface Plan { issue_summary: string; issue_type: string; relevant_files: string[]; affected_components: string[]; implementation_plan: Step[]; complexity: string; confidence: number; verified: boolean; sources: string[]; } interface IssueMapperProps { repoName?: string; } /** * Issue type tone. Red is reserved for an actual defect; a refactor is a * caution, and anything else is ordinary intelligence rather than a warning. */ function typeToneClass(type: string): string { const t = (type || '').toLowerCase(); if (t === 'bug') return 'text-danger'; if (t === 'refactor') return 'text-warn'; return 'text-primary'; } /** * Complexity is an estimate of effort, not a fault, so it never reaches red. * Medium stays neutral — colouring every reading would make none of them mean * anything. */ function complexityToneClass(complexity: string): string { const c = (complexity || '').toLowerCase(); if (c === 'high') return 'text-warn'; if (c === 'low') return 'text-success'; return 'text-text'; } /** Low confidence is the uncertainty signal; high confidence reads as healthy. */ function confidenceToneClass(confidence: number): string { if (confidence < 70) return 'text-warn'; if (confidence >= 90) return 'text-success'; return 'text-text'; } export const IssueMapper: React.FC = ({ repoName }) => { const [selectedRepo, setSelectedRepo] = useState(() => { if (repoName) return repoName; if (typeof window !== 'undefined') { const urlParams = new URLSearchParams(window.location.search); const owner = urlParams.get('owner'); const repo = urlParams.get('repo'); if (owner && repo) { return `${owner}/${repo}`; } const stored = localStorage.getItem('activeRepo'); if (stored) return stored; } return ''; }); const [recentRepos, setRecentRepos] = useState<{ name: string }[]>([]); const [title, setTitle] = useState(''); const [description, setDescription] = useState(''); const [isLoading, setIsLoading] = useState(false); const [plan, setPlan] = useState(null); const [completedSteps, setCompletedSteps] = useState>({}); /** * A failed mapping used to log to the console and otherwise look like nothing * had happened. The request itself is unchanged; the outcome is now reported. */ const [errorMsg, setErrorMsg] = useState(''); const titleRef = useRef(null); // Sync selectedRepo with repoName prop changes and clear stale plan data on repository change useEffect(() => { if (repoName) { setSelectedRepo(repoName); setPlan(null); setCompletedSteps({}); setErrorMsg(''); } }, [repoName]); // Sync global active-repo events (for standalone /issues page cross-island updates) useEffect(() => { const handleRepoChanged = (e: Event) => { const customEvent = e as CustomEvent; if (customEvent.detail && customEvent.detail !== selectedRepo) { setSelectedRepo(customEvent.detail); setPlan(null); setCompletedSteps({}); setErrorMsg(''); } }; const handleRepoCleared = () => { setSelectedRepo(''); setPlan(null); setCompletedSteps({}); setErrorMsg(''); }; window.addEventListener('active-repo-changed', handleRepoChanged); window.addEventListener('active-repo-cleared', handleRepoCleared); return () => { window.removeEventListener('active-repo-changed', handleRepoChanged); window.removeEventListener('active-repo-cleared', handleRepoCleared); }; }, [selectedRepo]); // Fetch recent repositories if no repoName is locked useEffect(() => { if (!repoName) { fetch(apiUrl('/api/v1/repos/recent')) .then(res => res.json()) .then(data => { if (Array.isArray(data)) { setRecentRepos(data); if (data.length > 0 && !selectedRepo) { setSelectedRepo(data[0].name); } } }) .catch(err => console.error("Failed to fetch recent repos", err)); } }, [repoName]); const runMapping = async () => { if (!title.trim() || !selectedRepo.trim()) return; setIsLoading(true); setErrorMsg(''); try { const response = await fetch(apiUrl('/api/v1/issues/map'), { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ repo: selectedRepo, issue: `${title}\n${description}`.trim() }) }); if (response.ok) { const data = await response.json(); setPlan(data); setCompletedSteps({}); } else { const errorData = await response.json().catch(() => ({})); setErrorMsg(extractErrorMessage(errorData)); } } catch (err) { console.error("Failed to map issue", err); setErrorMsg(extractErrorMessage(err)); } finally { setIsLoading(false); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); await runMapping(); }; const toggleStep = (stepNum: number) => { setCompletedSteps(prev => ({ ...prev, [stepNum]: !prev[stepNum] })); }; /** Opens a target file in the graph. Contract and fallback are unchanged. */ const openInGraph = (file: string) => { if (typeof window === 'undefined') return; const [owner, repo] = (selectedRepo || '').split('/'); window.dispatchEvent( new CustomEvent('aria-open-graph', { detail: { owner: owner || undefined, repo: repo || undefined, file, path: file, source: 'issues', }, }) ); if (window.location.pathname === '/issues' && selectedRepo) { if (owner && repo) { window.location.href = `/analysis?owner=${owner}&repo=${repo}&tab=graph&file=${encodeURIComponent(file)}`; } } }; /** Hands the generated plan to chat. Contract and fallback are unchanged. */ const explainPlanInChat = (currentPlan: Plan) => { if (typeof window === 'undefined') return; window.dispatchEvent( new CustomEvent('aria-open-chat', { detail: { prompt: `Review this implementation plan for "${title}":\n\n${currentPlan.implementation_plan .map((s) => `Step ${s.step_number}: ${s.description} (Files: ${s.files_to_modify.join(', ')})`) .join('\n')}`, }, }) ); if (window.location.pathname === '/issues' && selectedRepo) { const [owner, repo] = selectedRepo.split('/'); if (owner && repo) { window.location.href = `/chat`; } } }; const canSubmit = !isLoading && Boolean(title.trim()) && Boolean(selectedRepo.trim()); // ── Left column · issue command console ─────────────────────────────────── const commandConsole = (

ISSUE COMMAND

{selectedRepo && ( // Truncates independently of the value the request actually sends. {selectedRepo} )}
{!repoName && (
{recentRepos.length > 0 ? ( ) : ( setSelectedRepo(e.target.value)} placeholder="owner/repo" className="console-field font-mono text-[11.5px]" /> )}
)}
setTitle(e.target.value)} placeholder="e.g. Fix memory leaks in SQLite store" className="console-field text-[12.5px]" />