| 1 | 'use client' |
| 2 | |
| 3 | import { useCallback, useState } from 'react' |
| 4 | |
| 5 | export function useSetSelection<T |
| 6 | getId: (item |
| 7 | ) { |
| 8 | const [selectedIds, |
| 9 | |
| 10 | const toggle = |
| 11 | setSelectedIds((prev) |
| 12 | const next = |
| 13 | if (next.has |
| 14 | next.delete(id |
| 15 | } else { |
| 16 | next.add(id |
| 17 | } |
| 18 | return next |
| 19 | }) |
| 20 | }, []) |
| 21 | |
| 22 | const toggleAll = |
| 23 | (items: T |
| 24 | setSelectedIds((prev) |
| 25 | const allSelected = |
| 26 | items.length > 0 |
| 27 | if (allSelected) { |
| 28 | const next = |
| 29 | for (const i |
| 30 | next.delete(getId |
| 31 | } |
| 32 | return next |
| 33 | } |
| 34 | const next = |
| 35 | for (const i |
| 36 | next.add(getId |
| 37 | } |
| 38 | return next |
| 39 | }) |
| 40 | }, |
| 41 | [getId] |
| 42 | ) |
| 43 | |
| 44 | const clear = |
| 45 | |
| 46 | return { selectedIds, |
| 47 | } |