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.

5.4 KB
Raw Blame
import { useState } from 'react';
import {
  DndContext,
  DragOverlay,
  closestCenter,
  type DragEndEvent,
  type DragStartEvent,
} from '@dnd-kit/core';
import { arrayMove, SortableContext, horizontalListSortingStrategy } from '@dnd-kit/sortable';
import { LayoutDashboard, Plus } from 'lucide-react';
import { useTranslations } from 'next-intl';
import type { Dashboard } from '@/lib/api/endpoints/dashboards';
import { useDndSensors, useDroppedOrder } from '@/lib/dnd';
import { usePermissions } from '@/hooks/usePermissions';
import { cn } from '@/lib/utils';
import DashboardTab from './DashboardTab';
import DashboardNameDialog from './DashboardNameDialog';

// The row of dashboard tabs. Each named dashboard is a sortable tab; the active
// one exposes Rename/Delete. A "New dashboard" button and a name dialog handle
// create/rename. The built-in Overview remains available beside saved dashboards.
export default function DashboardTabs({
  dashboards,
  activeDashboardId,
  isVirtual,
  actions,
  onSelect,
  onNewDashboard,
  onRename,
  onDelete,
  onReorder,
}: {
  dashboards: Dashboard[];
  activeDashboardId: number | null;
  isVirtual: boolean;
  actions?: React.ReactNode;
  onSelect: (id: number | null) => void;
  onNewDashboard: (name: string) => void;
  onRename: (d: Dashboard, name: string) => void;
  onDelete: (d: Dashboard) => void;
  onReorder: (ids: number[]) => void;
}) {
  const t = useTranslations('dashboards');
  const { can } = usePermissions();
  const canCreate = can('dashboards', 'create');
  const canEdit = can('dashboards', 'edit');
  const canDelete = can('dashboards', 'delete');
  const sensors = useDndSensors();
  const [activeId, setActiveId] = useState<number | null>(null);
  const dragged = activeId != null ? dashboards.find((d) => d.id === activeId) : null;
  const { items: shown, drop } = useDroppedOrder(dashboards);
  // Name dialog state: 'new' to create, a dashboard to rename, or null (closed).
  const [dialog, setDialog] = useState<'new' | Dashboard | null>(null);
  const renaming = dialog && dialog !== 'new' ? dialog : null;

  function handleDragEnd(e: DragEndEvent) {
    setActiveId(null);
    const { active, over } = e;
    if (!over || active.id === over.id) return;
    const ids = dashboards.map((d) => d.id);
    const from = ids.indexOf(Number(active.id));
    const to = ids.indexOf(Number(over.id));
    if (from === -1 || to === -1) return;
    const next = arrayMove(ids, from, to);
    drop(next);
    onReorder(next);
  }

  return (
    <div className="flex items-center gap-1 border-b px-2 py-1.5 sm:px-3">
      <div className="flex min-w-0 flex-1 items-center gap-1 overflow-x-auto">
        <button
          type="button"
          aria-current={isVirtual ? 'page' : undefined}
          onClick={() => onSelect(null)}
          className={cn(
            'flex shrink-0 items-center gap-1.5 rounded-md px-2 py-1 text-sm transition-colors',
            isVirtual
              ? 'bg-secondary font-medium text-foreground'
              : 'text-muted-foreground hover:bg-accent hover:text-foreground',
          )}
        >
          <LayoutDashboard className="size-3.5" />
          {t('defaultName')}
        </button>

        {dashboards.length > 0 && (
          <DndContext
            sensors={sensors}
            collisionDetection={closestCenter}
            onDragStart={(e: DragStartEvent) => setActiveId(Number(e.active.id))}
            onDragCancel={() => setActiveId(null)}
            onDragEnd={handleDragEnd}
          >
            <SortableContext
              items={shown.map((d) => d.id)}
              strategy={horizontalListSortingStrategy}
            >
              {shown.map((d) => (
                <DashboardTab
                  key={d.id}
                  dashboard={d}
                  active={!isVirtual && activeDashboardId === d.id}
                  canEdit={canEdit}
                  canDelete={canDelete}
                  onSelect={() => onSelect(d.id)}
                  onRename={() => setDialog(d)}
                  onDelete={() => onDelete(d)}
                />
              ))}
            </SortableContext>
            <DragOverlay>
              {dragged ? (
                <span className="flex items-center gap-1.5 rounded-md bg-secondary px-2 py-1 text-sm font-medium text-foreground shadow-md">
                  <LayoutDashboard className="size-3.5" />
                  {dragged.name}
                </span>
              ) : null}
            </DragOverlay>
          </DndContext>
        )}

        {canCreate && (
          <button
            type="button"
            onClick={() => setDialog('new')}
            className="flex shrink-0 items-center gap-1 rounded-md px-2 py-1 text-sm text-muted-foreground hover:bg-accent hover:text-foreground"
          >
            <Plus className="size-3.5" />
            {t('newDashboard')}
          </button>
        )}
      </div>

      {actions && <div className="flex shrink-0 items-center gap-2 pl-2">{actions}</div>}

      <DashboardNameDialog
        key={renaming?.id ?? (dialog === 'new' ? 'new' : 'closed')}
        open={dialog != null}
        title={renaming ? t('renameDashboard') : t('newDashboard')}
        initial={renaming?.name ?? ''}
        onClose={() => setDialog(null)}
        onSubmit={(name) => {
          if (renaming) onRename(renaming, name);
          else if (dialog === 'new') onNewDashboard(name);
          setDialog(null);
        }}
      />
    </div>
  );
}