kaneo (minimalist kanban) fork to experiment adding a tangled integration
github.com/usekaneo/kaneo
1import type Task from "@/types/task";
2
3export type SortField =
4 | "position"
5 | "createdAt"
6 | "priority"
7 | "dueDate"
8 | "title"
9 | "number";
10
11export type SortDirection = "asc" | "desc";
12
13export type SortConfig = {
14 field: SortField;
15 direction: SortDirection;
16};
17
18const priorityOrder: Record<string, number> = {
19 urgent: 4,
20 high: 3,
21 medium: 2,
22 low: 1,
23};
24
25function getPriorityValue(priority: string | null): number {
26 if (!priority) return 0;
27 return priorityOrder[priority] ?? 0;
28}
29
30export function sortTasks(tasks: Task[], config: SortConfig): Task[] {
31 if (config.field === "position") {
32 return tasks;
33 }
34
35 const sorted = [...tasks].sort((a, b) => {
36 let comparison = 0;
37
38 switch (config.field) {
39 case "priority": {
40 comparison =
41 getPriorityValue(a.priority) - getPriorityValue(b.priority);
42 break;
43 }
44 case "dueDate": {
45 const aDate = a.dueDate ? new Date(a.dueDate).getTime() : 0;
46 const bDate = b.dueDate ? new Date(b.dueDate).getTime() : 0;
47 if (!a.dueDate && !b.dueDate) comparison = 0;
48 else if (!a.dueDate) comparison = 1;
49 else if (!b.dueDate) comparison = -1;
50 else comparison = aDate - bDate;
51 break;
52 }
53 case "createdAt": {
54 comparison =
55 new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime();
56 break;
57 }
58 case "title": {
59 comparison = a.title.localeCompare(b.title);
60 break;
61 }
62 case "number": {
63 comparison = (a.number ?? 0) - (b.number ?? 0);
64 break;
65 }
66 }
67
68 return config.direction === "asc" ? comparison : -comparison;
69 });
70
71 return sorted;
72}