import { Image as ExpoImage } from 'expo-image' import React, { useEffect, useRef, useState } from 'react' import { ActivityIndicator, Text, View } from 'react-native' import { background } from '../../assets' import GradientButton from '../../components/GradientButton' import MusicLandHeader from '../../components/MusicLandHeader' import loaderMessages from '../../config/loaderMessages' import { isWeb } from '../../hooks/useLayoutType' import Page from '../../layouts/Page' import { Routes } from '../../navigation' import { goBack, navigate } from '../../navigation/NavigationService' import { useUserData } from '../../providers/UserDataProvider' import { gutters, Palette, Style } from '../../styles' import { FONT_FAMILY } from '../../styles/Fonts' import CreateLyricsHeader from '../Writing/components/CreateLyricsHeader' import ProgressBar from '../../components/ProgressBar' const COVER_PROGRESS_MAX = 98 const COVER_PROGRESS_INTERVAL_MS = 500 const COVER_FAKE_DURATION_MS = 2 * 60 * 1000 const PhotoCover = () => { const [coverProgress, setCoverProgress] = useState(0) const progressIntervalRef = useRef(null) const progressStartRef = useRef(null) const { selectedProject } = useUserData() const isGenerating = selectedProject?.coverStatus === 'GENERATING' const coverGenerationMessage = isWeb ? loaderMessages.photoCoverGenerationWeb : '' const coverOptions = Array.isArray(selectedProject?.cover?.options) ? selectedProject.cover.options : [] const coverUrl = selectedProject?.cover?.result || selectedProject?.cover?.generatedBackground || coverOptions?.[0]?.finalUrl || coverOptions?.[0]?.generatedUrl || null const coverProgressValue = Math.max(0, Math.min(100, Math.round(coverProgress))) useEffect(() => { const clearProgressInterval = () => { if (progressIntervalRef.current) { global.clearInterval(progressIntervalRef.current) progressIntervalRef.current = null } } if (!isGenerating || coverUrl) { clearProgressInterval() progressStartRef.current = null setCoverProgress(coverUrl ? 100 : 0) return clearProgressInterval } progressStartRef.current = new Date() setCoverProgress(0) clearProgressInterval() progressIntervalRef.current = global.setInterval(() => { const start = progressStartRef.current if (!start) return const elapsed = Date.now() - start.getTime() const ratio = Math.max(0, Math.min(1, elapsed / COVER_FAKE_DURATION_MS)) const next = COVER_PROGRESS_MAX * ratio setCoverProgress((prev) => { if (prev >= COVER_PROGRESS_MAX) return COVER_PROGRESS_MAX return next >= COVER_PROGRESS_MAX ? COVER_PROGRESS_MAX : next }) }, COVER_PROGRESS_INTERVAL_MS) return clearProgressInterval }, [coverUrl, isGenerating]) return ( {coverUrl && !isGenerating ? ( ) : ( {isGenerating ? ( <> {isWeb && coverGenerationMessage ? ( {coverGenerationMessage} ) : ( Génération en cours. )} {coverProgressValue}% ) : ( La pochette sera disponible une fois la génération terminée. )} )} navigate(Routes.ValidateCover)} disabled={isGenerating || !coverUrl} /> ) } export default PhotoCover