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(app-project): optimistic updates for YourProjectStats #6589

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import { useAdminMode } from '@hooks'
import addQueryParams from '@helpers/addQueryParams'
import logToSentry from '@helpers/logger/logToSentry.js'
import ErrorMessage from './components/ErrorMessage'
import useYourProjectStats from '../YourProjectStats/useYourProjectStats.js'
import incrementStats from '../YourProjectStats/helpers/incrementStats.js'
import { updateYourStats } from '../YourProjectStats/useYourProjectStats.js'

function onError(error, errorInfo = {}) {
logToSentry(error, errorInfo)
Expand Down Expand Up @@ -61,18 +60,17 @@ export default function ClassifierWrapper({
Add the recently classified subject to the signed-in user's Recents.
*/
const projectID = project?.id
const { mutate } = useYourProjectStats({ projectID, userID })

const addRecents = recents?.add
const onCompleteClassification = useCallback((classification, subject) => {
personalization.incrementSessionCount()
incrementStats(mutate, projectID, userID)
updateYourStats(projectID, userID)
addRecents({
favorite: subject.favorite,
subjectId: subject.id,
locations: subject.locations
})
}, [addRecents, mutate, projectID, userID])
}, [addRecents, projectID, userID])

/*
If the page URL contains a subject ID, update that ID when the classification subject changes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import logToSentry from '@helpers/logger/logToSentry.js'

import { usePanoptesAuthToken } from '@hooks'
import { getTodayDateString, getNumDaysAgoDateString, getQueryPeriod } from './helpers/dateRangeHelpers.js'
import incrementStats from './helpers/incrementStats.js'

const SWROptions = {
revalidateIfStale: true,
Expand All @@ -24,6 +25,8 @@ function statsHost(env) {

const endpoint = '/classifications/users'

let mutate

/* user.created_at is needed for allTimeQuery, and not always available on the logged in user object */
async function fetchUserCreatedAt({ token, userID }) {
const authorization = `Bearer ${token}`
Expand Down Expand Up @@ -96,10 +99,22 @@ async function fetchStats({ endpoint, projectID, userID, token }) {
return null
}

/**
* Optimistically increment project stats, without revalidating the SWR cache.
* @param {string} projectID
* @param {string} userID
* @returns the mutated stats data
*/
export function updateYourStats(projectID, userID) {
return incrementStats(mutate, projectID, userID)
}

export default function useYourProjectStats({ projectID, userID }) {
const token = usePanoptesAuthToken()

// only fetch stats when a userID is available. Don't fetch if no user signed in.
const key = token && userID ? { endpoint, projectID, userID, token } : null
return useSWR(key, fetchStats, SWROptions)
const fetchedStats = useSWR(key, fetchStats, SWROptions)
mutate = fetchedStats.mutate
Copy link
Contributor Author

@eatyourgreens eatyourgreens Dec 20, 2024

Choose a reason for hiding this comment

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

Does anyone know what mutate evaluates to here when the key is null? The old stats code kept a running count of classifications in the current tab, if you weren’t logged in. Just so you’d have some visible feedback that your classification had been saved, after you pressed Done.

return fetchedStats
}
Loading