diff-changes-v2.tsx 981 B

12345678910111213141516171819202122232425262728
  1. import { createMemo, Show } from "solid-js"
  2. import "./diff-changes-v2.css"
  3. export function DiffChanges(props: {
  4. class?: string
  5. changes: { additions: number; deletions: number } | { additions: number; deletions: number }[]
  6. }) {
  7. const additions = createMemo(() =>
  8. Array.isArray(props.changes)
  9. ? props.changes.reduce((acc, diff) => acc + (diff.additions ?? 0), 0)
  10. : props.changes.additions,
  11. )
  12. const deletions = createMemo(() =>
  13. Array.isArray(props.changes)
  14. ? props.changes.reduce((acc, diff) => acc + (diff.deletions ?? 0), 0)
  15. : props.changes.deletions,
  16. )
  17. const total = createMemo(() => (additions() ?? 0) + (deletions() ?? 0))
  18. return (
  19. <Show when={total() > 0}>
  20. <div data-component="diff-changes" classList={{ [props.class ?? ""]: true }}>
  21. <span data-slot="diff-changes-additions">{`+${additions()}`}</span>
  22. <span data-slot="diff-changes-deletions">{`-${deletions()}`}</span>
  23. </div>
  24. </Show>
  25. )
  26. }