All files / src/components/collection CollectionView.tsx

95.83% Statements 23/24
97.56% Branches 40/41
100% Functions 9/9
100% Lines 21/21

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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235            37x 298x                                           37x     222x                                                       37x             84x                                       37x               360x                                                                           37x                                                                                                           478x                     478x 339x 273x       298x     175x               139x 133x   133x   222x     104x     478x                
import { ViewMode } from "@/components/buttons/ViewToggle/ViewToggle";
import type { LucideIcon } from "lucide-react";
import React from "react";
 
// ─── Skeletons ────────────────────────────────────────────────────────────────
 
export const SkeletonCard: React.FC = () => (
  <div className="rounded-xl border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 p-5 space-y-3 animate-pulse">
    <div className="flex items-start justify-between">
      <div className="flex items-center gap-3">
        <div className="size-9 rounded-lg bg-gray-200 dark:bg-gray-700" />
        <div className="space-y-1.5">
          <div className="h-4 w-32 rounded bg-gray-200 dark:bg-gray-700" />
          <div className="h-3 w-20 rounded bg-gray-200 dark:bg-gray-700" />
        </div>
      </div>
      <div className="h-5 w-16 rounded-full bg-gray-200 dark:bg-gray-700" />
    </div>
    <div className="space-y-1.5">
      <div className="h-3 w-full rounded bg-gray-200 dark:bg-gray-700" />
      <div className="h-3 w-3/4 rounded bg-gray-200 dark:bg-gray-700" />
    </div>
    <div className="flex gap-1.5">
      <div className="h-5 w-16 rounded-full bg-gray-200 dark:bg-gray-700" />
      <div className="h-5 w-20 rounded-full bg-gray-200 dark:bg-gray-700" />
    </div>
  </div>
);
 
export const SkeletonRow: React.FC<{ standalone?: boolean }> = ({
  standalone,
}) => (
  <div
    className={`flex items-center gap-3 px-4 py-3 animate-pulse ${
      standalone
        ? "rounded-xl border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800"
        : "border-b border-gray-100 dark:border-gray-700 last:border-0"
    }`}
  >
    <div className="size-8 rounded-lg bg-gray-200 dark:bg-gray-700 shrink-0" />
    <div className="flex-1 space-y-1.5 min-w-0">
      <div className="h-3.5 w-40 rounded bg-gray-200 dark:bg-gray-700" />
      <div className="h-3 w-56 rounded bg-gray-200 dark:bg-gray-700" />
    </div>
    <div className="h-5 w-16 rounded-full bg-gray-200 dark:bg-gray-700 shrink-0" />
  </div>
);
 
// ─── Empty state ──────────────────────────────────────────────────────────────
 
export interface EmptyStateProps {
  Icon: LucideIcon;
  title: string;
  hint?: React.ReactNode;
  /** Optional call-to-action, e.g. a `CreateButton`. */
  action?: React.ReactNode;
  /** Wraps the empty state in a dashed placeholder border. */
  dashed?: boolean;
}
 
export const EmptyState: React.FC<EmptyStateProps> = ({
  Icon,
  title,
  hint,
  action,
  dashed,
}) => (
  <div
    className={`flex flex-col items-center justify-center py-16 text-center px-6 ${
      dashed
        ? "border border-dashed border-gray-200 dark:border-gray-700 rounded-xl"
        : ""
    }`}
  >
    <Icon className="size-8 text-gray-200 dark:text-gray-600 mb-3" />
    <p className="text-sm font-medium text-gray-500 dark:text-gray-400">
      {title}
    </p>
    {hint && (
      <p className="text-xs text-gray-400 dark:text-gray-500 mt-1">{hint}</p>
    )}
    {action && <div className="mt-4">{action}</div>}
  </div>
);
 
// ─── Section heading ──────────────────────────────────────────────────────────
 
export const SectionHeading: React.FC<{
  label: string;
  /** A quiet line after the label, hidden on a phone. */
  hint?: React.ReactNode;
  count?: number;
  /** Right-aligned action, e.g. a `CreateButton`. */
  action?: React.ReactNode;
}> = ({ label, hint, count, action }) => (
  <div className="flex items-center gap-3 mb-4">
    <h3 className="text-sm font-semibold text-gray-700 dark:text-gray-300">
      {label}
    </h3>
    {count !== undefined && (
      <span className="text-xs text-gray-400 dark:text-gray-500 tabular-nums">
        {count}
      </span>
    )}
    {hint && (
      <span className="hidden sm:inline text-xs text-gray-500 dark:text-gray-400 truncate min-w-0">
        {hint}
      </span>
    )}
    <div className="flex-1 h-px bg-gray-100 dark:bg-gray-700" />
    {action}
  </div>
);
 
