~bigbes/lethe

ref: e048bdf74cd7459076a067ae22038a83e4627b6a lethe/web/src/primitives/HorizontalBars.tsx -rw-r--r-- 1.7 KiB
e048bdf7 — Eugene Blikh web: stats route with backend-driven chart primitives a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import type React from 'react'
import { Link } from '@tanstack/react-router'

interface HorizontalBarsRow {
  label: string
  count: number
  href?: string
}

interface HorizontalBarsProps {
  rows: HorizontalBarsRow[]
  max: number
}

export function HorizontalBars({ rows, max }: HorizontalBarsProps): React.JSX.Element {
  const safeMax = max > 0 ? max : 1

  return (
    <div>
      {rows.map((row, i) => {
        const ratio = row.count / safeMax
        const label = row.href != null ? (
          <Link
            to={row.href}
            className="mono truncate"
            style={{ fontSize: 11, color: 'var(--accent-ink)', textDecoration: 'none' }}
          >
            {row.label}
          </Link>
        ) : (
          <span className="mono truncate" style={{ fontSize: 11 }}>{row.label}</span>
        )

        return (
          <div
            key={i}
            style={{
              display: 'grid',
              gridTemplateColumns: '1fr 90px 30px',
              gap: 8,
              padding: '4px 0',
              borderBottom: i < rows.length - 1 ? '1px dotted var(--rule-2)' : 'none',
              alignItems: 'center',
            }}
          >
            {label}
            <span style={{ height: 5, background: 'var(--paper-2)', borderRadius: 2 }}>
              <div
                style={{
                  width: `${ratio * 100}%`,
                  height: '100%',
                  background: 'var(--accent)',
                  borderRadius: 2,
                }}
              />
            </span>
            <span className="right mono muted" style={{ fontSize: 11 }}>{row.count}</span>
          </div>
        )
      })}
    </div>
  )
}