● Zion Boggan
repos/Docket/components/vote-tally.tsx
zionboggan.com ↗
94 lines · tsx
History for this file →
1
import { clsx } from "@/lib/cx";
2
 
3
 
4
export function VoteTally({
5
  yea,
6
  nay,
7
  present = 0,
8
  result,
9
  className,
10
}: {
11
  yea: number;
12
  nay: number;
13
  present?: number;
14
  result?: "passed" | "failed";
15
  className?: string;
16
}) {
17
  const total = Math.max(1, yea + nay + present);
18
  const yeaPct = (yea / total) * 100;
19
  const nayPct = (nay / total) * 100;
20
  const presentPct = (present / total) * 100;
21
 
22
  return (
23
    <div className={clsx("w-full", className)}>
24
      <div className="mb-2 flex items-baseline justify-between font-mono">
25
        <span className="label">Tally</span>
26
        {result ? (
27
          <span
28
            className="label"
29
            style={{
30
              color:
31
                result === "passed"
32
                  ? "var(--dk-yea)"
33
                  : "var(--dk-nay)",
34
            }}
35
          >
36
            {result === "passed" ? "Passed" : "Failed"}
37
          </span>
38
        ) : null}
39
      </div>
40
      <div
41
        className="flex h-2.5 w-full overflow-hidden rounded-[2px] border border-line"
42
        role="img"
43
        aria-label={`Vote tally: ${yea} yea, ${nay} nay${present ? `, ${present} present` : ""}`}
44
      >
45
        <span style={{ width: `${yeaPct}%`, backgroundColor: "var(--dk-yea)" }} />
46
        <span style={{ width: `${nayPct}%`, backgroundColor: "var(--dk-nay)" }} />
47
        {present > 0 ? (
48
          <span style={{ width: `${presentPct}%`, backgroundColor: "var(--dk-ash-2)" }} />
49
        ) : null}
50
      </div>
51
      <div className="mt-2 grid grid-cols-3 gap-2 font-mono text-[13px]">
52
        <TallyCell label="Yea" value={yea} color="var(--dk-yea)" />
53
        <TallyCell label="Nay" value={nay} color="var(--dk-nay)" />
54
        <TallyCell
55
          label="Present"
56
          value={present}
57
          color="var(--dk-ash-2)"
58
          dim={present === 0}
59
        />
60
      </div>
61
    </div>
62
  );
63
}
64
 
65
function TallyCell({
66
  label,
67
  value,
68
  color,
69
  dim,
70
}: {
71
  label: string;
72
  value: number;
73
  color: string;
74
  dim?: boolean;
75
}) {
76
  return (
77
    <div className="border-l border-line pl-2">
78
      <div className="label flex items-center gap-1.5" style={{ color: dim ? "var(--dk-ash-2)" : color }}>
79
        <span
80
          aria-hidden="true"
81
          className="inline-block h-1.5 w-1.5 rounded-full"
82
          style={{ backgroundColor: color }}
83
        />
84
        {label}
85
      </div>
86
      <div
87
        className="mt-0.5 tabular-nums"
88
        style={{ color: dim ? "var(--dk-ash-2)" : "var(--dk-paper)" }}
89
      >
90
        {value}
91
      </div>
92
    </div>
93
  );
94
}