-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
75 lines (62 loc) · 2.75 KB
/
app.js
File metadata and controls
75 lines (62 loc) · 2.75 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
// Supabase configuration - will be replaced by GitHub Actions
const SUPABASE_URL = 'SUPABASE_URL_PLACEHOLDER';
const SUPABASE_ANON_KEY = 'SUPABASE_ANON_KEY_PLACEHOLDER';
// Fallback content if Supabase is not configured or fails
const fallbackContent = {
quote: "The question of whether a computer can think is no more interesting than the question of whether a submarine can swim. - Edsger Dijkstra",
joke: "Why do programmers prefer dark mode? Because light attracts bugs! 🐛",
funfact: "The first computer bug was an actual bug! In 1947, a moth caused a malfunction in the Harvard Mark II computer. Grace Hopper taped the bug in her logbook with the note: 'First actual case of bug being found.'"
};
// Check if Supabase is configured
const isSupabaseConfigured = SUPABASE_URL !== 'SUPABASE_URL_PLACEHOLDER'
&& SUPABASE_ANON_KEY !== 'SUPABASE_ANON_KEY_PLACEHOLDER'
&& SUPABASE_URL.startsWith('https://');
async function fetchFromSupabase() {
if (!isSupabaseConfigured) {
console.log('Supabase not configured, using fallback content');
return fallbackContent;
}
try {
// Import Supabase client from CDN
const { createClient } = supabase;
const supabaseClient = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
// Fetch content from a 'content' table
// Expected table structure: id, type, text
const { data, error } = await supabaseClient
.from('content')
.select('type, text');
if (error) {
console.error('Supabase error:', error);
return fallbackContent;
}
// Transform data into our expected format
const content = {};
data.forEach(item => {
content[item.type] = item.text;
});
return {
quote: content.quote || fallbackContent.quote,
joke: content.joke || fallbackContent.joke,
funfact: content.funfact || fallbackContent.funfact
};
} catch (error) {
console.error('Error fetching from Supabase:', error);
return fallbackContent;
}
}
async function loadContent() {
const content = await fetchFromSupabase();
// Update the DOM
document.getElementById('quote').textContent = content.quote;
document.getElementById('quote').classList.remove('loading');
document.getElementById('joke').textContent = content.joke;
document.getElementById('joke').classList.remove('loading');
document.getElementById('funfact').textContent = content.funfact;
document.getElementById('funfact').classList.remove('loading');
}
// Load content when the page loads
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', loadContent);
} else {
loadContent();
}