🐛 Fix forced color mode localStorage sync issue
Described the issue here: https://github.com/chakra-ui/chakra-ui/discussions/7980
This commit is contained in:
@@ -9,6 +9,7 @@ import { updateUserQuery } from './queries/updateUserQuery'
|
|||||||
import { useDebouncedCallback } from 'use-debounce'
|
import { useDebouncedCallback } from 'use-debounce'
|
||||||
import { env } from '@typebot.io/env'
|
import { env } from '@typebot.io/env'
|
||||||
import { identifyUser } from '../telemetry/posthog'
|
import { identifyUser } from '../telemetry/posthog'
|
||||||
|
import { useColorMode } from '@chakra-ui/react'
|
||||||
|
|
||||||
export const userContext = createContext<{
|
export const userContext = createContext<{
|
||||||
user?: User
|
user?: User
|
||||||
@@ -30,6 +31,22 @@ export const UserProvider = ({ children }: { children: ReactNode }) => {
|
|||||||
const [user, setUser] = useState<User | undefined>()
|
const [user, setUser] = useState<User | undefined>()
|
||||||
const { showToast } = useToast()
|
const { showToast } = useToast()
|
||||||
const [currentWorkspaceId, setCurrentWorkspaceId] = useState<string>()
|
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(() => {
|
useEffect(() => {
|
||||||
if (isDefined(user) || isNotDefined(session)) return
|
if (isDefined(user) || isNotDefined(session)) return
|
||||||
|
|||||||
@@ -42,10 +42,7 @@ export const UserPreferencesForm = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const changeAppearance = async (value: string) => {
|
const changeAppearance = async (value: string) => {
|
||||||
if (value === 'system') {
|
setColorMode(value)
|
||||||
setColorMode('system')
|
|
||||||
localStorage.removeItem('chakra-ui-color-mode')
|
|
||||||
} else setColorMode(value)
|
|
||||||
updateUser({ preferredAppAppearance: value })
|
updateUser({ preferredAppAppearance: value })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user