import { useCallback, useEffect, useMemo, useState } from "react"; import { Paperclip, Plus } from "lucide-react"; import { DndContext, closestCenter, PointerSensor, useSensor, useSensors, type DragEndEvent, } from "@dnd-kit/core"; import { SortableContext, useSortable, verticalListSortingStrategy, arrayMove, } from "@dnd-kit/sortable"; import { CSS } from "@dnd-kit/utilities"; import { useCompany } from "../context/CompanyContext"; import { useDialog } from "../context/DialogContext"; import { cn } from "../lib/utils"; import { Tooltip, TooltipContent, TooltipTrigger, } from "@/components/ui/tooltip"; import type { Company } from "@paperclip/shared"; const COMPANY_COLORS = [ "#6366f1", // indigo "#8b5cf6", // violet "#ec4899", // pink "#f43f5e", // rose "#f97316", // orange "#eab308", // yellow "#22c55e", // green "#14b8a6", // teal "#06b6d4", // cyan "#3b82f6", // blue ]; const ORDER_STORAGE_KEY = "paperclip.companyOrder"; function companyColor(name: string): string { let hash = 0; for (let i = 0; i < name.length; i++) { hash = name.charCodeAt(i) + ((hash << 5) - hash); } return COMPANY_COLORS[Math.abs(hash) % COMPANY_COLORS.length]!; } function getStoredOrder(): string[] { try { const raw = localStorage.getItem(ORDER_STORAGE_KEY); if (raw) return JSON.parse(raw); } catch { /* ignore */ } return []; } function saveOrder(ids: string[]) { localStorage.setItem(ORDER_STORAGE_KEY, JSON.stringify(ids)); } /** Sort companies by stored order, appending any new ones at the end. */ function sortByStoredOrder(companies: Company[]): Company[] { const order = getStoredOrder(); if (order.length === 0) return companies; const byId = new Map(companies.map((c) => [c.id, c])); const sorted: Company[] = []; for (const id of order) { const c = byId.get(id); if (c) { sorted.push(c); byId.delete(id); } } // Append any companies not in stored order for (const c of byId.values()) { sorted.push(c); } return sorted; } function SortableCompanyItem({ company, isSelected, onSelect, }: { company: Company; isSelected: boolean; onSelect: () => void; }) { const { attributes, listeners, setNodeRef, transform, transition, isDragging, } = useSortable({ id: company.id }); const style = { transform: CSS.Transform.toString(transform), transition, zIndex: isDragging ? 10 : undefined, opacity: isDragging ? 0.8 : 1, }; const color = companyColor(company.name); const initial = company.name.charAt(0).toUpperCase(); return (
{company.name}
Add company