import { createColumnHelper } from '@tanstack/react-table' import { useAtomValue, useSetAtom } from 'jotai' import { useMemo } from 'react' import { currentItemSelectionAtom, itemSelectionSetActionAtom } from '@/state/atoms' import { StatsColumns } from '../columns' import { ItemWithSelection } from './defs' const ch = createColumnHelper() const columns = { icon: ch.display({ id: 'icon', header: function Component(_col) { return
}, cell: function Component({ row }) { const setItemSelection = useSetAtom(itemSelectionSetActionAtom) const c = useAtomValue(currentItemSelectionAtom) const selected = useMemo(() => { return c[0].has(row.original.item.id) }, [c]) return ( ) }, }), count: ch.display({ id: 'count', header: function Component(_col) { return
#
}, cell: function Component({ row }) { const c = useAtomValue(currentItemSelectionAtom) const setItemSelection = useSetAtom(itemSelectionSetActionAtom) const currentValue = useMemo(() => { const got = c[0].get(row.original.item.id) if (got !== undefined) { return got.toString() } return '' }, [c]) const itemCount = row.original.item.item_count return (
{ if (e.target.value === '') { setItemSelection({ [row.original.item.id]: undefined }) return } if (e.target.value === '-') { setItemSelection({ [row.original.item.id]: itemCount, }) } let parsedInt = parseInt(e.target.value) if (Number.isNaN(parsedInt)) { return } if (parsedInt > itemCount) { parsedInt = itemCount } setItemSelection({ [row.original.item.id]: parsedInt, }) }} placeholder={itemCount.toString()} />
) }, }), name: ch.display({ id: 'name', header: _col => { return
name
}, cell: function Component({ row }) { return (
{row.original.item.item_name}
) }, }), slots: ch.display({ id: 'slots', header: _col => { return
slots
}, cell: function Component({ row }) { return (
{row.original.item.item_slots}
) }, }), stats: ch.group({ id: 'stats', header: _col => { return
stats
}, columns: [ ...StatsColumns.map(c => { return ch.display({ id: `stats.${c}`, header: _col => { return
{c}
}, cell: function Component({ row }) { const stats = row.original.item.stats const stat = stats ? stats[c] : '' return (
{stat}
) }, }) }), ], }), } as const export const InventoryColumns = columns