mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 18:04:18 +00:00
feat(todo): sort the task list by due date (#2205)
The list held its manual order, so an item due tomorrow sat behind one created earlier but due later. The sort-by section gains a due-date toggle next to the priority one, on desktop and in the phone tab: nearest deadline first, undated tasks after all dated ones, ties keep the manual order. One sort at a time, and manual drag-reorder pauses while either is active, exactly like the priority sort.
This commit is contained in:
@@ -36,7 +36,7 @@ export default function MTodoListTab({ planner }: { planner: TripPlanner }) {
|
||||
const tripMembers = planner.tripMembers
|
||||
|
||||
const [active, setActive] = useState<ActiveFilter>({ kind: 'smart', id: 'all' })
|
||||
const [sortByPriority, setSortByPriority] = useState(false)
|
||||
const [sortBy, setSortBy] = useState<'priority' | 'due' | null>(null)
|
||||
const [editingItemId, setEditingItemId] = useState<number | null>(null)
|
||||
const [creatingTask, setCreatingTask] = useState(false)
|
||||
|
||||
@@ -50,10 +50,10 @@ export default function MTodoListTab({ planner }: { planner: TripPlanner }) {
|
||||
active.kind === 'category'
|
||||
? filterTodoItemsByCategory(items, active.name)
|
||||
: filterTodoItems(items, active.id, currentUserId, today),
|
||||
sortByPriority,
|
||||
sortBy,
|
||||
today,
|
||||
),
|
||||
[items, active, currentUserId, today, sortByPriority],
|
||||
[items, active, currentUserId, today, sortBy],
|
||||
)
|
||||
|
||||
const pct = items.length > 0 ? Math.round((counts.done / items.length) * 100) : 0
|
||||
@@ -113,13 +113,22 @@ export default function MTodoListTab({ planner }: { planner: TripPlanner }) {
|
||||
))}
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setSortByPriority(v => !v)}
|
||||
aria-pressed={sortByPriority}
|
||||
className={filterPill(sortByPriority)}
|
||||
onClick={() => setSortBy(v => v === 'priority' ? null : 'priority')}
|
||||
aria-pressed={sortBy === 'priority'}
|
||||
className={filterPill(sortBy === 'priority')}
|
||||
>
|
||||
<Flag size={11} strokeWidth={2.2} />
|
||||
{t('todo.priority')}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setSortBy(v => v === 'due' ? null : 'due')}
|
||||
aria-pressed={sortBy === 'due'}
|
||||
className={filterPill(sortBy === 'due')}
|
||||
>
|
||||
<Calendar size={11} strokeWidth={2.2} />
|
||||
{t('todo.detail.dueDate')}
|
||||
</button>
|
||||
{categories.length > 0 && <span className="h-4 w-px flex-none bg-[color:var(--m-rowbr)]" />}
|
||||
{categories.map(cat => (
|
||||
<button
|
||||
|
||||
@@ -138,16 +138,22 @@ export function filterTodoItemsByCategory(items: TodoItem[], category: string):
|
||||
|
||||
/**
|
||||
* Row order (spec 03 §4.7): done sinks to the end, open-overdue floats to the
|
||||
* top, and — only while the priority toggle is on — ties break by ascending
|
||||
* priority (0/undefined sorts last within its bucket).
|
||||
* top. Ties break by the active sort toggle: ascending priority (0/undefined
|
||||
* last within its bucket), or nearest due date first with undated tasks after
|
||||
* all dated ones (#2205). With no toggle the manual order stands.
|
||||
*/
|
||||
export function sortTodoRows(items: TodoItem[], sortByPriority: boolean, today: string): TodoItem[] {
|
||||
export function sortTodoRows(items: TodoItem[], sortBy: 'priority' | 'due' | null, today: string): TodoItem[] {
|
||||
const rank = (i: TodoItem) => (i.checked ? 2 : isTodoOverdue(i, today) ? 0 : 1)
|
||||
return [...items].sort((a, b) => {
|
||||
const byRank = rank(a) - rank(b)
|
||||
if (byRank !== 0) return byRank
|
||||
if (!sortByPriority) return 0
|
||||
return (a.priority || 99) - (b.priority || 99)
|
||||
if (sortBy === 'priority') return (a.priority || 99) - (b.priority || 99)
|
||||
if (sortBy === 'due') {
|
||||
if (!a.due_date) return b.due_date ? 1 : 0
|
||||
if (!b.due_date) return -1
|
||||
return a.due_date < b.due_date ? -1 : a.due_date > b.due_date ? 1 : 0
|
||||
}
|
||||
return 0
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user