Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

2.4 KB
Raw Blame
'use client';

import { useMemo } from 'react';
import { useTranslations } from 'next-intl';
import type { StateType } from '@/lib/api/endpoints/columns';
import type { ProjectDetail } from '@/lib/api/endpoints/projects';

// The initiative's linked issues counted per state group. Complements the header
// progress pill with the full distribution (backlog through canceled).

// Colors are fixed per group rather than taken from the columns, because a group
// spans several columns with their own colors. The label of a group is a message
// under `initiatives.stateGroups`.
const GROUPS: { type: StateType; color: string }[] = [
  { type: 'backlog', color: '#a1a1aa' },
  { type: 'unstarted', color: '#64748b' },
  { type: 'started', color: '#eab308' },
  { type: 'completed', color: '#22c55e' },
  { type: 'canceled', color: '#ef4444' },
];

export default function InitiativeStateBreakdown({
  project,
  initiativeId,
}: {
  project: ProjectDetail;
  initiativeId: number;
}) {
  const t = useTranslations('initiatives');
  const counts = useMemo(() => {
    const stateByColumn = new Map(project.columns.map((c) => [c.id, c.stateType]));
    const acc: Record<StateType, number> = {
      backlog: 0,
      unstarted: 0,
      started: 0,
      completed: 0,
      canceled: 0,
    };
    for (const issue of project.issues) {
      if (issue.initiative?.id !== initiativeId) continue;
      const type = stateByColumn.get(issue.columnId);
      if (type) acc[type] += 1;
    }
    return acc;
  }, [project.columns, project.issues, initiativeId]);

  const total = GROUPS.reduce((sum, g) => sum + counts[g.type], 0);

  return (
    <div>
      <h4 className="mb-2.5 text-xs font-medium tracking-wide text-muted-foreground uppercase">
        {t('progress')}
      </h4>
      {total === 0 ? (
        <p className="text-sm text-muted-foreground">{t('noIssuesLinked')}</p>
      ) : (
        <ul className="flex flex-col gap-1.5">
          {GROUPS.map((g) => (
            <li key={g.type} className="flex items-center gap-2 text-sm">
              <span
                className="inline-block size-2 rounded-full"
                style={{ backgroundColor: g.color }}
              />
              <span className="text-muted-foreground">{t(`stateGroups.${g.type}`)}</span>
              <span className="ml-auto text-foreground tabular-nums">{counts[g.type]}</span>
            </li>
          ))}
        </ul>
      )}
    </div>
  );
}