import { useState } from 'react'; export function useListSelection( initialValue: Array = [], compareFn: (a: T, b: T) => boolean = (a, b) => a === b ) { const [selectedItems, setSelectedItems] = useState>(initialValue); function handleChangeSelect(currentItem: T, selected: boolean) { if (selected) { setSelectedItems((items) => [...items, currentItem]); } else { setSelectedItems((items) => items.filter((item) => !compareFn(item, currentItem)) ); } } return [selectedItems, handleChangeSelect] as const; }