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

BA-hotfix: current profile fixes #152

Merged
merged 1 commit into from
Dec 10, 2024
Merged
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
9 changes: 9 additions & 0 deletions packages/authentication/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# @baseapp-frontend/authentication

## 4.0.4

### Patch Changes

- Make sure the log out listener is loaded only once on `useCurrentProfile`.
- Fix `useLogin` adding optional chaining to a problematic expression.
- Move `InitialProfileProviderForTesting` around.


## 4.0.3

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/authentication/modules/access/useLogin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const useLogin = ({
if (user) {
// TODO: handle the absolute image path on the backend
const baseUrl = process.env.NEXT_PUBLIC_API_BASE_URL?.replace('/v1', '')
const absoluteImagePath = user.profile.image ? `${baseUrl}${user.profile.image}` : null
const absoluteImagePath = user?.profile?.image ? `${baseUrl}${user.profile.image}` : null
setCurrentProfile({
...user.profile,
image: absoluteImagePath,
Expand Down
5 changes: 2 additions & 3 deletions packages/authentication/modules/profile/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export { default as useCurrentProfile } from './useCurrentProfile'
export { default as InitialProfileProviderForTesting } from './useCurrentProfile/InitialProfileProviderForTesting'
export * from './useCurrentProfile/constants'
export type { MinimalProfile } from '../../types/profile'

export { InitialProfileProvider } from './useCurrentProfile/__tests__/__utils__/InitialProfileProvider'
export type { InitialProfileProp } from './useCurrentProfile/__tests__/__utils__/InitialProfileProvider'
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { FC } from 'react'

import { useHydrateAtoms } from 'jotai/utils'

import { profileAtom } from '..'
import { InitialProfileProviderForTestingProps } from './types'

/**
* InitialProfileProviderForTesting component.
*
* This component is used to provide an initial profile state for testing purposes.
* It uses the `useHydrateAtoms` hook from `jotai/utils` to set the initial state of the `profileAtom`.
*
* @component
* @example
* ```tsx
* <JotaiProvider>
* <InitialProfileProviderForTesting initialProfile={props.initialProfile}>
* // Your component goes here, it is passed the initialProfile
* </InitialProfileProviderForTesting>
* </JotaiProvider>
* ```
*/
const InitialProfileProviderForTesting: FC<InitialProfileProviderForTestingProps> = ({
initialProfile,
children,
}) => {
useHydrateAtoms([[profileAtom, initialProfile]])
return children
}

export default InitialProfileProviderForTesting
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { type PropsWithChildren } from 'react'

import { type MinimalProfile } from '../../../../types/profile'

export interface InitialProfileProviderForTestingProps extends PropsWithChildren {
initialProfile: MinimalProfile | null
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@

export const profileAtom = atom<MinimalProfile | null>(initialProfile)

profileAtom.onMount = (setAtom) => {
const removeCurrentProfile = () => {
setAtom(null)
removeCookie(CURRENT_PROFILE_KEY)
}
eventEmitter.on(LOGOUT_EVENT, removeCurrentProfile)
return () => {
eventEmitter.off(LOGOUT_EVENT, removeCurrentProfile)
}
}

/**
* By using `useCurrentProfile` with the `noSSR` option set to `false`, causes Next.js to dynamically render the affected pages, instead of statically rendering them.
*/
Expand Down Expand Up @@ -54,14 +65,14 @@
}
}

const removeCurrentProfile = useCallback(() => setCurrentProfile(null), [])

Check warning on line 68 in packages/authentication/modules/profile/useCurrentProfile/index.ts

View workflow job for this annotation

GitHub Actions / Lint

React Hook useCallback has a missing dependency: 'setCurrentProfile'. Either include it or remove the dependency array

useEffect(() => {
eventEmitter.on(LOGOUT_EVENT, removeCurrentProfile)
return () => {
eventEmitter.off(LOGOUT_EVENT, removeCurrentProfile)
}
}, [])

Check warning on line 75 in packages/authentication/modules/profile/useCurrentProfile/index.ts

View workflow job for this annotation

GitHub Actions / Lint

React Hook useEffect has a missing dependency: 'removeCurrentProfile'. Either include it or remove the dependency array

if (isSSR) {
return {
Expand Down
2 changes: 1 addition & 1 deletion packages/authentication/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@baseapp-frontend/authentication",
"description": "Authentication modules.",
"version": "4.0.3",
"version": "4.0.4",
"main": "./index.ts",
"types": "dist/index.d.ts",
"sideEffects": false,
Expand Down
8 changes: 8 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @baseapp-frontend/components

## 0.0.25

### Patch Changes

- Use `InitialProfileProviderForTesting`.
- Updated dependencies
- @baseapp-frontend/authentication@4.0.4

## 0.0.24

### Patch Changes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { FC } from 'react'

import { User, useJWTUser } from '@baseapp-frontend/authentication'
import { JWTContent, joinWithSeparator } from '@baseapp-frontend/utils'
import { useJWTUser } from '@baseapp-frontend/authentication'
import { joinWithSeparator } from '@baseapp-frontend/utils'

import { Box, Typography } from '@mui/material'

const CurrentUser: FC = () => {
const { user } = useJWTUser<User & JWTContent>()
const { user } = useJWTUser()

if (!user) {
return null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FC } from 'react'

import { InitialProfileProp, InitialProfileProvider } from '@baseapp-frontend/authentication'
import { InitialProfileProviderForTesting } from '@baseapp-frontend/authentication'
import { ThemeProvider } from '@baseapp-frontend/design-system'
import { RelayTestProvider } from '@baseapp-frontend/graphql'
import { NotificationProvider } from '@baseapp-frontend/utils'
Expand All @@ -16,13 +16,9 @@ const queryClient = new QueryClient()

const withProviders =
(Component: FC<AccountPopoverProps>) =>
({
environment,
initialProfile,
...props
}: WithProvidersOptions & AccountPopoverProps & InitialProfileProp) => (
({ environment, initialProfile, ...props }: WithProvidersOptions & AccountPopoverProps) => (
<JotaiProvider>
<InitialProfileProvider initialProfile={initialProfile}>
<InitialProfileProviderForTesting initialProfile={initialProfile}>
<QueryClientProvider client={queryClient}>
<RelayTestProvider environment={environment}>
<ThemeProvider {...defaultTheme}>
Expand All @@ -32,7 +28,7 @@ const withProviders =
</ThemeProvider>
</RelayTestProvider>
</QueryClientProvider>
</InitialProfileProvider>
</InitialProfileProviderForTesting>
</JotaiProvider>
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { MinimalProfile } from '@baseapp-frontend/authentication'

import type { RelayMockEnvironment } from 'relay-test-utils/lib/RelayModernMockEnvironment'

export type WithProvidersOptions = {
environment: RelayMockEnvironment
initialProfile: MinimalProfile | null
}
2 changes: 1 addition & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@baseapp-frontend/components",
"description": "BaseApp components modules such as comments, notifications, messages, and more.",
"version": "0.0.24",
"version": "0.0.25",
"main": "./index.ts",
"types": "dist/index.d.ts",
"sideEffects": false,
Expand Down
Loading