import { useRoute } from 'vue-router'; import type { Language } from '../../runtime'; import { AsyncData, getSelectedQuery } from '../../runtime'; import { sortChannels } from '../../lib'; import type { IChannelCard, PaginatedResponse } from '../../types'; export const useFetchChannels = async ( apiMethod: (...args: any[]) => Promise> ) => { const route = useRoute(); const mobile = !!useState('isMobile').value; const perPage = mobile ? 12 : 48; const lang = useLang() as Language; const key = AsyncData.Channels; const stateKey = computed(() => `data-${key}-${JSON.stringify(route.query)}-${lang}`); const { data, status, error } = await useAsyncData>( key, () => useApiFetcher>( stateKey.value, apiMethod, lang, { page: Number(route.query['page']) || 1, 'per-page': perPage, }, getSelectedQuery(route, sortChannels) || '-popularity', )(), { watch: [ () => { if (route.query) { const currentQuery = route.query; return JSON.stringify(currentQuery); } }, ], } ); return { data, status, error }; };