Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor selected note state from flux to redux #1851

Merged
merged 5 commits into from
Jan 30, 2020

Conversation

belcherj
Copy link
Contributor

@belcherj belcherj commented Jan 21, 2020

Fix

This PR moves the selected note state from flux to redux. It also eliminates
the need for selectedNoteId in appState.

Test

  1. Test the note selection flow
  2. Tets the remote note change flows
  3. Test note share flow
  4. Test note publish
  5. Test trash flow
  6. Test tag selection
  7. Test search

Review

Only one developer is required to review these changes, but anyone can perform the review.

Release

RELEASE-NOTES.txt was updated with:

  • Refactored selected note state 1851

@belcherj belcherj requested a review from a team January 21, 2020 20:12
@belcherj belcherj self-assigned this Jan 21, 2020
lib/note-editor/index.tsx Outdated Show resolved Hide resolved
lib/state/ui/middleware.ts Outdated Show resolved Hide resolved
lib/state/ui/middleware.ts Outdated Show resolved Hide resolved
Copy link
Member

@dmsnell dmsnell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You picked a hard one to start with 😀

I know we have some poor examples in the code already where our action creators are imperative and they suggest that we should move more logic into the middleware but I don't think there's any need in this PR to make any changes to the middleware - it looked like all of the code there could exist solely in the selectedNote reducer.

If that doesn't make sense I'd be happy to delve further into it but would you mind trying to make that change here? It will probably feel better after that.

lib/state/ui/reducer.ts Outdated Show resolved Hide resolved
lib/state/ui/reducer.ts Outdated Show resolved Hide resolved
lib/note-list/index.tsx Outdated Show resolved Hide resolved
lib/note-list/index.tsx Outdated Show resolved Hide resolved
lib/state/action-types.ts Outdated Show resolved Hide resolved
lib/state/index.ts Outdated Show resolved Hide resolved
lib/state/ui/middleware.ts Outdated Show resolved Hide resolved
lib/state/ui/reducer.ts Outdated Show resolved Hide resolved
lib/state/ui/reducer.ts Outdated Show resolved Hide resolved
lib/note-info/index.tsx Outdated Show resolved Hide resolved
lib/state/ui/reducer.ts Outdated Show resolved Hide resolved
lib/types.ts Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
@belcherj belcherj force-pushed the refactor/selected-note-state branch 3 times, most recently from 47d68b8 to 044bf8e Compare January 27, 2020 21:05
Copy link
Member

@dmsnell dmsnell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we need to rebase this ontop of the changes introduced in #1855.

Specifically noting the need in action-types where we're introducing a new string action-type export.

This PR moves the selected note state from flux to redux. It also eliminates
the need for selectedNoteId in appState.
@belcherj belcherj force-pushed the refactor/selected-note-state branch from 057fc0a to 54596f0 Compare January 29, 2020 14:55
@belcherj
Copy link
Contributor Author

@dmsnell This is ready for more review.

lib/note-editor/index.tsx Outdated Show resolved Hide resolved
lib/state/ui/actions.ts Outdated Show resolved Hide resolved
lib/app.tsx Outdated Show resolved Hide resolved
 - Add type annotations where we're introducing new state
 - Remove references to `noteUpdatedRemotely`
 - Remove ability to `selectNote(null)`
 - Remove unused `tags` prop
@dmsnell
Copy link
Member

dmsnell commented Jan 29, 2020

@belcherj I added a new commit to try out the changes I proposed.

  • Added type annotations where we're introducing new things
  • Renamed setSelectedNote to selectNote to focus more on the action we're doing rather than the data we're changing
  • Removed the tags prop
  • Removed lingering references to noteUpdatedRemotely

I'll be thoroughly testing this later on but I need a break from it at the moment

@belcherj
Copy link
Contributor Author

I have tested this a bunch today. I think it is ready to roll.

Copy link
Member

@dmsnell dmsnell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for everything here; it's really exciting to see state finally cleaning up. According to your own testing please merge at your convenience.

