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

State management refactor #42

Merged
merged 5 commits into from
Sep 26, 2024
Merged

State management refactor #42

merged 5 commits into from
Sep 26, 2024

Conversation

emcelroy
Copy link
Contributor

@emcelroy emcelroy commented Sep 20, 2024

We've added MobX State Tree to help with state management.

@emcelroy emcelroy changed the title State-management-refactor State management refactor Sep 20, 2024
@emcelroy emcelroy marked this pull request as ready for review September 20, 2024 20:39
Copy link
Member

@scytacki scytacki left a comment

Choose a reason for hiding this comment

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

This looks really good to me. I left a couple comments about future improvements.

I think the only thing that would be good to change in this PR is the duplicate code around creating the snapshot to send to applySnapshot.

Comment on lines 25 to 28
const rootCollection = useMemo(() => {
return collections.find((c: ICollection) => !c.parent);
}, [collections]);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [collections, collections.length]);
Copy link
Member

Choose a reason for hiding this comment

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

Probably not now, but this would be better moved into the model. So to support that we'd have to make the model not just be a types.map(... but instead be a types.model(... so it could have a view of rootCollection.

In theory the number of collections might be constant even though the root collection has changed. I don't know collections well enough to know how likely this is.

Comment on lines 38 to 39
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [collections, collections.length]);
Copy link
Member

Choose a reason for hiding this comment

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

This is another example of something that would be better as a view on the model. But again it probably doesn't make sense to change that in this PR unless there are bugs caused by the current approach.

Comment on lines 309 to 317
const newCollectionModels = colls.map((coll: ICollection) => {
const { areParentChildLinksConfigured, attrs, caseName, cases: _cases, childAttrName,
collapseChildren, guid, id, name, parent, title, type } = coll;
return {
areParentChildLinksConfigured, attrs, caseName, cases: _cases, childAttrName,
collapseChildren, guid, id, name, parent, title, type
};
});
applySnapshot(collections, newCollectionModels);
Copy link
Member

Choose a reason for hiding this comment

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

This looks like the same code that is in updateCollections, can you consolidate it?

@emcelroy
Copy link
Contributor Author

This looks really good to me. I left a couple comments about future improvements.

I think the only thing that would be good to change in this PR is the duplicate code around creating the snapshot to send to applySnapshot.

@scytacki Thanks. I consolidated the duplicate code. I also had a go at updating the collections model like I think you were suggesting. Do those changes make sense? They appear to work fine, though I find the structure a little awkward. It seems a bit weird to have a collectionsModel with a collections property, but I couldn't think of a better way to do it.

@kswenson
Copy link
Member

They appear to work fine, though I find the structure a little awkward. It seems a bit weird to have a collectionsModel with a collections property, but I couldn't think of a better way to do i

To be more analogous with CODAP itself, collections would be a property of the DataSetModel.

@emcelroy
Copy link
Contributor Author

They appear to work fine, though I find the structure a little awkward. It seems a bit weird to have a collectionsModel with a collections property, but I couldn't think of a better way to do i

To be more analogous with CODAP itself, collections would be a property of the DataSetModel.

@kswenson Thanks. That makes a lot of sense. I spent some time trying to make that work, but it doesn't seem as straightforward as you might expect, and it's still not clear to me how much more time it'd take to work that out. So I'd like to either stick with the current model changes or undo them and save that work for later like Scott originally suggested, if that sounds reasonable to you and @scytacki?

Copy link
Member

@scytacki scytacki left a comment

Choose a reason for hiding this comment

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

This looks good to me. Yes I think it is fine to punt the renaming.

There might be some issues with the useEffect dependencies just including collectionsModel. However we can probably wait until we find a problem before addressing that.

@emcelroy emcelroy merged commit 9bc4705 into main Sep 26, 2024
5 checks passed
@emcelroy emcelroy deleted the state-management-refactor branch September 26, 2024 14:08
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