feat: show bought items in a collapsible section for the current day only
Some checks failed
CI / Verify (push) Has been cancelled
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:
36
apps/web/src/components/items/bought-item-list.tsx
Normal file
36
apps/web/src/components/items/bought-item-list.tsx
Normal file
@@ -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<typeof useUpdateItem>
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 (
|
||||||
|
<Collapsible defaultOpen>
|
||||||
|
<CollapsibleTrigger className="group flex w-full items-center gap-1.5 py-1 text-left">
|
||||||
|
<ChevronDownIcon className="text-muted-foreground size-4 transition-transform group-data-[state=closed]:-rotate-90" />
|
||||||
|
<span className="text-muted-foreground text-sm font-medium">Comprados hoy</span>
|
||||||
|
<span className="text-muted-foreground text-sm">({items.length})</span>
|
||||||
|
</CollapsibleTrigger>
|
||||||
|
<CollapsibleContent>
|
||||||
|
<ul className="flex flex-col gap-2 pt-2">
|
||||||
|
{items.map((item) => (
|
||||||
|
<ItemRow key={item.id} item={item} updateItem={updateItem} />
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
</CollapsibleContent>
|
||||||
|
</Collapsible>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,20 +1,30 @@
|
|||||||
import { CheckIcon, ShoppingBasketIcon } from 'lucide-react'
|
import { ShoppingBasketIcon } from 'lucide-react'
|
||||||
|
|
||||||
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'
|
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'
|
||||||
import { Card, CardContent } from '@/components/ui/card'
|
import { Card, CardContent } from '@/components/ui/card'
|
||||||
|
import { Separator } from '@/components/ui/separator'
|
||||||
import { Skeleton } from '@/components/ui/skeleton'
|
import { Skeleton } from '@/components/ui/skeleton'
|
||||||
import { useItems, useUpdateItem } from '@/hooks/use-items'
|
import { useItems, useUpdateItem } from '@/hooks/use-items'
|
||||||
|
import { useToday } from '@/hooks/use-today'
|
||||||
import { ApiError } from '@/lib/api-client'
|
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 = {
|
type ItemListProps = {
|
||||||
listId: string
|
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) {
|
export function ItemList({ listId }: ItemListProps) {
|
||||||
const { data: items, isPending, error } = useItems(listId)
|
const { data: items, isPending, error } = useItems(listId)
|
||||||
const updateItem = useUpdateItem(listId)
|
const updateItem = useUpdateItem(listId)
|
||||||
|
const today = useToday()
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
return (
|
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 (
|
return (
|
||||||
<Card>
|
<Card>
|
||||||
<CardContent className="flex flex-col items-center gap-3 py-10 text-center">
|
<CardContent className="flex flex-col items-center gap-3 py-10 text-center">
|
||||||
@@ -58,45 +70,21 @@ export function ItemList({ listId }: ItemListProps) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ul className="flex flex-col gap-2">
|
<div className="flex flex-col gap-2">
|
||||||
{items.map((item) => (
|
{pending.length > 0 && (
|
||||||
<li key={item.id}>
|
<ul className="flex flex-col gap-2">
|
||||||
<Card>
|
{pending.map((item) => (
|
||||||
<CardContent className="p-0">
|
<ItemRow key={item.id} item={item} updateItem={updateItem} />
|
||||||
<button
|
))}
|
||||||
type="button"
|
</ul>
|
||||||
aria-pressed={item.completed}
|
)}
|
||||||
disabled={updateItem.isPending}
|
|
||||||
onClick={() => updateItem.mutate({ itemId: item.id, completed: !item.completed })}
|
{boughtToday.length > 0 && (
|
||||||
className="flex w-full cursor-pointer items-center gap-3 p-3 text-left disabled:cursor-default disabled:opacity-60"
|
<>
|
||||||
>
|
{pending.length > 0 && <Separator className="mt-2" />}
|
||||||
<span
|
<BoughtItemList items={boughtToday} updateItem={updateItem} />
|
||||||
className={cn(
|
</>
|
||||||
'flex size-5 shrink-0 items-center justify-center rounded-full border',
|
)}
|
||||||
item.completed
|
</div>
|
||||||
? 'bg-primary text-primary-foreground border-primary'
|
|
||||||
: 'border-muted-foreground/40 text-transparent',
|
|
||||||
)}
|
|
||||||
aria-hidden
|
|
||||||
>
|
|
||||||
<CheckIcon className="size-3" />
|
|
||||||
</span>
|
|
||||||
<span
|
|
||||||
className={cn(
|
|
||||||
'min-w-0 flex-1 truncate',
|
|
||||||
item.completed && 'text-muted-foreground line-through',
|
|
||||||
)}
|
|
||||||
>
|
|
||||||
{item.name}
|
|
||||||
</span>
|
|
||||||
{item.quantity && (
|
|
||||||
<span className="text-muted-foreground shrink-0 text-sm">{item.quantity}</span>
|
|
||||||
)}
|
|
||||||
</button>
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
</li>
|
|
||||||
))}
|
|
||||||
</ul>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
53
apps/web/src/components/items/item-row.tsx
Normal file
53
apps/web/src/components/items/item-row.tsx
Normal file
@@ -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<typeof useUpdateItem>
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A single item row: tapping it toggles its bought state. */
|
||||||
|
export function ItemRow({ item, updateItem }: ItemRowProps) {
|
||||||
|
return (
|
||||||
|
<li>
|
||||||
|
<Card>
|
||||||
|
<CardContent className="p-0">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
aria-pressed={item.completed}
|
||||||
|
disabled={updateItem.isPending}
|
||||||
|
onClick={() => updateItem.mutate({ itemId: item.id, completed: !item.completed })}
|
||||||
|
className="flex w-full cursor-pointer items-center gap-3 p-3 text-left disabled:cursor-default disabled:opacity-60"
|
||||||
|
>
|
||||||
|
<span
|
||||||
|
className={cn(
|
||||||
|
'flex size-5 shrink-0 items-center justify-center rounded-full border',
|
||||||
|
item.completed
|
||||||
|
? 'bg-primary text-primary-foreground border-primary'
|
||||||
|
: 'border-muted-foreground/40 text-transparent',
|
||||||
|
)}
|
||||||
|
aria-hidden
|
||||||
|
>
|
||||||
|
<CheckIcon className="size-3" />
|
||||||
|
</span>
|
||||||
|
<span
|
||||||
|
className={cn(
|
||||||
|
'min-w-0 flex-1 truncate',
|
||||||
|
item.completed && 'text-muted-foreground line-through',
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{item.name}
|
||||||
|
</span>
|
||||||
|
{item.quantity && (
|
||||||
|
<span className="text-muted-foreground shrink-0 text-sm">{item.quantity}</span>
|
||||||
|
)}
|
||||||
|
</button>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</li>
|
||||||
|
)
|
||||||
|
}
|
||||||
19
apps/web/src/components/ui/collapsible.tsx
Normal file
19
apps/web/src/components/ui/collapsible.tsx
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import { Collapsible as CollapsiblePrimitive } from 'radix-ui'
|
||||||
|
|
||||||
|
function Collapsible({ ...props }: React.ComponentProps<typeof CollapsiblePrimitive.Root>) {
|
||||||
|
return <CollapsiblePrimitive.Root data-slot="collapsible" {...props} />
|
||||||
|
}
|
||||||
|
|
||||||
|
function CollapsibleTrigger({
|
||||||
|
...props
|
||||||
|
}: React.ComponentProps<typeof CollapsiblePrimitive.CollapsibleTrigger>) {
|
||||||
|
return <CollapsiblePrimitive.CollapsibleTrigger data-slot="collapsible-trigger" {...props} />
|
||||||
|
}
|
||||||
|
|
||||||
|
function CollapsibleContent({
|
||||||
|
...props
|
||||||
|
}: React.ComponentProps<typeof CollapsiblePrimitive.CollapsibleContent>) {
|
||||||
|
return <CollapsiblePrimitive.CollapsibleContent data-slot="collapsible-content" {...props} />
|
||||||
|
}
|
||||||
|
|
||||||
|
export { Collapsible, CollapsibleContent, CollapsibleTrigger }
|
||||||
35
apps/web/src/hooks/use-today.ts
Normal file
35
apps/web/src/hooks/use-today.ts
Normal file
@@ -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
|
||||||
|
}
|
||||||
28
apps/web/src/lib/dates.test.ts
Normal file
28
apps/web/src/lib/dates.test.ts
Normal file
@@ -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)
|
||||||
|
})
|
||||||
|
})
|
||||||
14
apps/web/src/lib/dates.ts
Normal file
14
apps/web/src/lib/dates.ts
Normal file
@@ -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()
|
||||||
|
)
|
||||||
|
}
|
||||||
87
apps/web/src/lib/items.test.ts
Normal file
87
apps/web/src/lib/items.test.ts
Normal file
@@ -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>): 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([])
|
||||||
|
})
|
||||||
|
})
|
||||||
40
apps/web/src/lib/items.ts
Normal file
40
apps/web/src/lib/items.ts
Normal 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 }
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user