mailkarow / static /js /app.js
sarveshpatel's picture
Create static/js/app.js
745d958 verified
// Toast Notification System
function showToast(message, type = 'info') {
const container = document.getElementById('toast-container');
if (!container) return;
const toast = document.createElement('div');
const bgColor = {
success: 'bg-green-500',
error: 'bg-red-500',
warning: 'bg-yellow-500',
info: 'bg-blue-500'
}[type] || 'bg-gray-800';
const icon = {
success: 'fa-check-circle',
error: 'fa-exclamation-circle',
warning: 'fa-exclamation-triangle',
info: 'fa-info-circle'
}[type] || 'fa-info-circle';
toast.className = `toast ${bgColor} text-white px-6 py-4 rounded-lg shadow-lg flex items-center gap-3 min-w-[300px]`;
toast.innerHTML = `
<i class="fas ${icon} text-xl"></i>
<span class="flex-1">${message}</span>
<button onclick="this.parentElement.remove()" class="text-white/80 hover:text-white">
<i class="fas fa-times"></i>
</button>
`;
container.appendChild(toast);
// Auto remove after 5 seconds
setTimeout(() => {
toast.classList.add('toast-exit');
setTimeout(() => toast.remove(), 300);
}, 5000);
}
// Utility Functions
function formatDate(dateString) {
const date = new Date(dateString);
const now = new Date();
const diffMs = now - date;
const diffMins = Math.floor(diffMs / 60000);
const diffHours = Math.floor(diffMs / 3600000);
const diffDays = Math.floor(diffMs / 86400000);
if (diffMins < 1) return 'Just now';
if (diffMins < 60) return `${diffMins}m ago`;
if (diffHours < 24) return `${diffHours}h ago`;
if (diffDays < 7) return `${diffDays}d ago`;
return date.toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
year: date.getFullYear() !== now.getFullYear() ? 'numeric' : undefined
});
}
function formatSize(bytes) {
if (bytes === 0) return '0 B';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ' ' + sizes[i];
}
// Clipboard fallback for older browsers
function copyToClipboard(text) {
if (navigator.clipboard && navigator.clipboard.writeText) {
return navigator.clipboard.writeText(text);
}
// Fallback
const textarea = document.createElement('textarea');
textarea.value = text;
textarea.style.position = 'fixed';
textarea.style.left = '-9999px';
document.body.appendChild(textarea);
textarea.select();
try {
document.execCommand('copy');
document.body.removeChild(textarea);
return Promise.resolve();
} catch (err) {
document.body.removeChild(textarea);
return Promise.reject(err);
}
}
// Debounce function
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
// API Helper
async function apiRequest(url, options = {}) {
try {
const response = await fetch(url, {
...options,
headers: {
'Content-Type': 'application/json',
...options.headers
}
});
const data = await response.json().catch(() => ({}));
if (!response.ok) {
throw new Error(data.error || `HTTP error ${response.status}`);
}
return data;
} catch (error) {
console.error('API Error:', error);
throw error;
}
}
// Initialize tooltips
document.addEventListener('DOMContentLoaded', () => {
// Add any initialization code here
console.log('TempMail App Initialized');
});
// Service Worker Registration (optional, for PWA)
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
// navigator.serviceWorker.register('/sw.js').catch(console.error);
});
}
// Handle visibility change (pause/resume auto-refresh)
document.addEventListener('visibilitychange', () => {
if (document.hidden) {
console.log('Page hidden - pausing updates');
} else {
console.log('Page visible - resuming updates');
}
});