mirror of
https://github.com/suitenumerique/meet.git
synced 2026-08-14 20:53:26 +00:00
ec114808b2
Implement new hook that synchronizes user language and timezone preferences from frontend to backend. Ensures consistent localization across and notifications. Note: Current implementation works but auth code requires future refactoring.
48 lines
1.5 KiB
TypeScript
48 lines
1.5 KiB
TypeScript
import { useMutation } from '@tanstack/react-query'
|
|
import { keys } from '@/api/queryKeys'
|
|
import { useEffect } from 'react'
|
|
import { useTranslation } from 'react-i18next'
|
|
import { queryClient } from '@/api/queryClient'
|
|
import { updateUserPreferences } from './updateUserPreferences'
|
|
import { convertToBackendLanguage } from '@/utils/languages'
|
|
import { useUser } from './useUser'
|
|
|
|
/**
|
|
* Hook that synchronizes user browser preferences (language, timezone) with backend user settings.
|
|
* Automatically updates backend when browser settings change for logged-in users.
|
|
*/
|
|
export const useSyncUserPreferencesWithBackend = () => {
|
|
const { i18n } = useTranslation()
|
|
const { user, isLoggedIn } = useUser()
|
|
|
|
const { mutateAsync } = useMutation({
|
|
mutationFn: updateUserPreferences,
|
|
onSuccess: (updatedUser) => {
|
|
queryClient.setQueryData([keys.user], updatedUser)
|
|
},
|
|
})
|
|
|
|
useEffect(() => {
|
|
if (!user || !isLoggedIn) return
|
|
|
|
const syncBrowserPreferencesToBackend = async () => {
|
|
const currentTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone
|
|
const currentLanguage = convertToBackendLanguage(i18n.language)
|
|
if (
|
|
currentLanguage !== user.language ||
|
|
currentTimezone !== user.timezone
|
|
) {
|
|
await mutateAsync({
|
|
user: {
|
|
id: user.id,
|
|
timezone: currentTimezone,
|
|
language: currentLanguage,
|
|
},
|
|
})
|
|
}
|
|
}
|
|
|
|
syncBrowserPreferencesToBackend()
|
|
}, [i18n.language, isLoggedIn, user, mutateAsync])
|
|
}
|