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React, { useEffect, useState } from 'react';
|
| 2 |
+
import TmaButton from '../components/TmaButton';
|
| 3 |
+
import TmaInput from '../components/TmaInput';
|
| 4 |
+
import TmaCard from '../components/TmaCard';
|
| 5 |
+
import TmaLoader from '../components/TmaLoader';
|
| 6 |
+
|
| 7 |
+
export default function Home() {
|
| 8 |
+
const [user, setUser] = useState(null);
|
| 9 |
+
const [loading, setLoading] = useState(true);
|
| 10 |
+
const [formData, setFormData] = useState({
|
| 11 |
+
name: '',
|
| 12 |
+
message: '',
|
| 13 |
+
});
|
| 14 |
+
const [response, setResponse] = useState(null);
|
| 15 |
+
|
| 16 |
+
// Initialize Telegram WebApp SDK
|
| 17 |
+
useEffect(() => {
|
| 18 |
+
if (typeof window !== 'undefined' && window.Telegram?.WebApp) {
|
| 19 |
+
const tg = window.Telegram.WebApp;
|
| 20 |
+
|
| 21 |
+
// Expand the app to full height
|
| 22 |
+
tg.expand();
|
| 23 |
+
|
| 24 |
+
// Set theme colors based on user's Telegram theme
|
| 25 |
+
document.body.style.backgroundColor = tg.themeParams.bg_color || '#ffffff';
|
| 26 |
+
|
| 27 |
+
// Initialize user data if available
|
| 28 |
+
if (tg.initDataUnsafe?.user) {
|
| 29 |
+
setUser(tg.initDataUnsafe.user);
|
| 30 |
+
setFormData(prev => ({
|
| 31 |
+
...prev,
|
| 32 |
+
name: tg.initDataUnsafe.user.first_name || '',
|
| 33 |
+
}));
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
setLoading(false);
|
| 37 |
+
} else {
|
| 38 |
+
// Fallback for development environment (non-Telegram)
|
| 39 |
+
setLoading(false);
|
| 40 |
+
}
|
| 41 |
+
}, []);
|
| 42 |
+
|
| 43 |
+
const handleInputChange = (e) => {
|
| 44 |
+
const { name, value } = e.target;
|
| 45 |
+
setFormData(prev => ({
|
| 46 |
+
...prev,
|
| 47 |
+
[name]: value,
|
| 48 |
+
}));
|
| 49 |
+
};
|
| 50 |
+
|
| 51 |
+
const handleSubmit = async (e) => {
|
| 52 |
+
e.preventDefault();
|
| 53 |
+
setLoading(true);
|
| 54 |
+
|
| 55 |
+
try {
|
| 56 |
+
// Simulate API call
|
| 57 |
+
await new Promise(resolve => setTimeout(resolve, 1500));
|
| 58 |
+
setResponse({
|
| 59 |
+
status: 'success',
|
| 60 |
+
data: `Hello, ${formData.name}! Your message has been sent successfully.`,
|
| 61 |
+
});
|
| 62 |
+
} catch (error) {
|
| 63 |
+
setResponse({
|
| 64 |
+
status: 'error',
|
| 65 |
+
data: 'Something went wrong. Please try again.',
|
| 66 |
+
});
|
| 67 |
+
} finally {
|
| 68 |
+
setLoading(false);
|
| 69 |
+
}
|
| 70 |
+
};
|
| 71 |
+
|
| 72 |
+
if (loading) {
|
| 73 |
+
return <TmaLoader />;
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
return (
|
| 77 |
+
<div className="min-h-screen">
|
| 78 |
+
<div className="bg-tg-bg border-b border-tg-border">
|
| 79 |
+
<div className="max-w-md mx-auto px-4 py-4 flex items-center gap-3">
|
| 80 |
+
{user ? (
|
| 81 |
+
<>
|
| 82 |
+
<div className="w-10 h-10 rounded-full bg-tg-secondary text-white flex items-center justify-center font-bold text-lg">
|
| 83 |
+
{user.first_name?.[0]}
|
| 84 |
+
</div>
|
| 85 |
+
<div className="flex flex-col">
|
| 86 |
+
<span className="text-sm font-bold text-tg-text">{user.first_name} {user.last_name}</span>
|
| 87 |
+
<span className="text-xs text-tg-textSecondary">@{user.username || 'User'}</span>
|
| 88 |
+
</div>
|
| 89 |
+
</>
|
| 90 |
+
) : (
|
| 91 |
+
<div className="flex flex-col">
|
| 92 |
+
<span className="text-sm font-bold text-tg-text">Guest User</span>
|
| 93 |
+
</div>
|
| 94 |
+
)}
|
| 95 |
+
</div>
|
| 96 |
+
</div>
|
| 97 |
+
|
| 98 |
+
<div className="p-4">
|
| 99 |
+
<TmaCard title="User Profile" description="View your Telegram account details">
|
| 100 |
+
{user ? (
|
| 101 |
+
<div className="grid grid-cols-2 gap-3 text-sm">
|
| 102 |
+
<div className="bg-gray-50 p-3 rounded-lg">
|
| 103 |
+
<span className="text-tg-textSecondary block text-xs">ID</span>
|
| 104 |
+
<span className="font-medium text-tg-text">{user.id}</span>
|
| 105 |
+
</div>
|
| 106 |
+
<div className="bg-gray-50 p-3 rounded-lg">
|
| 107 |
+
<span className="text-tg-textSecondary block text-xs">Language</span>
|
| 108 |
+
<span className="font-medium text-tg-text">{user.language_code || 'N/A'}</span>
|
| 109 |
+
</div>
|
| 110 |
+
</div>
|
| 111 |
+
) : (
|
| 112 |
+
<p className="text-sm text-tg-textSecondary">Not logged in via Telegram.</p>
|
| 113 |
+
)}
|
| 114 |
+
</TmaCard>
|
| 115 |
+
|
| 116 |
+
<TmaCard title="Send Message" description="Interact with the bot">
|
| 117 |
+
<form onSubmit={handleSubmit} className="space-y-4">
|
| 118 |
+
<TmaInput
|
| 119 |
+
label="Name"
|
| 120 |
+
name="name"
|
| 121 |
+
value={formData.name}
|
| 122 |
+
onChange={handleInputChange}
|
| 123 |
+
placeholder="Enter your name"
|
| 124 |
+
type="text"
|
| 125 |
+
/>
|
| 126 |
+
<TmaInput
|
| 127 |
+
label="Message"
|
| 128 |
+
name="message"
|
| 129 |
+
value={formData.message}
|
| 130 |
+
onChange={handleInputChange}
|
| 131 |
+
placeholder="Type your message here..."
|
| 132 |
+
type="text"
|
| 133 |
+
/>
|
| 134 |
+
<TmaButton
|
| 135 |
+
text="Send Message"
|
| 136 |
+
loading={loading}
|
| 137 |
+
disabled={loading}
|
| 138 |
+
/>
|
| 139 |
+
</form>
|
| 140 |
+
</TmaCard>
|
| 141 |
+
|
| 142 |
+
{response && (
|
| 143 |
+
<TmaCard title="Response">
|
| 144 |
+
<div className={`p-3 rounded-lg text-sm ${
|
| 145 |
+
response.status === 'success' ? 'bg-green-50 text-green-700' : 'bg-red-50 text-red-700'
|
| 146 |
+
}`}>
|
| 147 |
+
{response.data}
|
| 148 |
+
</div>
|
| 149 |
+
</TmaCard>
|
| 150 |
+
)}
|
| 151 |
+
</div>
|
| 152 |
+
</div>
|
| 153 |
+
);
|
| 154 |
+
}
|