Skip to content

Commit

Permalink
🐛 Fix forced color mode localStorage sync issue
Browse files Browse the repository at this point in the history
Described the issue here: chakra-ui/chakra-ui#7980
  • Loading branch information
baptisteArno authored and jmgoncalves97 committed Jan 17, 2025
1 parent a259c8e commit 441471e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
17 changes: 17 additions & 0 deletions apps/builder/src/features/account/UserProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { updateUserQuery } from './queries/updateUserQuery'
import { useDebouncedCallback } from 'use-debounce'
import { env } from '@typebot.io/env'
import { identifyUser } from '../telemetry/posthog'
import { useColorMode } from '@chakra-ui/react'

export const userContext = createContext<{
user?: User
Expand All @@ -30,6 +31,22 @@ export const UserProvider = ({ children }: { children: ReactNode }) => {
const [user, setUser] = useState<User | undefined>()
const { showToast } = useToast()
const [currentWorkspaceId, setCurrentWorkspaceId] = useState<string>()
const { setColorMode } = useColorMode()

useEffect(() => {
if (
!user?.preferredAppAppearance ||
user.preferredAppAppearance === 'system'
)
return
const currentColorScheme = localStorage.getItem('chakra-ui-color-mode') as
| 'light'
| 'dark'
| null
if (currentColorScheme === user.preferredAppAppearance) return
console.log('SET')
setColorMode(user.preferredAppAppearance)
}, [setColorMode, user?.preferredAppAppearance])

useEffect(() => {
if (isDefined(user) || isNotDefined(session)) return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ export const UserPreferencesForm = () => {
}

const changeAppearance = async (value: string) => {
if (value === 'system') {
setColorMode('system')
localStorage.removeItem('chakra-ui-color-mode')
} else setColorMode(value)
setColorMode(value)
updateUser({ preferredAppAppearance: value })
}

Expand Down

0 comments on commit 441471e

Please sign in to comment.