Skip to content

Commit

Permalink
refactor(organizations): add of useSession hook TASK-1305 (#5303)
Browse files Browse the repository at this point in the history
### 👀 Preview steps
No behavior will change with this implementation, but it can be tested
by using the new hook somewhere.
As a test I've modified `MyProjectRoute` to verify that
`currentLoggedAccount` was properly dispatching component re-render and
that using hooks methods are working properly as well:
```typescript
...
export default function MyProjectsRoute() {

  const [counter, setCounter] = React.useState(0);

  const {currentLoggedAccount, refreshAccount} = useSession();

  useEffect(() => {
    setCounter((prev) => prev + 1);
  }, [currentLoggedAccount]);

  return (
    <>
    <button onClick={refreshAccount}>Refresh {counter} - {currentLoggedAccount?.username}</button>
    <UniversalProjectsRoute
...
```
Result:

![KPI_5303](https://github.com/user-attachments/assets/be5f9c7f-ce37-459b-90a7-9848bb88db7d)

### 💭 Notes

- This PR adds the `useSession` hook, aimed to be used instead of the
current `sessionStore` class to access the session data and methods.
- The `useSession` hook returns the `currentLoggedAccount` user, which
is already filtered to be an existent account returned from the `/me`
endpoint and verified not to be an anonymous account, which is currently
done in several places around the codebase where the `currentAccount`
from `sessionStore` is used.
- The internal implementation of this hook will eventually migrate to
use `react-query`, keeping the hook's signature.
- This implementation can be used as a base for the migration of the
other existing stores to allow for future dropping of `mob-x` in favor
of using react-query to cache data.
- First usage of this hook can be seen in
#5290
  • Loading branch information
pauloamorimbr authored Nov 27, 2024
1 parent 4f5af46 commit b8184e8
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions jsapp/js/stores/useSession.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import sessionStore from './session';
import {useEffect, useState} from 'react';
import {reaction} from 'mobx';
import type {AccountResponse} from '../dataInterface';

/**
* Hook to use the session store in functional components.
* This hook provides a way to access teh current logged account, information
* regarding the anonymous state of the login and session methods.
*
* This hook uses MobX reactions to track the current account and update the
* state accordingly.
* In the future we should update this hook to use react-query and drop the usage of mob-x
*/
export const useSession = () => {
const [currentLoggedAccount, setCurrentLoggedAccount] =
useState<AccountResponse>();
const [isAnonymous, setIsAnonymous] = useState<boolean>(true);
const [isPending, setIsPending] = useState<boolean>(false);

useEffect(() => {
// We need to setup a reaction for every observable we want to track
// Generic reaction to sessionStore won't fire the re-rendering of the hook
const currentAccountReactionDisposer = reaction(
() => sessionStore.currentAccount,
(currentAccount) => {
if (sessionStore.isLoggedIn) {
setCurrentLoggedAccount(currentAccount as AccountResponse);
setIsAnonymous(false);
setIsPending(sessionStore.isPending);
}
},
{fireImmediately: true}
);

return () => {
currentAccountReactionDisposer();
};
}, []);

return {
currentLoggedAccount,
isAnonymous,
isPending,
logOut: sessionStore.logOut.bind(sessionStore),
logOutAll: sessionStore.logOutAll.bind(sessionStore),
refreshAccount: sessionStore.refreshAccount.bind(sessionStore),
};
};

0 comments on commit b8184e8

Please sign in to comment.