feat(items): delete an item with a swipe right, undoable for 3 seconds

A long swipe right on an item (or "Eliminar" in the desktop context menu)
hides it and shows a toast with "Deshacer". The DELETE is only sent once
the 3 seconds are over, so undoing just cancels it; pending deletions are
flushed with keepalive if the page is closed in the meantime.

The API gets DELETE /api/lists/:listId/items/:itemId, members only, which
publishes item.removed so other members see the item disappear.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jose Selesan
2026-10-20 20:40:56 -03:00
parent feb8df14c0
commit eb1f6cb2a9
10 changed files with 412 additions and 14 deletions

View File

@@ -0,0 +1,67 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import {
DELETE_UNDO_MS,
flushPendingDeletions,
getPendingDeletions,
scheduleDeletion,
} from './pending-deletions'
describe('pending deletions', () => {
beforeEach(() => {
vi.useFakeTimers()
})
afterEach(() => {
vi.useRealTimers()
})
it('hides the item and sends the delete once the undo window is over', async () => {
const commit = vi.fn(async () => {})
scheduleDeletion('item-1', commit)
expect(getPendingDeletions().has('item-1')).toBe(true)
await vi.advanceTimersByTimeAsync(DELETE_UNDO_MS - 1)
expect(commit).not.toHaveBeenCalled()
await vi.advanceTimersByTimeAsync(1)
expect(commit).toHaveBeenCalledWith({ keepalive: false })
expect(getPendingDeletions().has('item-1')).toBe(false)
})
it('undo restores the item and never sends the delete', async () => {
const commit = vi.fn(async () => {})
const undo = scheduleDeletion('item-2', commit)
undo()
expect(getPendingDeletions().has('item-2')).toBe(false)
await vi.advanceTimersByTimeAsync(DELETE_UNDO_MS)
expect(commit).not.toHaveBeenCalled()
})
it('shows the item again when the delete fails', async () => {
const commit = vi.fn(async (_init: { keepalive: boolean }) => {
throw new Error('offline')
})
scheduleDeletion('item-3', (init) => commit(init).catch(() => {}))
await vi.advanceTimersByTimeAsync(DELETE_UNDO_MS)
expect(commit).toHaveBeenCalledOnce()
expect(getPendingDeletions().has('item-3')).toBe(false)
})
it('flushes waiting deletions right away with keepalive', async () => {
const commit = vi.fn(async () => {})
scheduleDeletion('item-4', commit)
flushPendingDeletions()
await vi.advanceTimersByTimeAsync(0)
expect(commit).toHaveBeenCalledWith({ keepalive: true })
expect(getPendingDeletions().has('item-4')).toBe(false)
await vi.advanceTimersByTimeAsync(DELETE_UNDO_MS)
expect(commit).toHaveBeenCalledOnce()
})
})

View File

@@ -0,0 +1,90 @@
/** How long a deleted item can still be restored before its `DELETE` is sent. */
export const DELETE_UNDO_MS = 3000
/** Sends the `DELETE`; `keepalive` is set when the page is going away. */
type Commit = (init: { keepalive: boolean }) => Promise<void>
type PendingDeletion = {
/** Pending until the `DELETE` is sent; `undefined` once it is in flight. */
timer: ReturnType<typeof setTimeout> | undefined
commit: Commit
}
/**
* Items the user deleted, still hidden while they can be restored or while
* their `DELETE` is in flight. It lives outside React so navigating away does
* not cancel a deletion; rows are filtered out at render time, so a refetch
* in the meantime cannot bring them back.
*/
const pending = new Map<string, PendingDeletion>()
const listeners = new Set<() => void>()
let snapshot: ReadonlySet<string> = new Set()
function emit() {
snapshot = new Set(pending.keys())
for (const listener of listeners) listener()
}
/** `useSyncExternalStore` subscription. */
export function subscribePendingDeletions(listener: () => void): () => void {
listeners.add(listener)
return () => listeners.delete(listener)
}
/** Ids of the items hidden because they are being deleted (a stable snapshot). */
export function getPendingDeletions(): ReadonlySet<string> {
return snapshot
}
async function run(itemId: string, keepalive: boolean) {
const entry = pending.get(itemId)
if (!entry) return
clearTimeout(entry.timer)
entry.timer = undefined
try {
await entry.commit({ keepalive })
} finally {
// On success the commit already dropped the item from the cache; on
// failure the row comes back.
pending.delete(itemId)
emit()
}
}
/**
* Hides the item and sends its `DELETE` after `DELETE_UNDO_MS`. Returns the
* undo function, which only works while the `DELETE` has not been sent.
*/
export function scheduleDeletion(itemId: string, commit: Commit): () => void {
if (!pending.has(itemId)) {
pending.set(itemId, {
timer: setTimeout(() => void run(itemId, false), DELETE_UNDO_MS),
commit,
})
emit()
}
return () => {
const entry = pending.get(itemId)
if (!entry?.timer) return
clearTimeout(entry.timer)
pending.delete(itemId)
emit()
}
}
/** Sends every deletion still waiting, without waiting for the undo window. */
export function flushPendingDeletions(): void {
for (const [itemId, entry] of pending) {
if (entry.timer) void run(itemId, true)
}
}
// Closing the tab or the installed app must not lose a deletion: `keepalive`
// lets the request outlive the page.
if (typeof window !== 'undefined') {
window.addEventListener('pagehide', flushPendingDeletions)
}