6};
7
8export default async function fetchWithCache(
9 apiUrl: string,
10 cacheKey: string,
37 }
38
39 // Fetch new data if no valid cache exists
40 const response = await fetch(apiUrl);
41 if (!response.ok) {
42 throw new Error(`API responded with status: ${response.status}`);
64 });
65 } catch (error) {
66 console.error("Error fetching data:", error);
67 return new Response(JSON.stringify({ error: "Failed to fetch data" }), {
68 status: 500,
69 headers: { "Content-Type": "application/json" },
1import fetchWithCache from "./fetchWithCache.ts";
2
3export type APOD = {
18 const cacheMinutes = 60;
19
20 const data: APOD = await fetchWithCache(url, cacheKey, cacheMinutes).then((res) => res.json());
21
22 if (!data) {
1export async function projectIdea(topic = "Ø§Ù„ØµØØ©") {
2 const response = await fetch("https://api.openai.com/v1/chat/completions", {
3 method: "POST",
4 headers: {
68 headers?: Record<string, string>;
69}) => {
70 let result = await fetch(
71 `${API_URL}/v1/email`,
72 {
146 }
147
148 fetch('/api/voicenotes', {
149 method: 'POST',
150 body: formData
14
15 useEffect(() => {
16 fetchVoiceNote();
17 }, [voiceNoteId]);
18
19 const fetchVoiceNote = async () => {
20 try {
21 setLoading(true);
22 const response = await fetch(`/api/voicenotes/${voiceNoteId}`);
23 const data = await response.json();
24
32 } catch (err) {
33 setError('Failed to load voice note');
34 console.error('Error fetching voice note:', err);
35 } finally {
36 setLoading(false);
23 return c.json({ success: true, voiceNotes });
24 } catch (error) {
25 console.error("Error fetching voice notes:", error);
26 return c.json({ success: true, voiceNotes: [] });
27 }
98
99 } catch (error) {
100 console.error("Error fetching voice note:", error);
101 return c.json({ success: false, error: "Failed to fetch voice note" }, 500);
102 }
103});
23app.route("/", staticRoutes);
24
25export default app.fetch;
9
10 useEffect(() => {
11 fetchVoiceNotes();
12 }, []);
13
14 const fetchVoiceNotes = async () => {
15 try {
16 setLoading(true);
17 const response = await fetch('/api/voicenotes');
18 const data = await response.json();
19
21 setVoiceNotes(data.voiceNotes || []);
22 } else {
23 setError(data.error || 'Failed to fetch voice notes');
24 }
25 } catch (err) {
26 setError('Failed to load voice notes');
27 console.error('Error fetching voice notes:', err);
28 } finally {
29 setLoading(false);
120
121 try {
122 const response = await fetch(`/api/voicenotes/${voiceNoteId}`, {
123 method: 'DELETE'
124 });
160 <p className="text-red-600 mb-4">{error}</p>
161 <button
162 onClick={fetchVoiceNotes}
163 className="px-6 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700"
164 >
177 </h2>
178 <button
179 onClick={fetchVoiceNotes}
180 className="px-4 py-2 bg-gray-100 text-gray-700 rounded-md hover:bg-gray-200 text-sm"
181 >
29 setLoading(false);
30 } else {
31 fetchMatches();
32 }
33 }, []);
72 }, [matches, liveUpdatesEnabled]);
73
74 // Fetch matches for selected date
75 useEffect(() => {
76 if (selectedDate !== new Date().toISOString().split('T')[0]) {
77 fetchMatchesByDate(selectedDate);
78 }
79 }, [selectedDate]);
80
81 const fetchMatches = async (isAutoRefresh = false) => {
82 try {
83 if (!isAutoRefresh) {
88 setError('');
89
90 const response = await fetch('/api/matches');
91 if (!response.ok) {
92 throw new Error('Failed to fetch matches');
93 }
94
98 } catch (err) {
99 setError(err instanceof Error ? err.message : 'An error occurred');
100 console.error('Error fetching matches:', err);
101 } finally {
102 setLoading(false);
105 };
106
107 const fetchMatchesByDate = async (date: string) => {
108 try {
109 setLoading(true);
110 setError('');
111
112 const response = await fetch(`/api/matches/${date}`);
113 if (!response.ok) {
114 throw new Error('Failed to fetch matches for selected date');
115 }
116
120 } catch (err) {
121 setError(err instanceof Error ? err.message : 'An error occurred');
122 console.error('Error fetching matches by date:', err);
123 } finally {
124 setLoading(false);
128 const handleRefresh = () => {
129 if (selectedDate === new Date().toISOString().split('T')[0]) {
130 fetchMatches();
131 } else {
132 fetchMatchesByDate(selectedDate);
133 }
134 };