# All files / src/contexts PreferencesContext.tsx

100% Statements 42/42
100% Branches 8/8
100% Functions 3/3
100% Lines 42/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    711x

    1x
    1x
    1x
    1x
    1x
    1x

    1x

    1x
    14x

    14x
    8x
    8x
    2x
    2x
    2x
    2x
    1x
    1x
    2x
    14x

    14x
    5x
    5x
    5x
    5x
    5x
    5x
    5x

    14x

    14x
    14x
    14x
    14x

    14x

    1x
    19x
    19x
    4x
    4x
    14x
    14x
     import React, { createContext, useContext, useState, useEffect, ReactNode } from 'react';

    export interface UserPreferences {
      oddsFormat: 'american' | 'decimal' | 'fractional';
      defaultSport: string;
      defaultStake: number;
      preferredBookmakers: string[];
    }

    interface PreferencesContextType {
      preferences: UserPreferences;
      updatePreference: <K extends keyof UserPreferences>(
        key: K,
        value: UserPreferences[K]
      ) => void;
      useDecimalOdds: boolean;
    }

    const DEFAULT_PREFERENCES: UserPreferences = {
      oddsFormat: 'american',
      defaultSport: 'all',
      defaultStake: 100,
      preferredBookmakers: []
    };

    const PreferencesContext = createContext<PreferencesContextType | undefined>(undefined);

    export function PreferencesProvider({ children }: { children: ReactNode }) {
      const [preferences, setPreferences] = useState<UserPreferences>(DEFAULT_PREFERENCES);

      // Load preferences from localStorage on mount
      useEffect(() => {
        const saved = localStorage.getItem('userPreferences');
        if (saved) {
          try {
            const parsed = JSON.parse(saved);
            setPreferences({ ...DEFAULT_PREFERENCES, ...parsed });
          } catch (error) {
            console.error('Failed to load preferences:', error);
          }
        }
      }, []);

      // Save to localStorage whenever preferences change
      const updatePreference = <K extends keyof UserPreferences>(
        key: K,
        value: UserPreferences[K]
      ) => {
        const newPreferences = { ...preferences, [key]: value };
        setPreferences(newPreferences);
        localStorage.setItem('userPreferences', JSON.stringify(newPreferences));
      };

      // Computed value for decimal odds
      const useDecimalOdds = preferences.oddsFormat === 'decimal';

      return (
        <PreferencesContext.Provider value={{ preferences, updatePreference, useDecimalOdds }}>
          {children}
        </PreferencesContext.Provider>
      );
    }

    export function usePreferences() {
      const context = useContext(PreferencesContext);
      if (context === undefined) {
        throw new Error('usePreferences must be used within a PreferencesProvider');
      }
      return context;
    }