From 4ff864a5579fb5f330ffba291ba5cc63b034e177 Mon Sep 17 00:00:00 2001 From: Jose Selesan Date: Fri, 16 Oct 2026 11:08:42 -0300 Subject: [PATCH] feat: show bought items in a collapsible section for the current day only 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. --- .../src/components/items/bought-item-list.tsx | 36 ++++++++ apps/web/src/components/items/item-list.tsx | 76 +++++++--------- apps/web/src/components/items/item-row.tsx | 53 +++++++++++ apps/web/src/components/ui/collapsible.tsx | 19 ++++ apps/web/src/hooks/use-today.ts | 35 ++++++++ apps/web/src/lib/dates.test.ts | 28 ++++++ apps/web/src/lib/dates.ts | 14 +++ apps/web/src/lib/items.test.ts | 87 +++++++++++++++++++ apps/web/src/lib/items.ts | 40 +++++++++ 9 files changed, 344 insertions(+), 44 deletions(-) create mode 100644 apps/web/src/components/items/bought-item-list.tsx create mode 100644 apps/web/src/components/items/item-row.tsx create mode 100644 apps/web/src/components/ui/collapsible.tsx create mode 100644 apps/web/src/hooks/use-today.ts create mode 100644 apps/web/src/lib/dates.test.ts create mode 100644 apps/web/src/lib/dates.ts create mode 100644 apps/web/src/lib/items.test.ts create mode 100644 apps/web/src/lib/items.ts diff --git a/apps/web/src/components/items/bought-item-list.tsx b/apps/web/src/components/items/bought-item-list.tsx new file mode 100644 index 0000000..e675017 --- /dev/null +++ b/apps/web/src/components/items/bought-item-list.tsx @@ -0,0 +1,36 @@ +import type { ListItem } from '@superlist/shared' +import { ChevronDownIcon } from 'lucide-react' + +import { Collapsible, CollapsibleContent, CollapsibleTrigger } from '@/components/ui/collapsible' +import type { useUpdateItem } from '@/hooks/use-items' + +import { ItemRow } from './item-row' + +type BoughtItemListProps = { + items: ListItem[] + updateItem: ReturnType +} + +/** + * Collapsible "Comprados hoy" section below the pending items. Expanded by + * default so marking an item visibly moves it down; the user can fold it. + * Tapping an item here un-buys it and it returns to the pending list. + */ +export function BoughtItemList({ items, updateItem }: BoughtItemListProps) { + return ( + + + + Comprados hoy + ({items.length}) + + +
    + {items.map((item) => ( + + ))} +
+
+
+ ) +} diff --git a/apps/web/src/components/items/item-list.tsx b/apps/web/src/components/items/item-list.tsx index cf92e66..1075142 100644 --- a/apps/web/src/components/items/item-list.tsx +++ b/apps/web/src/components/items/item-list.tsx @@ -1,20 +1,30 @@ -import { CheckIcon, ShoppingBasketIcon } from 'lucide-react' +import { ShoppingBasketIcon } from 'lucide-react' import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert' import { Card, CardContent } from '@/components/ui/card' +import { Separator } from '@/components/ui/separator' import { Skeleton } from '@/components/ui/skeleton' import { useItems, useUpdateItem } from '@/hooks/use-items' +import { useToday } from '@/hooks/use-today' import { ApiError } from '@/lib/api-client' -import { cn } from '@/lib/utils' +import { splitItems } from '@/lib/items' + +import { BoughtItemList } from './bought-item-list' +import { ItemRow } from './item-row' type ItemListProps = { listId: string } -/** Items loaded from the API, pending ones first. Tapping an item marks it as bought. */ +/** + * Items loaded from the API, pending ones first. Tapping an item marks it as + * bought; bought items move to a collapsible "Comprados hoy" section below + * and are only shown on the day they were bought. + */ export function ItemList({ listId }: ItemListProps) { const { data: items, isPending, error } = useItems(listId) const updateItem = useUpdateItem(listId) + const today = useToday() if (error) { return ( @@ -39,7 +49,9 @@ export function ItemList({ listId }: ItemListProps) { ) } - if (items.length === 0) { + const { pending, boughtToday } = splitItems(items, today) + + if (pending.length === 0 && boughtToday.length === 0) { return ( @@ -58,45 +70,21 @@ export function ItemList({ listId }: ItemListProps) { } return ( -
    - {items.map((item) => ( -
  • - - - - - -
  • - ))} -
