amelioro / ameliorate

A tool for analyzing debatable problems effectively, collaboratively, and with an open mind.
https://ameliorate.app
MIT License
26 stars 10 forks source link

Loading same topic with new data tries to save data again #556

Closed keyserj closed 2 weeks ago

keyserj commented 3 weeks ago

Describe the bug

See reproduction

To Reproduce

  1. log in
  2. load a user topic e.g. https://ameliorate.app/keyserj/harris-vs-trump-economics
  3. duplicate the tab into a second tab
  4. create a comment in the second tab
  5. (open dev tools to observe network requests in the first tab)
  6. in the first tab, navigate to a different page, and press the back button

Observe error in trpc/comment.handleChangesets API request:

Invalid `prisma.comment.createMany()` invocation:
Unique constraint failed on the fields: (`id`)

Expected behavior

No error, first tab should not even attempt to make a comment.handleChangesets request.

Additional context

No response

Technical ideas

Front-end Zustand stores are saving diffs when changes are made. Changes are made when a page is loaded, but the store prevents saving by avoiding doing so if the topic.id changes. If the topic.id does not change, we may create an unintended additional API request to set data.

We should not be relying on topic.id to prevent unintended saves. We should add a mechanism to the store like undo/redo has (for pausing undo/redo tracking) to pause store syncing (we'd pause before loading data and resume after it's loaded).