feat: the app
This commit is contained in:
208
public/app.js
Normal file
208
public/app.js
Normal file
@@ -0,0 +1,208 @@
|
||||
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 messageDiv = document.getElementById('message');
|
||||
|
||||
// State
|
||||
let allLinks = [];
|
||||
let searchTimeout = null;
|
||||
|
||||
// Initialize app
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
loadLinks();
|
||||
setupEventListeners();
|
||||
});
|
||||
|
||||
// Event listeners
|
||||
function setupEventListeners() {
|
||||
linkForm.addEventListener('submit', handleAddLink);
|
||||
searchInput.addEventListener('input', handleSearch);
|
||||
}
|
||||
|
||||
// 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();
|
||||
displayLinks(allLinks);
|
||||
} catch (error) {
|
||||
showMessage('Failed to load links', 'error');
|
||||
console.error('Error loading links:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 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');
|
||||
btnText.style.display = 'none';
|
||||
btnLoader.style.display = 'inline';
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
// Add to beginning of array
|
||||
allLinks.unshift(data);
|
||||
displayLinks(allLinks);
|
||||
|
||||
// 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;
|
||||
btnText.style.display = 'inline';
|
||||
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(allLinks);
|
||||
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();
|
||||
displayLinks(filteredLinks);
|
||||
} catch (error) {
|
||||
showMessage('Search failed', 'error');
|
||||
console.error('Error searching:', error);
|
||||
}
|
||||
}, 300);
|
||||
}
|
||||
|
||||
// Display links
|
||||
function displayLinks(links) {
|
||||
if (links.length === 0) {
|
||||
linksContainer.innerHTML = `
|
||||
<div class="empty-state">
|
||||
<p>No links found. ${allLinks.length === 0 ? 'Add your first link to get started!' : 'Try a different search term.'}</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);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 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>
|
||||
<button class="delete-btn">Delete</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
// 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(allLinks);
|
||||
showMessage('Link deleted successfully', 'success');
|
||||
} catch (error) {
|
||||
showMessage('Failed to delete link', 'error');
|
||||
console.error('Error deleting link:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// Show message
|
||||
function showMessage(text, type = 'success') {
|
||||
messageDiv.textContent = text;
|
||||
messageDiv.className = `message ${type}`;
|
||||
messageDiv.style.display = 'block';
|
||||
|
||||
setTimeout(() => {
|
||||
messageDiv.style.display = 'none';
|
||||
}, 3000);
|
||||
}
|
||||
|
||||
// Escape HTML to prevent XSS
|
||||
function escapeHtml(text) {
|
||||
const div = document.createElement('div');
|
||||
div.textContent = text;
|
||||
return div.innerHTML;
|
||||
}
|
||||
|
||||
55
public/index.html
Normal file
55
public/index.html
Normal file
@@ -0,0 +1,55 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>LinkDing - Your Link Collection</title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<header>
|
||||
<h1>🔗 LinkDing</h1>
|
||||
<p class="subtitle">Collect and organize your favorite links</p>
|
||||
</header>
|
||||
|
||||
<div class="input-section">
|
||||
<form id="linkForm" class="link-form">
|
||||
<div class="input-group">
|
||||
<input
|
||||
type="url"
|
||||
id="linkInput"
|
||||
placeholder="Paste a link here..."
|
||||
required
|
||||
autocomplete="off"
|
||||
>
|
||||
<button type="submit" id="addButton" class="btn-primary">
|
||||
<span class="btn-text">Add Link</span>
|
||||
<span class="btn-loader" style="display: none;">⏳</span>
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<div class="search-section">
|
||||
<input
|
||||
type="text"
|
||||
id="searchInput"
|
||||
placeholder="🔍 Search links by title, description, or URL..."
|
||||
autocomplete="off"
|
||||
>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="message" class="message" style="display: none;"></div>
|
||||
|
||||
<div id="linksContainer" class="links-container">
|
||||
<div class="empty-state">
|
||||
<p>No links yet. Add your first link to get started!</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="app.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
340
public/styles.css
Normal file
340
public/styles.css
Normal file
@@ -0,0 +1,340 @@
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
:root {
|
||||
--primary-color: #6366f1;
|
||||
--primary-hover: #4f46e5;
|
||||
--secondary-color: #8b5cf6;
|
||||
--background: #0f172a;
|
||||
--surface: #1e293b;
|
||||
--surface-light: #334155;
|
||||
--text-primary: #f1f5f9;
|
||||
--text-secondary: #cbd5e1;
|
||||
--text-muted: #94a3b8;
|
||||
--border: #334155;
|
||||
--success: #10b981;
|
||||
--error: #ef4444;
|
||||
--shadow: rgba(0, 0, 0, 0.3);
|
||||
--shadow-lg: rgba(0, 0, 0, 0.5);
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
|
||||
background: linear-gradient(135deg, var(--background) 0%, #1a1f3a 100%);
|
||||
color: var(--text-primary);
|
||||
min-height: 100vh;
|
||||
padding: 2rem 1rem;
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
.container {
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
header {
|
||||
text-align: center;
|
||||
margin-bottom: 3rem;
|
||||
animation: fadeIn 0.6s ease-out;
|
||||
}
|
||||
|
||||
header h1 {
|
||||
font-size: 3rem;
|
||||
font-weight: 700;
|
||||
background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
|
||||
-webkit-background-clip: text;
|
||||
-webkit-text-fill-color: transparent;
|
||||
background-clip: text;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.subtitle {
|
||||
color: var(--text-secondary);
|
||||
font-size: 1.1rem;
|
||||
}
|
||||
|
||||
.input-section {
|
||||
margin-bottom: 2rem;
|
||||
animation: fadeIn 0.8s ease-out;
|
||||
}
|
||||
|
||||
.link-form {
|
||||
margin-bottom: 1.5rem;
|
||||
}
|
||||
|
||||
.input-group {
|
||||
display: flex;
|
||||
gap: 1rem;
|
||||
background: var(--surface);
|
||||
padding: 0.5rem;
|
||||
border-radius: 12px;
|
||||
border: 1px solid var(--border);
|
||||
box-shadow: 0 4px 6px var(--shadow);
|
||||
transition: all 0.3s ease;
|
||||
}
|
||||
|
||||
.input-group:focus-within {
|
||||
border-color: var(--primary-color);
|
||||
box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
|
||||
}
|
||||
|
||||
#linkInput {
|
||||
flex: 1;
|
||||
background: transparent;
|
||||
border: none;
|
||||
color: var(--text-primary);
|
||||
font-size: 1rem;
|
||||
padding: 1rem;
|
||||
outline: none;
|
||||
}
|
||||
|
||||
#linkInput::placeholder {
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 1rem 2rem;
|
||||
border-radius: 8px;
|
||||
font-size: 1rem;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s ease;
|
||||
white-space: nowrap;
|
||||
box-shadow: 0 4px 6px var(--shadow);
|
||||
}
|
||||
|
||||
.btn-primary:hover:not(:disabled) {
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 6px 12px var(--shadow-lg);
|
||||
}
|
||||
|
||||
.btn-primary:active:not(:disabled) {
|
||||
transform: translateY(0);
|
||||
}
|
||||
|
||||
.btn-primary:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.search-section {
|
||||
background: var(--surface);
|
||||
padding: 0.5rem;
|
||||
border-radius: 12px;
|
||||
border: 1px solid var(--border);
|
||||
box-shadow: 0 4px 6px var(--shadow);
|
||||
}
|
||||
|
||||
#searchInput {
|
||||
width: 100%;
|
||||
background: transparent;
|
||||
border: none;
|
||||
color: var(--text-primary);
|
||||
font-size: 1rem;
|
||||
padding: 1rem;
|
||||
outline: none;
|
||||
}
|
||||
|
||||
#searchInput::placeholder {
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
.message {
|
||||
padding: 1rem;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 1.5rem;
|
||||
animation: slideDown 0.3s ease-out;
|
||||
}
|
||||
|
||||
.message.success {
|
||||
background: rgba(16, 185, 129, 0.2);
|
||||
border: 1px solid var(--success);
|
||||
color: var(--success);
|
||||
}
|
||||
|
||||
.message.error {
|
||||
background: rgba(239, 68, 68, 0.2);
|
||||
border: 1px solid var(--error);
|
||||
color: var(--error);
|
||||
}
|
||||
|
||||
.links-container {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
|
||||
gap: 1.5rem;
|
||||
animation: fadeIn 1s ease-out;
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
grid-column: 1 / -1;
|
||||
text-align: center;
|
||||
padding: 4rem 2rem;
|
||||
color: var(--text-muted);
|
||||
font-size: 1.1rem;
|
||||
}
|
||||
|
||||
.link-card {
|
||||
background: var(--surface);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 16px;
|
||||
overflow: hidden;
|
||||
transition: all 0.3s ease;
|
||||
box-shadow: 0 4px 6px var(--shadow);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
animation: fadeInUp 0.5s ease-out;
|
||||
}
|
||||
|
||||
.link-card:hover {
|
||||
transform: translateY(-4px);
|
||||
box-shadow: 0 8px 16px var(--shadow-lg);
|
||||
border-color: var(--primary-color);
|
||||
}
|
||||
|
||||
.link-image {
|
||||
width: 100%;
|
||||
height: 200px;
|
||||
object-fit: cover;
|
||||
background: var(--surface-light);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
color: var(--text-muted);
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.link-image img {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
}
|
||||
|
||||
.link-content {
|
||||
padding: 1.5rem;
|
||||
flex: 1;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.link-title {
|
||||
font-size: 1.25rem;
|
||||
font-weight: 600;
|
||||
margin-bottom: 0.75rem;
|
||||
color: var(--text-primary);
|
||||
line-height: 1.4;
|
||||
display: -webkit-box;
|
||||
-webkit-line-clamp: 2;
|
||||
-webkit-box-orient: vertical;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.link-description {
|
||||
color: var(--text-secondary);
|
||||
font-size: 0.95rem;
|
||||
margin-bottom: 1rem;
|
||||
flex: 1;
|
||||
display: -webkit-box;
|
||||
-webkit-line-clamp: 3;
|
||||
-webkit-box-orient: vertical;
|
||||
overflow: hidden;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.link-url {
|
||||
color: var(--primary-color);
|
||||
font-size: 0.85rem;
|
||||
text-decoration: none;
|
||||
margin-bottom: 1rem;
|
||||
word-break: break-all;
|
||||
transition: color 0.3s ease;
|
||||
}
|
||||
|
||||
.link-url:hover {
|
||||
color: var(--primary-hover);
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.link-footer {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding-top: 1rem;
|
||||
border-top: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.link-date {
|
||||
color: var(--text-muted);
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
|
||||
.delete-btn {
|
||||
background: rgba(239, 68, 68, 0.2);
|
||||
color: var(--error);
|
||||
border: 1px solid var(--error);
|
||||
padding: 0.5rem 1rem;
|
||||
border-radius: 6px;
|
||||
font-size: 0.85rem;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s ease;
|
||||
}
|
||||
|
||||
.delete-btn:hover {
|
||||
background: var(--error);
|
||||
color: white;
|
||||
}
|
||||
|
||||
@keyframes fadeIn {
|
||||
from {
|
||||
opacity: 0;
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes fadeInUp {
|
||||
from {
|
||||
opacity: 0;
|
||||
transform: translateY(20px);
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
transform: translateY(0);
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes slideDown {
|
||||
from {
|
||||
opacity: 0;
|
||||
transform: translateY(-10px);
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
transform: translateY(0);
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
header h1 {
|
||||
font-size: 2rem;
|
||||
}
|
||||
|
||||
.input-group {
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.links-container {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user