File size: 4,919 Bytes
b64de39
 
 
 
 
fc91a5e
4995d62
b64de39
 
 
 
 
75fda81
b64de39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75fda81
b64de39
75fda81
b64de39
 
 
75fda81
 
 
 
b64de39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75fda81
 
 
 
b64de39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* eslint-disable react-refresh/only-export-components */
/**
 * AppContext - Global application state.
 *
 * Manages:
 *  - Language preference (English / Hindi / Gujarati) with persistence
 *  - User preferences (notifications, etc.)
 *  - Global loading state
 *  - Global error state
 */

import { createContext, useReducer, useEffect, useCallback } from "react";
import { useTranslation } from "react-i18next";
import storage from "@utils/storage";
import {
  STORAGE_KEYS,
  LANGUAGES,
  DEFAULT_LANGUAGE,
} from "@utils/constants";

// ---------------------------------------------------------------------------
// Initial State
// ---------------------------------------------------------------------------

function loadPersistedPreferences() {
  return storage.get(STORAGE_KEYS.USER_PREFERENCES, {
    notifications: true,
  });
}

function buildInitialState() {
  return {
    language: storage.get(STORAGE_KEYS.LANGUAGE, DEFAULT_LANGUAGE),
    preferences: loadPersistedPreferences(),
    loading: false,
    loadingMessage: "",
    error: null,
  };
}

// ---------------------------------------------------------------------------
// Action Types
// ---------------------------------------------------------------------------

const ACTION_TYPES = {
  SET_LANGUAGE: "SET_LANGUAGE",
  SET_PREFERENCES: "SET_PREFERENCES",
  SET_LOADING: "SET_LOADING",
  SET_ERROR: "SET_ERROR",
  CLEAR_ERROR: "CLEAR_ERROR",
  RESET: "RESET",
};

// ---------------------------------------------------------------------------
// Reducer
// ---------------------------------------------------------------------------

function appReducer(state, action) {
  switch (action.type) {
    case ACTION_TYPES.SET_LANGUAGE:
      return { ...state, language: action.payload };

    case ACTION_TYPES.SET_PREFERENCES:
      return {
        ...state,
        preferences: { ...state.preferences, ...action.payload },
      };

    case ACTION_TYPES.SET_LOADING:
      return {
        ...state,
        loading: action.payload.loading,
        loadingMessage: action.payload.message || "",
      };

    case ACTION_TYPES.SET_ERROR:
      return { ...state, error: action.payload, loading: false };

    case ACTION_TYPES.CLEAR_ERROR:
      return { ...state, error: null };

    case ACTION_TYPES.RESET:
      return buildInitialState();

    default:
      return state;
  }
}

// ---------------------------------------------------------------------------
// Context
// ---------------------------------------------------------------------------

export const AppContext = createContext(null);

// ---------------------------------------------------------------------------
// Provider
// ---------------------------------------------------------------------------

export function AppProvider({ children }) {
  const [state, dispatch] = useReducer(appReducer, null, buildInitialState);
  const { i18n } = useTranslation();

  // Persist language and sync with i18n whenever it changes
  useEffect(() => {
    storage.set(STORAGE_KEYS.LANGUAGE, state.language);
    document.documentElement.lang = state.language;
    if (i18n.language !== state.language) {
      i18n.changeLanguage(state.language);
    }
  }, [state.language, i18n]);

  // Persist preferences whenever they change
  useEffect(() => {
    storage.set(STORAGE_KEYS.USER_PREFERENCES, state.preferences);
  }, [state.preferences]);

  // --- Actions ---------------------------------------------------------------

  const setLanguage = useCallback((lang) => {
    const validLangs = Object.values(LANGUAGES);
    if (!validLangs.includes(lang)) {
      console.error(
        `[AppContext] Invalid language "${lang}". Expected one of: ${validLangs.join(", ")}`,
      );
      return;
    }
    dispatch({ type: ACTION_TYPES.SET_LANGUAGE, payload: lang });
  }, []);

  const actions = {
    setLanguage,

    toggleLanguage: () => {
      const langs = [LANGUAGES.EN, LANGUAGES.HI, LANGUAGES.GU];
      const currentIndex = langs.indexOf(state.language);
      const nextIndex = (currentIndex + 1) % langs.length;
      dispatch({ type: ACTION_TYPES.SET_LANGUAGE, payload: langs[nextIndex] });
    },

    setPreferences: (prefs) => {
      dispatch({ type: ACTION_TYPES.SET_PREFERENCES, payload: prefs });
    },

    setLoading: (loading, message = "") => {
      dispatch({
        type: ACTION_TYPES.SET_LOADING,
        payload: { loading, message },
      });
    },

    setError: (error) => {
      dispatch({ type: ACTION_TYPES.SET_ERROR, payload: error });
    },

    clearError: () => {
      dispatch({ type: ACTION_TYPES.CLEAR_ERROR });
    },

    resetState: () => {
      storage.remove(STORAGE_KEYS.LANGUAGE);
      storage.remove(STORAGE_KEYS.USER_PREFERENCES);
      dispatch({ type: ACTION_TYPES.RESET });
    },
  };

  const value = { ...state, ...actions };

  return <AppContext.Provider value={value}>{children}</AppContext.Provider>;
}