import { SetItems } from '../hooks/usePagination' export interface Item { id: number [key: string]: any } export interface SocketData { action: 'create' | 'update' | 'delete' item: T } export interface HandleSocketDataOptions { setItems: SetItems data: SocketData } export type SocketListener = (data: SocketData) => void export const handleSocketData = ( options: HandleSocketDataOptions ): void => { const { data, setItems } = options setItems((oldItems) => { const newItems = [...oldItems] switch (data.action) { case 'create': { newItems.push(data.item) break } case 'delete': { const itemIndex = newItems.findIndex((item) => item.id === data.item.id) if (itemIndex !== -1) { newItems.splice(itemIndex, 1) } break } case 'update': { const itemIndex = newItems.findIndex((item) => item.id === data.item.id) if (itemIndex !== -1) { newItems[itemIndex] = data.item } break } } return newItems }) }