@belcherj belcherj merged commit c1cb40a into develop Jan 30, 2020
@belcherj belcherj deleted the refactor/selected-note-state branch January 30, 2020 20:10
dmsnell added a commit that referenced this pull request Jan 31, 2020
In #1851 we refactored the selected note state but during testing
missed a reference to `state.appState.note` which should have been
updated to `state.ui.note`. Nothing crashed in the app because it
was being used for its truthiness but the result was that when
checking if Markdown was enabled for any given note the answer
would always end up as "no."

In this patch we're restoring the state reference which fixes that
bug and restores the preview button in the note toolbar.
dmsnell added a commit that referenced this pull request Jan 31, 2020
In #1851 we refactored the selected note state but during testing
missed a reference to `state.appState.note` which should have been
updated to `state.ui.note`. Nothing crashed in the app because it
was being used for its truthiness but the result was that when
checking if Markdown was enabled for any given note the answer
would always end up as "no."

In this patch we're restoring the state reference which fixes that
bug and restores the preview button in the note toolbar.
dmsnell added a commit that referenced this pull request Feb 4, 2020
Resolves #1879

It appears that we removed a flow in #1851 which updated app state in response
to updates from the note editor[1]. While this didn't impact the operation of
the editor it did present itself as a bug when previewing markdown-rendered
notes. The editor is stateful on its own and so continued to function
independently of the state of the selected note's content. The preview,
however, grabs the contents of the selected note whenever it's activated and
then renders those into a DOM node. This means that it showed the old contents
of the note unless you navigated to another note and back, which swapped out
the selected note with the new updated contents.

In this patch, which currently is probably wrong in some way, we're updating
the selected note when calling `onChangeContent` so that we don't get out of
sync with the data sources. Hopefully at the end of our state cleanup and
stabilization project this will disappear but for now this fix catches the
gap that left split data sources.

Probably there were other bugs we didn't discover as a result of this.

Mainly we need to test and audit for traces of cycles/loops in the update
so that we don't start spinning or overwriting data we just entered. In my
brief testing the easy cases worked as I expected them to simply and singly.

[1]: https://github.com/Automattic/simplenote-electron/pull/1851/files#diff-3c87750a7cc32e0127447ffc78f5c0d5L300
dmsnell added a commit that referenced this pull request Feb 6, 2020
* Fix: Update selected note when making changes in the editor view

Resolves #1879

It appears that we removed a flow in #1851 which updated app state in response
to updates from the note editor[1]. While this didn't impact the operation of
the editor it did present itself as a bug when previewing markdown-rendered
notes. The editor is stateful on its own and so continued to function
independently of the state of the selected note's content. The preview,
however, grabs the contents of the selected note whenever it's activated and
then renders those into a DOM node. This means that it showed the old contents
of the note unless you navigated to another note and back, which swapped out
the selected note with the new updated contents.

In this patch, which currently is probably wrong in some way, we're updating
the selected note when calling `onChangeContent` so that we don't get out of
sync with the data sources. Hopefully at the end of our state cleanup and
stabilization project this will disappear but for now this fix catches the
gap that left split data sources.

Probably there were other bugs we didn't discover as a result of this.

Mainly we need to test and audit for traces of cycles/loops in the update
so that we don't start spinning or overwriting data we just entered. In my
brief testing the easy cases worked as I expected them to simply and singly.

[1]: https://github.com/Automattic/simplenote-electron/pull/1851/files#diff-3c87750a7cc32e0127447ffc78f5c0d5L300

* Handle tag updates too by always re-selecting note on updates from the server

When tags were being updated in the pap they also came through this function
but they weren't coming through `onUpdateContent`, meaning that our fix
overlooked tags and probably also system tags and the like.

In this patch we're no longer selectively updating the note if it has a remote
patch (indicating that it came from another device). Instead, we're always
updating and selectively setting `hasRemoteUpdate` based on that knowledge.
This ensures that we maintain consistency with local updates.
@codebykat codebykat added this to the state refactor milestone Oct 21, 2020
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.

3 participants