From 7a1ae312868de69b041fe6659eb3c986d3902048 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20An=C3=ADcio?= Date: Thu, 2 Oct 2025 11:36:17 -0300 Subject: [PATCH] BA-2778: load client cookies as initial data --- packages/authentication/CHANGELOG.md | 8 ++++ .../profile/useCurrentProfile/store.ts | 25 ++++++++--- packages/authentication/package.json | 2 +- packages/components/CHANGELOG.md | 10 +++++ packages/components/package.json | 2 +- packages/design-system/CHANGELOG.md | 8 ++++ .../hooks/web/useUISettings/index.tsx | 5 +-- .../hooks/web/useUISettings/store.ts | 42 +++++++++++++++---- packages/design-system/package.json | 2 +- packages/graphql/CHANGELOG.md | 8 ++++ packages/graphql/package.json | 2 +- packages/provider/CHANGELOG.md | 7 ++++ packages/provider/package.json | 2 +- packages/utils/CHANGELOG.md | 6 +++ packages/utils/hooks/useCookie/store.ts | 17 ++++++-- packages/utils/package.json | 2 +- packages/wagtail/CHANGELOG.md | 9 ++++ packages/wagtail/package.json | 2 +- 18 files changed, 133 insertions(+), 26 deletions(-) diff --git a/packages/authentication/CHANGELOG.md b/packages/authentication/CHANGELOG.md index ad9488f4..67537620 100644 --- a/packages/authentication/CHANGELOG.md +++ b/packages/authentication/CHANGELOG.md @@ -1,5 +1,13 @@ # @baseapp-frontend/authentication +## 5.0.4 + +### Patch Changes + +- Enhanced current profile store to automatically load profile from client-side cookies when no initial profile is provided +- Updated dependencies + - @baseapp-frontend/utils@4.0.3 + ## 5.0.3 ### Patch Changes diff --git a/packages/authentication/modules/profile/useCurrentProfile/store.ts b/packages/authentication/modules/profile/useCurrentProfile/store.ts index 7b439245..c67d5167 100644 --- a/packages/authentication/modules/profile/useCurrentProfile/store.ts +++ b/packages/authentication/modules/profile/useCurrentProfile/store.ts @@ -7,11 +7,26 @@ import type { CurrentProfileState } from './types' let profileStore: StoreApi | null = null +const getClientSideCurrentProfile = (): MinimalProfile | null => { + const storedProfile = Cookies.get(CURRENT_PROFILE_KEY_NAME) + if (storedProfile) { + try { + return JSON.parse(storedProfile) + } catch { + return null + } + } + return null +} + const createProfileStore = ( - initialProfile: MinimalProfile | null = null, -): StoreApi => - createStore()((set) => ({ - currentProfile: initialProfile, + initialProfile?: MinimalProfile | null, +): StoreApi => { + // If no initialProfile provided, try to get it from client-side cookies + const profileToUse = initialProfile || getClientSideCurrentProfile() + + return createStore()((set) => ({ + currentProfile: profileToUse, setCurrentProfile: (profile: MinimalProfile | null) => { Cookies.set(CURRENT_PROFILE_KEY_NAME, JSON.stringify(profile)) set(() => ({ currentProfile: profile })) @@ -26,6 +41,7 @@ const createProfileStore = ( }) }, })) +} export const initializeProfileStore = ( initialProfile: MinimalProfile | null = null, @@ -37,7 +53,6 @@ export const initializeProfileStore = ( (initialProfile && Object.keys(initialProfile).length > 0) ) { profileStore = createProfileStore(initialProfile) - return profileStore } return profileStore diff --git a/packages/authentication/package.json b/packages/authentication/package.json index 8ac9a24c..53254db2 100644 --- a/packages/authentication/package.json +++ b/packages/authentication/package.json @@ -1,7 +1,7 @@ { "name": "@baseapp-frontend/authentication", "description": "Authentication modules.", - "version": "5.0.3", + "version": "5.0.4", "main": "./index.ts", "types": "dist/index.d.ts", "sideEffects": false, diff --git a/packages/components/CHANGELOG.md b/packages/components/CHANGELOG.md index 4e3d6bfd..9d9b221b 100644 --- a/packages/components/CHANGELOG.md +++ b/packages/components/CHANGELOG.md @@ -1,5 +1,15 @@ # @baseapp-frontend/components +## 1.4.4 + +### Patch Changes + +- Updated dependencies + - @baseapp-frontend/authentication@5.0.4 + - @baseapp-frontend/design-system@1.1.2 + - @baseapp-frontend/utils@4.0.3 + - @baseapp-frontend/graphql@1.3.5 + ## 1.4.3 ### Patch Changes diff --git a/packages/components/package.json b/packages/components/package.json index dd3cec74..e1922216 100644 --- a/packages/components/package.json +++ b/packages/components/package.json @@ -1,7 +1,7 @@ { "name": "@baseapp-frontend/components", "description": "BaseApp components modules such as comments, notifications, messages, and more.", - "version": "1.4.3", + "version": "1.4.4", "sideEffects": false, "scripts": { "build": "rm -rf dist && pnpm relay && tsc --build tsconfig.build.json", diff --git a/packages/design-system/CHANGELOG.md b/packages/design-system/CHANGELOG.md index 63509dd5..80d0a9e7 100644 --- a/packages/design-system/CHANGELOG.md +++ b/packages/design-system/CHANGELOG.md @@ -1,5 +1,13 @@ # @baseapp-frontend/design-system +## 1.1.2 + +### Patch Changes + +- Enhanced UI settings store to automatically load settings from client-side cookies when no initial settings are provided +- Updated dependencies + - @baseapp-frontend/utils@4.0.3 + ## 1.1.1 ### Patch Changes diff --git a/packages/design-system/hooks/web/useUISettings/index.tsx b/packages/design-system/hooks/web/useUISettings/index.tsx index 1d597b24..96493dde 100644 --- a/packages/design-system/hooks/web/useUISettings/index.tsx +++ b/packages/design-system/hooks/web/useUISettings/index.tsx @@ -5,7 +5,7 @@ import { createContext, useContext, useEffect, useRef } from 'react' import { type StoreApi, useStore } from 'zustand' import { Palette, PresetType, getPresetOptions } from '../../../styles/web' -import { DEFAULT_UI_SETTINGS, MISSING_UI_SETTINGS_STORE_ERROR } from './constants' +import { MISSING_UI_SETTINGS_STORE_ERROR } from './constants' import { initializeSettingsStore } from './store' import type { UISettingsProviderProps, UISettingsState } from './types' @@ -35,8 +35,7 @@ export const UISettingsProvider = ({ const storeRef = useRef>(undefined) if (!storeRef.current) { - const initialSettings = initialUISettings || DEFAULT_UI_SETTINGS - storeRef.current = initializeSettingsStore(initialSettings) + storeRef.current = initializeSettingsStore(initialUISettings) } const store = storeRef.current diff --git a/packages/design-system/hooks/web/useUISettings/store.ts b/packages/design-system/hooks/web/useUISettings/store.ts index 0a3b35a6..df22eb4d 100644 --- a/packages/design-system/hooks/web/useUISettings/store.ts +++ b/packages/design-system/hooks/web/useUISettings/store.ts @@ -2,7 +2,11 @@ import Cookies from 'js-cookie' import { type StoreApi, createStore } from 'zustand' import { UISettings } from '../../../styles/web' -import { MISSING_UI_SETTINGS_STORE_ERROR, UI_SETTINGS_KEY_NAME } from './constants' +import { + DEFAULT_UI_SETTINGS, + MISSING_UI_SETTINGS_STORE_ERROR, + UI_SETTINGS_KEY_NAME, +} from './constants' import type { UISettingsState } from './types' const handleTailwindThemeMode = (themeMode: string) => { @@ -13,9 +17,25 @@ const handleTailwindThemeMode = (themeMode: string) => { let settingsStore: StoreApi | null = null -const createSettingsStore = (initialSettings: UISettings): StoreApi => - createStore()((set) => ({ - settings: initialSettings, +const getClientSideUISettings = (initialSettings?: UISettings): UISettings => { + const storedSettings = Cookies.get(UI_SETTINGS_KEY_NAME) + const defaultSettings = { ...DEFAULT_UI_SETTINGS, ...initialSettings } + + if (storedSettings) { + try { + return { ...defaultSettings, ...JSON.parse(storedSettings) } + } catch { + return defaultSettings + } + } + return defaultSettings +} + +const createSettingsStore = (initialSettings?: UISettings): StoreApi => { + const settingsToUse = getClientSideUISettings(initialSettings) + + return createStore()((set) => ({ + settings: settingsToUse, setSettings: (newSettings: Partial) => set((state) => { const settings = { ...state.settings, ...newSettings } @@ -26,11 +46,17 @@ const createSettingsStore = (initialSettings: UISettings): StoreApi => { - // Create a new store in dev mode to prevent HMR from preserving stale data - if (process.env.NODE_ENV === 'development' || !settingsStore) { - return createSettingsStore(initialSettings) +export const initializeSettingsStore = ( + initialSettings?: UISettings, +): StoreApi => { + if ( + // Create a new store in dev mode to prevent HMR from preserving stale data + process.env.NODE_ENV === 'development' || + !settingsStore + ) { + settingsStore = createSettingsStore(initialSettings) } return settingsStore diff --git a/packages/design-system/package.json b/packages/design-system/package.json index 7f9f3e56..0f9ed6c3 100644 --- a/packages/design-system/package.json +++ b/packages/design-system/package.json @@ -1,7 +1,7 @@ { "name": "@baseapp-frontend/design-system", "description": "Design System components and configurations.", - "version": "1.1.1", + "version": "1.1.2", "sideEffects": false, "scripts": { "build": "rm -rf dist && tsc --build tsconfig.build.json", diff --git a/packages/graphql/CHANGELOG.md b/packages/graphql/CHANGELOG.md index 7d06b907..cb24c29f 100644 --- a/packages/graphql/CHANGELOG.md +++ b/packages/graphql/CHANGELOG.md @@ -1,5 +1,13 @@ # @baseapp-frontend/graphql +## 1.3.5 + +### Patch Changes + +- Updated dependencies + - @baseapp-frontend/authentication@5.0.4 + - @baseapp-frontend/utils@4.0.3 + ## 1.3.4 ### Patch Changes diff --git a/packages/graphql/package.json b/packages/graphql/package.json index c0563d1c..bfa05049 100644 --- a/packages/graphql/package.json +++ b/packages/graphql/package.json @@ -1,7 +1,7 @@ { "name": "@baseapp-frontend/graphql", "description": "GraphQL configurations and utilities", - "version": "1.3.4", + "version": "1.3.5", "main": "./index.ts", "types": "dist/index.d.ts", "sideEffects": false, diff --git a/packages/provider/CHANGELOG.md b/packages/provider/CHANGELOG.md index 13449590..e64c5bdf 100644 --- a/packages/provider/CHANGELOG.md +++ b/packages/provider/CHANGELOG.md @@ -1,5 +1,12 @@ # @baseapp-frontend/provider +## 2.0.17 + +### Patch Changes + +- Updated dependencies + - @baseapp-frontend/utils@4.0.3 + ## 2.0.16 ### Patch Changes diff --git a/packages/provider/package.json b/packages/provider/package.json index 2dcb4e4a..bc6c781c 100644 --- a/packages/provider/package.json +++ b/packages/provider/package.json @@ -1,7 +1,7 @@ { "name": "@baseapp-frontend/provider", "description": "Providers for React Query and Emotion.", - "version": "2.0.16", + "version": "2.0.17", "main": "./index.ts", "types": "dist/index.d.ts", "sideEffects": false, diff --git a/packages/utils/CHANGELOG.md b/packages/utils/CHANGELOG.md index 79c5f26a..693b1a7a 100644 --- a/packages/utils/CHANGELOG.md +++ b/packages/utils/CHANGELOG.md @@ -1,5 +1,11 @@ # @baseapp-frontend/utils +## 4.0.3 + +### Patch Changes + +- Enhanced cookie store to automatically load cookies from client-side when no initial cookies are provided + ## 4.0.2 ### Patch Changes diff --git a/packages/utils/hooks/useCookie/store.ts b/packages/utils/hooks/useCookie/store.ts index fd57781d..1b4558c6 100644 --- a/packages/utils/hooks/useCookie/store.ts +++ b/packages/utils/hooks/useCookie/store.ts @@ -1,15 +1,25 @@ +import Cookies from 'js-cookie' import { type StoreApi, createStore } from 'zustand' +import { ACCESS_KEY_NAME, REFRESH_KEY_NAME } from '../../constants/jwt' import { MISSING_COOKIE_STORE_ERROR } from './constants' import type { BaseCookies, CookieState } from './types' let cookieStore: StoreApi | null = null +const getClientSideCookies = (): BaseCookies => ({ + [ACCESS_KEY_NAME]: Cookies.get(ACCESS_KEY_NAME), + [REFRESH_KEY_NAME]: Cookies.get(REFRESH_KEY_NAME), +}) + const createCookieStore = = {}>( initialCookies?: BaseCookies & T, -): StoreApi> => - createStore>()((set) => ({ - cookies: initialCookies, +): StoreApi> => { + // If no initialCookies provided, try to get them from client-side cookies + const cookiesToUse = initialCookies || (getClientSideCookies() as BaseCookies & T) + + return createStore>()((set) => ({ + cookies: cookiesToUse, setCookie: (key: string, value: string) => set((state) => ({ cookies: { ...state.cookies, [key]: value } as BaseCookies & T, @@ -21,6 +31,7 @@ const createCookieStore = = {}>( return { cookies: newCookies as BaseCookies & T } }), })) +} export const initializeCookieStore = = {}>( initialCookies?: BaseCookies & T, diff --git a/packages/utils/package.json b/packages/utils/package.json index 062ca460..a39b3912 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -1,7 +1,7 @@ { "name": "@baseapp-frontend/utils", "description": "Util functions, constants and types.", - "version": "4.0.2", + "version": "4.0.3", "main": "./index.ts", "types": "dist/index.d.ts", "sideEffects": false, diff --git a/packages/wagtail/CHANGELOG.md b/packages/wagtail/CHANGELOG.md index d6381526..1b417e6e 100644 --- a/packages/wagtail/CHANGELOG.md +++ b/packages/wagtail/CHANGELOG.md @@ -1,5 +1,14 @@ # @baseapp-frontend/wagtail +## 1.0.39 + +### Patch Changes + +- Updated dependencies + - @baseapp-frontend/design-system@1.1.2 + - @baseapp-frontend/utils@4.0.3 + - @baseapp-frontend/graphql@1.3.5 + ## 1.0.38 ### Patch Changes diff --git a/packages/wagtail/package.json b/packages/wagtail/package.json index 82af95f1..5bd41827 100644 --- a/packages/wagtail/package.json +++ b/packages/wagtail/package.json @@ -1,7 +1,7 @@ { "name": "@baseapp-frontend/wagtail", "description": "BaseApp Wagtail", - "version": "1.0.38", + "version": "1.0.39", "main": "./index.ts", "types": "dist/index.d.ts", "sideEffects": false,