import { useUser, useGames, useAuth, useIframe, useGameToast, usePlayGameInWebview, } from '#lib/composables' import { MAPPING_IFRAME_GAMES } from '#lib/constants' import { useCheckGame } from '#lib/composables/game/use-check-game' import { isValidURL } from '#lib/utils' import { useDevice, useRouter } from '#imports' import { IFrameGameURLEnum } from '#lib/enums' import { useGamesStore } from '#lib/stores' import { storeToRefs } from 'pinia' /** * Provides navigation functions and state management for handling game URLs and user authentication. * * @returns {Object} An object with functions for navigating to URLs and handling navigation logic. * @namespace */ export function useNavigate() { const { isMobile } = useDevice() const router = useRouter() const { getGameUrl, getIframeGameMapGameApi, openModalMaintainAthenaGames } = useGames() const { enrichUrl } = useIframe() const { isMaintainAthenaGames } = useCheckGame() const { isLogged } = useUser() const { openAuthModal } = useAuth() const { openModeGameVersion } = useGameToast() const { isPlayingInWebview } = usePlayGameInWebview() const gameStore = useGamesStore() const { lastURL } = storeToRefs(gameStore) /** * Navigates to a specified URL, handling authentication and maintenance checks. * * If login is required and the user is not logged in, it opens the authentication modal. * If the URL corresponds to maintenance games, it opens the maintenance modal. * It also decides whether to open the URL in a new tab based on device type and URL mapping. * * @param {string} url - The URL to navigate to. * @param {boolean} [loginRequired=false] - Whether login is required to access the URL. * @param {boolean} [isNewTab=false] - Whether to open the URL in a new tab. * @returns {Promise} A promise that resolves when navigation is complete. */ const navigate = async ( url: string, loginRequired: boolean = false, isNewTab: boolean = false, ): Promise => { gameStore.setLastURL(url) if (loginRequired && !isLogged.value) { openAuthModal() return } if (isMaintainAthenaGames(url.replaceAll('/', ''))) { openModalMaintainAthenaGames() return } const [splitUrl] = url.split('?') if ( isMobile && (url in MAPPING_IFRAME_GAMES || splitUrl in MAPPING_IFRAME_GAMES) ) { isNewTab = true } // Check version games C sports if (url === IFrameGameURLEnum.C_SPORTS && isMobile) { openModeGameVersion() return } navigateWithURL(url, isNewTab, isMobile) } /** * Opens a given URL in the same window. * If the URL is a valid external URL, it directly sets `window.location.href`. * Otherwise, it fetches the game URL from the API, enriches it, and then sets `window.location.href`. * * @param {string} url - The URL to open. * @param {boolean} isMobile - Indicates if the current device is mobile. * @returns {Promise} A promise that resolves when the URL is opened. */ const openInSameWindow = async (url: string, isMobile: boolean) => { try { if (isValidURL(url)) { window.location.href = url return } const urlResponse = await getGameUrl( getIframeGameMapGameApi(url), isMobile, ) const enrichedUrl = enrichUrl(url, urlResponse?.content) if (enrichedUrl) { window.location.href = enrichedUrl } return } catch (error) { console.error('Error opening URL in same tab:', error) } } /** * Handles the actual navigation to a URL, either opening it in a new tab or navigating using the router. * * If opening in a new tab, it verifies the URL and either sets the location or fetches a valid URL from an API. * If not opening in a new tab, it uses the router to navigate. * * @param {string} url - The URL to navigate to. * @param {boolean} isNewTab - Whether to open the URL in a new tab. * @param {boolean} [isDeviceMobile=isMobile] - Whether the device is mobile (defaults to the current device type). * @returns {Promise} A promise that resolves when navigation is complete, or undefined. */ const navigateWithURL = async ( url: string, isNewTab: boolean, isDeviceMobile: boolean = isMobile, ): Promise => { let newTab: Window | null if (isNewTab) { if (isPlayingInWebview()) { openInSameWindow(url, isDeviceMobile) return } newTab = window.open('', '_blank') if (isValidURL(url) && newTab) { newTab.location.href = url return } try { const urlResponse = await getGameUrl( getIframeGameMapGameApi(url), isDeviceMobile, ) const enrichedUrl = enrichUrl(url, urlResponse?.content) if (enrichedUrl && newTab) { newTab.location.href = enrichedUrl } else if (newTab) { newTab.close() } } catch (ex) { console.error(' ~ getMaintenanceGames ~ ex:', ex) } return } router.push(url) } return { lastURL, navigate, navigateWithURL, } }