31 lines
866 B
JavaScript
31 lines
866 B
JavaScript
import { useEffect, useState } from 'preact/hooks'
|
|
|
|
// Loads the recipe index once, for the dish name suggestions.
|
|
// A failing index is never worth a broken screen: the user keeps typing freely.
|
|
export function useRecipeIndex(source) {
|
|
const [recipes, setRecipes] = useState([])
|
|
const [ready, setReady] = useState(false)
|
|
|
|
useEffect(function loadIndexOnce() {
|
|
let active = true
|
|
|
|
async function loadFromSource() {
|
|
const result = await source.getIndex()
|
|
if (!active) return
|
|
if (result && result.ok && result.index) {
|
|
setRecipes(result.index.recipes || [])
|
|
} else {
|
|
console.error('useRecipeIndex: the recipe index could not be loaded', result)
|
|
}
|
|
setReady(true)
|
|
}
|
|
|
|
loadFromSource()
|
|
|
|
return function cancelIndexLoad() {
|
|
active = false
|
|
}
|
|
}, [source])
|
|
|
|
return { recipes, ready }
|
|
}
|