"use client"; import type { DragEndEvent, DragStartEvent } from "@dnd-kit/core"; import { closestCenter, DndContext, DragOverlay, KeyboardSensor, PointerSensor, useSensor, useSensors, } from "@dnd-kit/core"; import { arrayMove, SortableContext, sortableKeyboardCoordinates, useSortable, verticalListSortingStrategy, } from "@dnd-kit/sortable"; import { CSS } from "@dnd-kit/utilities"; import { Filter, GripVertical, Plus, SortAsc, Star, X } from "lucide-react"; import { useMemo, useState } from "react"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Checkbox } from "@/components/ui/checkbox"; import { DropdownMenu, DropdownMenuCheckboxItem, DropdownMenuContent, DropdownMenuLabel, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Input } from "@/components/ui/input"; import { Item, ItemContent, ItemGroup, ItemMedia, ItemTitle, } from "@/components/ui/item"; import { Separator } from "@/components/ui/separator"; import { cn } from "@/lib/utils"; type Priority = "high" | "medium" | "low"; type SortOption = "manual" | "title" | "priority"; interface Todo { id: string; title: string; completed: boolean; priority: Priority; starred: boolean; } interface Filters { starredOnly: boolean; priorities: Priority[]; } interface Todo6Props { heading?: string; description?: string; filterLabel?: string; filterByLabel?: string; starredOnlyLabel?: string; prioritySectionLabel?: string; clearFiltersLabel?: string; sortLabel?: string; sortByLabel?: string; manualOrderLabel?: string; titleSortLabel?: string; prioritySortLabel?: string; addTaskLabel?: string; taskPlaceholder?: string; cancelLabel?: string; submitLabel?: string; emptyFiltersMessage?: string; taskCountLabel?: (count: number) => string; priorityLabels?: Record; sortBadges?: Record; starredAriaLabel?: string; initialTodos?: Todo[]; className?: string; } const defaultPriorityLabels: Record = { high: "زیاد", medium: "متوسط", low: "کم", }; const defaultSortBadges: Record = { manual: "د", title: "ع", priority: "ا", }; const defaultTodos: Todo[] = [ { id: "1", title: "تکمیل مستندات فنی پروژه", completed: false, priority: "high", starred: true, }, { id: "2", title: "بررسی و تأیید طراحی رابط کاربری", completed: false, priority: "medium", starred: false, }, { id: "3", title: "آماده‌سازی گزارش هفتگی", completed: false, priority: "low", starred: false, }, { id: "4", title: "بازبینی کد منابع", completed: true, priority: "medium", starred: false, }, { id: "5", title: "هماهنگی با تیم طراحی", completed: false, priority: "high", starred: true, }, ]; interface SortableTodoItemProps { todo: Todo; onToggle: (id: string) => void; priorityLabels: Record; starredAriaLabel: string; } const SortableTodoItem = ({ todo, onToggle, priorityLabels, starredAriaLabel, }: SortableTodoItemProps) => { const { attributes, listeners, setNodeRef, transform, transition, isDragging, } = useSortable({ id: todo.id }); const style = { transform: CSS.Translate.toString(transform), transition, opacity: isDragging ? 0.5 : 1, }; return (
onToggle(todo.id)} >
onToggle(todo.id)} onClick={(e) => e.stopPropagation()} aria-label={ todo.completed ? `علامت‌گذاری «${todo.title}» به‌عنوان انجام‌نشده` : `علامت‌گذاری «${todo.title}» به‌عنوان انجام‌شده` } />
{todo.title} {todo.starred && ( )} {priorityLabels[todo.priority]}
); }; const Todo6 = ({ heading = "مدیریت وظایف با فیلتر و مرتب‌سازی", description = "وظیفه جدید بسازید، بر اساس اولویت و علاقه‌مندی فیلتر کنید و بر اساس عنوان یا اولویت مرتب کنید.", filterLabel = "فیلتر", filterByLabel = "فیلتر بر اساس", starredOnlyLabel = "فقط علاقه‌مندی‌ها", prioritySectionLabel = "اولویت", clearFiltersLabel = "پاک کردن فیلترها", sortLabel = "مرتب‌سازی", sortByLabel = "مرتب‌سازی بر اساس", manualOrderLabel = "ترتیب دستی", titleSortLabel = "عنوان (الف–ی)", prioritySortLabel = "اولویت", addTaskLabel = "افزودن وظیفه جدید", taskPlaceholder = "عنوان وظیفه", cancelLabel = "انصراف", submitLabel = "افزودن", emptyFiltersMessage = "هیچ وظیفه‌ای با فیلترهای شما مطابقت ندارد.", taskCountLabel = (count) => `${count} وظیفه`, priorityLabels = defaultPriorityLabels, sortBadges = defaultSortBadges, starredAriaLabel = "علاقه‌مندی", initialTodos = defaultTodos, className, }: Todo6Props) => { const [todos, setTodos] = useState(initialTodos); const [isFormExpanded, setIsFormExpanded] = useState(false); const [newTaskTitle, setNewTaskTitle] = useState(""); const [sortBy, setSortBy] = useState("manual"); const [filters, setFilters] = useState({ starredOnly: false, priorities: [], }); const [activeTodo, setActiveTodo] = useState(null); const sensors = useSensors( useSensor(PointerSensor), useSensor(KeyboardSensor, { coordinateGetter: sortableKeyboardCoordinates, }), ); const toggleTodo = (id: string) => { setTodos((prevTodos) => prevTodos.map((todo) => todo.id === id ? { ...todo, completed: !todo.completed } : todo, ), ); }; const handleAddTask = () => { if (newTaskTitle.trim()) { const newTodo: Todo = { id: Date.now().toString(), title: newTaskTitle.trim(), completed: false, priority: "medium", starred: false, }; setTodos((prevTodos) => [...prevTodos, newTodo]); setNewTaskTitle(""); setIsFormExpanded(false); } }; const togglePriorityFilter = (priority: Priority) => { setFilters((prev) => ({ ...prev, priorities: prev.priorities.includes(priority) ? prev.priorities.filter((p) => p !== priority) : [...prev.priorities, priority], })); }; const clearFilters = () => { setFilters({ starredOnly: false, priorities: [], }); }; const activeFilterCount = (filters.starredOnly ? 1 : 0) + filters.priorities.length; const filteredAndSortedTodos = useMemo(() => { let filtered = todos.filter((todo) => { if (filters.starredOnly && !todo.starred) return false; if ( filters.priorities.length > 0 && !filters.priorities.includes(todo.priority) ) return false; return true; }); if (sortBy === "title") { filtered = [...filtered].sort((a, b) => a.title.localeCompare(b.title, "fa"), ); } else if (sortBy === "priority") { const priorityOrder: Record = { high: 1, medium: 2, low: 3, }; filtered = [...filtered].sort( (a, b) => priorityOrder[a.priority] - priorityOrder[b.priority], ); } return filtered; }, [todos, filters, sortBy]); const handleDragStart = (event: DragStartEvent) => { const { active } = event; const todo = filteredAndSortedTodos.find((t) => t.id === active.id); setActiveTodo(todo || null); if (sortBy !== "manual") { setSortBy("manual"); } }; const handleDragEnd = (event: DragEndEvent) => { const { active, over } = event; if (over && active.id !== over.id) { const oldIndex = filteredAndSortedTodos.findIndex( (item) => item.id === active.id, ); const newIndex = filteredAndSortedTodos.findIndex( (item) => item.id === over.id, ); if (oldIndex !== -1 && newIndex !== -1) { const reorderedFiltered = arrayMove( filteredAndSortedTodos, oldIndex, newIndex, ); const reorderedIds = reorderedFiltered.map((t) => t.id); setTodos((items) => { const sortedItems = [...items].sort((a, b) => { const aIndex = reorderedIds.indexOf(a.id); const bIndex = reorderedIds.indexOf(b.id); if (aIndex === -1) return 1; if (bIndex === -1) return -1; return aIndex - bIndex; }); return sortedItems; }); } } setActiveTodo(null); }; return (

{heading}

{description}

{filterByLabel} setFilters((prev) => ({ ...prev, starredOnly: checked, })) } > {starredOnlyLabel} {prioritySectionLabel} togglePriorityFilter("high")} > {priorityLabels.high} togglePriorityFilter("medium")} > {priorityLabels.medium} togglePriorityFilter("low")} > {priorityLabels.low} {activeFilterCount > 0 && ( <> )} {sortByLabel} setSortBy(value as SortOption)} > {manualOrderLabel} {titleSortLabel} {prioritySortLabel}
{taskCountLabel(filteredAndSortedTodos.length)}
{!isFormExpanded ? ( ) : (
setNewTaskTitle(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter" && newTaskTitle.trim()) { handleAddTask(); } if (e.key === "Escape") { setNewTaskTitle(""); setIsFormExpanded(false); } }} className="text-base font-medium" aria-label={taskPlaceholder} />
)} t.id)} strategy={verticalListSortingStrategy} > {filteredAndSortedTodos.length === 0 ? (
{emptyFiltersMessage}
) : ( filteredAndSortedTodos.map((todo) => ( )) )}
{activeTodo ? (
{activeTodo.title} {activeTodo.starred && ( )} {priorityLabels[activeTodo.priority]}
) : null}
); }; export { Todo6 };