Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | 1x 1x 1x 30x 30x 30x 30x 20x 9x 9x 30x 29x 18x 18x 18x 18x 30x 30x 11x 30x | import { useState, useEffect, type Dispatch, type SetStateAction } from 'react'
import { toast } from 'sonner'
import { api, type RecipeDetail as RecipeDetailType } from '../api/client'
import { handleQuotaError } from '../lib/utils'
const POLL_INTERVAL = 3000 // 3 seconds
const MAX_POLL_DURATION = 30000 // 30 seconds
const RECENT_THRESHOLD = 60000 // 60 seconds
interface UseTipsPollingOptions {
recipe: RecipeDetailType | null
aiAvailable: boolean
activeTab: string
setRecipe: Dispatch<SetStateAction<RecipeDetailType | null>>
}
interface UseTipsPollingReturn {
tips: string[]
tipsLoading: boolean
tipsPolling: boolean
handleGenerateTips: (regenerate?: boolean) => Promise<void>
}
export function useTipsPolling({
recipe,
aiAvailable,
activeTab,
setRecipe,
}: UseTipsPollingOptions): UseTipsPollingReturn {
const [tips, setTips] = useState<string[]>([])
const [tipsLoading, setTipsLoading] = useState(false)
const [tipsPolling, setTipsPolling] = useState(false)
// Initialize tips when recipe changes
useEffect(() => {
if (!recipe) return
const id = requestAnimationFrame(() => setTips(recipe.ai_tips || []))
return () => cancelAnimationFrame(id)
}, [recipe?.id]) // eslint-disable-line react-hooks/exhaustive-deps -- only reset on new recipe
// Poll for tips if recipe is recently imported and has no tips yet
useEffect(() => {
if (!recipe) return
const recipeAge = Date.now() - new Date(recipe.scraped_at).getTime()
const isRecent = recipeAge < RECENT_THRESHOLD
Eif (!isRecent || tips.length > 0) {
return
}
const startTime = Date.now()
// Set polling in rAF to avoid synchronous setState in effect
const rafId = requestAnimationFrame(() => setTipsPolling(true))
const interval = setInterval(async () => {
if (Date.now() - startTime > MAX_POLL_DURATION) {
clearInterval(interval)
setTipsPolling(false)
return
}
try {
const updated = await api.recipes.get(recipe.id)
if (updated.ai_tips && updated.ai_tips.length > 0) {
setTips(updated.ai_tips)
setRecipe((prev) => prev ? { ...prev, ai_tips: updated.ai_tips } : prev)
clearInterval(interval)
setTipsPolling(false)
}
} catch {
// Ignore polling errors, will retry on next interval
}
}, POLL_INTERVAL)
return () => {
cancelAnimationFrame(rafId)
clearInterval(interval)
setTipsPolling(false)
}
// eslint-disable-next-line react-hooks/exhaustive-deps -- recipe object changes on fetch, only care about id/scraped_at/tips changes
}, [recipe?.id, recipe?.scraped_at, tips.length])
const handleGenerateTips = async (regenerate: boolean = false) => {
if (!recipe || tipsLoading) return
setTipsLoading(true)
try {
const result = await api.ai.tips(recipe.id, regenerate)
setTips(result.tips)
setRecipe((prev) => prev ? { ...prev, ai_tips: result.tips } : prev)
toast.success(regenerate ? 'Tips regenerated!' : 'Tips generated!')
} catch (error) {
console.error('Failed to generate tips:', error)
handleQuotaError(error, 'Failed to generate tips')
} finally {
setTipsLoading(false)
}
}
// Auto-generate tips when viewing Tips tab for old recipes without tips.
// Inline the async logic to avoid the linter tracing setState through function calls.
useEffect(() => {
Eif (activeTab !== 'tips' || tips.length > 0 || !aiAvailable || tipsLoading || tipsPolling || !recipe) return
let cancelled = false
;(async () => {
setTipsLoading(true)
try {
const result = await api.ai.tips(recipe.id, false)
if (!cancelled) {
setTips(result.tips)
setRecipe((prev) => prev ? { ...prev, ai_tips: result.tips } : prev)
toast.success('Tips generated!')
}
} catch (error) {
if (!cancelled) {
console.error('Failed to generate tips:', error)
handleQuotaError(error, 'Failed to generate tips')
}
} finally {
if (!cancelled) setTipsLoading(false)
}
})()
return () => { cancelled = true }
// eslint-disable-next-line react-hooks/exhaustive-deps -- intentionally only trigger on tab change, other deps checked inside
}, [activeTab])
return { tips, tipsLoading, tipsPolling, handleGenerateTips }
}
|