/** * Import types from Nadal. */ import { useDevice } from '#imports' import type { BannersResponseData } from '@kira-dancer/nadal' import type { JackpotXocdia } from '#lib/stores' import { useNavigate } from '@/composables/common/use-navigate' import { useApiFetch } from '#lib/composables/service/use-api-fetch' import { APP_API } from '#lib/configs' import { useAsyncData, useNuxtData } from 'nuxt/app' import { AppDataEnum } from '#lib/enums' import { useGames } from '@/composables/game' import { useJackpotStore, } from '#lib/stores' import cacheManager from '#lib/utils/cache-manager' /** * Provides functions and state for managing banners. * * @returns {Object} An object with functions to fetch banners, handle banner clicks, and the banners state. * @namespace */ export function useBanners() { const { playGame } = useGames() const { navigate } = useNavigate() const { request } = useApiFetch() const { isMobile } = useDevice() const jackpotStore = useJackpotStore() const { getDeviceState } = useDeviceOverride() /** * Reactive state holding the list of banners. * */ const { data: banners } = useNuxtData( AppDataEnum.Banners, ) /** * Fetches banners from the Nadal site and updates the state. * * BannersResponseData type is imported from the Nadal module. * * @returns {Promise} A promise that resolves once banners are successfully fetched and the state is updated. */ const fetchBanners = async (): Promise => { await useAsyncData(AppDataEnum.Banners, async () => { // check cache const cachedData = cacheManager.get(AppDataEnum.Banners) if (cachedData) { return cachedData } try { const { data } = await request< { isMobile?: boolean }, BannersResponseData[] >( APP_API.BANNERS, isMobile ? { mobile: isMobile, } : {}, ) cacheManager.set(AppDataEnum.Banners, data) return data } catch { cacheManager.delete(AppDataEnum.Banners) } return [] }) } /** * Fetches banners from the Nadal site and updates the state. Non-cached version * * BannersResponseData type is imported from the Nadal module. * * @returns {Promise} A promise that resolves once banners are successfully fetched and the state is updated. */ const fetchBannersNoCache = async (): Promise => { await useAsyncData(AppDataEnum.Banners, async () => { try { const { data } = await request< { isMobile?: boolean }, BannersResponseData[] >( APP_API.BANNERS, isMobile ? { mobile: getDeviceState().isMobile, } : {}, ) return data } catch { return [] } return [] }) } /** * Handles the click event on a banner, navigating to a link or playing a game based on the banner's data. * * @param {BannersResponseData} item - The banner data to process. * @returns {Promise} A promise that resolves when the click action is completed. */ const onClickBanner = async (item: BannersResponseData): Promise => { if (item.link) { void navigate(item.link, item.loginRequired, item.newTab) } else { void playGame(item) } } const jackpotXocdia = computed((): JackpotXocdia => { return jackpotStore.getXocDiaJackpot() }) return { onClickBanner, fetchBanners, fetchBannersNoCache, banners, jackpotXocdia } }