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 | 12x 12x 12x 12x 12x 6x 6x 12x 12x 12x | import { useState, useRef, useEffect, useCallback } from 'react'
import { api, type DiscoverSuggestion } from '../api/client'
import { handleQuotaError } from '../lib/utils'
interface UseDiscoverTabOptions {
profileId: number | undefined
aiAvailable: boolean
}
interface UseDiscoverTabResult {
suggestions: DiscoverSuggestion[]
loading: boolean
error: boolean
load: (refresh?: boolean) => void
loadIfEmpty: () => void
}
export function useDiscoverTab({ profileId, aiAvailable }: UseDiscoverTabOptions): UseDiscoverTabResult {
const [suggestions, setSuggestions] = useState<DiscoverSuggestion[]>([])
const [loading, setLoading] = useState(false)
const [error, setError] = useState(false)
const mountedRef = useRef(true)
useEffect(() => {
mountedRef.current = true
return () => { mountedRef.current = false }
}, [])
const load = useCallback(async (refresh = false) => {
if (!profileId) return
setLoading(true)
setError(false)
try {
const result = await api.ai.discover(profileId, refresh)
if (!mountedRef.current) return
setSuggestions(result.suggestions)
} catch (err) {
if (!mountedRef.current) return
const isAbort =
err instanceof Error &&
(err.name === 'AbortError' || err.message.includes('abort'))
if (isAbort) {
console.debug('Discover fetch aborted:', err)
} else if (!handleQuotaError(err, 'Failed to load discover suggestions')) {
setError(true)
}
} finally {
if (mountedRef.current) {
setLoading(false)
}
}
}, [profileId])
const loadIfEmpty = useCallback(() => {
if (suggestions.length === 0 && !loading && !error && aiAvailable) {
load()
}
}, [suggestions.length, loading, error, aiAvailable, load])
return { suggestions, loading, error, load, loadIfEmpty }
}
|