多维表格
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.0 KiB

import type { ProductFeedItem } from '../lib/types'
2 months ago
export const useProductFeed = createSharedComposable(() => {
const activeTab = ref('recents')
const { $api } = useNuxtApp()
const youtubeFeed = ref<ProductFeedItem[]>([])
2 months ago
const githubFeed = ref<ProductFeedItem[]>([])
2 months ago
const socialFeed = ref<ProductFeedItem[]>([])
2 months ago
const isErrorOccurred = reactive({
youtube: false,
github: false,
social: false,
})
const loadFeed = async ({ loadMore, type }: { loadMore: boolean; type: 'youtube' | 'github' | 'all' }) => {
2 months ago
try {
let page = 1
if (loadMore) {
switch (type) {
case 'youtube':
page = Math.ceil(youtubeFeed.value.length / 10) + 1
break
case 'github':
page = Math.ceil(githubFeed.value.length / 10) + 1
break
case 'all':
page = Math.ceil(socialFeed.value.length / 10) + 1
break
}
}
2 months ago
const response = await $api.utils.feed({ page, per_page: 10, type })
if (type === 'all' && page === 1 && response.length) {
localStorage.setItem('last_published_at', response[0]['Published Time'] as string)
}
2 months ago
switch (type) {
case 'youtube':
youtubeFeed.value = [...youtubeFeed.value, ...response] as ProductFeedItem[]
break
case 'github':
githubFeed.value = [...githubFeed.value, ...response] as ProductFeedItem[]
break
case 'all':
socialFeed.value = [...socialFeed.value, ...response] as ProductFeedItem[]
break
}
2 months ago
} catch (error) {
switch (type) {
case 'youtube':
isErrorOccurred.youtube = true
break
case 'github':
isErrorOccurred.github = true
break
case 'all':
isErrorOccurred.social = true
break
}
2 months ago
console.error(error)
return []
}
2 months ago
}
return {
isErrorOccurred,
2 months ago
activeTab,
youtubeFeed,
githubFeed,
socialFeed,
loadFeed,
2 months ago
}
})