feat: show bought items in a collapsible section for the current day only
Some checks failed
CI / Verify (push) Has been cancelled

Bought items now move down to a 'Comprados hoy' collapsible section
(expanded by default) below the pending ones, and are only visible on
the local day they were bought. Items bought on previous days stay
completed in the database but are hidden from the list.
This commit is contained in:
Jose Selesan
2026-10-16 11:08:42 -03:00
parent c4f6b8bc85
commit 4ff864a557
9 changed files with 344 additions and 44 deletions

40
apps/web/src/lib/items.ts Normal file
View File

@@ -0,0 +1,40 @@
import type { ListItem } from '@superlist/shared'
import { isSameLocalDay } from './dates'
export type SplitItems = {
/** Items still to buy, in the API's order (by `order`). */
pending: ListItem[]
/** Items bought during the current local day, most recent first. */
boughtToday: ListItem[]
}
/**
* Splits a list's items into the two groups the UI renders: pending items
* and items bought today. Items bought on a previous local day are dropped
* entirely — they stay `completed` in the database but are only shown on
* the day they were bought.
*/
export function splitItems(items: ListItem[], now: Date): SplitItems {
const pending: ListItem[] = []
const boughtToday: ListItem[] = []
for (const item of items) {
if (!item.completed) {
pending.push(item)
continue
}
// A completed item without `boughtAt` is legacy data with no known
// purchase day, so it is not considered bought today.
if (item.boughtAt && isSameLocalDay(new Date(item.boughtAt), now)) {
boughtToday.push(item)
}
}
boughtToday.sort(
(a, b) => new Date(b.boughtAt ?? 0).getTime() - new Date(a.boughtAt ?? 0).getTime(),
)
return { pending, boughtToday }
}