front 작업 중 .

This commit is contained in:
HyeongBaeYang committed 2025-12-29 17:56:57 +09:00
1 parent ecbd81d091
commit 24d61f925a
8 files changed
+796 -124

No files matched your search

+52 -34
View File
@@ -1,6 +1,6 @@
import React, { useState, useRef, useEffect } from 'react';
import { generateLyric, generateSong, waitForSongComplete } from '../../utils/api';
import { generateLyric, waitForLyricComplete, generateSong, waitForSongComplete } from '../../utils/api';
import { LANGUAGE_MAP } from '../../types/api';
interface BusinessInfo {
@@ -11,14 +11,16 @@ interface BusinessInfo {
interface SoundStudioContentProps {
onBack: () => void;
onNext: () => void;
onNext: (songTaskId: string) => void;
businessInfo?: BusinessInfo;
imageTaskId: string | null;
}
type GenerationStatus = 'idle' | 'generating_lyric' | 'generating_song' | 'polling' | 'complete' | 'error';
interface SavedGenerationState {
taskId: string;
sunoTaskId: string;
lyrics: string;
status: GenerationStatus;
timestamp: number;
@@ -28,7 +30,7 @@ const STORAGE_KEY = 'castad_song_generation';
const STORAGE_EXPIRY = 30 * 60 * 1000;
const MAX_RETRY_COUNT = 3;
const SoundStudioContent: React.FC<SoundStudioContentProps> = ({ onBack, onNext, businessInfo }) => {
const SoundStudioContent: React.FC<SoundStudioContentProps> = ({ onBack, onNext, businessInfo, imageTaskId }) => {
const [selectedType, setSelectedType] = useState('보컬');
const [selectedLang, setSelectedLang] = useState('한국어');
const [selectedGenre, setSelectedGenre] = useState('AI 추천');
@@ -44,13 +46,15 @@ const SoundStudioContent: React.FC<SoundStudioContentProps> = ({ onBack, onNext,
const [errorMessage, setErrorMessage] = useState<string | null>(null);
const [statusMessage, setStatusMessage] = useState('');
const [retryCount, setRetryCount] = useState(0);
const [songTaskId, setSongTaskId] = useState<string | null>(null);
const progressBarRef = useRef<HTMLDivElement>(null);
const audioRef = useRef<HTMLAudioElement>(null);
const saveToStorage = (taskId: string, currentLyrics: string, currentStatus: GenerationStatus) => {
const saveToStorage = (taskId: string, sunoTaskId: string, currentLyrics: string, currentStatus: GenerationStatus) => {
const data: SavedGenerationState = {
taskId,
sunoTaskId,
lyrics: currentLyrics,
status: currentStatus,
timestamp: Date.now(),
@@ -90,15 +94,16 @@ const SoundStudioContent: React.FC<SoundStudioContentProps> = ({ onBack, onNext,
}
setStatus('polling');
setStatusMessage('음악을 처리하고 있습니다... (새로고침 후 복구됨)');
resumePolling(savedState.taskId, savedState.lyrics, 0);
resumePolling(savedState.taskId, savedState.sunoTaskId, savedState.lyrics, 0);
}
}, []);
const resumePolling = async (taskId: string, currentLyrics: string, currentRetryCount: number = 0) => {
const resumePolling = async (taskId: string, sunoTaskId: string, currentLyrics: string, currentRetryCount: number = 0) => {
try {
const downloadResponse = await waitForSongComplete(
taskId,
(pollStatus) => {
sunoTaskId,
(pollStatus: string) => {
if (pollStatus === 'pending') {
setStatusMessage('대기 중...');
} else if (pollStatus === 'processing') {
@@ -111,7 +116,8 @@ const SoundStudioContent: React.FC<SoundStudioContentProps> = ({ onBack, onNext,
throw new Error(downloadResponse.error_message || '음악 다운로드에 실패했습니다.');
}
setAudioUrl(downloadResponse.file_url);
setAudioUrl(downloadResponse.song_result_url);
setSongTaskId(downloadResponse.task_id);
setStatus('complete');
setStatusMessage('');
setRetryCount(0);
@@ -142,7 +148,7 @@ const SoundStudioContent: React.FC<SoundStudioContentProps> = ({ onBack, onNext,
};
const regenerateSongOnly = async (currentLyrics: string, currentRetryCount: number) => {
if (!businessInfo) return;
if (!businessInfo || !imageTaskId) return;
try {
const language = LANGUAGE_MAP[selectedLang] || 'Korean';
@@ -153,7 +159,7 @@ const SoundStudioContent: React.FC<SoundStudioContentProps> = ({ onBack, onNext,
'어쿠스틱': 'acoustic',
};
const songResponse = await generateSong({
const songResponse = await generateSong(imageTaskId, {
genre: genreMap[selectedGenre] || 'pop',
language,
lyrics: currentLyrics,
@@ -163,8 +169,8 @@ const SoundStudioContent: React.FC<SoundStudioContentProps> = ({ onBack, onNext,
throw new Error(songResponse.error_message || '음악 생성 요청에 실패했습니다.');
}
saveToStorage(songResponse.task_id, currentLyrics, 'polling');
await resumePolling(songResponse.task_id, currentLyrics, currentRetryCount);
saveToStorage(songResponse.task_id, songResponse.suno_task_id, currentLyrics, 'polling');
await resumePolling(songResponse.task_id, songResponse.suno_task_id, currentLyrics, currentRetryCount);
} catch (error) {
console.error('Song regeneration failed:', error);
@@ -264,40 +270,52 @@ const SoundStudioContent: React.FC<SoundStudioContentProps> = ({ onBack, onNext,
return;
}
if (!imageTaskId) {
setErrorMessage('이미지 업로드 정보가 없습니다. 이전 단계로 돌아가 다시 시도해주세요.');
return;
}
setStatus('generating_lyric');
setErrorMessage(null);
setStatusMessage('가사를 생성하고 있습니다...');
try {
const language = LANGUAGE_MAP[selectedLang] || 'Korean';
let lyricResponse = await generateLyric({
// 1. 가사 생성 요청
const lyricResponse = await generateLyric({
customer_name: businessInfo.customer_name,
detail_region_info: businessInfo.detail_region_info,
language,
region: businessInfo.region,
task_id: imageTaskId,
});
let retryCount = 0;
while (lyricResponse.lyric.includes("I'm sorry") && retryCount < 3) {
retryCount++;
setStatusMessage(`가사 재생성 중... (${retryCount}/3)`);
lyricResponse = await generateLyric({
customer_name: businessInfo.customer_name,
detail_region_info: businessInfo.detail_region_info,
language,
region: businessInfo.region,
});
if (!lyricResponse.success || !lyricResponse.task_id) {
throw new Error(lyricResponse.error_message || '가사 생성 요청에 실패했습니다.');
}
if (!lyricResponse.success) {
throw new Error(lyricResponse.error_message || '가사 생성에 실패했습니다.');
// 2. 가사 생성 상태 폴링 → 완료 시 상세 조회
setStatusMessage('가사를 처리하고 있습니다...');
const lyricDetailResponse = await waitForLyricComplete(
lyricResponse.task_id,
(status: string) => {
if (status === 'processing') {
setStatusMessage('가사를 처리하고 있습니다...');
}
}
);
if (!lyricDetailResponse.lyric_result) {
throw new Error('가사를 받지 못했습니다.');
}
if (lyricResponse.lyric.includes("I'm sorry")) {
// "I'm sorry" 체크
if (lyricDetailResponse.lyric_result.includes("I'm sorry")) {
throw new Error('가사 생성에 실패했습니다. 다시 시도해주세요.');
}
setLyrics(lyricResponse.lyric);
setLyrics(lyricDetailResponse.lyric_result);
setShowLyrics(true);
setStatus('generating_song');
@@ -310,10 +328,10 @@ const SoundStudioContent: React.FC<SoundStudioContentProps> = ({ onBack, onNext,
'어쿠스틱': 'acoustic',
};
const songResponse = await generateSong({
const songResponse = await generateSong(imageTaskId, {
genre: genreMap[selectedGenre] || 'pop',
language,
lyrics: lyricResponse.lyric,
lyrics: lyricDetailResponse.lyric_result,
});
if (!songResponse.success) {
@@ -322,9 +340,9 @@ const SoundStudioContent: React.FC<SoundStudioContentProps> = ({ onBack, onNext,
setStatus('polling');
setStatusMessage('음악을 처리하고 있습니다...');
saveToStorage(songResponse.task_id, lyricResponse.lyric, 'polling');
saveToStorage(songResponse.task_id, songResponse.suno_task_id, lyricDetailResponse.lyric_result, 'polling');
await resumePolling(songResponse.task_id, lyricResponse.lyric, 0);
await resumePolling(songResponse.task_id, songResponse.suno_task_id, lyricDetailResponse.lyric_result, 0);
} catch (error) {
console.error('Music generation failed:', error);
@@ -554,9 +572,9 @@ const SoundStudioContent: React.FC<SoundStudioContentProps> = ({ onBack, onNext,
{/* Bottom Button */}
<div className="bottom-button-container">
<button
onClick={onNext}
disabled={status !== 'complete'}
className={`btn-primary ${status !== 'complete' ? 'disabled' : ''}`}
onClick={() => songTaskId && onNext(songTaskId)}
disabled={status !== 'complete' || !songTaskId}
className={`btn-primary ${status !== 'complete' || !songTaskId ? 'disabled' : ''}`}
>
영상 생성하기
</button>