-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Classifier: Refactor ClassifierContainer hooks (#2793)
* Refactor useStore and useWorkflowSnapshot - 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. * Add useHydratedStore Move the store hydration and initialisation code into a `useHydratedStore` hook. * Separate out classifier callbacks from hydration * Clean up useHydratedStore Move store clean-up into `ClassifierContainer`. `useHydratedStore` hydrates the store but doesn't try to alter it after loading it. * Add store.startClassification `store.startClassification` allows us to start a new classification automatically, when the subject queue advances, or manually, after loading a workflow from Panoptes. * Add debugging logs to Classifier hooks * Remove unused i18n Co-authored-by: Delilah C. <23665803+goplayoutside3@users.noreply.github.com> * whitespace Co-authored-by: Delilah C. <23665803+goplayoutside3@users.noreply.github.com> * Add a hooks Readme Co-authored-by: Delilah C. <23665803+goplayoutside3@users.noreply.github.com>
- Loading branch information
1 parent
51f0731
commit 88bc3b8
Showing
8 changed files
with
166 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/lib-classifier/src/components/Classifier/hooks/Readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Classifier hooks | ||
|
||
## useHydratedStore | ||
|
||
Hydrate a `mobx-state-tree` store from a stored [snapshot](https://mobx-state-tree.js.org/concepts/snapshots). Wraps the store in [`mst-persist`](https://github.com/agilgur5/mst-persist), which runs `applySnapshot(store, snapshot)` to hydrate the store, and adds an `onSnapshot` handler to keep the stored snapshot updated when the store changes. | ||
|
||
Runs asynchronously and returns `true` when hydration is complete. Snapshots are stored in session storage, so that they don't persist across tabs or windows. | ||
|
||
```js | ||
const loaded = useHydratedStore(store, enableStorage = false, storageKey) | ||
``` | ||
|
||
## useStore | ||
|
||
Create a `mobx-state-tree` store, using the Panoptes API clients and an optional snapshot. Adapted from [the NextJS example](https://github.com/vercel/next.js/blob/5201cdbaeaa72b54badc8f929ddc73c09f414dc4/examples/with-mobx-state-tree/store.js#L49-L52), which is also used in `app-project`. `initialState` must be a valid store snapshot. | ||
|
||
```js | ||
const classifierStore = useStore({ authClient, client, initialState }) | ||
```` | ||
## useWorkflowSnapshot | ||
|
||
A wrapper for [`useSWR`](https://swr.vercel.app/), which fetches a workflow by ID, using the default SWR options. The workflow will refresh on visibility change (eg. waking from sleep), or when the classifier receives focus. | ||
|
||
```js | ||
const workflowSnapshot = useWorkflowSnapshot(workflowID) | ||
``` |
3 changes: 3 additions & 0 deletions
3
packages/lib-classifier/src/components/Classifier/hooks/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { default as useHydratedStore } from './useHydratedStore' | ||
export { default as useStore } from './useStore' | ||
export { default as useWorkflowSnapshot } from './useWorkflowSnapshot' |
33 changes: 33 additions & 0 deletions
33
packages/lib-classifier/src/components/Classifier/hooks/useHydratedStore.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { persist } from 'mst-persist' | ||
import { useEffect, useState } from 'react' | ||
|
||
import { asyncSessionStorage } from '@helpers' | ||
|
||
async function hydrateStore(storageKey, classifierStore) { | ||
try { | ||
await persist(storageKey, classifierStore, { | ||
storage: asyncSessionStorage, | ||
whitelist: ['fieldGuide', 'projects', 'subjects', 'subjectSets', 'tutorials', 'workflows', 'workflowSteps'] | ||
}) | ||
console.log('store hydrated from local storage') | ||
} catch (error) { | ||
console.log('store snapshot error.') | ||
console.error(error) | ||
} | ||
} | ||
|
||
export default function useHydratedStore(classifierStore, cachePanoptesData = false, storageKey) { | ||
const [loaded, setLoaded] = useState(false) | ||
|
||
async function onMount() { | ||
if (cachePanoptesData) { | ||
await hydrateStore(storageKey, classifierStore) | ||
} | ||
setLoaded(true) | ||
} | ||
|
||
useEffect(() => { | ||
onMount() | ||
}, []) | ||
return loaded | ||
} |
25 changes: 25 additions & 0 deletions
25
packages/lib-classifier/src/components/Classifier/hooks/useStore.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import makeInspectable from 'mobx-devtools-mst' | ||
import { useMemo } from 'react' | ||
|
||
import RootStore from '@store' | ||
|
||
let store | ||
|
||
function initStore({ authClient, client, initialState }) { | ||
if (!store) { | ||
store = RootStore.create(initialState, { | ||
authClient, | ||
client | ||
}) | ||
makeInspectable(store) | ||
} | ||
return store | ||
} | ||
/** | ||
useStore hook adapted from | ||
https://github.com/vercel/next.js/blob/5201cdbaeaa72b54badc8f929ddc73c09f414dc4/examples/with-mobx-state-tree/store.js#L49-L52 | ||
*/ | ||
export default function useStore({ authClient, client, initialState }) { | ||
const _store = useMemo(() => initStore({ authClient, client, initialState }), [authClient, initialState]) | ||
return _store | ||
} |
25 changes: 25 additions & 0 deletions
25
packages/lib-classifier/src/components/Classifier/hooks/useWorkflowSnapshot.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import useSWR from 'swr' | ||
|
||
import { panoptes } from '@zooniverse/panoptes-js' | ||
|
||
const SWRoptions = { | ||
revalidateIfStale: true, | ||
revalidateOnMount: true, | ||
revalidateOnFocus: true, | ||
revalidateOnReconnect: true, | ||
refreshInterval: 0 | ||
} | ||
|
||
async function fetchWorkflow(workflowID) { | ||
if (workflowID) { | ||
const { body } = await panoptes.get(`/workflows/${workflowID}`) | ||
const [ workflowSnapshot ] = body.workflows | ||
return workflowSnapshot | ||
} | ||
return null | ||
} | ||
|
||
export default function useWorkflowSnapshot(workflowID) { | ||
const { data } = useSWR(workflowID, fetchWorkflow, SWRoptions) | ||
return data ?? null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters