import type { MapParameters } from "../types"; import { BASE_URL } from "."; import { MMKVStorage } from "../storages"; import { useNetworkStore } from "../storages/network"; export const mapKeys = { snippet: (id: number) => ['snippet', id] as const, } const getSnippet = async (emmid: number): Promise => { const queryKey = mapKeys.snippet(emmid); if (useNetworkStore.getState().isOnline) { const response = await fetch(`${BASE_URL}/livemaps/${emmid}`).then((response) => response.json()); MMKVStorage.setItem(queryKey.toString(), JSON.stringify(response)); return response; } const cachedData = JSON.parse(MMKVStorage.getItem(mapKeys.snippet(emmid).toString()) ?? 'null'); if (cachedData) return cachedData; throw new Error('Offline - cannot fetch data and cache is empty'); } export { getSnippet };