Files

36 lines
929 B
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { RecipeItem } from '~/types'
import { useStorage } from '@vueuse/core'
import { namespace } from '~/constants'
/**
* 随机几道菜:候选跟随首页选的食材、厨具和忌口(见 recipe store 的 randomPool)
* @param total
*/
export function useRandomRecipe(total: Ref<number>) {
const rStore = useRecipeStore()
const randomRecipes = useStorage<(RecipeItem | undefined)[]>(`${namespace}:random:recipes`, [])
async function random() {
await rStore.loadRecipes()
const pool = rStore.randomPool
const indexes = generateRandomArray(pool.length, Math.min(total.value, pool.length))
randomRecipes.value = indexes.map(i => pool[i]).filter(item => !!item)
}
watch(total, () => {
random()
})
onMounted(() => {
// 如果没有随机菜谱,就生成一次
if (randomRecipes.value.length <= 0)
random()
})
return {
random,
randomRecipes,
}
}