"use client"; import { GripVertical, PencilLine, PlusIcon } from "lucide-react"; import Image from "next/image"; import Link from "next/link"; import type { ComponentProps } from "react"; import { useState, useTransition } from "react"; import { toast } from "sonner"; import { SimpleTooltip } from "@/components/tooltip"; import { Button } from "@/components/ui/button"; import type { tierlistBadges } from "@/lib/db/schema"; import { cn } from "@/lib/utils"; import { reorderFlatItems } from "../components/order"; import { SortableGrid, SortableGridItem } from "../components/sortable-grid"; import { reorderBadges } from "./actions"; export function BadgeSection({ typeId, typeName, badges: initialBadges, }: { typeId: string; typeName: string; badges: (typeof tierlistBadges.$inferSelect)[]; }) { const [badges, setBadges] = useState(initialBadges); const [pending, startTransition] = useTransition(); const badgeSize = 48; function commitReorder(activeId: string, overId: string) { const next = reorderFlatItems(badges, activeId, overId); if (next === badges) return; const previous = badges; setBadges(next); startTransition(() => { reorderBadges({ typeId, activeId, ids: next.map((badge) => badge.id), }).catch((error) => { console.error(error); setBadges(previous); toast.error( error instanceof Error ? error.message : "Failed to reorder badges.", ); }); }); } return (
Badges (blue = global, green = {typeName} local)
badge.id)} onReorder={commitReorder} disabled={pending} overlay={(id) => { const badge = badges.find((entry) => entry.id === id); return badge ? ( ) : null; }} >
{badges.map((badge) => ( {({ attributes, listeners, setNodeRef, style, isDragging }) => (
)}
))}
); } function BadgeTile({ badge, typeId, badgeSize, dragHandle, overlay = false, }: { badge: typeof tierlistBadges.$inferSelect; typeId: string; badgeSize: number; dragHandle?: ComponentProps<"button">; overlay?: boolean; }) { const borderClass = badge.type === null ? "border-blue-500" : "border-emerald-400"; return (
{!overlay && (
)} {badge.image ? ( {badge.name} ) : (
{badge.name}
)}
); }