pulse-whoop / components /mood-logger.js
CSNET's picture
**Project Name:** ชีพจร (Pulse) - The Ultimate Thai Health & Recovery Platform
4a7cb62 verified
Raw History Blame Contribute Delete
14.2 kB
class MoodLogger extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
this.moods = [
{ emoji: '😊', label: 'สดชื่น', value: 'happy' },
{ emoji: '😌', label: 'สงบ', value: 'calm' },
{ emoji: '😐', label: 'ธรรมดา', value: 'neutral' },
{ emoji: '😔', label: 'เศร้า', value: 'sad' },
{ emoji: '😠', label: 'หงุดหงิด', value: 'angry' },
{ emoji: '😴', label: 'ง่วง', value: 'tired' },
{ emoji: '😰', label: 'เครียด', value: 'stressed' },
{ emoji: '🤩', label: 'ตื่นเต้น', value: 'excited' }
];
this.currentMood = null;
}
connectedCallback() {
this.render();
this.loadTodaysMood();
}
render() {
this.shadowRoot.innerHTML = `
<style>
:host {
display: block;
margin: 0 1rem 1rem;
}
.mood-container {
background: rgba(31, 41, 55, 0.4);
backdrop-filter: blur(10px);
border: 1px solid rgba(75, 85, 99, 0.3);
border-radius: 1rem;
padding: 1rem;
}
.mood-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 0.75rem;
}
.mood-title {
font-size: 0.85rem;
font-weight: 600;
color: #e5e7eb;
display: flex;
align-items: center;
gap: 0.5rem;
}
.streak-badge {
background: linear-gradient(135deg, #f59e0b, #f97316);
color: white;
padding: 0.25rem 0.5rem;
border-radius: 9999px;
font-size: 0.65rem;
font-weight: 600;
}
.mood-grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 0.5rem;
margin-bottom: 0.75rem;
}
.mood-item {
display: flex;
flex-direction: column;
align-items: center;
padding: 0.5rem 0.25rem;
border-radius: 0.75rem;
border: 1px solid transparent;
cursor: pointer;
transition: all 0.3s ease;
background: rgba(17, 24, 39, 0.3);
}
.mood-item:hover {
border-color: rgba(52, 211, 153, 0.5);
background: rgba(52, 211, 153, 0.1);
transform: translateY(-3px);
}
.mood-item.selected {
border-color: #34d399;
background: rgba(52, 211, 153, 0.2);
box-shadow: 0 4px 12px rgba(52, 211, 153, 0.2);
}
.mood-emoji {
font-size: 1.5rem;
margin-bottom: 0.25rem;
transition: transform 0.3s ease;
}
.mood-item:hover .mood-emoji {
transform: scale(1.2);
}
.mood-label {
font-size: 0.6rem;
color: #9ca3af;
text-align: center;
font-weight: 500;
}
.mood-note {
width: 100%;
padding: 0.5rem;
background: rgba(17, 24, 39, 0.4);
border: 1px solid rgba(75, 85, 99, 0.3);
border-radius: 0.5rem;
color: #e5e7eb;
font-size: 0.75rem;
font-family: 'Prompt', sans-serif;
resize: vertical;
min-height: 60px;
margin-bottom: 0.5rem;
display: none;
}
.mood-note.show {
display: block;
}
.mood-note::placeholder {
color: #6b7280;
}
.mood-actions {
display: flex;
gap: 0.5rem;
justify-content: space-between;
}
.mood-btn {
flex: 1;
padding: 0.5rem;
border-radius: 0.5rem;
font-size: 0.75rem;
font-weight: 500;
cursor: pointer;
transition: all 0.3s ease;
border: none;
font-family: 'Prompt', sans-serif;
}
.save-btn {
background: linear-gradient(135deg, #34d399, #10b981);
color: white;
}
.save-btn:hover {
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(52, 211, 153, 0.3);
}
.history-btn {
background: rgba(107, 114, 128, 0.3);
color: #e5e7eb;
}
.history-btn:hover {
background: rgba(107, 114, 128, 0.5);
}
.mood-streak {
display: flex;
align-items: center;
gap: 0.5rem;
margin-top: 0.5rem;
padding-top: 0.5rem;
border-top: 1px solid rgba(75, 85, 99, 0.3);
}
.streak-info {
flex: 1;
font-size: 0.7rem;
color: #9ca3af;
}
.streak-count {
color: #fbbf24;
font-weight: 600;
}
</style>
<div class="mood-container">
<div class="mood-header">
<div class="mood-title">
<i data-feather="heart" class="w-4 h-4 text-pink-400"></i>
อารมณ์ประจำวัน
</div>
<div class="streak-badge" id="streakBadge">
🔥 7 วัน
</div>
</div>
<div class="mood-grid" id="moodGrid">
${this.moods.map(mood => `
<div class="mood-item" data-value="${mood.value}" data-emoji="${mood.emoji}" data-label="${mood.label}">
<div class="mood-emoji">${mood.emoji}</div>
<div class="mood-label">${mood.label}</div>
</div>
`).join('')}
</div>
<textarea class="mood-note" id="moodNote" placeholder="บันทึกความรู้สึกเพิ่มเติม... (ไม่บังคับ)"></textarea>
<div class="mood-actions">
<button class="mood-btn save-btn" id="saveMoodBtn">บันทึกอารมณ์</button>
<button class="mood-btn history-btn" id="historyBtn">ประวัติ</button>
</div>
<div class="mood-streak">
<div class="streak-info">
คุณบันทึกอารมณ์ติดต่อกันมา <span class="streak-count">7 วัน</span> แล้ว!
</div>
</div>
</div>
`;
this.setupEventListeners();
}
setupEventListeners() {
const moodItems = this.shadowRoot.querySelectorAll('.mood-item');
const saveBtn = this.shadowRoot.getElementById('saveMoodBtn');
const historyBtn = this.shadowRoot.getElementById('historyBtn');
const moodNote = this.shadowRoot.getElementById('moodNote');
moodItems.forEach(item => {
item.addEventListener('click', () => {
// Remove previous selection
moodItems.forEach(i => i.classList.remove('selected'));
// Select current
item.classList.add('selected');
this.currentMood = {
value: item.dataset.value,
emoji: item.dataset.emoji,
label: item.dataset.label
};
// Show note textarea
moodNote.classList.add('show');
moodNote.focus();
});
});
saveBtn.addEventListener('click', () => {
if (!this.currentMood) {
this.showNotification('กรุณาเลือกอารมณ์ก่อนบันทึก', 'warning');
return;
}
const note = moodNote.value.trim();
this.saveMood(this.currentMood, note);
});
historyBtn.addEventListener('click', () => {
this.showMoodHistory();
});
}
loadTodaysMood() {
// Simulate loading today's mood from localStorage
const savedMood = localStorage.getItem('todayMood');
if (savedMood) {
const mood = JSON.parse(savedMood);
const moodItem = this.shadowRoot.querySelector(`[data-value="${mood.value}"]`);
if (moodItem) {
moodItem.classList.add('selected');
this.currentMood = mood;
this.shadowRoot.getElementById('moodNote').value = mood.note || '';
this.shadowRoot.getElementById('moodNote').classList.add('show');
}
}
}
saveMood(mood, note) {
const data = {
...mood,
note,
timestamp: new Date().toISOString(),
date: new Date().toLocaleDateString('th-TH')
};
// Save to localStorage
localStorage.setItem('todayMood', JSON.stringify(data));
// Add to history
const history = JSON.parse(localStorage.getItem('moodHistory') || '[]');
history.unshift(data);
localStorage.setItem('moodHistory', JSON.stringify(history.slice(0, 30)));
this.showNotification(`บันทึกอารมณ์ ${mood.emoji} ${mood.label} สำเร็จ!`, 'success');
// Update streak
this.updateStreak();
// Send event to update AI coach
this.dispatchEvent(new CustomEvent('mood-updated', {
detail: data,
bubbles: true,
composed: true
}));
}
updateStreak() {
const history = JSON.parse(localStorage.getItem('moodHistory') || '[]');
let streak = 0;
const today = new Date();
for (let i = 0; i < history.length; i++) {
const moodDate = new Date(history[i].timestamp);
const daysDiff = Math.floor((today - moodDate) / (1000 * 60 * 60 * 24));
if (daysDiff === streak) {
streak++;
} else {
break;
}
}
const streakBadge = this.shadowRoot.getElementById('streakBadge');
const streakCount = this.shadowRoot.querySelector('.streak-count');
if (streak > 1) {
streakBadge.textContent = `🔥 ${streak} วัน`;
streakCount.textContent = `${streak} วัน`;
} else {
streakBadge.textContent = 'เริ่มต้นใหม่';
streakCount.textContent = '1 วัน';
}
}
showMoodHistory() {
const history = JSON.parse(localStorage.getItem('moodHistory') || '[]');
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-md w-full border border-gray-700/50 max-h-[80vh] overflow-y-auto">
<h3 class="text-xl font-bold mb-4">ประวัติอารมณ์</h3>
${history.length > 0 ? `
<div class="space-y-2">
${history.slice(0, 7).map(record => `
<div class="p-3 bg-gray-800/50 rounded-lg">
<div class="flex items-center justify-between mb-1">
<div class="flex items-center gap-2">
<span class="text-2xl">${record.emoji}</span>
<span class="font-medium">${record.label}</span>
</div>
<span class="text-xs text-gray-400">${record.date}</span>
</div>
${record.note ? `<p class="text-sm text-gray-300">${record.note}</p>` : ''}
</div>
`).join('')}
</div>
` : '<p class="text-gray-400 text-center py-4">ยังไม่มีประวัติการบันทึก</p>'}
<button onclick="this.closest('.fixed').remove()" class="w-full mt-4 bg-gray-700 hover:bg-gray-600 py-3 rounded-xl font-medium transition-colors">
ปิด
</button>
</div>
`;
document.body.appendChild(modal);
}
showNotification(message, type = 'info') {
if (window.showNotification) {
window.showNotification(message, type);
} else {
alert(message);
}
}
}
customElements.define('mood-logger', MoodLogger);