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

Classifier: Refactor ClassifierContainer hooks #2793

Merged
merged 9 commits into from
Feb 18, 2022

Conversation

eatyourgreens
Copy link
Contributor

@eatyourgreens eatyourgreens commented Feb 7, 2022

  • Add a hooks directory to the Classifier component. Import the useStore and useWorkflowSnapshot hooks from that directory.
  • Explicitly declare SWR options in the useWorkflowSnapshot hook.
  • Add a useHydratedStore hook, which hydrates the store from session storage when cachePanoptesData is set. Otherwise, it just returns true.
  • Add a useEffect hook to clean up the store and set up volatile callbacks for the parent page after hydration has finished.
  • Add a store.startClassification() action, to replace the subjects.setActiveSubject(subjects.active?.id) hack.
  • Additional console logging when the classifier mounts and creates the store.

This PR includes #2792. It shouldn't change anything, just refactor the hooks to make the classifier component easier to read and debug.

Package:
lib-classifier

Review Checklist

General

  • Are the tests passing locally and on Travis?
  • Is the documentation up to date?

Components

Apps

  • Does it work in all major browsers: Firefox, Chrome, Edge, Safari?
  • Does it work on mobile?
  • Can you yarn panic && yarn bootstrap or docker-compose up --build and app works as expected?

Publishing

  • Is the changelog updated?
  • Are the dependencies updated for apps and libraries that are using the newly published library?

Post-merging

@eatyourgreens eatyourgreens added the refactor Refactoring existing code label Feb 7, 2022
@eatyourgreens eatyourgreens requested a review from a team February 7, 2022 10:37
@eatyourgreens eatyourgreens force-pushed the refactor-classifier-hooks branch from 3df2d43 to 65121a0 Compare February 7, 2022 10:37
@eatyourgreens eatyourgreens mentioned this pull request Feb 7, 2022
18 tasks
@eatyourgreens eatyourgreens changed the title Classifier: Refactor useStore and useWorkflowSnapshot Classifier: Refactor ClassifierContainer hooks Feb 7, 2022
@eatyourgreens eatyourgreens force-pushed the refactor-classifier-hooks branch 2 times, most recently from 3998f30 to 877bb0a Compare February 7, 2022 13:07
@eatyourgreens eatyourgreens force-pushed the refactor-classifier-hooks branch 4 times, most recently from 18a958c to 852f74f Compare February 8, 2022 13:09
@eatyourgreens eatyourgreens force-pushed the refactor-classifier-hooks branch from c954986 to b51ab8c Compare February 9, 2022 20:41
@eatyourgreens eatyourgreens force-pushed the refactor-classifier-hooks branch from b51ab8c to ecf8fe6 Compare February 10, 2022 11:30
@goplayoutside3 goplayoutside3 self-assigned this Feb 14, 2022
Copy link
Contributor

@goplayoutside3 goplayoutside3 left a comment

Choose a reason for hiding this comment

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

PR Review

I tested this refactor PR by making real classifications to the i-fancy-cats staging project, and I made demo classifications to the live FEM project Corresponding with Quakers. Functionality is unchanging 👍

Classifier and ClassifierContainer do not have any test files, which is on our radar as a team, so in an effort to add better tests and documentation to lib-classifier (like your question in #2809), can a spec file be added for each custom hook in this PR? I’m thinking a unit test similar to useStores, or if mocking workflows and stores is overkill, then a README for each custom hook will be super helpful going forward.

@@ -37,6 +37,7 @@ export default function Classifier({
useEffect(function onURLChange() {
const { workflows } = classifierStore
if (workflowID) {
console.log('starting new subject queue', { workflowID, subjectSetID, subjectID })
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
console.log('starting new subject queue', { workflowID, subjectSetID, subjectID })
// console.log('starting new subject queue', { workflowID, subjectSetID, subjectID })

Console logs in this PR are helpful for classifier debugging, but the ones that log data will be noisy if these are committed to master. I'm suggesting commenting them out until they're specifically used for debugging locally.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Console logs are also recorded in Sentry. I've added these so that we can better use Sentry to debug problems on Davy Notebooks and HMS NHS.

I'd prefer to leave them in until we're sure that those projects are working properly.

@@ -51,9 +52,10 @@ export default function Classifier({
if (workflowSnapshot) {
// pass the subjectSetID prop into the store as part of the new workflow data
workflowSnapshot.subjectSet = subjectSetID
console.log('Refreshing workflow snapshot', workflowSnapshot.id)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
console.log('Refreshing workflow snapshot', workflowSnapshot.id)
// console.log('Refreshing workflow snapshot', workflowSnapshot.id)

@eatyourgreens
Copy link
Contributor Author

Testing hooks in isolation isn't recommended. Better to test them in the component that uses them.

https://react-hooks-testing-library.com/

Here, that would involve mounting and rendering the classifier, maybe with mocked local storage.

@goplayoutside3
Copy link
Contributor

Testing hooks in isolation isn't recommended. Better to test them in the component that uses them.

Makes sense, and in that case can a README be added to the /hooks folder? For example, it should explain the purpose and role of mst-persist in useHydratedStore and how SWR is important to useWorkflowSnapshot. Keeping the team up to speed on data flow in Classifier and ClassifierContainer will be immensely helpful to reviewing bug fixes, especially because those component files are missing tests.

No rush! This is a refactor PR.

@eatyourgreens eatyourgreens force-pushed the refactor-classifier-hooks branch from d245960 to db28fa5 Compare February 17, 2022 12:19
@eatyourgreens
Copy link
Contributor Author

I've added a short Readme for the hooks. They're mostly wrappers for external libraries, so I've included links to those libraries too.

Copy link
Contributor

@goplayoutside3 goplayoutside3 left a comment

Choose a reason for hiding this comment

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

I've added a short Readme for the hooks. They're mostly wrappers for external libraries, so I've included links to those libraries too.

Looks good thanks!

@github-actions github-actions bot added the approved This PR is approved for merging label Feb 18, 2022
- Add a `hooks` directory to the `Classifier` component. Import the `useStore` and `useWorkflowSnapshot` hooks from that directory.
- Explicitly declare `SWR` options in the the `useWorkflowSnapshot` hook.
Move the store hydration and initialisation code into a `useHydratedStore` hook.
Move store clean-up into `ClassifierContainer`. `useHydratedStore` hydrates the store but doesn't try to alter it after loading it.
`store.startClassification` allows us to start a new classification automatically, when the subject queue advances, or manually, after loading a workflow from Panoptes.
eatyourgreens and others added 4 commits February 18, 2022 05:07
Co-authored-by: Delilah C. <23665803+goplayoutside3@users.noreply.github.com>
Co-authored-by: Delilah C. <23665803+goplayoutside3@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved This PR is approved for merging refactor Refactoring existing code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants