import { DEFAULT_SERVICE, type ServiceId } from "@/constants/services"; import type { DriverStore, LocationStore, MarkerData } from "@/types/type"; import { create } from "zustand"; export const useLocationStore = create((set) => ({ userAddress: null, userLongitude: null, userLatitude: null, destinationLongitude: null, destinationLatitude: null, destinationAddress: null, setUserLocation: ({ latitude, longitude, address, }: { latitude: number; longitude: number; address: string; }) => { set(() => ({ userLatitude: latitude, userLongitude: longitude, userAddress: address, })); }, setDestinationLocation: ({ latitude, longitude, address, }: { latitude: number; longitude: number; address: string; }) => { set(() => ({ destinationLatitude: latitude, destinationLongitude: longitude, destinationAddress: address, })); }, /** * Forget where the rider was going. * * The destination is a search in progress, not durable state, but nothing * ever cleared it — so once a rider had looked up an address it stayed in * the store for the life of the process. Android keeps that process alive * across backgrounding, so reopening the app showed a route to a trip that * had already finished, and the map stayed zoomed out to fit a destination * the rider had long since arrived at. * * Called when a ride reaches a terminal state and on sign-out. The origin is * deliberately left alone: that's the rider's own position, still true. */ clearDestination: () => { set(() => ({ destinationLatitude: null, destinationLongitude: null, destinationAddress: null, })); }, })); type ServiceStore = { service: ServiceId; setService: (service: ServiceId) => void; }; /** Which service the rider picked on the home screen. */ export const useServiceStore = create((set) => ({ service: DEFAULT_SERVICE, setService: (service: ServiceId) => set(() => ({ service })), })); export const useDriverStore = create((set) => ({ drivers: [] as MarkerData[], selectedDriver: null, setSelectedDriver: (driverId: number) => set(() => ({ selectedDriver: driverId })), setDrivers: (drivers: MarkerData[]) => set(() => ({ drivers })), clearSelectedDriver: () => set(() => ({ selectedDriver: null })), }));