-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
130 lines (119 loc) · 4.57 KB
/
App.tsx
File metadata and controls
130 lines (119 loc) · 4.57 KB
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
import React, { useState, useCallback, useEffect } from 'react';
import { AppStep, ArtStyle, Genre, ImageFile } from './types';
import * as geminiService from './services/geminiService';
import Header from './components/Header';
import ImageUploader from './components/ImageUploader';
import StyleSelector from './components/StyleSelector';
import StoryCanvas from './components/StoryCanvas';
import Loader from './components/Loader';
import { GENERATING_MESSAGES } from './constants';
const App: React.FC = () => {
const [step, setStep] = useState<AppStep>(AppStep.UPLOAD);
const [userImage, setUserImage] = useState<ImageFile | null>(null);
const [artStyle, setArtStyle] = useState<ArtStyle | null>(null);
const [genre, setGenre] = useState<Genre | null>(null);
const [characterSheet, setCharacterSheet] = useState<ImageFile | null>(null);
const [currentScene, setCurrentScene] = useState<ImageFile | null>(null);
const [isLoading, setIsLoading] = useState<boolean>(false);
const [loadingMessage, setLoadingMessage] = useState<string>(GENERATING_MESSAGES[0]);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
// Fix: Changed NodeJS.Timeout to ReturnType<typeof setInterval> for browser compatibility.
let interval: ReturnType<typeof setInterval>;
if (isLoading) {
interval = setInterval(() => {
setLoadingMessage(prev => {
const currentIndex = GENERATING_MESSAGES.indexOf(prev);
const nextIndex = (currentIndex + 1) % GENERATING_MESSAGES.length;
return GENERATING_MESSAGES[nextIndex];
});
}, 2500);
}
return () => {
if (interval) {
clearInterval(interval);
}
};
}, [isLoading]);
const handleImageUpload = (image: ImageFile) => {
setUserImage(image);
setStep(AppStep.STYLE_SELECTION);
setError(null);
};
const handleStyleAndGenreSelect = async (selectedArtStyle: ArtStyle, selectedGenre: Genre) => {
if (!userImage) return;
setArtStyle(selectedArtStyle);
setGenre(selectedGenre);
setStep(AppStep.GENERATING);
setIsLoading(true);
setError(null);
try {
const newCharacterSheet = await geminiService.generateCharacterSheet(userImage, selectedArtStyle, selectedGenre);
setCharacterSheet(newCharacterSheet);
setLoadingMessage("Character created! Generating first scene...");
const initialScene = await geminiService.generateInitialScene(newCharacterSheet, selectedArtStyle, selectedGenre);
setCurrentScene(initialScene);
setStep(AppStep.STORY);
} catch (err) {
console.error(err);
setError(err instanceof Error ? err.message : "An unknown error occurred.");
setStep(AppStep.STYLE_SELECTION);
} finally {
setIsLoading(false);
}
};
const handleEditScene = async (prompt: string) => {
if (!currentScene) return;
setIsLoading(true);
setError(null);
try {
const editedScene = await geminiService.editScene(currentScene, prompt);
setCurrentScene(editedScene);
} catch (err) {
console.error(err);
setError(err instanceof Error ? err.message : "Failed to edit the scene.");
} finally {
setIsLoading(false);
}
};
const handleReset = () => {
setStep(AppStep.UPLOAD);
setUserImage(null);
setArtStyle(null);
setGenre(null);
setCharacterSheet(null);
setCurrentScene(null);
setIsLoading(false);
setError(null);
};
const renderContent = () => {
if (isLoading || step === AppStep.GENERATING) {
return <Loader message={loadingMessage} />;
}
switch (step) {
case AppStep.UPLOAD:
return <ImageUploader onImageUpload={handleImageUpload} />;
case AppStep.STYLE_SELECTION:
return userImage && <StyleSelector onSelect={handleStyleAndGenreSelect} />;
case AppStep.STORY:
return currentScene && <StoryCanvas scene={currentScene} onEdit={handleEditScene} onReset={handleReset} />;
default:
return <ImageUploader onImageUpload={handleImageUpload} />;
}
};
return (
<div className="min-h-screen bg-slate-900 text-white flex flex-col items-center p-4 sm:p-6 lg:p-8">
<Header />
<main className="w-full max-w-5xl mx-auto flex-grow flex flex-col items-center justify-center">
{error && (
<div className="bg-red-500/20 border border-red-500 text-red-300 p-4 rounded-lg mb-6 w-full max-w-md text-center">
<p className="font-semibold">An Error Occurred</p>
<p className="text-sm">{error}</p>
</div>
)}
{renderContent()}
</main>
</div>
);
};
export default App;