+
+ {pending.length > 0 && ( +
    + {pending.map((item) => ( + + ))} +
+ )} + + {boughtToday.length > 0 && ( + <> + {pending.length > 0 && } + + + )} +
) } diff --git a/apps/web/src/components/items/item-row.tsx b/apps/web/src/components/items/item-row.tsx new file mode 100644 index 0000000..4508e39 --- /dev/null +++ b/apps/web/src/components/items/item-row.tsx @@ -0,0 +1,53 @@ +import type { ListItem } from '@superlist/shared' +import { CheckIcon } from 'lucide-react' + +import { Card, CardContent } from '@/components/ui/card' +import type { useUpdateItem } from '@/hooks/use-items' +import { cn } from '@/lib/utils' + +type ItemRowProps = { + item: ListItem + updateItem: ReturnType +} + +/** A single item row: tapping it toggles its bought state. */ +export function ItemRow({ item, updateItem }: ItemRowProps) { + return ( +
  • + + + + + +
  • + ) +} diff --git a/apps/web/src/components/ui/collapsible.tsx b/apps/web/src/components/ui/collapsible.tsx new file mode 100644 index 0000000..fdbc70d --- /dev/null +++ b/apps/web/src/components/ui/collapsible.tsx @@ -0,0 +1,19 @@ +import { Collapsible as CollapsiblePrimitive } from 'radix-ui' + +function Collapsible({ ...props }: React.ComponentProps) { + return +} + +function CollapsibleTrigger({ + ...props +}: React.ComponentProps) { + return +} + +function CollapsibleContent({ + ...props +}: React.ComponentProps) { + return +} + +export { Collapsible, CollapsibleContent, CollapsibleTrigger } diff --git a/apps/web/src/hooks/use-today.ts b/apps/web/src/hooks/use-today.ts new file mode 100644 index 0000000..ad16f79 --- /dev/null +++ b/apps/web/src/hooks/use-today.ts @@ -0,0 +1,35 @@ +import { useEffect, useState } from 'react' + +/** Milliseconds until the next local midnight, plus a small buffer. */ +function msUntilNextLocalDay(now: Date): number { + const nextMidnight = new Date(now) + nextMidnight.setHours(24, 0, 0, 0) + + return nextMidnight.getTime() - now.getTime() + 5_000 +} + +/** + * The current date, refreshed when the local day changes (via a timer set to + * the next midnight) and when the tab regains focus. Components selling a + * "today only" view use it so bought items disappear once the day rolls over + * even if the page has been open all night. + */ +export function useToday(): Date { + const [today, setToday] = useState(() => new Date()) + + useEffect(() => { + const refresh = () => setToday(new Date()) + + const timer = setTimeout(refresh, msUntilNextLocalDay(new Date())) + window.addEventListener('focus', refresh) + document.addEventListener('visibilitychange', refresh) + + return () => { + clearTimeout(timer) + window.removeEventListener('focus', refresh) + document.removeEventListener('visibilitychange', refresh) + } + }, []) + + return today +} diff --git a/apps/web/src/lib/dates.test.ts b/apps/web/src/lib/dates.test.ts new file mode 100644 index 0000000..41f3b4e --- /dev/null +++ b/apps/web/src/lib/dates.test.ts @@ -0,0 +1,28 @@ +import { describe, expect, it } from 'vitest' + +import { isSameLocalDay } from './dates' + +describe('isSameLocalDay', () => { + it('matches two moments of the same local day', () => { + const morning = new Date(2026, 9, 15, 8, 30) + const night = new Date(2026, 9, 15, 23, 59) + + expect(isSameLocalDay(morning, night)).toBe(true) + }) + + it('does not match moments across local midnight', () => { + const justBefore = new Date(2026, 9, 15, 23, 59) + const justAfter = new Date(2026, 9, 16, 0, 1) + + expect(isSameLocalDay(justBefore, justAfter)).toBe(false) + }) + + it('compares in the local timezone, not in UTC', () => { + // 23:30 local on Oct 15th is already Oct 16th in UTC for UTC-negative + // timezones, so a naive UTC comparison would report different days. + const localNight = new Date(2026, 9, 15, 23, 30) + const nextLocalMidnight = new Date(2026, 9, 16, 0, 0) + + expect(isSameLocalDay(localNight, nextLocalMidnight)).toBe(false) + }) +}) diff --git a/apps/web/src/lib/dates.ts b/apps/web/src/lib/dates.ts new file mode 100644 index 0000000..9b87f81 --- /dev/null +++ b/apps/web/src/lib/dates.ts @@ -0,0 +1,14 @@ +/** + * Local-day helpers. A shopping day belongs to the user's timezone (the + * browser's), which the API cannot know, so "bought today" is decided here + * and never on the server. + */ + +/** True when both moments fall on the same calendar day in the local timezone. */ +export function isSameLocalDay(a: Date, b: Date): boolean { + return ( + a.getFullYear() === b.getFullYear() && + a.getMonth() === b.getMonth() && + a.getDate() === b.getDate() + ) +} diff --git a/apps/web/src/lib/items.test.ts b/apps/web/src/lib/items.test.ts new file mode 100644 index 0000000..3c56e01 --- /dev/null +++ b/apps/web/src/lib/items.test.ts @@ -0,0 +1,87 @@ +import type { ListItem } from '@superlist/shared' +import { describe, expect, it } from 'vitest' + +import { splitItems } from './items' + +function item(overrides: Partial): ListItem { + return { + id: 'item-1', + listId: 'list-1', + name: 'Leche', + completed: false, + boughtAt: null, + quantity: null, + notes: null, + order: 0, + createdAt: '2026-10-15T10:00:00.000Z', + updatedAt: '2026-10-15T10:00:00.000Z', + ...overrides, + } +} + +const NOW = new Date(2026, 9, 15, 18, 0) + +describe('splitItems', () => { + it('keeps pending items untouched, in API order', () => { + const first = item({ id: 'a', order: 1 }) + const second = item({ id: 'b', order: 2 }) + + const { pending, boughtToday } = splitItems([first, second], NOW) + + expect(pending).toEqual([first, second]) + expect(boughtToday).toEqual([]) + }) + + it('moves items bought today to boughtToday, most recent first', () => { + const boughtEarlier = item({ + id: 'a', + completed: true, + boughtAt: new Date(2026, 9, 15, 9, 0).toISOString(), + }) + const boughtJustNow = item({ + id: 'b', + completed: true, + boughtAt: new Date(2026, 9, 15, 17, 30).toISOString(), + }) + + const { pending, boughtToday } = splitItems([boughtEarlier, boughtJustNow], NOW) + + expect(pending).toEqual([]) + expect(boughtToday.map((i) => i.id)).toEqual(['b', 'a']) + }) + + it('hides items bought on a previous local day', () => { + const boughtYesterday = item({ + completed: true, + boughtAt: new Date(2026, 9, 14, 23, 30).toISOString(), + }) + + const { pending, boughtToday } = splitItems([boughtYesterday], NOW) + + expect(pending).toEqual([]) + expect(boughtToday).toEqual([]) + }) + + it('compares in the local timezone, not in UTC', () => { + // 23:30 local on Oct 15th already belongs to Oct 16th in UTC for + // UTC-negative timezones; the item must still count as bought today. + const boughtLateAtNight = item({ + completed: true, + boughtAt: new Date(2026, 9, 15, 23, 30).toISOString(), + }) + + const lateNightNow = new Date(2026, 9, 15, 23, 45) + const { boughtToday } = splitItems([boughtLateAtNight], lateNightNow) + + expect(boughtToday).toHaveLength(1) + }) + + it('hides completed items without boughtAt', () => { + const legacy = item({ completed: true, boughtAt: null }) + + const { pending, boughtToday } = splitItems([legacy], NOW) + + expect(pending).toEqual([]) + expect(boughtToday).toEqual([]) + }) +}) diff --git a/apps/web/src/lib/items.ts b/apps/web/src/lib/items.ts new file mode 100644 index 0000000..25829a6 --- /dev/null +++ b/apps/web/src/lib/items.ts @@ -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 } +}