Spaces:
Running
Running
Download components/ai-coach-card.js from CSNET/pulse-whoop: direct link, hf CLI and curl.
- Browser
- Download file 19.2 kB
-
https://huggingface.co/spaces/CSNET/pulse-whoop/resolve/main/components/ai-coach-card.js
- Command line
-
hf download hf://spaces/CSNET/pulse-whoop/components/ai-coach-card.js
-
curl -L -o ai-coach-card.js https://huggingface.co/spaces/CSNET/pulse-whoop/resolve/main/components/ai-coach-card.js
19.2 kB
| class AICoachCard extends HTMLElement { | |
| constructor() { | |
| super(); | |
| this.defaultMessage = "วันนี้คุณฟื้นตัวเต็มที่! เหมาะสำหรับการออกกำลังกายระดับกลาง-หนัก อย่าลืมทานอาหารให้ครบ 5 หมู่นะคะ"; | |
| this.currentMessage = this.defaultMessage; | |
| } | |
| connectedCallback() { | |
| this.attachShadow({ mode: 'open' }); | |
| this.render(); | |
| this.startTypingAnimation(); | |
| } | |
| render() { | |
| this.shadowRoot.innerHTML = ` | |
| <style> | |
| :host { | |
| display: block; | |
| margin: 0 1rem 1.5rem; | |
| } | |
| .coach-card { | |
| background: linear-gradient(135deg, rgba(52, 211, 153, 0.15), rgba(16, 185, 129, 0.1)); | |
| border: 1px solid rgba(52, 211, 153, 0.3); | |
| border-radius: 1.25rem; | |
| padding: 1.25rem; | |
| position: relative; | |
| overflow: hidden; | |
| backdrop-filter: blur(10px); | |
| -webkit-backdrop-filter: blur(10px); | |
| } | |
| .coach-avatar { | |
| position: absolute; | |
| top: -10px; | |
| right: -10px; | |
| width: 60px; | |
| height: 60px; | |
| background: linear-gradient(135deg, #34d399, #10b981); | |
| border-radius: 50%; | |
| display: flex; | |
| align-items: center; | |
| justify-content: center; | |
| box-shadow: 0 4px 15px rgba(52, 211, 153, 0.3); | |
| } | |
| .coach-icon { | |
| width: 30px; | |
| height: 30px; | |
| color: white; | |
| } | |
| .coach-header { | |
| display: flex; | |
| align-items: center; | |
| gap: 0.75rem; | |
| margin-bottom: 0.75rem; | |
| } | |
| .coach-title { | |
| font-size: 0.9rem; | |
| font-weight: 600; | |
| color: #34d399; | |
| font-family: 'Prompt', sans-serif; | |
| } | |
| .coach-badge { | |
| background: rgba(52, 211, 153, 0.2); | |
| color: #34d399; | |
| padding: 0.25rem 0.75rem; | |
| border-radius: 9999px; | |
| font-size: 0.7rem; | |
| font-weight: 500; | |
| } | |
| .coach-message { | |
| font-size: 0.85rem; | |
| line-height: 1.5; | |
| color: #e5e7eb; | |
| margin-bottom: 0.75rem; | |
| font-family: 'Prompt', sans-serif; | |
| min-height: 2.5rem; | |
| } | |
| .coach-message.typing { | |
| border-right: 2px solid #34d399; | |
| animation: typing 3s steps(40, end), blink-caret 0.75s step-end infinite; | |
| } | |
| @keyframes typing { | |
| from { width: 0 } | |
| to { width: 100% } | |
| } | |
| @keyframes blink-caret { | |
| from, to { border-color: transparent } | |
| 50% { border-color: #34d399 } | |
| } | |
| .coach-actions { | |
| display: flex; | |
| gap: 0.75rem; | |
| } | |
| .coach-btn { | |
| background: rgba(52, 211, 153, 0.1); | |
| border: 1px solid rgba(52, 211, 153, 0.3); | |
| color: #34d399; | |
| padding: 0.5rem 1rem; | |
| border-radius: 0.75rem; | |
| font-size: 0.75rem; | |
| font-weight: 500; | |
| cursor: pointer; | |
| transition: all 0.3s ease; | |
| text-decoration: none; | |
| display: inline-flex; | |
| align-items: center; | |
| gap: 0.5rem; | |
| font-family: 'Prompt', sans-serif; | |
| } | |
| .coach-btn:hover { | |
| background: rgba(52, 211, 153, 0.2); | |
| transform: translateY(-2px); | |
| } | |
| .pulse-ring { | |
| position: absolute; | |
| top: 0; | |
| left: 0; | |
| right: 0; | |
| bottom: 0; | |
| border-radius: 1.25rem; | |
| border: 2px solid transparent; | |
| animation: pulse-ring 2s cubic-bezier(0.455, 0.03, 0.515, 0.955) infinite; | |
| } | |
| @keyframes pulse-ring { | |
| 0% { | |
| transform: scale(0.9); | |
| border-color: rgba(52, 211, 153, 0.6); | |
| } | |
| 100% { | |
| transform: scale(1.05); | |
| border-color: transparent; | |
| } | |
| } | |
| .typing-dots { | |
| display: inline-flex; | |
| gap: 0.2rem; | |
| } | |
| .typing-dots { | |
| display: inline-flex; | |
| gap: 0.2rem; | |
| margin-left: 0.25rem; | |
| } | |
| .typing-dot { | |
| width: 4px; | |
| height: 4px; | |
| background: #34d399; | |
| border-radius: 50%; | |
| animation: typingDot 1.4s infinite; | |
| } | |
| .typing-dot:nth-child(2) { | |
| animation-delay: 0.2s; | |
| } | |
| .typing-dot:nth-child(3) { | |
| animation-delay: 0.4s; | |
| } | |
| @keyframes typingDot { | |
| 0%, 60%, 100% { | |
| transform: translateY(0); | |
| } | |
| 30% { | |
| transform: translateY(-10px); | |
| } | |
| } | |
| .coach-message.typing::after { | |
| content: ''; | |
| display: inline-block; | |
| width: 2px; | |
| height: 1em; | |
| background: #34d399; | |
| margin-left: 2px; | |
| animation: blink 1s infinite; | |
| } | |
| @keyframes blink { | |
| 0%, 50% { opacity: 1; } | |
| 51%, 100% { opacity: 0; } | |
| } | |
| .pulse-ring { | |
| position: absolute; | |
| top: 0; | |
| left: 0; | |
| right: 0; | |
| bottom: 0; | |
| border-radius: 1.25rem; | |
| border: 2px solid transparent; | |
| animation: pulse-ring 2s cubic-bezier(0.455, 0.03, 0.515, 0.955) infinite; | |
| } | |
| @keyframes pulse-ring { | |
| 0% { | |
| transform: scale(0.9); | |
| border-color: rgba(52, 211, 153, 0.6); | |
| } | |
| 100% { | |
| transform: scale(1.05); | |
| border-color: transparent; | |
| } | |
| } | |
| .typing-indicator { | |
| position: absolute; | |
| bottom: 0.5rem; | |
| right: 0.5rem; | |
| font-size: 0.6rem; | |
| color: #34d399; | |
| opacity: 0; | |
| } | |
| .typing-indicator.active { | |
| opacity: 1; | |
| } | |
| .typing-indicator::after { | |
| content: 'กำลังพิมพ์'; | |
| animation: typingText 2s infinite; | |
| } | |
| @keyframes typingText { | |
| 0% { content: 'กำลังพิมพ์'; } | |
| 33% { content: 'กำลังพิมพ์.'; } | |
| 66% { content: 'กำลังพิมพ์..'; } | |
| 100% { content: 'กำลังพิมพ์...'; } | |
| } | |
| .coach-timestamp { | |
| font-size: 0.6rem; | |
| color: #6b7280; | |
| position: absolute; | |
| bottom: 0.5rem; | |
| right: 0.5rem; | |
| } | |
| .action-buttons { | |
| display: flex; | |
| gap: 0.5rem; | |
| margin-top: 0.75rem; | |
| } | |
| .action-btn { | |
| flex: 1; | |
| padding: 0.4rem 0.75rem; | |
| background: rgba(52, 211, 153, 0.1); | |
| border: 1px solid rgba(52, 211, 153, 0.3); | |
| border-radius: 0.5rem; | |
| color: #34d399; | |
| font-size: 0.7rem; | |
| font-weight: 500; | |
| cursor: pointer; | |
| transition: all 0.3s ease; | |
| font-family: 'Prompt', sans-serif; | |
| } | |
| .action-btn:hover { | |
| background: rgba(52, 211, 153, 0.2); | |
| transform: translateY(-1px); | |
| } | |
| .action-btn.secondary { | |
| color: #a78bfa; | |
| border-color: rgba(167, 139, 250, 0.3); | |
| background: rgba(167, 139, 250, 0.1); | |
| } | |
| .action-btn.secondary:hover { | |
| background: rgba(167, 139, 250, 0.2); | |
| } | |
| </style> | |
| <div class="coach-card"> | |
| <div class="pulse-ring"></div> | |
| <div class="coach-header"> | |
| <div class="coach-info"> | |
| <div class="coach-title"> | |
| <div class="coach-avatar"> | |
| <svg class="coach-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"> | |
| <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2z"/> | |
| <path d="M8 14s1.5 2 4 2 4-2 4-2"/> | |
| <line x1="9" y1="9" x2="9.01" y2="9"/> | |
| <line x1="15" y1="9" x2="15.01" y2="9"/> | |
| </svg> | |
| </div> | |
| <div class="coach-title-text"> | |
| <div class="coach-name">AI Coach พิมพ์</div> | |
| <div class="coach-badge">อัจฉริยะ</div> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="coach-actions-container"> | |
| <div class="typing-indicator" id="typingIndicator"></div> | |
| <div class="coach-timestamp" id="timestamp"></div> | |
| </div> | |
| </div> | |
| <div class="coach-message ${this.isTyping ? 'typing' : ''}" id="message"> | |
| ${this.currentMessage} | |
| </div> | |
| <div class="action-buttons" id="actionButtons"></div> | |
| </div> | |
| `; | |
| } | |
| // Public API method | |
| updateMessage(message, actions = []) { | |
| try { | |
| this.currentMessage = message; | |
| this.isTyping = true; | |
| this.render(); | |
| // Simulate typing delay | |
| setTimeout(() => { | |
| try { | |
| this.isTyping = false; | |
| this.render(); | |
| this.updateTimestamp(); | |
| this.renderActions(actions); | |
| } catch (error) { | |
| console.error('AI Coach render error:', error); | |
| } | |
| }, 1000 + Math.random() * 1500); | |
| } catch (error) { | |
| console.error('AI Coach update message error:', error); | |
| } | |
| } | |
| renderActions(actions) { | |
| const container = this.shadowRoot.getElementById('actionButtons'); | |
| if (actions.length === 0) { | |
| container.innerHTML = ''; | |
| return; | |
| } | |
| container.innerHTML = actions.map(action => ` | |
| <button class="action-btn ${action.type || ''}" data-action="${action.id}"> | |
| ${action.label} | |
| </button> | |
| `).join(''); | |
| // Add event listeners | |
| container.querySelectorAll('.action-btn').forEach(btn => { | |
| btn.addEventListener('click', (e) => { | |
| const actionId = e.target.dataset.action; | |
| this.handleAction(actionId); | |
| }); | |
| }); | |
| } | |
| updateTimestamp() { | |
| const now = new Date(); | |
| const timestamp = this.shadowRoot.getElementById('timestamp'); | |
| timestamp.textContent = `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}`; | |
| } | |
| generateSmartMessage() { | |
| try { | |
| const templates = [ | |
| { | |
| condition: () => AppState && AppState.currentUser && AppState.currentUser.recovery > 80, | |
| message: "วันนี้คุณฟื้นตัวเต็มที่! เหมาะสำหรับการออกกำลังกายระดับกลาง-หนัก", | |
| actions: [ | |
| { id: 'workout', label: 'เริ่มออกกำลัง', type: 'primary' }, | |
| { id: 'nutrition', label: 'วางแผนมื้ออาหาร', type: 'secondary' } | |
| ] | |
| }, | |
| { | |
| condition: () => AppState && AppState.currentUser && AppState.currentUser.recovery < 50, | |
| message: "วันนี้ร่างกายของคุณยังฟื้นตัวไม่เต็มที่ ควรพักผ่อนเพิ่มเติม", | |
| actions: [ | |
| { id: 'meditation', label: 'นั่งสมาธิ', type: 'primary' }, | |
| { id: 'sleep', label: 'ดูคุณภาพการนอน', type: 'secondary' } | |
| ] | |
| }, | |
| { | |
| condition: () => AppState && AppState.currentUser && AppState.currentUser.strain > 15, | |
| message: "ความเครียดสะสมอยู่ในระดับสูง แนะนำให้ทำกิจกรรมผ่อนคลาย", | |
| actions: [ | |
| { id: 'breathing', label: 'หายใจลึกๆ', type: 'primary' }, | |
| { id: 'yoga', label: 'โยคะคลายเครียด', type: 'secondary' } | |
| ] | |
| } | |
| ]; | |
| const match = templates.find(t => { | |
| try { | |
| return t.condition(); | |
| } catch (e) { | |
| return false; | |
| } | |
| }); | |
| if (match) { | |
| this.updateMessage(match.message, match.actions); | |
| } | |
| } catch (error) { | |
| console.error('Generate smart message error:', error); | |
| } | |
| } | |
| handleAction(actionId) { | |
| switch(actionId) { | |
| case 'workout': | |
| window.location.href = 'activity.html'; | |
| break; | |
| case 'nutrition': | |
| window.location.href = 'nutrition.html'; | |
| break; | |
| case 'meditation': | |
| showNotification('กำลังเปิดการฝึกสมาธิ...', 'success'); | |
| break; | |
| case 'sleep': | |
| window.location.href = 'sleep.html'; | |
| break; | |
| case 'breathing': | |
| this.startBreathingExercise(); | |
| break; | |
| case 'yoga': | |
| showNotification('กำลังเปิดวิดีโอโยคะ...', 'success'); | |
| break; | |
| default: | |
| showNotification('ฟีเจอร์นี้กำลังพัฒนา...', 'info'); | |
| } | |
| } | |
| startBreathingExercise() { | |
| const modal = document.createElement('div'); | |
| modal.className = 'fixed inset-0 bg-black/50 z-50 flex items-center justify-center p-4'; | |
| modal.innerHTML = ` | |
| <div class="glass-card rounded-2xl p-6 max-w-sm w-full border border-gray-700/50"> | |
| <h3 class="text-xl font-bold mb-4 text-center">ฝึกหายใจลึกๆ</h3> | |
| <div class="w-32 h-32 mx-auto mb-4 rounded-full bg-gradient-to-r from-blue-400 to-indigo-500 flex items-center justify-center" id="breathingCircle"> | |
| <span class="text-white font-bold text-lg" id="breathingText">เริ่ม</span> | |
| </div> | |
| <p class="text-center text-sm text-gray-400 mb-4">หายใจเข้าลึกๆ 4 วินาที กลั้น 4 วินาที หายใจออก 4 วินาที</p> | |
| <button onclick="this.closest('.fixed').remove()" class="w-full bg-gray-700 hover:bg-gray-600 py-3 rounded-xl font-medium transition-colors"> | |
| หยุด | |
| </button> | |
| </div> | |
| `; | |
| document.body.appendChild(modal); | |
| // Breathing animation logic would go here | |
| this.runBreathingCycle(modal); | |
| } | |
| runBreathingCycle(modal) { | |
| const circle = modal.querySelector('#breathingCircle'); | |
| const text = modal.querySelector('#breathingText'); | |
| const phases = ['หายใจเข้า', 'กลั้น', 'หายใจออก', 'กลั้น']; | |
| let phase = 0; | |
| const interval = setInterval(() => { | |
| text.textContent = phases[phase]; | |
| circle.style.transform = phase === 0 ? 'scale(1.3)' : phase === 2 ? 'scale(0.8)' : 'scale(1)'; | |
| phase = (phase + 1) % 4; | |
| }, 4000); | |
| modal.addEventListener('remove', () => clearInterval(interval)); | |
| } | |
| connectedCallback() { | |
| this.attachShadow({ mode: 'open' }); | |
| this.currentMessage = this.defaultMessage; | |
| this.isTyping = false; | |
| this.render(); | |
| this.updateTimestamp(); | |
| this.startTypingAnimation(); | |
| this.setupEventListeners(); | |
| } | |
| startTypingAnimation() { | |
| // Simulate AI thinking periodically | |
| setInterval(() => { | |
| if (Math.random() > 0.7) { | |
| this.generateSmartMessage(); | |
| } | |
| }, 30000); // Every 30 seconds | |
| } | |
| setupEventListeners() { | |
| // Listen for health data updates | |
| window.addEventListener('health-data-updated', (e) => { | |
| this.generateSmartMessage(); | |
| }); | |
| } | |
| } | |
| // Register the custom element | |
| customElements.define('ai-coach-card', AICoachCard); | |