Spaces:
Sleeping
Sleeping
Create client/src/App.jsx
Browse files- client/src/App.jsx +99 -0
client/src/App.jsx
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useState, useEffect } from 'react';
|
| 2 |
+
import { createClient } from '@supabase/supabase-js';
|
| 3 |
+
|
| 4 |
+
const supabase = createClient(
|
| 5 |
+
import.meta.env.VITE_SUPABASE_URL,
|
| 6 |
+
import.meta.env.VITE_SUPABASE_ANON_KEY
|
| 7 |
+
);
|
| 8 |
+
|
| 9 |
+
function App() {
|
| 10 |
+
const [session, setSession] = useState(null);
|
| 11 |
+
const [driveConnected, setDriveConnected] = useState(false);
|
| 12 |
+
const [prompt, setPrompt] = useState("");
|
| 13 |
+
const [generating, setGenerating] = useState(false);
|
| 14 |
+
|
| 15 |
+
useEffect(() => {
|
| 16 |
+
supabase.auth.getSession().then(({ data: { session } }) => {
|
| 17 |
+
setSession(session);
|
| 18 |
+
if (session) checkDrive(session.user.id);
|
| 19 |
+
});
|
| 20 |
+
// Puter check
|
| 21 |
+
if (window.puter) {
|
| 22 |
+
// Puter loaded
|
| 23 |
+
}
|
| 24 |
+
}, []);
|
| 25 |
+
|
| 26 |
+
const checkDrive = async (userId) => {
|
| 27 |
+
const { data } = await supabase.from('drive_accounts').select('id').eq('user_id', userId).single();
|
| 28 |
+
if (data) setDriveConnected(true);
|
| 29 |
+
};
|
| 30 |
+
|
| 31 |
+
const handleGenerate = async () => {
|
| 32 |
+
setGenerating(true);
|
| 33 |
+
try {
|
| 34 |
+
if (!window.puter.auth.isSignedIn()) await window.puter.auth.signIn();
|
| 35 |
+
const imgElement = await window.puter.ai.txt2img(prompt);
|
| 36 |
+
|
| 37 |
+
const res = await fetch(imgElement.src);
|
| 38 |
+
const blob = await res.blob();
|
| 39 |
+
|
| 40 |
+
if (driveConnected) {
|
| 41 |
+
const formData = new FormData();
|
| 42 |
+
formData.append('image', blob);
|
| 43 |
+
formData.append('prompt', prompt);
|
| 44 |
+
const { data: { session } } = await supabase.auth.getSession();
|
| 45 |
+
|
| 46 |
+
await fetch('/api/drive/upload', {
|
| 47 |
+
method: 'POST',
|
| 48 |
+
headers: { 'Authorization': `Bearer ${session.access_token}` },
|
| 49 |
+
body: formData
|
| 50 |
+
});
|
| 51 |
+
alert("Saved to Google Drive!");
|
| 52 |
+
} else {
|
| 53 |
+
alert("Generated! (Not saved - connect Drive first)");
|
| 54 |
+
}
|
| 55 |
+
} catch (e) {
|
| 56 |
+
alert("Error: " + e.message);
|
| 57 |
+
} finally {
|
| 58 |
+
setGenerating(false);
|
| 59 |
+
}
|
| 60 |
+
};
|
| 61 |
+
|
| 62 |
+
if (!session) return (
|
| 63 |
+
<div style={{height:'100vh', display:'flex', alignItems:'center', justifyContent:'center', background:'#111', color:'white'}}>
|
| 64 |
+
<button onClick={() => supabase.auth.signInWithOAuth({ provider: 'google' })} style={{padding:'15px', fontSize:'18px', cursor:'pointer'}}>
|
| 65 |
+
Log in with Google
|
| 66 |
+
</button>
|
| 67 |
+
</div>
|
| 68 |
+
);
|
| 69 |
+
|
| 70 |
+
return (
|
| 71 |
+
<div style={{padding: '20px', background: '#111', color: '#fff', minHeight: '100vh', fontFamily: 'sans-serif'}}>
|
| 72 |
+
<h1>FluxGen + Drive</h1>
|
| 73 |
+
{!driveConnected && (
|
| 74 |
+
<button
|
| 75 |
+
onClick={() => window.location.href=`/api/drive/connect?user_id=${session.user.id}`}
|
| 76 |
+
style={{width:'100%', padding:'10px', background:'#d97706', color:'white', border:'none', borderRadius:'5px', cursor:'pointer', marginBottom:'20px'}}
|
| 77 |
+
>
|
| 78 |
+
Connect Google Drive
|
| 79 |
+
</button>
|
| 80 |
+
)}
|
| 81 |
+
|
| 82 |
+
<textarea
|
| 83 |
+
value={prompt}
|
| 84 |
+
onChange={e => setPrompt(e.target.value)}
|
| 85 |
+
placeholder="Describe your image..."
|
| 86 |
+
style={{width:'100%', padding: '10px', height:'100px', borderRadius:'5px', border:'1px solid #333', background:'#222', color:'white'}}
|
| 87 |
+
/>
|
| 88 |
+
|
| 89 |
+
<button
|
| 90 |
+
onClick={handleGenerate}
|
| 91 |
+
disabled={generating}
|
| 92 |
+
style={{width:'100%', padding:'15px', marginTop:'20px', background: generating ? '#555' : '#7c3aed', color:'white', border:'none', borderRadius:'5px', cursor:'pointer', fontSize:'16px'}}
|
| 93 |
+
>
|
| 94 |
+
{generating ? "Generating..." : "Generate Image"}
|
| 95 |
+
</button>
|
| 96 |
+
</div>
|
| 97 |
+
);
|
| 98 |
+
}
|
| 99 |
+
export default App;
|