(props: SelectedCodePanelProps)
| 110 | } |
| 111 | |
| 112 | export default function SelectedCodePanel(props: SelectedCodePanelProps) { |
| 113 | const { filePath, symbol, selection, ir, imports } = props; |
| 114 | const [openEvidence, setOpenEvidence] = useState<string>(); |
| 115 | const identity = `${filePath}:${symbol?.qualifiedName ?? symbol?.name ?? ''}:${ir?.generation ?? ''}`; |
| 116 | useEffect(() => setOpenEvidence(undefined), [identity]); |
| 117 | const ready = props.status === 'ready' && ir !== undefined && symbol !== undefined; |
| 118 | const sections = ready ? buildSections(ir).filter((section) => |
| 119 | section.name !== 'purpose' && (section.name !== 'effects' || section.populated) |
| 120 | && (section.name !== 'risks' || section.populated)) : []; |
| 121 | const runtime = ready && ir.runtime && ir.runtime.value.length > 0 ? runtimeSection(ir) : undefined; |
| 122 | if (runtime) sections.push(runtime); |
| 123 | if (imports) sections.push(importsSection(imports)); |
| 124 | const entriesFor = (path: string): Evidence[] => |
| 125 | imports?.entries.find((entry) => entry.factPath === path)?.evidence |
| 126 | ?? (ready ? evidenceFor(ir, path) : []); |
| 127 | const hasFile = filePath.length > 0; |
| 128 | const range = symbol |
| 129 | ? `${symbol.range.start.line + 1}-${Math.max(symbol.range.start.line + 1, symbol.range.end.line + (symbol.range.end.character > 0 ? 1 : 0))}` |
| 130 | : undefined; |
| 131 | const noSymbolMessage = props.status === 'loading' ? 'Reading selected code...' |
| 132 | : props.status === 'failed' ? (props.message || 'Could not read symbol details.') |
| 133 | : props.status === 'not-indexed' ? (props.message || 'This file is not indexed yet.') |
| 134 | : undefined; |
| 135 | return ( |
| 136 | <section className="selected-code" aria-label="Selected code" data-testid="selected-code-panel" data-pinned={props.pinned}> |
| 137 | <header className="selected-code-header"> |
| 138 | <h2>Selected code</h2> |
| 139 | <button type="button" className="selected-code-pin" disabled={!hasFile || (!props.pinned && (props.status === 'loading' || props.fileSymbolsStatus === 'loading'))} |
| 140 | aria-pressed={props.pinned} onClick={props.onTogglePin} |
| 141 | title={props.pinned ? 'Follow the current editor selection' : 'Keep this context while exploring elsewhere'}> |
| 142 | {props.pinned ? 'Pinned' : 'Pin'} |
| 143 | </button> |
| 144 | </header> |
| 145 | {!hasFile ? ( |
| 146 | <p className="selected-code-message">Open a file to inspect its symbols and connections.</p> |
| 147 | ) : ( |
| 148 | <> |
| 149 | <div className="selected-code-identity"> |
| 150 | {symbol && <div className="selected-code-symbol"><strong>{symbol.name}</strong><span>{symbol.kind}</span></div>} |
| 151 | <div className="selected-code-path" title={filePath}>{filePath}{range && <span>:{range}</span>}</div> |
| 152 | {props.pinned && <p className="selected-code-note">Context pinned. Unpin to follow the editor.</p>} |
| 153 | {ready && ir.signature?.value && <code className="selected-code-signature">{ir.signature.value}</code>} |
| 154 | </div> |
| 155 | {selection && selection.text.length > 0 && ( |
| 156 | <div className="selected-code-selection" data-testid="selected-code-selection"> |
| 157 | <details> |
| 158 | <summary>Exact selection <span>{selection.startLine}:{selection.startColumn} to {selection.endLine}:{selection.endColumn}</span></summary> |
| 159 | <pre><code>{selection.text}</code></pre> |
| 160 | </details> |
| 161 | <p className="selected-code-note">Chat receives the marked text.{symbol ? ` Relationships below describe ${symbol.name}.` : ' File connections remain separate from the selection.'}</p> |
| 162 | </div> |
| 163 | )} |
| 164 | <div className="selected-code-actions"> |
| 165 | <button type="button" className="selected-code-ask" onClick={props.onAsk}>Ask about this</button> |
| 166 | <button type="button" onClick={props.onShowGraph}>Show in graph</button> |
| 167 | </div> |
| 168 | {props.selectionContext} |
| 169 | {props.impact} |
nothing calls this directly
no test coverage detected