Files
Ninjaserver/public/index.html
2025-09-03 11:07:29 +02:00

427 lines
13 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ninja Server - Times Display</title>
<script src="https://unpkg.com/@supabase/supabase-js@2"></script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
}
.header {
background: rgba(255, 255, 255, 0.95);
backdrop-filter: blur(10px);
padding: 1rem 2rem;
box-shadow: 0 2px 20px rgba(0, 0, 0, 0.1);
display: flex;
justify-content: space-between;
align-items: center;
}
.logo h1 {
color: #333;
font-size: 1.8rem;
font-weight: 700;
}
.nav-buttons {
display: flex;
gap: 1rem;
align-items: center;
}
.btn {
padding: 0.5rem 1rem;
border: none;
border-radius: 8px;
font-size: 0.9rem;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
text-decoration: none;
display: inline-block;
}
.btn-admin {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
}
.btn-admin:hover {
transform: translateY(-1px);
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.3);
}
.btn-dashboard {
background: #28a745;
color: white;
}
.btn-dashboard:hover {
background: #218838;
transform: translateY(-1px);
}
.btn-logout {
background: #dc3545;
color: white;
}
.btn-logout:hover {
background: #c82333;
transform: translateY(-1px);
}
.container {
max-width: 1200px;
margin: 2rem auto;
padding: 0 2rem;
}
.welcome-card {
background: white;
border-radius: 20px;
padding: 2rem;
box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
margin-bottom: 2rem;
text-align: center;
}
.welcome-card h2 {
color: #333;
font-size: 2rem;
margin-bottom: 1rem;
}
.welcome-card p {
color: #666;
font-size: 1.1rem;
line-height: 1.6;
}
.times-section {
background: white;
border-radius: 20px;
padding: 2rem;
box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
margin-bottom: 2rem;
}
.times-section h3 {
color: #333;
font-size: 1.5rem;
margin-bottom: 1.5rem;
display: flex;
align-items: center;
gap: 0.5rem;
}
.times-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 1.5rem;
}
.time-card {
background: #f8f9fa;
border-radius: 15px;
padding: 1.5rem;
border-left: 4px solid #667eea;
transition: transform 0.3s ease;
}
.time-card:hover {
transform: translateY(-2px);
}
.time-card h4 {
color: #333;
margin-bottom: 0.5rem;
font-size: 1.1rem;
}
.time-card p {
color: #666;
margin-bottom: 0.25rem;
font-size: 0.9rem;
}
.time-value {
font-size: 1.2rem;
font-weight: bold;
color: #667eea;
}
.loading {
text-align: center;
padding: 2rem;
color: #666;
}
.spinner {
border: 3px solid #f3f3f3;
border-top: 3px solid #667eea;
border-radius: 50%;
width: 40px;
height: 40px;
animation: spin 1s linear infinite;
margin: 0 auto 1rem;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.no-data {
text-align: center;
padding: 2rem;
color: #666;
font-style: italic;
}
.user-info {
display: flex;
align-items: center;
gap: 0.5rem;
color: #666;
font-size: 0.9rem;
}
.user-avatar {
width: 30px;
height: 30px;
border-radius: 50%;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
display: flex;
align-items: center;
justify-content: center;
color: white;
font-weight: bold;
font-size: 0.8rem;
}
@media (max-width: 768px) {
.header {
padding: 1rem;
flex-direction: column;
gap: 1rem;
}
.nav-buttons {
flex-wrap: wrap;
justify-content: center;
}
.container {
padding: 0 1rem;
}
.times-grid {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<div class="header">
<div class="logo">
<h1>🥷 Ninja Server</h1>
</div>
<div class="nav-buttons">
<div id="userInfo" class="user-info" style="display: none;">
<div class="user-avatar" id="userAvatar">U</div>
<span id="userEmail">user@example.com</span>
</div>
<a href="login.html" class="btn btn-admin" id="adminLoginBtn">Admin Login</a>
<a href="dashboard.html" class="btn btn-dashboard" id="dashboardBtn" style="display: none;">Dashboard</a>
<button class="btn btn-logout" id="logoutBtn" onclick="logout()" style="display: none;">Logout</button>
</div>
</div>
<div class="container">
<div class="welcome-card">
<h2>Welcome to Ninja Server! 🥷</h2>
<p>View the latest times and performance data from our ninja training sessions.</p>
</div>
<div class="times-section">
<h3>⏱️ Latest Times</h3>
<div id="loading" class="loading">
<div class="spinner"></div>
<p>Loading times...</p>
</div>
<div id="timesContent" style="display: none;">
<div class="times-grid" id="timesGrid">
<!-- Times will be loaded here -->
</div>
</div>
<div id="noData" class="no-data" style="display: none;">
No times available yet. Check back later!
</div>
</div>
</div>
<script>
// Supabase configuration
const SUPABASE_URL = 'https://lfxlplnypzvjrhftaoog.supabase.co';
const SUPABASE_ANON_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImxmeGxwbG55cHp2anJoZnRhb29nIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NDkyMTQ3NzIsImV4cCI6MjA2NDc5MDc3Mn0.XR4preBqWAQ1rT4PFbpkmRdz57BTwIusBI89fIxDHM8';
// Initialize Supabase client
const supabase = window.supabase.createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
// Check authentication status
async function checkAuth() {
try {
const { data: { session } } = await supabase.auth.getSession();
if (session) {
// User is logged in, show user info and dashboard button
displayUserInfo(session.user);
showAuthenticatedUI();
} else {
// User is not logged in, show admin login button
showPublicUI();
}
} catch (error) {
console.error('Error checking auth:', error);
showPublicUI();
}
}
// Display user information
function displayUserInfo(user) {
const userEmail = document.getElementById('userEmail');
const userAvatar = document.getElementById('userAvatar');
userEmail.textContent = user.email;
userAvatar.textContent = user.email.charAt(0).toUpperCase();
}
// Show UI for authenticated users
function showAuthenticatedUI() {
document.getElementById('userInfo').style.display = 'flex';
document.getElementById('adminLoginBtn').style.display = 'none';
document.getElementById('dashboardBtn').style.display = 'inline-block';
document.getElementById('logoutBtn').style.display = 'inline-block';
}
// Show UI for public users
function showPublicUI() {
document.getElementById('userInfo').style.display = 'none';
document.getElementById('adminLoginBtn').style.display = 'inline-block';
document.getElementById('dashboardBtn').style.display = 'none';
document.getElementById('logoutBtn').style.display = 'none';
}
// Load times from local database
async function loadTimes() {
try {
// Fetch times from local database
const response = await fetch('/api/times');
if (!response.ok) {
throw new Error('Failed to fetch times');
}
const times = await response.json();
if (times && times.length > 0) {
displayTimes(times);
} else {
showNoData();
}
} catch (error) {
console.error('Error loading times:', error);
showNoData();
}
}
// Display times in the grid
function displayTimes(times) {
const timesGrid = document.getElementById('timesGrid');
timesGrid.innerHTML = '';
times.forEach(time => {
const timeCard = document.createElement('div');
timeCard.className = 'time-card';
// Format time from recorded_time object
let timeString = 'N/A';
if (time.recorded_time) {
const { minutes, seconds, milliseconds } = time.recorded_time;
timeString = `${minutes}:${seconds.toString().padStart(2, '0')}.${milliseconds}`;
}
const userName = time.player ?
`${time.player.firstname || ''} ${time.player.lastname || ''}`.trim() || 'Unknown Player' :
'Unknown Player';
const locationName = time.location ? time.location.name : 'Unknown Location';
const createdAt = new Date(time.created_at).toLocaleString();
timeCard.innerHTML = `
<h4>${userName}</h4>
<p><strong>Time:</strong> <span class="time-value">${timeString}</span></p>
<p><strong>Location:</strong> ${locationName}</p>
<p><strong>Date:</strong> ${createdAt}</p>
`;
timesGrid.appendChild(timeCard);
});
document.getElementById('loading').style.display = 'none';
document.getElementById('timesContent').style.display = 'block';
}
// Show no data message
function showNoData() {
document.getElementById('loading').style.display = 'none';
document.getElementById('noData').style.display = 'block';
}
// Logout function
async function logout() {
try {
const { error } = await supabase.auth.signOut();
if (error) {
console.error('Error logging out:', error);
} else {
// Refresh the page to update UI
window.location.reload();
}
} catch (error) {
console.error('Error during logout:', error);
}
}
// Listen for auth state changes
supabase.auth.onAuthStateChange((event, session) => {
if (event === 'SIGNED_OUT') {
showPublicUI();
} else if (event === 'SIGNED_IN' && session) {
displayUserInfo(session.user);
showAuthenticatedUI();
}
});
// Initialize page
async function init() {
await checkAuth();
await loadTimes();
}
// Start the application
init();
</script>
</body>
</html>