()
| 6 | * Loads logos on-demand when the component is opened |
| 7 | */ |
| 8 | export const useLogoSelection = () => { |
| 9 | const [isLoading, setIsLoading] = useState(false); |
| 10 | const [isInitialized, setIsInitialized] = useState(false); |
| 11 | |
| 12 | const logos = useLogosStore((s) => s.logos); |
| 13 | const fetchLogos = useLogosStore((s) => s.fetchLogos); // Check if we have a reasonable number of logos loaded |
| 14 | const hasEnoughLogos = Object.keys(logos).length > 0; |
| 15 | |
| 16 | const ensureLogosLoaded = useCallback(async () => { |
| 17 | if (isLoading || (hasEnoughLogos && isInitialized)) { |
| 18 | return; |
| 19 | } |
| 20 | |
| 21 | setIsLoading(true); |
| 22 | try { |
| 23 | await fetchLogos(); |
| 24 | setIsInitialized(true); |
| 25 | } catch (error) { |
| 26 | console.error('Failed to load logos for selection:', error); |
| 27 | } finally { |
| 28 | setIsLoading(false); |
| 29 | } |
| 30 | }, [isLoading, hasEnoughLogos, isInitialized, fetchLogos]); |
| 31 | |
| 32 | return { |
| 33 | logos, |
| 34 | isLoading, |
| 35 | ensureLogosLoaded, |
| 36 | hasLogos: hasEnoughLogos, |
| 37 | }; |
| 38 | }; |
| 39 | |
| 40 | /** |
| 41 | * Hook for channel forms that need channel logos |
no test coverage detected