|
| 1 | +import type { AgentAnnotation, LayoutMode } from "../../../core/types"; |
| 2 | +import { wrapText } from "../../lib/agentPopover"; |
| 3 | +import { annotationRangeLabel } from "../../lib/agentAnnotations"; |
| 4 | +import { fitText, padText } from "../../lib/text"; |
| 5 | +import type { AppTheme } from "../../themes"; |
| 6 | + |
| 7 | +function inlineNoteTitle(noteIndex: number, noteCount: number) { |
| 8 | + return noteCount > 1 ? `AI note ${noteIndex + 1}/${noteCount}` : "AI note"; |
| 9 | +} |
| 10 | + |
| 11 | +interface AgentInlineNoteLine { |
| 12 | + kind: "summary" | "rationale"; |
| 13 | + text: string; |
| 14 | +} |
| 15 | + |
| 16 | +function clamp(value: number, min: number, max: number) { |
| 17 | + return Math.min(Math.max(value, min), max); |
| 18 | +} |
| 19 | + |
| 20 | +function splitColumnWidths(width: number) { |
| 21 | + const markerWidth = 1; |
| 22 | + const separatorWidth = 1; |
| 23 | + const usableWidth = Math.max(0, width - markerWidth - separatorWidth); |
| 24 | + const leftWidth = Math.max(0, markerWidth + Math.floor(usableWidth / 2)); |
| 25 | + const rightWidth = Math.max(0, separatorWidth + usableWidth - Math.floor(usableWidth / 2)); |
| 26 | + return { leftWidth, rightWidth }; |
| 27 | +} |
| 28 | + |
| 29 | +/** Render the note card itself before the start of an annotated range. */ |
| 30 | +export function AgentInlineNote({ |
| 31 | + annotation, |
| 32 | + anchorSide, |
| 33 | + layout, |
| 34 | + noteCount = 1, |
| 35 | + noteIndex = 0, |
| 36 | + onClose, |
| 37 | + theme, |
| 38 | + width, |
| 39 | +}: { |
| 40 | + annotation: AgentAnnotation; |
| 41 | + anchorSide?: "old" | "new"; |
| 42 | + layout: Exclude<LayoutMode, "auto">; |
| 43 | + noteCount?: number; |
| 44 | + noteIndex?: number; |
| 45 | + onClose?: () => void; |
| 46 | + theme: AppTheme; |
| 47 | + width: number; |
| 48 | +}) { |
| 49 | + const closeText = onClose ? "[x]" : ""; |
| 50 | + const titleText = `${inlineNoteTitle(noteIndex, noteCount)} · ${annotationRangeLabel(annotation)}`; |
| 51 | + const splitWidths = splitColumnWidths(width); |
| 52 | + const canDockRight = layout === "split" && anchorSide === "new" && width >= 84; |
| 53 | + const canDockLeft = layout === "split" && anchorSide === "old" && width >= 84; |
| 54 | + const preferredDockWidth = canDockRight |
| 55 | + ? splitWidths.rightWidth |
| 56 | + : canDockLeft |
| 57 | + ? splitWidths.leftWidth |
| 58 | + : Math.max(34, width - 4); |
| 59 | + const boxWidth = clamp(preferredDockWidth, 28, Math.max(28, width - 4)); |
| 60 | + const boxLeft = canDockRight |
| 61 | + ? Math.max(0, width - boxWidth) |
| 62 | + : canDockLeft |
| 63 | + ? 0 |
| 64 | + : Math.min(4, Math.max(0, width - boxWidth)); |
| 65 | + const innerWidth = Math.max(1, boxWidth - 2); |
| 66 | + const titleWidth = Math.max(1, innerWidth - (closeText ? closeText.length + 1 : 0)); |
| 67 | + const bodyWidth = innerWidth; |
| 68 | + const lines: AgentInlineNoteLine[] = [ |
| 69 | + ...wrapText(annotation.summary, bodyWidth).map((text) => ({ kind: "summary" as const, text })), |
| 70 | + ...(annotation.rationale |
| 71 | + ? wrapText(annotation.rationale, bodyWidth).map((text) => ({ |
| 72 | + kind: "rationale" as const, |
| 73 | + text, |
| 74 | + })) |
| 75 | + : []), |
| 76 | + ]; |
| 77 | + const topBorder = `┌${"─".repeat(Math.max(0, boxWidth - 2))}┐`; |
| 78 | + const bottomBorder = |
| 79 | + anchorSide === "new" && canDockRight |
| 80 | + ? `└${"─".repeat(Math.max(0, boxWidth - 2))}┤` |
| 81 | + : anchorSide === "old" && canDockLeft |
| 82 | + ? `├${"─".repeat(Math.max(0, boxWidth - 2))}┘` |
| 83 | + : `└${"─".repeat(Math.max(0, boxWidth - 2))}┘`; |
| 84 | + |
| 85 | + return ( |
| 86 | + <box style={{ width: "100%", flexDirection: "column", backgroundColor: theme.panel }}> |
| 87 | + <box style={{ width: "100%", height: 1, flexDirection: "row", backgroundColor: theme.panel }}> |
| 88 | + <box style={{ width: boxLeft, height: 1, backgroundColor: theme.panel }}> |
| 89 | + <text>{" ".repeat(boxLeft)}</text> |
| 90 | + </box> |
| 91 | + <box style={{ width: boxWidth, height: 1, backgroundColor: theme.panel }}> |
| 92 | + <text fg={theme.noteBorder} bg={theme.noteBackground}> |
| 93 | + {topBorder} |
| 94 | + </text> |
| 95 | + </box> |
| 96 | + </box> |
| 97 | + |
| 98 | + <box style={{ width: "100%", height: 1, flexDirection: "row", backgroundColor: theme.panel }}> |
| 99 | + <box style={{ width: boxLeft, height: 1, backgroundColor: theme.panel }}> |
| 100 | + <text>{" ".repeat(boxLeft)}</text> |
| 101 | + </box> |
| 102 | + <box style={{ width: 1, height: 1, backgroundColor: theme.panel }}> |
| 103 | + <text fg={theme.noteBorder} bg={theme.noteBackground}> |
| 104 | + │ |
| 105 | + </text> |
| 106 | + </box> |
| 107 | + <box style={{ width: titleWidth, height: 1, backgroundColor: theme.panel }}> |
| 108 | + <text fg={theme.noteTitleText} bg={theme.noteTitleBackground}> |
| 109 | + {padText(fitText(titleText, titleWidth), titleWidth)} |
| 110 | + </text> |
| 111 | + </box> |
| 112 | + {closeText ? ( |
| 113 | + <box |
| 114 | + onMouseUp={onClose} |
| 115 | + style={{ width: closeText.length + 1, height: 1, backgroundColor: theme.panel }} |
| 116 | + > |
| 117 | + <text fg={theme.noteTitleText} bg={theme.noteTitleBackground}>{` ${closeText}`}</text> |
| 118 | + </box> |
| 119 | + ) : null} |
| 120 | + <box style={{ width: 1, height: 1, backgroundColor: theme.panel }}> |
| 121 | + <text fg={theme.noteBorder} bg={theme.noteBackground}> |
| 122 | + │ |
| 123 | + </text> |
| 124 | + </box> |
| 125 | + </box> |
| 126 | + |
| 127 | + {lines.map((line, index) => ( |
| 128 | + <box |
| 129 | + key={`${line.kind}:${index}`} |
| 130 | + style={{ width: "100%", height: 1, flexDirection: "row", backgroundColor: theme.panel }} |
| 131 | + > |
| 132 | + <box style={{ width: boxLeft, height: 1, backgroundColor: theme.panel }}> |
| 133 | + <text>{" ".repeat(boxLeft)}</text> |
| 134 | + </box> |
| 135 | + <box style={{ width: 1, height: 1, backgroundColor: theme.panel }}> |
| 136 | + <text fg={theme.noteBorder} bg={theme.noteBackground}> |
| 137 | + │ |
| 138 | + </text> |
| 139 | + </box> |
| 140 | + <box style={{ width: bodyWidth, height: 1, backgroundColor: theme.panel }}> |
| 141 | + <text fg={line.kind === "summary" ? theme.text : theme.muted} bg={theme.noteBackground}> |
| 142 | + {padText(line.text, bodyWidth)} |
| 143 | + </text> |
| 144 | + </box> |
| 145 | + <box style={{ width: 1, height: 1, backgroundColor: theme.panel }}> |
| 146 | + <text fg={theme.noteBorder} bg={theme.noteBackground}> |
| 147 | + │ |
| 148 | + </text> |
| 149 | + </box> |
| 150 | + </box> |
| 151 | + ))} |
| 152 | + |
| 153 | + <box style={{ width: "100%", height: 1, flexDirection: "row", backgroundColor: theme.panel }}> |
| 154 | + <box style={{ width: boxLeft, height: 1, backgroundColor: theme.panel }}> |
| 155 | + <text>{" ".repeat(boxLeft)}</text> |
| 156 | + </box> |
| 157 | + <box style={{ width: boxWidth, height: 1, backgroundColor: theme.panel }}> |
| 158 | + <text fg={theme.noteBorder} bg={theme.noteBackground}> |
| 159 | + {bottomBorder} |
| 160 | + </text> |
| 161 | + </box> |
| 162 | + </box> |
| 163 | + |
| 164 | + {(anchorSide === "new" || anchorSide === "old") && layout === "split" ? ( |
| 165 | + <box |
| 166 | + style={{ width: "100%", height: 1, flexDirection: "row", backgroundColor: theme.panel }} |
| 167 | + > |
| 168 | + {anchorSide === "old" ? ( |
| 169 | + <> |
| 170 | + <box style={{ width: 1, height: 1, backgroundColor: theme.panel }}> |
| 171 | + <text fg={theme.noteBorder}>│</text> |
| 172 | + </box> |
| 173 | + <box |
| 174 | + style={{ width: Math.max(0, width - 1), height: 1, backgroundColor: theme.panel }} |
| 175 | + > |
| 176 | + <text>{" ".repeat(Math.max(0, width - 1))}</text> |
| 177 | + </box> |
| 178 | + </> |
| 179 | + ) : ( |
| 180 | + <> |
| 181 | + <box |
| 182 | + style={{ width: Math.max(0, width - 1), height: 1, backgroundColor: theme.panel }} |
| 183 | + > |
| 184 | + <text>{" ".repeat(Math.max(0, width - 1))}</text> |
| 185 | + </box> |
| 186 | + <box style={{ width: 1, height: 1, backgroundColor: theme.panel }}> |
| 187 | + <text fg={theme.noteBorder}>│</text> |
| 188 | + </box> |
| 189 | + </> |
| 190 | + )} |
| 191 | + </box> |
| 192 | + ) : null} |
| 193 | + </box> |
| 194 | + ); |
| 195 | +} |
| 196 | + |
| 197 | +/** Render the small cap shown after the last diff row in a note's range. */ |
| 198 | +export function AgentInlineNoteGuideCap({ |
| 199 | + side, |
| 200 | + theme, |
| 201 | + width, |
| 202 | +}: { |
| 203 | + side: "old" | "new"; |
| 204 | + theme: AppTheme; |
| 205 | + width: number; |
| 206 | +}) { |
| 207 | + return ( |
| 208 | + <box style={{ width: "100%", height: 1, flexDirection: "row", backgroundColor: theme.panel }}> |
| 209 | + {side === "old" ? ( |
| 210 | + <> |
| 211 | + <box style={{ width: 1, height: 1, backgroundColor: theme.panel }}> |
| 212 | + <text fg={theme.noteBorder}>╵</text> |
| 213 | + </box> |
| 214 | + <box style={{ width: Math.max(0, width - 1), height: 1, backgroundColor: theme.panel }}> |
| 215 | + <text>{" ".repeat(Math.max(0, width - 1))}</text> |
| 216 | + </box> |
| 217 | + </> |
| 218 | + ) : ( |
| 219 | + <> |
| 220 | + <box style={{ width: Math.max(0, width - 1), height: 1, backgroundColor: theme.panel }}> |
| 221 | + <text>{" ".repeat(Math.max(0, width - 1))}</text> |
| 222 | + </box> |
| 223 | + <box style={{ width: 1, height: 1, backgroundColor: theme.panel }}> |
| 224 | + <text fg={theme.noteBorder}>╵</text> |
| 225 | + </box> |
| 226 | + </> |
| 227 | + )} |
| 228 | + </box> |
| 229 | + ); |
| 230 | +} |
0 commit comments