feat: add random recipes localStorage

This commit is contained in:
YunYouJun
2023-11-13 00:31:18 +08:00
parent b2cf053446
commit 1ab402af8b
2 changed files with 11 additions and 6 deletions

View File

@@ -1,3 +1,5 @@
import { useStorage } from '@vueuse/core'
import { namespace } from '~/constants'
import type { DbRecipeItem } from '~/utils/db'
/**
@@ -5,7 +7,7 @@ import type { DbRecipeItem } from '~/utils/db'
* @param total
*/
export function useRandomRecipe(total: Ref<number>) {
const randomRecipes = ref<(DbRecipeItem | undefined)[]>([])
const randomRecipes = useStorage<(DbRecipeItem | undefined)[]>(`${namespace}:random:recipes`, [])
async function random() {
const length = await db.recipes.count()
const randomArr = generateRandomArray(length, total.value)
@@ -19,7 +21,9 @@ export function useRandomRecipe(total: Ref<number>) {
})
onMounted(() => {
random()
// 如果没有随机菜谱,就生成一次
if (randomRecipes.value.length <= 0)
random()
})
return {