237 lines
7 KiB
TypeScript
237 lines
7 KiB
TypeScript
import { create } from 'zustand'
|
|
import type { QueryClient } from '@tanstack/react-query'
|
|
import type { RegionSummary } from '@packages/shared'
|
|
import { applyRegionSwitch } from '@/src/lib/neo-region-switch'
|
|
import { writeNeoRegionSnapshot } from '@/src/lib/neo-region-cache'
|
|
import { addressCoords, type NeoAddress, type NeoCoords } from '@/src/lib/neo-address'
|
|
import type { NeoOrigin } from '@/src/lib/neo-region-decision'
|
|
|
|
export interface NeoRegionData {
|
|
regionId: number
|
|
boundId: number
|
|
regionName: string
|
|
coords: NeoCoords
|
|
selectedAddressId: number | null
|
|
/** The full selected address object (null when the region has no address). */
|
|
selectedAddress: NeoAddress | null
|
|
/** The selected address formatted as one line. */
|
|
fullAddress: string | null
|
|
isCache: boolean
|
|
changeRegion: (regionId: number, boundId?: number | null) => Promise<void>
|
|
selectAddress: (id: number) => Promise<void>
|
|
}
|
|
|
|
interface ApplyRegionInput {
|
|
regionId: number
|
|
boundId: number
|
|
regionName: string
|
|
coords: NeoCoords
|
|
origin: NeoOrigin
|
|
selectedAddressId: number | null
|
|
}
|
|
|
|
interface NeoRegionState {
|
|
regionId: number | null
|
|
boundId: number | null
|
|
regionName: string | null
|
|
coords: NeoCoords | null
|
|
origin: NeoOrigin | null
|
|
|
|
regions: RegionSummary[]
|
|
addresses: NeoAddress[]
|
|
selectedAddressId: number | null
|
|
|
|
needsAddressFormFlow: boolean
|
|
needsAddressConfirmation: boolean
|
|
|
|
applyRegion: (data: ApplyRegionInput) => void
|
|
setRegions: (regions: RegionSummary[]) => void
|
|
setAddresses: (addresses: NeoAddress[]) => void
|
|
clearRegion: () => void
|
|
openAddressFlow: () => void
|
|
closeAddressFlow: () => void
|
|
setNeedsConfirmation: (value: boolean) => void
|
|
resetForLogout: () => void
|
|
|
|
changeRegion: (regionId: number, boundId?: number | null) => Promise<void>
|
|
selectAddress: (id: number) => Promise<void>
|
|
}
|
|
|
|
// The cart/cache side of a region switch needs the app's query client. The hook
|
|
// binds it once; every other consumer only uses the actions below.
|
|
let boundQueryClient: QueryClient | null = null
|
|
|
|
export function bindNeoRegionQueryClient(queryClient: QueryClient): void {
|
|
boundQueryClient = queryClient
|
|
}
|
|
|
|
function regionCoords(region: RegionSummary | undefined | null): NeoCoords | null {
|
|
if (region?.center == null) return null
|
|
return { latitude: region.center.lat, longitude: region.center.lng }
|
|
}
|
|
|
|
export const useNeoRegionStore = create<NeoRegionState>((set, get) => ({
|
|
regionId: null,
|
|
boundId: null,
|
|
regionName: null,
|
|
coords: null,
|
|
origin: null,
|
|
|
|
regions: [],
|
|
addresses: [],
|
|
selectedAddressId: null,
|
|
|
|
needsAddressFormFlow: false,
|
|
needsAddressConfirmation: false,
|
|
|
|
/**
|
|
* The only writer of the region fields. The selection is validated against
|
|
* the region here, so `selectedAddressId != null` always means "the active
|
|
* address belongs to the active region".
|
|
*/
|
|
applyRegion: (data) => {
|
|
const state = get()
|
|
const selection =
|
|
data.selectedAddressId != null
|
|
? state.addresses.find((address) => address.id === data.selectedAddressId) ?? null
|
|
: null
|
|
const validSelection =
|
|
selection != null && selection.regionId === data.regionId ? selection : null
|
|
const regionName =
|
|
state.regions.find((region) => region.id === data.regionId)?.name ?? data.regionName
|
|
const coords = (validSelection ? addressCoords(validSelection) : null) ?? data.coords
|
|
const boundId = validSelection?.boundId ?? data.boundId
|
|
|
|
set({
|
|
regionId: data.regionId,
|
|
boundId,
|
|
regionName,
|
|
coords,
|
|
origin: validSelection != null ? 'address' : data.origin,
|
|
selectedAddressId: validSelection?.id ?? null,
|
|
needsAddressFormFlow: false,
|
|
needsAddressConfirmation: false,
|
|
})
|
|
|
|
void writeNeoRegionSnapshot({
|
|
regionId: data.regionId,
|
|
boundId,
|
|
regionName,
|
|
coords,
|
|
selectedAddress: validSelection,
|
|
})
|
|
},
|
|
|
|
setRegions: (regions) => {
|
|
const state = get()
|
|
const current = state.regionId != null
|
|
? regions.find((region) => region.id === state.regionId) ?? null
|
|
: null
|
|
set({
|
|
regions,
|
|
regionName: current?.name ?? state.regionName,
|
|
})
|
|
},
|
|
|
|
setAddresses: (addresses) => {
|
|
const usable = addresses.filter((address) => address.regionId != null)
|
|
const state = get()
|
|
if (state.selectedAddressId == null) {
|
|
set({ addresses: usable })
|
|
return
|
|
}
|
|
const selected = usable.find((address) => address.id === state.selectedAddressId) ?? null
|
|
const stillValid =
|
|
selected != null &&
|
|
selected.regionId === state.regionId &&
|
|
(selected.boundId == null || state.boundId == null || selected.boundId === state.boundId)
|
|
set({
|
|
addresses: usable,
|
|
selectedAddressId: stillValid ? state.selectedAddressId : null,
|
|
})
|
|
},
|
|
|
|
clearRegion: () => {
|
|
set({
|
|
regionId: null,
|
|
boundId: null,
|
|
regionName: null,
|
|
coords: null,
|
|
origin: null,
|
|
selectedAddressId: null,
|
|
needsAddressFormFlow: false,
|
|
needsAddressConfirmation: false,
|
|
})
|
|
},
|
|
|
|
openAddressFlow: () => set({ needsAddressFormFlow: true, needsAddressConfirmation: false }),
|
|
|
|
closeAddressFlow: () => set({ needsAddressFormFlow: false }),
|
|
|
|
setNeedsConfirmation: (value) =>
|
|
set({
|
|
needsAddressConfirmation: value,
|
|
...(value ? { needsAddressFormFlow: false } : null),
|
|
}),
|
|
|
|
resetForLogout: () =>
|
|
set({
|
|
addresses: [],
|
|
selectedAddressId: null,
|
|
needsAddressFormFlow: false,
|
|
needsAddressConfirmation: false,
|
|
}),
|
|
|
|
changeRegion: async (regionId, boundId) => {
|
|
const state = get()
|
|
const region = state.regions.find((candidate) => candidate.id === regionId)
|
|
if (region == null) return
|
|
|
|
const nextBoundId = boundId ?? region.effectiveBoundId ?? null
|
|
if (nextBoundId == null) return
|
|
|
|
if (state.regionId !== regionId && boundQueryClient != null) {
|
|
await applyRegionSwitch(state.regionId, regionId, boundQueryClient)
|
|
}
|
|
|
|
const coords = regionCoords(region) ?? state.coords
|
|
if (coords == null) return
|
|
|
|
get().applyRegion({
|
|
regionId,
|
|
boundId: nextBoundId,
|
|
regionName: region.name,
|
|
coords,
|
|
origin: 'manual',
|
|
selectedAddressId: null,
|
|
})
|
|
},
|
|
|
|
selectAddress: async (id) => {
|
|
const state = get()
|
|
const address = state.addresses.find((candidate) => candidate.id === id)
|
|
if (address == null || address.regionId == null) return
|
|
|
|
const region = state.regions.find((candidate) => candidate.id === address.regionId)
|
|
if (region == null) return
|
|
|
|
const boundId = address.boundId ?? region.effectiveBoundId ?? null
|
|
if (boundId == null) return
|
|
|
|
if (state.regionId !== address.regionId && boundQueryClient != null) {
|
|
await applyRegionSwitch(state.regionId, address.regionId, boundQueryClient)
|
|
}
|
|
|
|
const coords = addressCoords(address) ?? regionCoords(region) ?? state.coords
|
|
if (coords == null) return
|
|
|
|
get().applyRegion({
|
|
regionId: address.regionId,
|
|
boundId,
|
|
regionName: region.name,
|
|
coords,
|
|
origin: 'address',
|
|
selectedAddressId: address.id,
|
|
})
|
|
},
|
|
}))
|