Skip to content

fix : copy the wrong game PGN after a deletion - #98

Open
mateuskb wants to merge 2 commits into
GuillaumeSD:mainfrom
mateuskb:fix/database-row-lookup
Open

fix : copy the wrong game PGN after a deletion#98
mateuskb wants to merge 2 commits into
GuillaumeSD:mainfrom
mateuskb:fix/database-row-lookup

Conversation

@mateuskb

Copy link
Copy Markdown

Problem

On /database, the games grid is rendered with rows={games} and no getRowId, so MUI DataGrid derives each row id from Game.id — the IndexedDB auto-increment key.

handleCopyGameRow treats that key as an array index:

await navigator.clipboard?.writeText?.(games[id - 1].pgn);

Keys are only dense until a game is deleted. Afterwards they go sparse (1, 2, 4, 5) while games stays contiguous, so the two stop lining up.

Steps to reproduce

  1. Save three games, so ids are 1, 2, 3.
  2. Delete the first one. Remaining ids are 2, 3; games has length 2.
  3. Click copy on the game with id 3.

games[2] is undefined, so it throws on undefined.pgn. With more games saved it silently copies a different game's PGN instead, which is the worse outcome since there is no visible error.

Fix

Look the game up by key rather than by position — the same value handleDeleteGameRow already passes straight to deleteGame(id):

const game = games.find((g) => g.id === id);
if (!game) {
  throw new Error(`Game ${id} not found`);
}
await navigator.clipboard?.writeText?.(game.pgn);

The second commit drops an unrelated console.log(games) that was firing on every render of the same page. Happy to split that out if you would rather keep the PR to a single concern.

npm run lint passes.

Mateus Ribeiro added 2 commits August 21, 2026 18:11
The DataGrid is fed `rows={games}` with no `getRowId`, so it derives row ids
from `Game.id`, which is the IndexedDB auto-increment key. `handleCopyGameRow`
treated that key as an array index (`games[id - 1]`).

Keys stay dense only until a game is deleted. After that they go sparse
(1, 2, 4, 5...) while the array stays contiguous, so copying a game whose id
sits past the gap yields another game's PGN, or throws on `undefined.pgn` for
the last row.

Look the game up by key instead, matching what `handleDeleteGameRow` already
does with the same value.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant