-
-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #904 from The-Commit-Company/avoid-request-waterfalls
fix: active users indicator reliability and removed context
- Loading branch information
Showing
8 changed files
with
115 additions
and
52 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
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,55 @@ | ||
import { UserContext } from '@/utils/auth/UserProvider' | ||
import { useFrappeEventListener, useFrappeGetCall, useSWRConfig } from 'frappe-react-sdk' | ||
import { useContext } from 'react' | ||
import { useActiveState } from '../useActiveState' | ||
|
||
/** | ||
* Hook to fetch active users from the server. | ||
* SWRKey: active_users | ||
*/ | ||
const useFetchActiveUsers = () => { | ||
const res = useFrappeGetCall<{ message: string[] }>('raven.api.user_availability.get_active_users', | ||
undefined, | ||
'active_users', | ||
{ | ||
dedupingInterval: 1000 * 60 * 5, // 5 minutes - do not refetch if the data is fresh | ||
} | ||
) | ||
|
||
return res | ||
} | ||
|
||
/** | ||
* Hook to listen to user_active_state_updated event and update the active_users list in realtime | ||
* Also handles the user's active state via visibilty change and idle timer | ||
*/ | ||
export const useFetchActiveUsersRealtime = () => { | ||
const { currentUser } = useContext(UserContext) | ||
|
||
const { mutate } = useSWRConfig() | ||
|
||
useActiveState() | ||
|
||
/** Hook to listen to user_active_state */ | ||
useFrappeEventListener('raven:user_active_state_updated', (data) => { | ||
if (data.user !== currentUser) { | ||
// If the user is not the current user, update the active_users list | ||
// No need to revalidate the data as the websocket event has emitted the new data for that user | ||
mutate('active_users', (res?: { message: string[] }) => { | ||
if (res) { | ||
if (data.active) { | ||
return { message: [...res.message, data.user] } | ||
} else { | ||
return { message: res.message.filter(user => user !== data.user) } | ||
} | ||
} else { | ||
return undefined | ||
} | ||
}, { | ||
revalidate: false | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
export default useFetchActiveUsers |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,22 @@ | ||
import { useContext, useMemo } from 'react'; | ||
import { ActiveUsersContext } from '../utils/users/ActiveUsersProvider'; | ||
import { UserContext } from '@/utils/auth/UserProvider'; | ||
import useFetchActiveUsers from './fetchers/useFetchActiveUsers'; | ||
|
||
export const useIsUserActive = (userID?: string): boolean => { | ||
|
||
const { currentUser } = useContext(UserContext) | ||
const activeUsers = useContext(ActiveUsersContext) | ||
|
||
const { data } = useFetchActiveUsers() | ||
|
||
const isActive = useMemo(() => { | ||
if (userID === currentUser) { | ||
return true | ||
} else if (userID) { | ||
return activeUsers.includes(userID) | ||
return data?.message.includes(userID) ?? false | ||
} else { | ||
return false | ||
} | ||
}, [userID, activeUsers]) | ||
}, [userID, data]) | ||
|
||
return isActive | ||
} |
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
This file was deleted.
Oops, something went wrong.