bugfix: handle table update when column is missing

This commit is contained in:
Kendall Garner 2025-07-01 19:03:54 -07:00
parent 217a4d65fd
commit ce6aaa709f
No known key found for this signature in database
GPG key ID: 9355F387FE765C94

View file

@ -33,16 +33,17 @@ export const useTableChange = (
const api = tableRef.current?.api; const api = tableRef.current?.api;
if (!api) return; if (!api) return;
const rowNodes: RowNode[] = [];
const idSet = new Set(ids); const idSet = new Set(ids);
api.forEachNode((node: RowNode<Song>) => { api.forEachNode((node: RowNode<Song>) => {
if (!node.data || !idSet.has(node.data.id)) return; if (!node.data || !idSet.has(node.data.id)) return;
// Make sure to use setData instead of setDataValue. setDataValue
// will error if the column does not exist, whereas setData won't care
switch (event.event) { switch (event.event) {
case 'favorite': { case 'favorite': {
if (node.data.userFavorite !== event.favorite) { if (node.data.userFavorite !== event.favorite) {
node.setDataValue('userFavorite', event.favorite); node.setData({ ...node.data, userFavorite: event.favorite });
} }
break; break;
} }
@ -58,18 +59,12 @@ export const useTableChange = (
break; break;
case 'rating': { case 'rating': {
if (node.data.userRating !== event.rating) { if (node.data.userRating !== event.rating) {
node.setDataValue('userRating', event.rating); node.setData({ ...node.data, userRating: event.rating });
rowNodes.push(node);
} }
break; break;
} }
} }
}); });
// This is required to redraw star rows
if (rowNodes.length > 0) {
api.redrawRows({ rowNodes });
}
}, },
[tableRef], [tableRef],
); );