| |
| |
| |
| |
| |
| |
| import { create } from 'zustand'; |
| import { immer } from 'zustand/middleware/immer'; |
|
|
| |
|
|
| interface LocationState { |
| |
| currentLocation: { lat: number; lng: number } | null; |
| |
| locationAddress: string | null; |
| |
| locationError: string | null; |
| |
| isTracking: boolean; |
| } |
|
|
| |
|
|
| interface LocationActions { |
| |
| setLocation: (lat: number, lng: number) => void; |
| |
| setAddress: (address: string | null) => void; |
| |
| setError: (error: string | null) => void; |
| |
| clearLocation: () => void; |
| |
| setTracking: (value: boolean) => void; |
| } |
|
|
| |
|
|
| export const useLocationStore = create<LocationState & LocationActions>()( |
| immer((set) => ({ |
| |
| currentLocation: null, |
| locationAddress: null, |
| locationError: null, |
| isTracking: false, |
|
|
| |
| setLocation: (lat, lng) => |
| set((state) => { |
| state.currentLocation = { lat, lng }; |
| state.locationError = null; |
| }), |
|
|
| setAddress: (address) => |
| set((state) => { |
| state.locationAddress = address; |
| }), |
|
|
| setError: (error) => |
| set((state) => { |
| state.locationError = error; |
| }), |
|
|
| clearLocation: () => |
| set((state) => { |
| state.currentLocation = null; |
| state.locationAddress = null; |
| state.locationError = null; |
| state.isTracking = false; |
| }), |
|
|
| setTracking: (value) => |
| set((state) => { |
| state.isTracking = value; |
| }), |
| })), |
| ); |
|
|
| |
|
|
| export const selectHasLocation = (state: LocationState) => |
| state.currentLocation !== null; |
|
|
| export const selectFormattedLocation = (state: LocationState) => { |
| if (!state.currentLocation) return null; |
| const { lat, lng } = state.currentLocation; |
| return { |
| lat, |
| lng, |
| address: state.locationAddress ?? `${lat.toFixed(6)}, ${lng.toFixed(6)}`, |
| }; |
| }; |
|
|