402 lines
13 KiB
JavaScript
402 lines
13 KiB
JavaScript
const API_BASE = '/api/links';
|
|
|
|
// DOM elements
|
|
const linkForm = document.getElementById('linkForm');
|
|
const linkInput = document.getElementById('linkInput');
|
|
const addButton = document.getElementById('addButton');
|
|
const searchInput = document.getElementById('searchInput');
|
|
const linksContainer = document.getElementById('linksContainer');
|
|
const toastContainer = document.getElementById('toastContainer');
|
|
const archiveToggle = document.getElementById('archiveToggle');
|
|
const scrollToTopBtn = document.getElementById('scrollToTopBtn');
|
|
|
|
// State
|
|
let allLinks = [];
|
|
let searchTimeout = null;
|
|
let showArchived = false;
|
|
|
|
// Initialize app
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
loadLinks();
|
|
setupEventListeners();
|
|
setupScrollToTop();
|
|
});
|
|
|
|
// Event listeners
|
|
function setupEventListeners() {
|
|
linkForm.addEventListener('submit', handleAddLink);
|
|
searchInput.addEventListener('input', handleSearch);
|
|
archiveToggle.addEventListener('click', handleToggleArchive);
|
|
}
|
|
|
|
// Setup scroll to top button
|
|
function setupScrollToTop() {
|
|
// Show/hide button based on scroll position
|
|
window.addEventListener('scroll', () => {
|
|
if (window.pageYOffset > 300) {
|
|
scrollToTopBtn.classList.add('show');
|
|
} else {
|
|
scrollToTopBtn.classList.remove('show');
|
|
}
|
|
});
|
|
|
|
// Scroll to top when button is clicked
|
|
scrollToTopBtn.addEventListener('click', () => {
|
|
window.scrollTo({
|
|
top: 0,
|
|
behavior: 'smooth'
|
|
});
|
|
});
|
|
}
|
|
|
|
// Load all links
|
|
async function loadLinks() {
|
|
try {
|
|
const response = await fetch(API_BASE);
|
|
if (!response.ok) throw new Error('Failed to load links');
|
|
|
|
allLinks = await response.json();
|
|
// Ensure all links have archived property
|
|
allLinks = allLinks.map(link => ({
|
|
...link,
|
|
archived: link.archived || false
|
|
}));
|
|
displayLinks(getFilteredLinks());
|
|
} catch (error) {
|
|
showMessage('Failed to load links', 'error');
|
|
console.error('Error loading links:', error);
|
|
}
|
|
}
|
|
|
|
// Get filtered links based on archived status
|
|
function getFilteredLinks() {
|
|
return allLinks.filter(link => {
|
|
const isArchived = link.archived === true;
|
|
return showArchived ? isArchived : !isArchived;
|
|
});
|
|
}
|
|
|
|
// Handle add link form submission
|
|
async function handleAddLink(e) {
|
|
e.preventDefault();
|
|
|
|
const url = linkInput.value.trim();
|
|
if (!url) return;
|
|
|
|
// Disable form
|
|
addButton.disabled = true;
|
|
const btnText = addButton.querySelector('.btn-text');
|
|
const btnLoader = addButton.querySelector('.btn-loader');
|
|
if (btnText) btnText.style.display = 'none';
|
|
if (btnLoader) btnLoader.style.display = 'flex';
|
|
|
|
try {
|
|
const response = await fetch(API_BASE, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({ url })
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (!response.ok) {
|
|
throw new Error(data.error || 'Failed to add link');
|
|
}
|
|
|
|
// Ensure archived property exists
|
|
data.archived = data.archived || false;
|
|
// Add to beginning of array
|
|
allLinks.unshift(data);
|
|
displayLinks(getFilteredLinks());
|
|
|
|
// Clear input
|
|
linkInput.value = '';
|
|
showMessage('Link added successfully!', 'success');
|
|
} catch (error) {
|
|
showMessage(error.message, 'error');
|
|
console.error('Error adding link:', error);
|
|
} finally {
|
|
// Re-enable form
|
|
addButton.disabled = false;
|
|
if (btnText) btnText.style.display = 'inline';
|
|
if (btnLoader) btnLoader.style.display = 'none';
|
|
}
|
|
}
|
|
|
|
// Handle search input
|
|
function handleSearch(e) {
|
|
const query = e.target.value.trim();
|
|
|
|
// Debounce search
|
|
clearTimeout(searchTimeout);
|
|
searchTimeout = setTimeout(async () => {
|
|
if (!query) {
|
|
displayLinks(getFilteredLinks());
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const response = await fetch(`${API_BASE}/search?q=${encodeURIComponent(query)}`);
|
|
if (!response.ok) throw new Error('Search failed');
|
|
|
|
const filteredLinks = await response.json();
|
|
// Filter by archived status
|
|
const archivedFiltered = filteredLinks.filter(link => {
|
|
const isArchived = link.archived === true;
|
|
return showArchived ? isArchived : !isArchived;
|
|
});
|
|
displayLinks(archivedFiltered);
|
|
} catch (error) {
|
|
showMessage('Search failed', 'error');
|
|
console.error('Error searching:', error);
|
|
}
|
|
}, 300);
|
|
}
|
|
|
|
// Handle archive toggle
|
|
function handleToggleArchive() {
|
|
showArchived = !showArchived;
|
|
const toggleWrapper = archiveToggle.closest('.archive-toggle-wrapper');
|
|
archiveToggle.classList.toggle('active', showArchived);
|
|
toggleWrapper.classList.toggle('active', showArchived);
|
|
|
|
// Re-filter and display links
|
|
const query = searchInput.value.trim();
|
|
if (query) {
|
|
// Re-run search with new filter
|
|
handleSearch({ target: searchInput });
|
|
} else {
|
|
displayLinks(getFilteredLinks());
|
|
}
|
|
}
|
|
|
|
// Display links
|
|
function displayLinks(links) {
|
|
if (links.length === 0) {
|
|
const filteredCount = getFilteredLinks().length;
|
|
const totalCount = allLinks.length;
|
|
const message = showArchived
|
|
? (totalCount === 0 ? 'Add your first link to get started!' : 'No archived links found.')
|
|
: (totalCount === 0 ? 'Add your first link to get started!' : (filteredCount === 0 ? 'No active links found. Try a different search term or toggle to view archived links.' : 'Try a different search term.'));
|
|
linksContainer.innerHTML = `
|
|
<div class="empty-state">
|
|
<p>No links found. ${message}</p>
|
|
</div>
|
|
`;
|
|
return;
|
|
}
|
|
|
|
linksContainer.innerHTML = links.map(link => createLinkCard(link)).join('');
|
|
|
|
// Add delete event listeners
|
|
document.querySelectorAll('.delete-btn').forEach(btn => {
|
|
btn.addEventListener('click', (e) => {
|
|
const linkId = e.target.closest('.link-card').dataset.id;
|
|
handleDeleteLink(linkId);
|
|
});
|
|
});
|
|
|
|
// Add archive event listeners
|
|
document.querySelectorAll('.archive-btn').forEach(btn => {
|
|
btn.addEventListener('click', (e) => {
|
|
const linkId = e.target.closest('.link-card').dataset.id;
|
|
handleArchiveLink(linkId);
|
|
});
|
|
});
|
|
}
|
|
|
|
// Create link card HTML
|
|
function createLinkCard(link) {
|
|
const date = new Date(link.createdAt);
|
|
const formattedDate = date.toLocaleDateString('en-US', {
|
|
year: 'numeric',
|
|
month: 'short',
|
|
day: 'numeric'
|
|
});
|
|
|
|
const imageHtml = link.image
|
|
? `<img src="${escapeHtml(link.image)}" alt="${escapeHtml(link.title)}" onerror="this.parentElement.textContent='No image available'">`
|
|
: '<span>No image available</span>';
|
|
|
|
return `
|
|
<div class="link-card" data-id="${link.id}">
|
|
<div class="link-image">
|
|
${imageHtml}
|
|
</div>
|
|
<div class="link-content">
|
|
<h3 class="link-title">${escapeHtml(link.title)}</h3>
|
|
${link.description ? `<p class="link-description">${escapeHtml(link.description)}</p>` : ''}
|
|
<a href="${escapeHtml(link.url)}" target="_blank" rel="noopener noreferrer" class="link-url">
|
|
${escapeHtml(link.url)}
|
|
</a>
|
|
<div class="link-footer">
|
|
<span class="link-date">${formattedDate}</span>
|
|
<div class="link-actions">
|
|
<button class="archive-btn">${link.archived ? 'Unarchive' : 'Archive'}</button>
|
|
<button class="delete-btn">Delete</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
// Update a single link card in place
|
|
function updateLinkCard(link) {
|
|
const linkCard = document.querySelector(`.link-card[data-id="${link.id}"]`);
|
|
if (!linkCard) return;
|
|
|
|
// Replace the card content
|
|
linkCard.outerHTML = createLinkCard(link);
|
|
|
|
// Re-attach event listeners to the new card
|
|
const newCard = document.querySelector(`.link-card[data-id="${link.id}"]`);
|
|
if (newCard) {
|
|
const deleteBtn = newCard.querySelector('.delete-btn');
|
|
const archiveBtn = newCard.querySelector('.archive-btn');
|
|
|
|
if (deleteBtn) {
|
|
deleteBtn.addEventListener('click', () => handleDeleteLink(link.id));
|
|
}
|
|
if (archiveBtn) {
|
|
archiveBtn.addEventListener('click', () => handleArchiveLink(link.id));
|
|
}
|
|
}
|
|
}
|
|
|
|
// Remove a single link card
|
|
function removeLinkCard(id) {
|
|
const linkCard = document.querySelector(`.link-card[data-id="${id}"]`);
|
|
if (linkCard) {
|
|
linkCard.remove();
|
|
|
|
// Check if we need to show empty state
|
|
const remainingCards = document.querySelectorAll('.link-card');
|
|
if (remainingCards.length === 0) {
|
|
const filteredCount = getFilteredLinks().length;
|
|
const totalCount = allLinks.length;
|
|
const message = showArchived
|
|
? (totalCount === 0 ? 'Add your first link to get started!' : 'No archived links found.')
|
|
: (totalCount === 0 ? 'Add your first link to get started!' : (filteredCount === 0 ? 'No active links found. Try a different search term or toggle to view archived links.' : 'Try a different search term.'));
|
|
linksContainer.innerHTML = `
|
|
<div class="empty-state">
|
|
<p>No links found. ${message}</p>
|
|
</div>
|
|
`;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Check if a link should be visible based on current filters
|
|
function shouldLinkBeVisible(link) {
|
|
const isArchived = link.archived === true;
|
|
const matchesArchiveFilter = showArchived ? isArchived : !isArchived;
|
|
|
|
const query = searchInput.value.trim();
|
|
if (query) {
|
|
const queryLower = query.toLowerCase();
|
|
const titleMatch = link.title?.toLowerCase().includes(queryLower);
|
|
const descMatch = link.description?.toLowerCase().includes(queryLower);
|
|
const urlMatch = link.url?.toLowerCase().includes(queryLower);
|
|
const matchesSearch = titleMatch || descMatch || urlMatch;
|
|
return matchesArchiveFilter && matchesSearch;
|
|
}
|
|
|
|
return matchesArchiveFilter;
|
|
}
|
|
|
|
// Handle archive link
|
|
async function handleArchiveLink(id) {
|
|
try {
|
|
const link = allLinks.find(l => l.id === id);
|
|
if (!link) return;
|
|
|
|
const newArchivedStatus = !link.archived;
|
|
|
|
const response = await fetch(`${API_BASE}/${id}/archive`, {
|
|
method: 'PATCH',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({ archived: newArchivedStatus })
|
|
});
|
|
|
|
if (!response.ok) throw new Error('Failed to archive link');
|
|
|
|
// Update local array
|
|
link.archived = newArchivedStatus;
|
|
|
|
// Update or remove the link card without refreshing the whole list
|
|
if (shouldLinkBeVisible(link)) {
|
|
// Link should still be visible, update it in place
|
|
updateLinkCard(link);
|
|
} else {
|
|
// Link should no longer be visible, remove it
|
|
removeLinkCard(id);
|
|
}
|
|
|
|
showMessage(newArchivedStatus ? 'Link archived successfully' : 'Link unarchived successfully', 'success');
|
|
} catch (error) {
|
|
showMessage('Failed to archive link', 'error');
|
|
console.error('Error archiving link:', error);
|
|
}
|
|
}
|
|
|
|
// Handle delete link
|
|
async function handleDeleteLink(id) {
|
|
if (!confirm('Are you sure you want to delete this link?')) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const response = await fetch(`${API_BASE}/${id}`, {
|
|
method: 'DELETE'
|
|
});
|
|
|
|
if (!response.ok) throw new Error('Failed to delete link');
|
|
|
|
// Remove from local array
|
|
allLinks = allLinks.filter(link => link.id !== id);
|
|
displayLinks(getFilteredLinks());
|
|
showMessage('Link deleted successfully', 'success');
|
|
} catch (error) {
|
|
showMessage('Failed to delete link', 'error');
|
|
console.error('Error deleting link:', error);
|
|
}
|
|
}
|
|
|
|
// Show toast message
|
|
function showMessage(text, type = 'success') {
|
|
const toast = document.createElement('div');
|
|
toast.className = `toast toast-${type}`;
|
|
toast.textContent = text;
|
|
|
|
// Add to container
|
|
toastContainer.appendChild(toast);
|
|
|
|
// Trigger animation
|
|
requestAnimationFrame(() => {
|
|
toast.classList.add('show');
|
|
});
|
|
|
|
// Remove after delay
|
|
setTimeout(() => {
|
|
toast.classList.remove('show');
|
|
setTimeout(() => {
|
|
if (toast.parentNode) {
|
|
toast.parentNode.removeChild(toast);
|
|
}
|
|
}, 300); // Wait for fade-out animation
|
|
}, 3000);
|
|
}
|
|
|
|
// Escape HTML to prevent XSS
|
|
function escapeHtml(text) {
|
|
const div = document.createElement('div');
|
|
div.textContent = text;
|
|
return div.innerHTML;
|
|
}
|
|
|