|
|
|
|
@ -1,5 +1,5 @@
|
|
|
|
|
import { create } from 'zustand'
|
|
|
|
|
import {persist} from 'zustand/middleware'
|
|
|
|
|
import {persist,createJSONStorage} from 'zustand/middleware'
|
|
|
|
|
const CACHE_EXPIRY = 10 * 60 * 1000;
|
|
|
|
|
|
|
|
|
|
export interface Location {
|
|
|
|
|
@ -71,23 +71,68 @@ export interface WeatherStore {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const useWeatherStore = create<WeatherStore>()(persist((set,get)=>({currentWeather:null,weatherCache:new Map(),isLoading:false,error:null,
|
|
|
|
|
setCurrentWeather:(weather)=>set({currentWeather:weather}),
|
|
|
|
|
getWeatherFromCache:(city)=>{
|
|
|
|
|
const cashe = get().weatherCache.get(city.toLowerCase());
|
|
|
|
|
if(!cashe)return null;
|
|
|
|
|
const isExpired = Date.now()-cashe.timestamp>CACHE_EXPIRY;
|
|
|
|
|
return isExpired?null:cashe.data;
|
|
|
|
|
},
|
|
|
|
|
cacheWeather:(city,weather)=>set((state)=>{
|
|
|
|
|
const newCache = new Map(state.weatherCache);
|
|
|
|
|
newCache.set(city.toLowerCase(),{data:weather,timestamp:Date.now()});
|
|
|
|
|
return{weatherCache:newCache}
|
|
|
|
|
}),
|
|
|
|
|
setLoading:(loading)=>set({error})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
))
|
|
|
|
|
export const useWeatherStore = create<WeatherStore>()(
|
|
|
|
|
persist(
|
|
|
|
|
(set,get)=>({
|
|
|
|
|
currentWeather:null,weatherCache:new Map(),isLoading:false,error:null,
|
|
|
|
|
setCurrentWeather:(weather)=>set({currentWeather:weather}),
|
|
|
|
|
getWeatherFromCache:(city)=>{
|
|
|
|
|
const cashe = get().weatherCache.get(city.toLowerCase());
|
|
|
|
|
if(!cashe)return null;
|
|
|
|
|
const isExpired = Date.now()-cashe.timestamp>CACHE_EXPIRY;
|
|
|
|
|
return isExpired?null:cashe.data;
|
|
|
|
|
},
|
|
|
|
|
cacheWeather:(city,weather)=>set((state)=>{
|
|
|
|
|
const newCache = new Map(state.weatherCache);
|
|
|
|
|
newCache.set(city.toLowerCase(),{data:weather,timestamp:Date.now()});
|
|
|
|
|
return{weatherCache:newCache}
|
|
|
|
|
}),
|
|
|
|
|
setLoading:(loading)=>set({isLoading:loading}),
|
|
|
|
|
setError:(error)=>set({error}),
|
|
|
|
|
reset:()=>set({currentWeather:null,weatherCache:new Map(),isLoading:false,error:null}),
|
|
|
|
|
searchWeather:async(city:string)=>{
|
|
|
|
|
try{
|
|
|
|
|
set({isLoading:true,error:null});
|
|
|
|
|
const cachedWeather = get().getWeatherFromCache(city);
|
|
|
|
|
if(cachedWeather){
|
|
|
|
|
set({currentWeather:cachedWeather,isLoading:false});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const WeatherData = await WeatherAPI.getcurrentWeather(city);
|
|
|
|
|
set({currentWeather:WeatherData,isLoading:false});
|
|
|
|
|
get().cacheWeather(city,WeatherData);
|
|
|
|
|
}catch(error){
|
|
|
|
|
set({error:'获取失败',isLoading:false});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
refreshWeather:async()=>{
|
|
|
|
|
const currentWeather = get().currentWeather;
|
|
|
|
|
if(!currentWeather)return;
|
|
|
|
|
try{
|
|
|
|
|
set({isLoading:true,error:null});
|
|
|
|
|
const city = currentWeather.location.name;
|
|
|
|
|
const WeatherData = await WeatherAPI.getcurrentWeather(city);
|
|
|
|
|
set({currentWeather:WeatherData,isLoading:false});
|
|
|
|
|
get().cacheWeather(city,WeatherData);
|
|
|
|
|
}catch(error){
|
|
|
|
|
set({error:'刷新失败',isLoading:false});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
{
|
|
|
|
|
name:'weather-strorage',
|
|
|
|
|
storage:createJSONStorage(()=>localStorage),
|
|
|
|
|
partialize:(state)=>({
|
|
|
|
|
weatherCache:Array.from(state.weatherCache.entries())
|
|
|
|
|
}),
|
|
|
|
|
onRehydrateStorage:(state)=>{
|
|
|
|
|
if(state && Array.isArray(state.weatherCache)){
|
|
|
|
|
state.weatherCache = new Map(state.weatherCache as any);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|