~bigbes/lethe

ref: f9b7f23950aa6951ac4d3e464aa870056f969b87 lethe/web/src/primitives/HorizontalBars.tsx -rw-r--r-- 2.0 KiB
f9b7f239 — Eugene Blikh docs(lethe-oidc-stub): record verify-driven fix-up deviations 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
64
65
66
67
68
69
70
71
72
import type React from 'react'

interface HorizontalBarsRow {
  label: string
  count: number
}

interface HorizontalBarsProps<R extends HorizontalBarsRow = HorizontalBarsRow> {
  rows: R[]
  max: number
  onActivate?: (row: R) => void
}

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

  return (
    <div>
      {rows.map((row, i) => {
        const ratio = row.count / safeMax
        const activatable = onActivate != null
        const labelStyle: React.CSSProperties = {
          fontSize: 11,
          ...(activatable
            ? { color: 'var(--accent-ink)', cursor: 'pointer' }
            : {}),
        }
        const label = (
          <span
            className="mono truncate"
            style={labelStyle}
            onClick={activatable ? () => onActivate(row) : undefined}
            role={activatable ? 'button' : undefined}
            tabIndex={activatable ? 0 : undefined}
            onKeyDown={activatable ? (e) => { if (e.key === 'Enter') onActivate(row) } : undefined}
          >
            {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>
  )
}