| 1 | import { createMemo, For, Match, Show, Switch } from "solid-js" |
| 2 | |
| 3 | export function DiffChanges(props: { |
| 4 | class?: string |
| 5 | changes: { additions: number; deletions: number } | { additions: number; deletions: number }[] |
| 6 | variant?: "default" | "bars" |
| 7 | }) { |
| 8 | const variant = () => props.variant ?? "default" |
| 9 | |
| 10 | const additions = createMemo(() => |
| 11 | Array.isArray(props.changes) |
| 12 | ? props.changes.reduce((acc, diff) => acc + (diff.additions ?? 0), 0) |
| 13 | : props.changes.additions, |
| 14 | ) |
| 15 | const deletions = createMemo(() => |
| 16 | Array.isArray(props.changes) |
| 17 | ? props.changes.reduce((acc, diff) => acc + (diff.deletions ?? 0), 0) |
| 18 | : props.changes.deletions, |
| 19 | ) |
| 20 | const total = createMemo(() => (additions() ?? 0) + (deletions() ?? 0)) |
| 21 | |
| 22 | const blockCounts = createMemo(() => { |
| 23 | const TOTAL_BLOCKS = 5 |
| 24 | |
| 25 | const adds = additions() ?? 0 |
| 26 | const dels = deletions() ?? 0 |
| 27 | |
| 28 | if (adds === 0 && dels === 0) { |
| 29 | return { added: 0, deleted: 0, neutral: TOTAL_BLOCKS } |
| 30 | } |
| 31 | |
| 32 | const total = adds + dels |
| 33 | |
| 34 | if (total < 5) { |
| 35 | const added = adds > 0 ? 1 : 0 |
| 36 | const deleted = dels > 0 ? 1 : 0 |
| 37 | const neutral = TOTAL_BLOCKS - added - deleted |
| 38 | return { added, deleted, neutral } |
| 39 | } |
| 40 | |
| 41 | const ratio = adds > dels ? adds / dels : dels / adds |
| 42 | let BLOCKS_FOR_COLORS = TOTAL_BLOCKS |
| 43 | |
| 44 | if (total < 20) { |
| 45 | BLOCKS_FOR_COLORS = TOTAL_BLOCKS - 1 |
| 46 | } else if (ratio < 4) { |
| 47 | BLOCKS_FOR_COLORS = TOTAL_BLOCKS - 1 |
| 48 | } |
| 49 | |
| 50 | const percentAdded = adds / total |
| 51 | const percentDeleted = dels / total |
| 52 | |
| 53 | const added_raw = percentAdded * BLOCKS_FOR_COLORS |
| 54 | const deleted_raw = percentDeleted * BLOCKS_FOR_COLORS |
| 55 | |
| 56 | let added = adds > 0 ? Math.max(1, Math.round(added_raw)) : 0 |
| 57 | let deleted = dels > 0 ? Math.max(1, Math.round(deleted_raw)) : 0 |
| 58 | |
| 59 | // Cap bars based on actual change magnitude |
| 60 | if (adds > 0 && adds <= 5) added = Math.min(added, 1) |