pulse-whoop / components /meal-logger-enhanced.js
CSNET's picture
**Project Name:** ชีพจร (Pulse) - The Ultimate Thai Health & Recovery Platform
4a7cb62 verified
Raw History Blame Contribute Delete
21.6 kB
class MealLoggerEnhanced extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
// Thai food database
this.thaiFoodDatabase = [
{ name: 'ข้าวมันไก่', calories: 580, protein: 25, carbs: 65, fat: 20, image: 'http://static.photos/food/200x200/567' },
{ name: 'ส้มตำไทย + ไก่ย่าง', calories: 450, protein: 30, carbs: 35, fat: 18, image: 'http://static.photos/food/200x200/789' },
{ name: 'กะเพราไก่ไข่ดาว', calories: 520, protein: 28, carbs: 45, fat: 25, image: 'http://static.photos/food/200x200/133' },
{ name: 'ข้าวขาหมู', calories: 620, protein: 35, carbs: 55, fat: 28, image: 'http://static.photos/food/200x200/234' },
{ name: 'บะหมี่เกี๊ยว', calories: 480, protein: 22, carbs: 58, fat: 16, image: 'http://static.photos/food/200x200/345' },
{ name: 'มาม่าต้มยำกุ้ง', calories: 320, protein: 12, carbs: 42, fat: 10, image: 'http://static.photos/food/200x200/456' },
{ name: 'ข้าวผัดกระเพรากุ้ง', calories: 580, protein: 25, carbs: 65, fat: 22, image: 'http://static.photos/food/200x200/567' },
{ name: 'สุกี้แห้งหมู', calories: 520, protein: 20, carbs: 68, fat: 18, image: 'http://static.photos/food/200x200/678' },
{ name: 'ชานมไข่มุก', calories: 280, protein: 4, carbs: 45, fat: 8, image: 'http://static.photos/food/200x200/789' },
{ name: 'ข้าวเหนียวมะม่วง', calories: 350, protein: 3, carbs: 58, fat: 12, image: 'http://static.photos/food/200x200/890' }
];
}
connectedCallback() {
this.render();
}
render() {
this.shadowRoot.innerHTML = `
<style>
:host {
display: block;
margin: 0 1rem 1rem;
}
.meal-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;
}
.meal-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 0.75rem;
}
.meal-title {
font-size: 0.85rem;
font-weight: 600;
color: #e5e7eb;
display: flex;
align-items: center;
gap: 0.5rem;
}
.ai-photo-btn {
background: linear-gradient(135deg, #8b5cf6, #a855f7);
color: white;
padding: 0.25rem 0.5rem;
border-radius: 0.5rem;
font-size: 0.65rem;
font-weight: 500;
cursor: pointer;
transition: all 0.3s ease;
border: none;
display: flex;
align-items: center;
gap: 0.25rem;
}
.ai-photo-btn:hover {
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(139, 92, 246, 0.3);
}
.search-container {
position: relative;
margin-bottom: 0.75rem;
}
.search-input {
width: 100%;
padding: 0.5rem 1rem;
background: rgba(17, 24, 39, 0.4);
border: 1px solid rgba(75, 85, 99, 0.3);
border-radius: 0.75rem;
color: #e5e7eb;
font-size: 0.75rem;
font-family: 'Prompt', sans-serif;
}
.search-input::placeholder {
color: #6b7280;
}
.search-input:focus {
outline: none;
border-color: #34d399;
box-shadow: 0 0 0 2px rgba(52, 211, 153, 0.2);
}
.search-results {
position: absolute;
top: 100%;
left: 0;
right: 0;
background: rgba(31, 41, 55, 0.95);
border: 1px solid rgba(75, 85, 99, 0.5);
border-radius: 0.75rem;
margin-top: 0.25rem;
max-height: 200px;
overflow-y: auto;
z-index: 10;
display: none;
}
.search-results.show {
display: block;
}
.search-item {
padding: 0.5rem;
cursor: pointer;
transition: all 0.2s ease;
border-bottom: 1px solid rgba(75, 85, 99, 0.2);
}
.search-item:last-child {
border-bottom: none;
}
.search-item:hover {
background: rgba(52, 211, 153, 0.1);
}
.recent-meals {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.meal-item {
display: flex;
align-items: center;
gap: 0.75rem;
padding: 0.5rem;
background: rgba(17, 24, 39, 0.3);
border-radius: 0.75rem;
transition: all 0.3s ease;
cursor: pointer;
}
.meal-item:hover {
background: rgba(31, 41, 55, 0.6);
transform: translateX(5px);
}
.meal-image {
width: 48px;
height: 48px;
border-radius: 0.5rem;
object-fit: cover;
border: 1px solid rgba(75, 85, 99, 0.3);
}
.meal-info {
flex: 1;
display: flex;
flex-direction: column;
}
.meal-name {
font-size: 0.8rem;
font-weight: 500;
color: #e5e7eb;
margin-bottom: 0.25rem;
}
.meal-meta {
font-size: 0.65rem;
color: #9ca3af;
display: flex;
gap: 0.5rem;
}
.meal-calories {
background: rgba(251, 146, 60, 0.2);
color: #fb923c;
padding: 0.1rem 0.5rem;
border-radius: 9999px;
font-weight: 600;
}
.meal-macros {
display: flex;
gap: 0.25rem;
}
.macro {
background: rgba(107, 114, 128, 0.2);
padding: 0.1rem 0.3rem;
border-radius: 0.25rem;
}
.ai-suggestion {
background: linear-gradient(135deg, rgba(139, 92, 246, 0.1), rgba(168, 85, 247, 0.1));
border: 1px solid rgba(139, 92, 246, 0.3);
border-radius: 0.75rem;
padding: 0.75rem;
margin-bottom: 0.75rem;
display: none;
}
.ai-suggestion.show {
display: block;
}
.ai-header {
display: flex;
align-items: center;
gap: 0.5rem;
margin-bottom: 0.25rem;
}
.ai-icon {
width: 16px;
height: 16px;
background: linear-gradient(135deg, #8b5cf6, #a855f7);
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-size: 0.6rem;
}
.ai-title {
font-size: 0.7rem;
font-weight: 600;
color: #c4b5fd;
}
.ai-message {
font-size: 0.65rem;
color: #d1d5db;
line-height: 1.4;
}
.quick-add-section {
display: flex;
gap: 0.5rem;
overflow-x: auto;
padding-bottom: 0.5rem;
margin-top: 0.75rem;
}
.quick-add-item {
flex-shrink: 0;
padding: 0.5rem 1rem;
background: rgba(52, 211, 153, 0.1);
border: 1px solid rgba(52, 211, 153, 0.3);
border-radius: 9999px;
font-size: 0.65rem;
color: #34d399;
cursor: pointer;
transition: all 0.3s ease;
}
.quick-add-item:hover {
background: rgba(52, 211, 153, 0.2);
transform: translateY(-2px);
}
.hidden-file-input {
display: none;
}
</style>
<div class="meal-container">
<div class="meal-header">
<div class="meal-title">
<i data-feather="coffee" class="w-4 h-4 text-orange-400"></i>
มื้ออาหารล่าสุด
</div>
<div class="flex gap-2">
<button class="ai-photo-btn" id="aiPhotoBtn">
<i data-feather="camera" class="w-3 h-3"></i>
AI ถ่ายรูป
</button>
<button class="ai-photo-btn" style="background: linear-gradient(135deg, #f59e0b, #f97316);" id="barcodeBtn">
<i data-feather="maximize" class="w-3 h-3"></i>
สแกนบาร์โค้ด
</button>
</div>
</div>
<input type="file" accept="image/*" class="hidden-file-input" id="photoInput" capture="environment">
<input type="file" accept="image/*" class="hidden-file-input" id="barcodeInput">
<div class="search-container">
<input type="text" class="search-input" id="searchInput" placeholder="ค้นหาอาหารไทย... (เช่น กะเพรา, ส้มตำ, ข้าวมันไก่)">
<div class="search-results" id="searchResults"></div>
</div>
<div class="ai-suggestion" id="aiSuggestion">
<div class="ai-header">
<div class="ai-icon">AI</div>
<div class="ai-title">คำแนะนำอัจฉริยะ</div>
</div>
<div class="ai-message" id="aiMessage"></div>
</div>
<div class="recent-meals" id="recentMeals">
${this.thaiFoodDatabase.slice(0, 3).map(food => this.renderMealItem(food)).join('')}
</div>
<div class="quick-add-section">
<div class="quick-add-item" data-meal="ข้าวมันไก่">ข้าวมันไก่</div>
<div class="quick-add-item" data-meal="ส้มตำ">ส้มตำ</div>
<div class="quick-add-item" data-meal="กะเพรา">กะเพรา</div>
<div class="quick-add-item" data-meal="ชานม">ชานมไข่มุก</div>
<div class="quick-add-item" data-meal="ข้าวเหนียว">ข้าวเหนียวมะม่วง</div>
</div>
<div class="mood-actions" style="margin-top: 0.75rem;">
<button class="mood-btn save-btn" id="viewAllMealsBtn">ดูทั้งหมด</button>
<button class="mood-btn history-btn" id="customMealBtn">สร้างเมนู</button>
</div>
</div>
`;
this.setupEventListeners();
}
renderMealItem(food) {
return `
<div class="meal-item" data-food="${food.name}">
<img src="${food.image}" alt="${food.name}" class="meal-image">
<div class="meal-info">
<div class="meal-name">${food.name}</div>
<div class="meal-meta">
<span class="meal-calories">${food.calories} แคล</span>
<div class="meal-macros">
<span class="macro">P:${food.protein}g</span>
<span class="macro">C:${food.carbs}g</span>
<span class="macro">F:${food.fat}g</span>
</div>
</div>
</div>
<i data-feather="plus-circle" class="w-5 h-5 text-green-400"></i>
</div>
`;
}
setupEventListeners() {
const searchInput = this.shadowRoot.getElementById('searchInput');
const searchResults = this.shadowRoot.getElementById('searchResults');
const aiPhotoBtn = this.shadowRoot.getElementById('aiPhotoBtn');
const barcodeBtn = this.shadowRoot.getElementById('barcodeBtn');
const photoInput = this.shadowRoot.getElementById('photoInput');
const barcodeInput = this.shadowRoot.getElementById('barcodeInput');
const quickAddItems = this.shadowRoot.querySelectorAll('.quick-add-item');
// Search functionality
searchInput.addEventListener('input', (e) => {
const query = e.target.value.toLowerCase().trim();
if (query.length < 2) {
searchResults.classList.remove('show');
return;
}
const matches = this.thaiFoodDatabase.filter(food =>
food.name.toLowerCase().includes(query) ||
food.name.replace(/\s/g, '').includes(query.replace(/\s/g, ''))
);
if (matches.length > 0) {
searchResults.innerHTML = matches.slice(0, 5).map(food => `
<div class="search-item" data-food='${JSON.stringify(food)}'>
<div class="flex items-center gap-2">
<img src="${food.image}" alt="${food.name}" class="w-8 h-8 rounded object-cover">
<div>
<div class="font-medium text-sm">${food.name}</div>
<div class="text-xs text-gray-400">${food.calories} แคลอรี</div>
</div>
</div>
</div>
`).join('');
searchResults.classList.add('show');
// Add click handlers
searchResults.querySelectorAll('.search-item').forEach(item => {
item.addEventListener('click', () => {
const food = JSON.parse(item.dataset.food);
this.addMeal(food);
searchResults.classList.remove('show');
searchInput.value = '';
});
});
} else {
searchResults.innerHTML = '<div class="search-item text-gray-400">ไม่พบอาหาร ลองสร้างเมนูเอง?</div>';
searchResults.classList.add('show');
}
});
// Hide search results when clicking outside
document.addEventListener('click', (e) => {
if (!this.shadowRoot.contains(e.target)) {
searchResults.classList.remove('show');
}
});
// AI Photo logging
aiPhotoBtn.addEventListener('click', () => {
photoInput.click();
});
photoInput.addEventListener('change', (e) => {
const file = e.target.files[0];
if (file) {
this.processFoodPhoto(file);
}
});
// Barcode scanning
barcodeBtn.addEventListener('click', () => {
barcodeInput.click();
});
barcodeInput.addEventListener('change', (e) => {
const file = e.target.files[0];
if (file) {
this.processBarcode(file);
}
});
// Quick add buttons
quickAddItems.forEach(item => {
item.addEventListener('click', () => {
const mealName = item.dataset.meal;
const food = this.thaiFoodDatabase.find(f => f.name.includes(mealName));
if (food) {
this.addMeal(food);
}
});
});
// Meal item clicks
this.shadowRoot.addEventListener('click', (e) => {
const mealItem = e.target.closest('.meal-item');
if (mealItem) {
const foodName = mealItem.dataset.food;
const food = this.thaiFoodDatabase.find(f => f.name === foodName);
if (food) {
this.addMeal(food);
}
}
});
}
processFoodPhoto(file) {
this.showNotification('กำลังวิเคราะห์รูปภาพด้วย AI...', 'info');
// Simulate AI processing
setTimeout(() => {
const detectedFood = this.thaiFoodDatabase[Math.floor(Math.random() * this.thaiFoodDatabase.length)];
this.showAIsuggestion(
`ระบบตรวจพบ "${detectedFood.name}" ด้วยความมั่นใจ 85%`,
`คลิกเพื่อบันทึก ${detectedFood.calories} แคลอรี หรือแก้ไขถ้าไม่ถูกต้อง`
);
// Auto-select the detected food
setTimeout(() => {
this.addMeal(detectedFood);
}, 2000);
}, 3000);
}
processBarcode(file) {
this.showNotification('กำลังสแกนบาร์โค้ด...', 'info');
setTimeout(() => {
this.showNotification('พบสินค้า: โยเกิร์ตไขมันต่ำ 180 แคลอรี', 'success');
}, 2000);
}
showAIsuggestion(title, message) {
const suggestion = this.shadowRoot.getElementById('aiSuggestion');
const aiMessage = this.shadowRoot.getElementById('aiMessage');
suggestion.classList.add('show');
aiMessage.innerHTML = `<strong>${title}</strong><br>${message}`;
setTimeout(() => {
suggestion.classList.remove('show');
}, 5000);
}
addMeal(food) {
this.showNotification(`บันทึก ${food.name} ${food.calories} แคลอรี สำเร็จ!`, 'success');
// Add to recent meals
const recentMeals = this.shadowRoot.getElementById('recentMeals');
const newMeal = document.createElement('div');
newMeal.className = 'meal-item';
newMeal.innerHTML = this.renderMealItem(food);
recentMeals.insertBefore(newMeal, recentMeals.firstChild);
// Keep only 3 items
while (recentMeals.children.length > 3) {
recentMeals.removeChild(recentMeals.lastChild);
}
// Save to localStorage
const todayMeals = JSON.parse(localStorage.getItem('todayMeals') || '[]');
todayMeals.unshift({
...food,
timestamp: new Date().toISOString(),
mealType: this.getCurrentMealType()
});
localStorage.setItem('todayMeals', JSON.stringify(todayMeals.slice(0, 10)));
// Dispatch event
this.dispatchEvent(new CustomEvent('meal-logged', {
detail: food,
bubbles: true,
composed: true
}));
}
getCurrentMealType() {
const hour = new Date().getHours();
if (hour < 11) return 'เช้า';
if (hour < 16) return 'เที่ยง';
if (hour < 19) return 'เย็น';
return 'ดึก';
}
showNotification(message, type = 'info') {
if (window.showNotification) {
window.showNotification(message, type);
} else {
alert(message);
}
}
}
customElements.define('meal-logger-enhanced', MealLoggerEnhanced);