Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

4.2 KB
Raw Blame
"use client";

import { PieChart, Pie, Cell, ResponsiveContainer, Tooltip } from "recharts";
import { Card, CardContent, CardHeader, CardTitle } from "../ui/card";
import { Code } from "lucide-react";
import { SERIES_TAIL, seriesColor } from "../shared/series";

const MAX_NAMED = 6;

const HIDDEN_LANGUAGE_USAGE_FORMATS = new Set(["json", "yaml", "toml"]);

/** Largest language first in the series colours; the folded rest in grey. */
function langFill(rank: number): string {
  return rank < MAX_NAMED ? seriesColor(rank) : SERIES_TAIL;
}

function shouldShowLanguageUsage(name: string): boolean {
  return !HIDDEN_LANGUAGE_USAGE_FORMATS.has(name.trim().toLowerCase());
}

interface LanguageDonutProps {
  /** Map of language → file count */
  distribution: Record<string, number>;
  /** Optional "View in Graph →" target, rendered in the card header. */
  viewAllHref?: string;
}

export function LanguageDonut({ distribution, viewAllHref }: LanguageDonutProps) {
  const entries = Object.entries(distribution)
    .filter(([name]) => shouldShowLanguageUsage(name))
    .map(([name, value]) => ({ name, value }))
    .sort((a, b) => b.value - a.value);

  const total = entries.reduce((s, e) => s + e.value, 0);
  if (total === 0) return null;

  const shown = entries.slice(0, MAX_NAMED);
  const otherValue = entries.slice(MAX_NAMED).reduce((s, e) => s + e.value, 0);
  if (otherValue > 0) shown.push({ name: "other", value: otherValue });

  return (
    <Card className="h-full">
      <CardHeader className="pb-2">
        <CardTitle className="text-sm flex items-center justify-between">
          <span className="flex items-center gap-2">
            <Code className="h-4 w-4 text-[var(--color-text-secondary)]" />
            Languages
          </span>
          {viewAllHref && (
            <a
              href={viewAllHref}
              className="text-[10px] text-[var(--color-accent-primary)] hover:underline font-normal"
            >
              View in Graph →
            </a>
          )}
        </CardTitle>
      </CardHeader>
      <CardContent className="pt-0">
        <div className="flex items-center gap-4">
          <div className="w-[120px] h-[120px] shrink-0">
            <ResponsiveContainer width="100%" height="100%">
              <PieChart>
                <Pie
                  data={shown}
                  cx="50%"
                  cy="50%"
                  innerRadius={32}
                  outerRadius={55}
                  paddingAngle={2}
                  dataKey="value"
                  stroke="none"
                >
                  {shown.map((entry, i) => (
                    <Cell key={entry.name} fill={langFill(i)} />
                  ))}
                </Pie>
                <Tooltip
                  contentStyle={{
                    background: "var(--color-bg-overlay)",
                    border: "1px solid var(--color-border-default)",
                    borderRadius: 8,
                    fontSize: 11,
                    color: "var(--color-text-primary)",
                  }}
                  formatter={(value) => {
                    const n = typeof value === "number" ? value : 0;
                    return [`${n} files (${Math.round((n / total) * 100)}%)`, ""];
                  }}
                />
              </PieChart>
            </ResponsiveContainer>
          </div>
          <div className="space-y-1.5 min-w-0 flex-1">
            {shown.map((entry, i) => (
              <div key={entry.name} className="flex items-center justify-between text-xs">
                <div className="flex items-center gap-1.5 min-w-0">
                  <span
                    className="h-2 w-2 rounded-full shrink-0"
                    style={{ backgroundColor: langFill(i) }}
                  />
                  <span className="text-[var(--color-text-secondary)] capitalize truncate">
                    {entry.name}
                  </span>
                </div>
                <span className="text-[var(--color-text-tertiary)] tabular-nums ml-2 shrink-0">
                  {Math.round((entry.value / total) * 100)}%
                </span>
              </div>
            ))}
          </div>
        </div>
      </CardContent>
    </Card>
  );
}