// ─── Grid density ─────────────────────────────────────────────────────────────
 
/** How much room a card gets in grid view. */
export type GridVariant = "default" | "wide" | "full";
 
/**
 * Three columns is the default because most resources are a name, a line of
 * description and a tag or two. A page's *primary* resource is not — an agent
 * card carries its skills, tools and knowledge counts, a repository card its
 * grouped sources — and at a third of the width those cards read as the same
 * object as the supporting resources beside them. "wide" gives them half the
 * row, so the thing the page is about looks like it. "full" is one card
 * per row, for a section holding a single featured item — a built-in agent
 * above the user's own — that should read as an invitation, not a peer.
 *
 * Whole class strings, never interpolated: Tailwind only ships the classes it
 * can see in the source, so a built `grid-cols-${n}` is purged and the grid
 * silently collapses to one column in production.
 */
const GRID_CLS: Record<GridVariant, string> = {
  default: "grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4",
  wide: "grid grid-cols-1 lg:grid-cols-2 gap-4",
  full: "grid grid-cols-1 gap-4",
};
 
// ─── Collection view ──────────────────────────────────────────────────────────
 
export interface CollectionViewProps<T> {
  items: T[];
  itemKey: (item: T) => string;
  /** Row renderer for list view; omit for grid-only collections. In the
   *  "panel" variant rows should carry their own `border-b … last:border-0`
   *  divider. */
  renderRow?: (item: T) => React.ReactNode;
  /** Card renderer for grid view; omit for list-only collections. */
  renderCard?: (item: T) => React.ReactNode;
  view?: ViewMode;
  loading?: boolean;
  skeletonCount?: number;
  /** "panel" renders rows inside one bordered container; "rows" renders
   *  self-contained rows with a gap (rows carry their own borders). */
  listVariant?: "panel" | "rows";
  /** "wide" halves the number of grid columns — for the primary resource of
   *  a page, whose cards carry more than a name and a tag. */
  gridVariant?: GridVariant;
  empty: EmptyStateProps;
  /** True while a search/filter is narrowing results — switches the empty
   *  state to the "no matches" copy. */
  filtered?: boolean;
  emptyFilteredTitle?: string;
  emptyFilteredHint?: string;
}
 
/**
 * The standard grid/list body of a collection subpage: handles the view
 * switch, loading skeletons, and (filtered) empty states so subpages only
 * provide their card/row renderers.
 */
export function CollectionView<T>({
  items,
  itemKey,
  renderRow,
  renderCard,
  view = "list",
  loading,
  skeletonCount = 6,
  listVariant = "panel",
  gridVariant = "default",
  empty,
  filtered,
  emptyFilteredTitle = "No results match your filters.",
  emptyFilteredHint = "Try adjusting your search or filters.",
}: CollectionViewProps<T>) {
  const emptyState = filtered ? (
    <EmptyState
      Icon={empty.Icon}
      title={emptyFilteredTitle}
      hint={emptyFilteredHint}
      dashed={empty.dashed}
    />
  ) : (
    <EmptyState {...empty} />
  );
 
  if (renderCard && (view === "grid" || !renderRow)) {
    if (!loading && items.length === 0) return emptyState;
    return (
      <div className={GRID_CLS[gridVariant]}>
        {loading
          ? Array.from({ length: skeletonCount }).map((_, i) => (
              <SkeletonCard key={i} />
            ))
          : items.map((item) => (
              <React.Fragment key={itemKey(item)}>
                {renderCard(item)}
              </React.Fragment>
            ))}
      </div>
    );
  }
 
  if (!loading && items.length === 0) return emptyState;
  Iif (!renderRow) return emptyState;
 
  const rows = loading
    ? Array.from({ length: skeletonCount }).map((_, i) => (
        <SkeletonRow key={i} standalone={listVariant === "rows"} />
      ))
    : items.map((item) => (
        <React.Fragment key={itemKey(item)}>{renderRow(item)}</React.Fragment>
      ));
 
  return listVariant === "rows" ? (
    <div className="flex flex-col gap-2">{rows}</div>
  ) : (
    <div className="rounded-xl border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 overflow-hidden">
      {rows}
    </div>
  );
}