Skip to content

Commit 688f0cb

Browse files
committed
API calls for restart, update
1 parent 82ac72c commit 688f0cb

File tree

2 files changed

+106
-65
lines changed

2 files changed

+106
-65
lines changed

ultroid/app/page.tsx

Lines changed: 39 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,8 @@ export default function Home() {
8787

8888
const showPopup = useShowPopup();
8989
const [colorScheme, themeParams] = useThemeParams();
90-
const [initDataUnsafe, initData] = useInitData();
9190
const router = useRouter();
9291

93-
// Update the webAppParams object
94-
const webAppParams = {
95-
colorScheme,
96-
themeParams,
97-
initData,
98-
initDataUnsafe,
99-
};
100-
101-
// Add action buttons configuration
10292
const actionButtons: ActionButton[] = [
10393
{
10494
label: "View Assistant",
@@ -116,11 +106,23 @@ export default function Home() {
116106
},
117107
{
118108
label: "Restart",
119-
action: () => {
120-
showPopup({
121-
message: 'Restarting bot...',
122-
buttons: [{ type: 'ok' }]
123-
});
109+
action: async () => {
110+
try {
111+
showPopup({
112+
message: 'Restarting bot...',
113+
buttons: [{ type: 'ok' }]
114+
});
115+
const result = await api.api.restartBot();
116+
showPopup({
117+
message: result.status || 'Bot restarted successfully',
118+
buttons: [{ type: 'ok' }]
119+
});
120+
} catch (error) {
121+
showPopup({
122+
message: 'Failed to restart bot. Please try again.',
123+
buttons: [{ type: 'ok' }]
124+
});
125+
}
124126
},
125127
icon: (
126128
<svg className="w-5 h-5" viewBox="0 0 24 24" fill="none" stroke="currentColor">
@@ -130,11 +132,23 @@ export default function Home() {
130132
},
131133
{
132134
label: "Update",
133-
action: () => {
134-
showPopup({
135-
message: 'Checking for updates...',
136-
buttons: [{ type: 'ok' }]
137-
});
135+
action: async () => {
136+
try {
137+
showPopup({
138+
message: 'Checking for updates...',
139+
buttons: [{ type: 'ok' }]
140+
});
141+
const result = await api.api.updateBot();
142+
showPopup({
143+
message: result.status || 'Bot updated successfully',
144+
buttons: [{ type: 'ok' }]
145+
});
146+
} catch (error) {
147+
showPopup({
148+
message: 'Failed to update bot. Please try again.',
149+
buttons: [{ type: 'ok' }]
150+
});
151+
}
138152
},
139153
icon: (
140154
<svg className="w-5 h-5" viewBox="0 0 24 24" fill="none" stroke="currentColor">
@@ -144,7 +158,6 @@ export default function Home() {
144158
}
145159
];
146160

147-
// Update scroll handler to include header hiding
148161
useEffect(() => {
149162
const handleScroll = () => {
150163
const currentScrollY = window.scrollY;
@@ -158,7 +171,6 @@ export default function Home() {
158171
setLastScrollY(currentScrollY);
159172
setScrollY(currentScrollY);
160173

161-
// Check if elements are in viewport
162174
const observer = new IntersectionObserver(
163175
(entries) => {
164176
entries.forEach((entry) => {
@@ -240,7 +252,6 @@ export default function Home() {
240252

241253
const avatarUrl = getUserAvatar(userData);
242254

243-
// Enhanced parallax transforms with smooth easing
244255
const headerTransform = `translateY(${scrollY * 0.2}px) scale(${1 - scrollY * 0.0005})`;
245256
const statsTransform = `translateY(${scrollY * 0.1}px)`;
246257
const skillsTransform = `translateY(${scrollY * 0.05}px)`;
@@ -344,7 +355,7 @@ export default function Home() {
344355
className={`text-center mb-16 relative transform transition-all duration-700 ${
345356
isVisible ? 'translate-y-0 opacity-100' : 'translate-y-10 opacity-0'
346357
}`}
347-
style={{ transform: headerTransform, transition: 'transform 0.2s cubic-bezier(0.4, 0, 0.2, 1)' }}
358+
style={{ transform: headerTransform }}
348359
>
349360
<div className="relative w-48 h-48 mx-auto mb-8">
350361
<div className="relative w-full h-full rounded-full overflow-hidden shadow-2xl ring-2 ring-primary/20 transition-transform duration-300 hover:scale-105">
@@ -386,14 +397,14 @@ export default function Home() {
386397
className={`mb-16 relative transform transition-all duration-700 ${
387398
isVisible ? 'translate-y-0 opacity-100' : 'translate-y-10 opacity-0'
388399
}`}
389-
style={{ transform: statsTransform, transition: 'transform 0.2s cubic-bezier(0.4, 0, 0.2, 1)' }}
400+
style={{ transform: statsTransform }}
390401
>
391402
<div className="p-8 rounded-2xl bg-white/5 text-center shadow-xl backdrop-blur-md border border-white/10 hover:border-primary/20 transition-all duration-500 group">
392403
<div className="relative">
393404
<div className="absolute inset-0 bg-gradient-to-r from-primary/20 to-accent/20 blur-xl rounded-full opacity-0 group-hover:opacity-100 transition-opacity duration-500"></div>
394405
<div className="relative">
395-
<div className="text-sm text-gray-400 uppercase tracking-wider font-medium">Uptime since</div>
396-
<div className="text-5xl font-bold text-primary mb-2 transition-all duration-300 group-hover:scale-110">{userData.stats.uptime}</div>
406+
<div className="text-sm text-gray-400 uppercase tracking-wider font-medium">Uptime since</div>
407+
<div className="text-5xl font-bold text-primary mb-2 transition-all duration-300 group-hover:scale-110">{userData.stats.uptime}</div>
397408
</div>
398409
</div>
399410
</div>
@@ -404,7 +415,7 @@ export default function Home() {
404415
className={`mb-16 relative transform transition-all duration-700 ${
405416
isVisible ? 'translate-y-0 opacity-100' : 'translate-y-10 opacity-0'
406417
}`}
407-
style={{ transform: skillsTransform, transition: 'transform 0.2s cubic-bezier(0.4, 0, 0.2, 1)' }}
418+
style={{ transform: skillsTransform }}
408419
>
409420
<h2 className="text-2xl font-bold mb-6 text-white relative inline-block">
410421
<span className="relative">
@@ -430,34 +441,6 @@ export default function Home() {
430441
</div>
431442
</div>
432443

433-
<div className="mb-16 space-y-6">
434-
<div className="p-6 rounded-2xl bg-white/5 backdrop-blur-md border border-white/10">
435-
<h2 className="text-2xl font-bold mb-4 text-white">
436-
Theme Parameters
437-
</h2>
438-
<pre className="overflow-auto p-4 rounded-lg bg-black/30 text-white/90">
439-
{JSON.stringify({ colorScheme, themeParams }, null, 2)}
440-
</pre>
441-
</div>
442-
443-
<div className="p-6 rounded-2xl bg-white/5 backdrop-blur-md border border-white/10">
444-
<h2 className="text-2xl font-bold mb-4 text-white">
445-
Auth Data
446-
</h2>
447-
<pre className="overflow-auto p-4 rounded-lg bg-black/30 text-white/90">
448-
{JSON.stringify(
449-
{
450-
user: initDataUnsafe?.user || null,
451-
startParam: initDataUnsafe?.start_param || null,
452-
initData: initData || null,
453-
},
454-
null,
455-
2
456-
)}
457-
</pre>
458-
</div>
459-
</div>
460-
461444
<div className="mb-16">
462445
<div className="grid grid-cols-3 gap-4">
463446
{actionButtons.map((button, index) => (

ultroid/utils/api.ts

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,60 @@ export type UserData = {
1818
uptime: string;
1919
};
2020
skills: string[];
21+
authenticated_user?: Record<string, any>;
22+
start_param?: string;
23+
auth_date?: number;
24+
};
25+
26+
// Helper function to get init data
27+
const getInitData = (): string | null => {
28+
try {
29+
// Check if we're in a Telegram Web App environment
30+
if (typeof window !== 'undefined' && window.Telegram?.WebApp) {
31+
return window.Telegram.WebApp.initData;
32+
}
33+
return null;
34+
} catch (error) {
35+
console.error('Error getting init data:', error);
36+
return null;
37+
}
38+
};
39+
40+
// Helper function to make authenticated API calls
41+
const makeAuthenticatedRequest = async (endpoint: string, method: string = 'GET') => {
42+
const initData = getInitData();
43+
const headers: HeadersInit = {
44+
'Content-Type': 'application/json'
45+
};
46+
47+
if (initData) {
48+
headers['Authorization'] = `tma ${initData}`;
49+
} else {
50+
console.warn('No init data available - request will not be authenticated');
51+
}
52+
53+
const response = await fetch(`${BASE_URL}${endpoint}`, {
54+
method,
55+
headers
56+
});
57+
58+
if (!response.ok) {
59+
const errorData = await response.text();
60+
console.error('API Error Response:', {
61+
status: response.status,
62+
statusText: response.statusText,
63+
data: errorData
64+
});
65+
throw new Error(`API request failed: ${response.status} - ${errorData}`);
66+
}
67+
68+
return response.json();
2169
};
2270

2371
export const api = {
2472
async getUserData(): Promise<UserData> {
2573
try {
26-
// Use the correct endpoint that we defined in the server
27-
const response = await fetch(`${BASE_URL}/api/user`);
28-
29-
if (!response.ok) {
30-
throw new Error(`Failed to fetch user data: ${response.status}`);
31-
}
32-
33-
const data = await response.json();
34-
return data;
74+
return await makeAuthenticatedRequest('/api/user');
3575
} catch (error) {
3676
console.error('API Error:', error);
3777
// Return fallback data for development
@@ -47,5 +87,23 @@ export const api = {
4787
skills: ["Telegram Bot Management", "Automation", "Python"]
4888
};
4989
}
90+
},
91+
92+
async restartBot(): Promise<{ status: string }> {
93+
try {
94+
return await makeAuthenticatedRequest('/api/admin/restart', 'POST');
95+
} catch (error) {
96+
console.error('API Error:', error);
97+
throw error;
98+
}
99+
},
100+
101+
async updateBot(): Promise<{ status: string }> {
102+
try {
103+
return await makeAuthenticatedRequest('/api/admin/update', 'POST');
104+
} catch (error) {
105+
console.error('API Error:', error);
106+
throw error;
107+
}
50108
}
51109
};

0 commit comments

Comments
 (0)