Files
tolaria/src/hooks/useBulkActions.ts
Luca Rossi 6acc802824 test: extract useBulkActions hook and add unit tests (#192)
useBulkActions was embedded in App.tsx with zero test coverage. It handles
bulk archive, trash, and restore operations — all with partial-failure
semantics and toast messaging.

Extract to src/hooks/useBulkActions.ts and add 15 unit tests covering:
- Plural/singular toast messages ("2 notes archived" vs "1 note archived")
- Partial failures: only successful operations counted in toast
- All-failure case: no toast shown
- Empty array: no operations called, no toast

Risk mitigated: silent bugs in batch operations (wrong count in toast,
toast shown when nothing succeeded, partial failure not handled).

Co-authored-by: Test <test@test.com>
2026-03-12 03:19:32 +01:00

42 lines
1.5 KiB
TypeScript

import { useCallback } from 'react'
interface BulkEntryActions {
handleArchiveNote: (path: string) => Promise<void>
handleTrashNote: (path: string) => Promise<void>
handleRestoreNote: (path: string) => Promise<void>
}
export function useBulkActions(
entryActions: BulkEntryActions,
setToastMessage: (msg: string | null) => void,
) {
const handleBulkArchive = useCallback(async (paths: string[]) => {
let ok = 0
for (const path of paths) {
try { await entryActions.handleArchiveNote(path); ok++ }
catch { /* error toast already shown by flushBeforeAction */ }
}
if (ok > 0) setToastMessage(`${ok} note${ok > 1 ? 's' : ''} archived`)
}, [entryActions, setToastMessage])
const handleBulkTrash = useCallback(async (paths: string[]) => {
let ok = 0
for (const path of paths) {
try { await entryActions.handleTrashNote(path); ok++ }
catch { /* error toast already shown by flushBeforeAction */ }
}
if (ok > 0) setToastMessage(`${ok} note${ok > 1 ? 's' : ''} moved to trash`)
}, [entryActions, setToastMessage])
const handleBulkRestore = useCallback(async (paths: string[]) => {
let ok = 0
for (const path of paths) {
try { await entryActions.handleRestoreNote(path); ok++ }
catch { /* skip — error toast already shown */ }
}
if (ok > 0) setToastMessage(`${ok} note${ok > 1 ? 's' : ''} restored`)
}, [entryActions, setToastMessage])
return { handleBulkArchive, handleBulkTrash, handleBulkRestore }
}