You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

58 lines
2.0 KiB

  1. /*
  2. Copyright (C) 2025 QuantumNous
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. For commercial licensing, please contact support@quantumnous.com
  14. */
  15. import React, { useEffect } from 'react';
  16. import { useTranslation } from 'react-i18next';
  17. import { reducer, initialState } from './reducer';
  18. import { StatusContext } from '../Status';
  19. export const UserContext = React.createContext({
  20. state: initialState,
  21. dispatch: () => null,
  22. });
  23. export const UserProvider = ({ children }) => {
  24. const [state, dispatch] = React.useReducer(reducer, initialState);
  25. const { i18n } = useTranslation();
  26. const [statusState] = React.useContext(StatusContext);
  27. // Sync language preference when user data is loaded
  28. useEffect(() => {
  29. if (state.user?.setting) {
  30. try {
  31. const settings = JSON.parse(state.user.setting);
  32. if (settings.language && settings.language !== i18n.language) {
  33. i18n.changeLanguage(settings.language);
  34. } else if (!settings.language && statusState.status?.default_language) {
  35. // No personal preference — fall back to admin default
  36. i18n.changeLanguage(statusState.status.default_language);
  37. }
  38. } catch (e) {
  39. // Ignore parse errors
  40. }
  41. }
  42. }, [state.user?.setting, statusState.status?.default_language, i18n]);
  43. return (
  44. <UserContext.Provider value={[state, dispatch]}>
  45. {children}
  46. </UserContext.Provider>
  47. );
  48. };