import { useQuery } from '@tanstack/react-query' import axios from 'axios' import { trpc } from '@/src/trpc-client' import { AllProductsApiType, StoresApiType, SlotsApiType, EssentialConstsApiType, BannersApiType, StoreWithProductsApiType } from "@backend/trpc/router"; import { CACHE_FILENAMES } from "@packages/shared"; // Local useGetEssentialConsts hook export const useGetEssentialConsts = () => { const query = trpc.common.essentialConsts.useQuery(undefined, { refetchInterval: 60000, }) return { ...query, refetch: query.refetch } } type ProductsResponse = AllProductsApiType; type StoresResponse = StoresApiType; type SlotsResponse = SlotsApiType; type EssentialConstsResponse = EssentialConstsApiType; type BannersResponse = BannersApiType; type StoreWithProductsResponse = StoreWithProductsApiType; function useCacheUrl(filename: string): string | null { const { data: essentialConsts } = useGetEssentialConsts() const assetsDomain = essentialConsts?.assetsDomain const apiCacheKey = essentialConsts?.apiCacheKey return assetsDomain && apiCacheKey ? `${assetsDomain}${apiCacheKey}/${filename}` : null } export function useAllProducts() { const cacheUrl = useCacheUrl(CACHE_FILENAMES.products) return useQuery({ queryKey: ['all-products', cacheUrl], queryFn: async () => { if (!cacheUrl) { throw new Error('Cache URL not available') } const response = await axios.get(cacheUrl) return response.data }, staleTime: 60000, // 1 minute enabled: !!cacheUrl, }) } export function useStores() { const cacheUrl = useCacheUrl(CACHE_FILENAMES.stores) return useQuery({ queryKey: ['stores', cacheUrl], queryFn: async () => { if (!cacheUrl) { throw new Error('Cache URL not available') } const response = await axios.get(cacheUrl) return response.data }, staleTime: 60000, // 1 minute enabled: !!cacheUrl, }) } export function useSlots() { const cacheUrl = useCacheUrl(CACHE_FILENAMES.slots) return useQuery({ queryKey: ['slots', cacheUrl], queryFn: async () => { if (!cacheUrl) { throw new Error('Cache URL not available') } const response = await axios.get(cacheUrl) return response.data }, staleTime: 60000, // 1 minute enabled: !!cacheUrl, }) } export function useBanners() { const cacheUrl = useCacheUrl(CACHE_FILENAMES.banners) return useQuery({ queryKey: ['banners', cacheUrl], queryFn: async () => { if (!cacheUrl) { throw new Error('Cache URL not available') } const response = await axios.get(cacheUrl) return response.data }, staleTime: 60000, // 1 minute enabled: !!cacheUrl, }) } export function useStoreWithProducts(storeId: number) { const { data: essentialConsts } = useGetEssentialConsts() const assetsDomain = essentialConsts?.assetsDomain const apiCacheKey = essentialConsts?.apiCacheKey const cacheUrl = assetsDomain && apiCacheKey ? `${assetsDomain}${apiCacheKey}/stores/${storeId}.json` : null return useQuery({ queryKey: ['store-with-products', storeId, cacheUrl], queryFn: async () => { if (!cacheUrl) { throw new Error('Cache URL not available') } const response = await axios.get(cacheUrl) return response.data }, staleTime: 60000, // 1 minute enabled: !!cacheUrl, }) }