All files / src/components/cost DailyCostBars.tsx

80% Statements 12/15
40.74% Branches 11/27
62.5% Functions 5/8
78.57% Lines 11/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101                      34x       9x 9x 9x 18x       9x   9x                                               9x 9x 9x                                                                         9x                          
import { CostDay } from "@/objects/Cost";
import { parseServerDate } from "@/util/time";
import React, { useState } from "react";
import { formatUsd } from "./format";
 
/**
 * Spend per day, as bars — one series, so one hue and no legend; the card's
 * title names it. Bars rise from a shared baseline with rounded tops and a
 * 2px gap. Hovering (or focusing) a day shows its figure; a visually hidden
 * table carries the same numbers for a screen reader.
 */
const DailyCostBars: React.FC<{ days: CostDay[]; height?: number }> = ({
  days,
  height = 56,
}) => {
  const [active, setActive] = useState<number | null>(null);
  const max = Math.max(...days.map((d) => d.cost_usd ?? 0), 0);
  const label = (d: CostDay) =>
    parseServerDate(d.date).toLocaleDateString(undefined, {
      month: "short",
      day: "numeric",
    });
  const shown = active !== null ? days[active] : null;
 
  return (
    <div>
      <div className="flex items-center justify-between text-[11px] text-gray-500 dark:text-gray-400 mb-1 h-4">
        <span>Daily spend</span>
        {shown && (
          <span className="text-gray-700 dark:text-gray-200 tabular-nums">
            {label(shown)} ·{" "}
            {shown.cost_usd === null
              ? shown.invocations
                ? "unpriced"
                : "nothing"
              : formatUsd(shown.cost_usd)}{" "}
            · {shown.invocations} call{shown.invocations === 1 ? "" : "s"}
          </span>
        )}
      </div>
      <div
        className="flex items-end gap-[2px] border-b border-gray-200 dark:border-gray-700"
        style={{ height }}
        role="img"
        aria-label={`Daily spend over ${days.length} days`}
        onMouseLeave={() => setActive(null)}
      >
        {days.map((d, i) => {
          const value = d.cost_usd ?? 0;
          const h = max > 0 ? Math.max((value / max) * height, value > 0 ? 2 : 0) : 0;
          return (
            <button
              key={d.date}
              type="button"
              tabIndex={-1}
              aria-hidden
              onMouseEnter={() => setActive(i)}
              onFocus={() => setActive(i)}
              className="flex-1 h-full flex flex-col justify-end min-w-0 p-0"
            >
              <span
                className={`block w-full shrink-0 rounded-t-[4px] transition-colors ${
                  active === i
                    ? "bg-violet-700 dark:bg-violet-300"
                    : "bg-violet-500 dark:bg-violet-400"
                }`}
                style={{ height: h }}
              />
            </button>
          );
        })}
      </div>
      <div className="flex justify-between text-[10px] text-gray-400 dark:text-gray-500 mt-1">
        <span>{days.length ? label(days[0]) : ""}</span>
        <span>{days.length ? label(days[days.length - 1]) : ""}</span>
      </div>
      <table className="sr-only">
        <caption>Daily spend</caption>
        <thead>
          <tr>
            <th>Date</th>
            <th>Cost</th>
            <th>Invocations</th>
          </tr>
        </thead>
        <tbody>
          {days.map((d) => (
            <tr key={d.date}>
              <td>{d.date}</td>
              <td>{d.cost_usd === null ? "—" : formatUsd(d.cost_usd)}</td>
              <td>{d.invocations}</td>
            </tr>
          ))}
        </tbody>
      </table>
    </div>
  );
};
 
export default DailyCostBars;