pulse-whoop / script.js
CSNET's picture
**Project Name:** ชีพจร (Pulse) - The Ultimate Thai Health & Recovery Platform
4a7cb62 verified
Raw History Blame
10.2 kB
// Global App State
const AppState = {
currentUser: {
name: 'พิมพ์',
recovery: 78,
strain: 12.5,
sleep: 82,
heartRate: 68
},
// Mock data for chart
heartRateData: {
labels: ['00:00', '04:00', '08:00', '12:00', '16:00', '20:00', '24:00'],
datasets: [{
label: 'Heart Rate',
data: [58, 62, 75, 82, 78, 70, 62],
borderColor: '#34d399',
backgroundColor: 'rgba(52, 211, 153, 0.1)',
tension: 0.4,
fill: true,
pointRadius: 0,
pointHoverRadius: 6
}]
}
};
// Initialize Heart Rate Chart
function initHeartRateChart() {
const ctx = document.getElementById('hrChart');
if (!ctx) return;
new Chart(ctx, {
type: 'line',
data: AppState.heartRateData,
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: { display: false },
tooltip: {
backgroundColor: 'rgba(17, 24, 39, 0.9)',
titleColor: '#34d399',
bodyColor: '#e5e7eb',
borderColor: '#34d399',
borderWidth: 1,
callbacks: {
label: (context) => `${context.parsed.y} bpm`
}
}
},
scales: {
x: {
grid: { display: false },
ticks: {
color: '#9ca3af',
font: { family: 'Prompt' }
}
},
y: {
grid: {
color: 'rgba(75, 85, 99, 0.3)',
borderDash: [5, 5]
},
ticks: {
color: '#9ca3af',
font: { family: 'Prompt' }
}
}
},
interaction: {
intersect: false,
mode: 'index'
}
}
});
}
// Simulate Real-time Heart Rate Updates
function simulateRealtimeHR() {
const hrElement = document.querySelector('.text-sm.font-medium');
if (!hrElement) return;
setInterval(() => {
const variation = Math.floor(Math.random() * 10) - 5;
const newHR = Math.max(55, Math.min(85, AppState.currentUser.heartRate + variation));
hrElement.textContent = `${newHR} bpm`;
}, 3000);
}
// Quick Log Button Actions
function setupQuickLogButtons() {
const buttons = document.querySelectorAll('.quick-log-btn');
buttons.forEach(button => {
button.addEventListener('click', function() {
// Add ripple effect
const ripple = document.createElement('div');
ripple.className = 'absolute inset-0 bg-white/20 rounded-xl animate-ping';
this.appendChild(ripple);
setTimeout(() => ripple.remove(), 600);
// Show Thai notification
const text = this.textContent.trim();
showNotification(`กำลังบันทึก: ${text}...`, 'success');
});
});
}
// Notification System
function showNotification(message, type = 'info') {
const notification = document.createElement('div');
notification.className = `fixed top-4 left-4 right-4 z-50 p-4 rounded-xl shadow-lg transform translate-y-[-100px] opacity-0 transition-all duration-300 ${
type === 'success' ? 'bg-green-500' :
type === 'error' ? 'bg-red-500' : 'bg-blue-500'
}`;
notification.textContent = message;
document.body.appendChild(notification);
// Animate in
setTimeout(() => {
notification.style.transform = 'translateY(0)';
notification.style.opacity = '1';
}, 100);
// Remove after 3 seconds
setTimeout(() => {
notification.style.transform = 'translateY(-100px)';
notification.style.opacity = '0';
setTimeout(() => notification.remove(), 300);
}, 3000);
}
// Animate elements on scroll
function setupScrollAnimations() {
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('animate-fadeInUp');
}
});
}, { threshold: 0.1 });
document.querySelectorAll('.glass-card').forEach(card => {
observer.observe(card);
});
}
// AI Coach Dynamic Messages
function updateAICoachMessage() {
const messages = [
"วันนี้คุณฟื้นตัวเต็มที่! เหมาะสำหรับการออกกำลังกายระดับกลาง-หนัก อย่าลืมทานอาหารให้ครบ 5 หมู่นะคะ",
"ค่า HRV ของคุณดีขึ้น 15% จากสัปดาห์ที่แล้ว! แสดงว่าร่างกายกำลังฟื้นตัวได้ดีมากเลยค่ะ",
"พบว่าคืนที่คุณดื่มกาแฟหลัง 3 ทุ่ม คุณภาพการนอนจะลดลง 30% ลองเลี่ยงดื่มกาแฟในช่วงบ่ายดูนะคะ",
"วันนี้ความเครียดสะสมอยู่ในระดับปานกลาง แนะนำให้นั่งสมาธิ 10 นาทีก่อนนอนเพื่อลดระดับคอร์ติซอลค่ะ"
];
const coachCard = document.querySelector('ai-coach-card');
if (coachCard && Math.random() > 0.7) {
const randomMessage = messages[Math.floor(Math.random() * messages.length)];
coachCard.updateMessage(randomMessage);
}
}
// Hardware Pairing Simulation
function simulateHardwareConnection() {
const devices = [
{ name: 'Apple Watch Series 9', connected: true },
{ name: 'Oura Ring Gen 3', connected: false },
{ name: 'Polar H10', connected: false }
];
// Check if hardware is connected
const connectedDevice = devices.find(d => d.connected);
if (connectedDevice) {
console.log(`เชื่อมต่อกับ ${connectedDevice.name} เรียบร้อยแล้ว`);
}
}
// Office Syndrome Alert
function setupSedentaryAlert() {
let sittingTime = 0;
setInterval(() => {
// Simulate sedentary behavior (in real app, use accelerometer data)
const isSitting = Math.random() > 0.3;
if (isSitting) {
sittingTime += 1;
if (sittingTime >= 60) { // 1 hour
showOfficeSyndromeAlert();
sittingTime = 0;
}
} else {
sittingTime = 0;
}
}, 60000); // Check every minute
}
function showOfficeSyndromeAlert() {
const alert = document.createElement('div');
alert.className = 'fixed inset-0 bg-black/50 z-50 flex items-center justify-center p-4';
alert.innerHTML = `
<div class="glass-card rounded-2xl p-6 max-w-sm w-full border border-gray-700/50">
<div class="text-center">
<div class="w-16 h-16 bg-red-500 rounded-full mx-auto mb-4 flex items-center justify-center pulse-animation">
<i data-feather="alert-circle" class="w-8 h-8 text-white"></i>
</div>
<h3 class="text-xl font-bold mb-2">เตือนภัย Office Syndrome!</h3>
<p class="text-gray-400 mb-4">คุณนั่งนิ่งนานเกิน 1 ชั่วโมงแล้ว ลุกขึ้นมายืดเส้นยืดสายกันดีกว่าค่ะ</p>
<div class="flex gap-3">
<button onclick="this.closest('.fixed').remove()" class="flex-1 bg-gray-700 hover:bg-gray-600 py-3 rounded-xl font-medium transition-colors">
เลื่อนไปก่อน
</button>
<button onclick="startMicroWorkout(); this.closest('.fixed').remove()" class="flex-1 bg-gradient-to-r from-green-500 to-emerald-600 py-3 rounded-xl font-medium transition-all hover:scale-[1.02]">
เริ่มเลย!
</button>
</div>
</div>
</div>
`;
document.body.appendChild(alert);
feather.replace();
// Auto close after 30 seconds
setTimeout(() => alert.remove(), 30000);
}
function startMicroWorkout() {
showNotification('กำลังเปิดวิดีโอท่ายืดเหยียด...', 'success');
// In real app, would open video player
}
// Initialize App
document.addEventListener('DOMContentLoaded', function() {
// Set initial animations
document.querySelectorAll('section').forEach((section, index) => {
section.style.animationDelay = `${index * 0.1}s`;
section.classList.add('animate-fadeInUp');
});
// Initialize components
initHeartRateChart();
simulateRealtimeHR();
setupQuickLogButtons();
setupScrollAnimations();
simulateHardwareConnection();
// Simulate periodic updates
setInterval(updateAICoachMessage, 10000);
setTimeout(setupSedentaryAlert, 5000); // Start checking after 5 seconds
// Request permissions (in real app)
console.log('กำลังขอสิทธิ์เข้าถึงข้อมูลสุขภาพ...');
// Show welcome notification
setTimeout(() => {
showNotification('ยินดีต้อนรับสู่ ชีพจร! AI กำลังวิเคราะห์ข้อมูลของคุณ...', 'success');
}, 1000);
});
// Export for other modules
window.AppState = AppState;
window.showNotification = showNotification;