aboutsummaryrefslogtreecommitdiff
path: root/src/AppContext.tsx
blob: a9af2086e64f6a25eaaab4fe86a2a0b69333ddf5 (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* eslint-disable react-refresh/only-export-components */
import { createContext, useContext, useEffect, useState, ReactNode } from 'react';
import { LatLngTuple } from 'leaflet';

type Theme = 'light' | 'dark';
type TableStyle = 'regular'|'grouped';

interface MapState {
  center: LatLngTuple;
  zoom: number;
  userLocation: LatLngTuple | null;
  hasLocationPermission: boolean;
}

interface AppContextProps {
  theme: Theme;
  setTheme: React.Dispatch<React.SetStateAction<Theme>>;
  toggleTheme: () => void;
  
  tableStyle: TableStyle;
  setTableStyle: React.Dispatch<React.SetStateAction<TableStyle>>;
  toggleTableStyle: () => void;

  mapState: MapState;
  setMapCenter: (center: LatLngTuple) => void;
  setMapZoom: (zoom: number) => void;
  setUserLocation: (location: LatLngTuple | null) => void;
  setLocationPermission: (hasPermission: boolean) => void;
  updateMapState: (center: LatLngTuple, zoom: number) => void;
}

// Coordenadas por defecto centradas en Vigo
const DEFAULT_CENTER: LatLngTuple = [42.229188855975046, -8.72246955783102];
const DEFAULT_ZOOM = 14;

const AppContext = createContext<AppContextProps | undefined>(undefined);

export const AppProvider = ({ children }: { children: ReactNode }) => {
  //#region Theme
  const [theme, setTheme] = useState<Theme>(() => {
    const savedTheme = localStorage.getItem('theme');
    if (savedTheme) {
      return savedTheme as Theme;
    }
    const prefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
    return prefersDark ? 'dark' : 'light';
  });

  const toggleTheme = () => {
    setTheme((prevTheme) => (prevTheme === 'light' ? 'dark' : 'light'));
  };

  useEffect(() => {
    document.documentElement.setAttribute('data-theme', theme);
    localStorage.setItem('theme', theme);
  }, [theme]);
  //#endregion

  //#region Table Style
  const [tableStyle, setTableStyle] = useState<TableStyle>(() => {
    const savedTableStyle = localStorage.getItem('tableStyle');
    if (savedTableStyle) {
      return savedTableStyle as TableStyle;
    }
    return 'regular';
  });

  const toggleTableStyle = () => {
    setTableStyle((prevTableStyle) => (prevTableStyle === 'regular' ? 'grouped' : 'regular'));
  }

  useEffect(() => {
    localStorage.setItem('tableStyle', tableStyle);
  }, [tableStyle]);
  //#endregion

  //#region Map State
  const [mapState, setMapState] = useState<MapState>(() => {
    const savedMapState = localStorage.getItem('mapState');
    if (savedMapState) {
      try {
        const parsed = JSON.parse(savedMapState);
        return {
          center: parsed.center || DEFAULT_CENTER,
          zoom: parsed.zoom || DEFAULT_ZOOM,
          userLocation: parsed.userLocation || null,
          hasLocationPermission: parsed.hasLocationPermission || false
        };
      } catch (e) {
        console.error('Error parsing saved map state', e);
      }
    }
    return {
      center: DEFAULT_CENTER,
      zoom: DEFAULT_ZOOM,
      userLocation: null,
      hasLocationPermission: false
    };
  });

  const setMapCenter = (center: LatLngTuple) => {
    setMapState(prev => {
      const newState = { ...prev, center };
      localStorage.setItem('mapState', JSON.stringify(newState));
      return newState;
    });
  };

  const setMapZoom = (zoom: number) => {
    setMapState(prev => {
      const newState = { ...prev, zoom };
      localStorage.setItem('mapState', JSON.stringify(newState));
      return newState;
    });
  };

  const setUserLocation = (userLocation: LatLngTuple | null) => {
    setMapState(prev => {
      const newState = { ...prev, userLocation };
      localStorage.setItem('mapState', JSON.stringify(newState));
      return newState;
    });
  };

  const setLocationPermission = (hasLocationPermission: boolean) => {
    setMapState(prev => {
      const newState = { ...prev, hasLocationPermission };
      localStorage.setItem('mapState', JSON.stringify(newState));
      return newState;
    });
  };

  const updateMapState = (center: LatLngTuple, zoom: number) => {
    setMapState(prev => {
      const newState = { ...prev, center, zoom };
      localStorage.setItem('mapState', JSON.stringify(newState));
      return newState;
    });
  };
  //#endregion

  // Tratar de obtener la ubicación del usuario cuando se carga la aplicación si ya se había concedido permiso antes
  useEffect(() => {
    if (mapState.hasLocationPermission && !mapState.userLocation) {
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(
          (position) => {
            const { latitude, longitude } = position.coords;
            setUserLocation([latitude, longitude]);
          },
          (error) => {
            console.error('Error getting location:', error);
            setLocationPermission(false);
          }
        );
      }
    }
  }, [mapState.hasLocationPermission, mapState.userLocation]);

  return (
    <AppContext.Provider value={{ 
      theme, 
      setTheme, 
      toggleTheme, 
      tableStyle, 
      setTableStyle, 
      toggleTableStyle,
      mapState,
      setMapCenter,
      setMapZoom,
      setUserLocation,
      setLocationPermission,
      updateMapState
    }}>
      {children}
    </AppContext.Provider>
  );
};

export const useApp = () => {
  const context = useContext(AppContext);
  if (!context) {
    throw new Error('useApp must be used within a AppProvider');
  }
  return context;
};