File size: 13,614 Bytes
f58c9d4 | 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
// Initialize Supabase
const supabase = createClient(
'https://iiwthrqlshgmzvgudpkt.supabase.co',
'sb_publishable_slaECC8UMtgUni2caodaZA_N-uidsoa'
);
// DOM Elements
const scriptsContainer = document.getElementById('scripts-container');
const contributorsContainer = document.getElementById('contributors-container');
const newestBtn = document.getElementById('newest-btn');
const popularBtn = document.getElementById('popular-btn');
const trendingBtn = document.getElementById('trending-btn');
const loadMoreBtn = document.getElementById('load-more');
// State
let currentSort = 'newest';
let currentPage = 1;
let isLoading = false;
// Format date
function formatDate(dateString) {
const date = new Date(dateString);
return date.toLocaleDateString('en-US', { year: 'numeric', month: 'short', day: 'numeric' });
}
// Render scripts
async function renderScripts(sortBy = 'newest', page = 1) {
try {
isLoading = true;
scriptsContainer.innerHTML = `
<div class="col-span-3 text-center py-12">
<div class="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-indigo-500 mx-auto mb-4"></div>
<p>Loading scripts...</p>
</div>
`;
const scripts = await fetchScripts(sortBy, page);
if (scripts.error) throw scripts.error;
if (scripts.length === 0) {
scriptsContainer.innerHTML = `
<div class="col-span-3 text-center py-12">
<i data-feather="alert-circle" class="w-12 h-12 text-indigo-500 mx-auto mb-4"></i>
<p class="text-xl">No scripts found</p>
<p class="text-gray-400 mt-2">Be the first to upload a script!</p>
</div>
`;
feather.replace();
return;
}
let html = '';
scripts.forEach(script => {
html += `
<div class="bg-gray-800 rounded-xl p-6 hover:shadow-lg transition">
<div class="flex items-center space-x-3 mb-4">
<div class="w-10 h-10 rounded-full bg-indigo-500 flex items-center justify-center">
<i data-feather="user" class="text-white"></i>
</div>
<span class="font-medium">${script.username || 'Anonymous'}</span>
</div>
<h3 class="text-xl font-bold mb-2">${script.title}</h3>
<p class="text-gray-400 mb-4">${script.description || 'No description provided'}</p>
<div class="flex justify-between text-sm text-gray-500">
<span><i data-feather="eye" class="inline mr-1"></i> ${script.view_count || 0}</span>
<span><i data-feather="heart" class="inline mr-1"></i> ${script.like_count || 0}</span>
<span><i data-feather="calendar" class="inline mr-1"></i> ${formatDate(script.created_at)}</span>
</div>
</div>
`;
});
scriptsContainer.innerHTML = html;
feather.replace();
loadMoreBtn.classList.toggle('hidden', scripts.length < 10);
} catch (error) {
console.error('Render error:', error);
scriptsContainer.innerHTML = `
<div class="col-span-3 text-center py-12">
<i data-feather="alert-triangle" class="w-12 h-12 text-red-500 mx-auto mb-4"></i>
<p class="text-xl">Error loading scripts</p>
<p class="text-gray-400 mt-2">${error.message}</p>
</div>
`;
feather.replace();
} finally {
isLoading = false;
}
}
// Render contributors
async function renderContributors() {
try {
contributorsContainer.innerHTML = `
<div class="col-span-6 text-center py-8">
<div class="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-indigo-500 mx-auto mb-4"></div>
<p>Loading contributors...</p>
</div>
`;
const { data: contributors, error } = await supabase
.from('profiles')
.select('username, avatar_url, script_count')
.order('script_count', { ascending: false })
.limit(6);
if (error) throw error;
let html = '';
contributors.forEach(contributor => {
html += `
<div class="bg-gray-800 rounded-lg p-4 text-center">
<div class="w-16 h-16 rounded-full bg-indigo-500 mx-auto mb-3 flex items-center justify-center overflow-hidden">
${contributor.avatar_url ?
`<img src="${contributor.avatar_url}" alt="${contributor.username}" class="w-full h-full object-cover">` :
`<i data-feather="user" class="text-white text-2xl"></i>`}
</div>
<h4 class="font-medium">${contributor.username}</h4>
<p class="text-sm text-gray-400">${contributor.script_count || 0} scripts</p>
</div>
`;
});
contributorsContainer.innerHTML = html;
feather.replace();
} catch (error) {
console.error('Contributors error:', error);
contributorsContainer.innerHTML = `
<div class="col-span-6 text-center py-8">
<i data-feather="alert-triangle" class="w-12 h-12 text-red-500 mx-auto mb-4"></i>
<p>Error loading contributors</p>
</div>
`;
feather.replace();
}
}
// Event Listeners
newestBtn.addEventListener('click', () => {
if (currentSort === 'newest' || isLoading) return;
currentSort = 'newest';
currentPage = 1;
newestBtn.classList.replace('bg-gray-700', 'bg-indigo-600');
popularBtn.classList.replace('bg-indigo-600', 'bg-gray-700');
trendingBtn.classList.replace('bg-indigo-600', 'bg-gray-700');
renderScripts('newest', 1);
});
popularBtn.addEventListener('click', () => {
if (currentSort === 'most_liked' || isLoading) return;
currentSort = 'most_liked';
currentPage = 1;
newestBtn.classList.replace('bg-indigo-600', 'bg-gray-700');
popularBtn.classList.replace('bg-gray-700', 'bg-indigo-600');
trendingBtn.classList.replace('bg-indigo-600', 'bg-gray-700');
renderScripts('most_liked', 1);
});
trendingBtn.addEventListener('click', () => {
if (currentSort === 'trending' || isLoading) return;
currentSort = 'trending';
currentPage = 1;
newestBtn.classList.replace('bg-indigo-600', 'bg-gray-700');
popularBtn.classList.replace('bg-indigo-600', 'bg-gray-700');
trendingBtn.classList.replace('bg-gray-700', 'bg-indigo-600');
renderScripts('trending', 1);
});
loadMoreBtn.addEventListener('click', () => {
if (isLoading) return;
currentPage++;
renderScripts(currentSort, currentPage);
});
// Initialize
document.addEventListener('DOMContentLoaded', () => {
renderScripts();
renderContributors();
feather.replace();
// Auth state listener
supabase.auth.onAuthStateChange((event, session) => {
if (event === 'SIGNED_IN') {
console.log('User signed in:', session.user);
// Update UI for logged in state
} else if (event === 'SIGNED_OUT') {
console.log('User signed out');
// Update UI for logged out state
}
});
});
// Auth functions
async function handleSignUp(email, password) {
try {
const { data, error } = await supabase.auth.signUp({
email,
password,
});
if (error) throw error;
return data;
} catch (error) {
console.error('Sign up error:', error.message);
return { error };
}
}
async function handleSignIn(email, password) {
try {
const { data, error } = await supabase.auth.signInWithPassword({
email,
password,
});
if (error) throw error;
return data;
} catch (error) {
console.error('Sign in error:', error.message);
return { error };
}
}
async function handleSignOut() {
try {
const { error } = await supabase.auth.signOut();
if (error) throw error;
return { success: true };
} catch (error) {
console.error('Sign out error:', error.message);
return { error };
}
}
// Script functions
async function fetchScripts(sortBy = 'newest', page = 1, searchQuery = '') {
try {
let query = supabase
.from('scripts')
.select('*')
.eq('visibility', 'public')
.order('created_at', { ascending: sortBy === 'newest' ? false : true });
if (searchQuery) {
query = query.or(`title.ilike.%${searchQuery}%,description.ilike.%${searchQuery}%`);
}
if (sortBy === 'most_liked') {
query = query.order('like_count', { ascending: false });
} else if (sortBy === 'trending') {
// Trending could be a combination of views and likes over time
query = query.order('view_count', { ascending: false });
}
const { data, error } = await query.range((page - 1) * 10, page * 10 - 1);
if (error) throw error;
return data;
} catch (error) {
console.error('Fetch scripts error:', error.message);
return { error };
}
}
async function fetchScriptById(id) {
try {
// Increment view count
await supabase.rpc('increment_script_views', { script_id: id });
const { data, error } = await supabase
.from('scripts')
.select('*, script_versions(*), profiles(username, avatar_url)')
.eq('id', id)
.single();
if (error) throw error;
return data;
} catch (error) {
console.error('Fetch script error:', error.message);
return { error };
}
}
// Comment functions
async function fetchComments(scriptId) {
try {
const { data, error } = await supabase
.from('comments_safe')
.select('*')
.eq('script_id', scriptId)
.order('created_at', { ascending: true });
if (error) throw error;
return data;
} catch (error) {
console.error('Fetch comments error:', error.message);
return { error };
}
}
async function postComment(scriptId, content, parentId = null) {
try {
const { data: { user } } = await supabase.auth.getUser();
if (!user) throw new Error('Not authenticated');
const { data, error } = await supabase
.from('comments')
.insert({
script_id: scriptId,
user_id: user.id,
content,
parent_id: parentId
})
.select()
.single();
if (error) throw error;
return data;
} catch (error) {
console.error('Post comment error:', error.message);
return { error };
}
}
// Like functions
async function toggleLike(scriptId) {
try {
const { data: { user } } = await supabase.auth.getUser();
if (!user) throw new Error('Not authenticated');
// Check if already liked
const { data: existingLike, error: likeError } = await supabase
.from('likes')
.select('id')
.eq('script_id', scriptId)
.eq('user_id', user.id)
.maybeSingle();
if (likeError) throw likeError;
if (existingLike) {
// Unlike
const { error } = await supabase
.from('likes')
.delete()
.eq('id', existingLike.id);
if (error) throw error;
return { action: 'unliked' };
} else {
// Like
const { error } = await supabase
.from('likes')
.insert({
script_id: scriptId,
user_id: user.id
});
if (error) throw error;
return { action: 'liked' };
}
} catch (error) {
console.error('Toggle like error:', error.message);
return { error };
}
}
// Report functions
async function submitReport(scriptId, reason) {
try {
const { data: { user } } = await supabase.auth.getUser();
if (!user) throw new Error('Not authenticated');
const { data, error } = await supabase
.from('reports')
.insert({
script_id: scriptId,
reporter_id: user.id,
reason
})
.select()
.single();
if (error) throw error;
return data;
} catch (error) {
console.error('Submit report error:', error.message);
return { error };
}
}
// Initialize event listeners when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
// Check auth state
supabase.auth.onAuthStateChange((event, session) => {
if (event === 'SIGNED_IN') {
console.log('User signed in:', session.user);
// Update UI to show logged in state
} else if (event === 'SIGNED_OUT') {
console.log('User signed out');
// Update UI to show logged out state
}
});
}); |