-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseful-links.html
More file actions
224 lines (195 loc) · 9.19 KB
/
useful-links.html
File metadata and controls
224 lines (195 loc) · 9.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Useful Links</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="style.css">
<link rel="icon" type="image/png" href="scholarsphere-icon.png">
</head>
<body>
<div class="container">
<h1 class="text-center my-3">Useful Links</h1>
<div class="mb-3 d-flex flex-wrap gap-2">
<a href="index.html" class="btn btn-secondary mr-2 mb-2">Read Papers</a>
<a href="unread-papers.html" class="btn btn-primary mr-2 mb-2">Unread Papers</a>
<a href="datasets.html" class="btn btn-warning mr-2 mb-2">Datasets</a>
<a href="topics.html" class="btn btn-dark mr-2 mb-2">Topics</a>
<a href="useful-links.html" class="btn btn-info mb-2 disabled" tabindex="-1" aria-disabled="true">Useful Links</a>
</div>
<div class="card mb-4 subitems-panel">
<div class="card-body">
<button type="button" id="link-form-toggle" class="btn btn-info btn-sm mb-3">Add Useful Link</button>
<div id="link-form-panel" style="display:none;">
<form id="link-form">
<div class="form-group">
<label for="link-title">Title</label>
<input type="text" id="link-title" class="form-control" placeholder="e.g., Great survey on LLM safety" required>
</div>
<div class="form-group">
<label for="link-url">URL</label>
<input type="url" id="link-url" class="form-control" placeholder="https://example.com" required>
</div>
<div class="form-group">
<label for="link-description">Short Description</label>
<textarea id="link-description" class="form-control" rows="2" placeholder="Why this link is useful"></textarea>
</div>
<button type="submit" class="btn btn-info">Save Link</button>
</form>
</div>
</div>
</div>
<div class="card">
<div class="card-body">
<h3 class="card-title">Saved Links</h3>
<div id="links-list" class="list-group mt-3"></div>
</div>
</div>
</div>
<script>
const LINKS_ENDPOINT = '/useful-links';
let usefulLinks = [];
let isLinkFormVisible = false;
function setLinkFormVisible(visible, options = {}) {
const panel = document.getElementById('link-form-panel');
const toggleBtn = document.getElementById('link-form-toggle');
if (!panel || !toggleBtn) return;
isLinkFormVisible = visible;
panel.style.display = visible ? 'block' : 'none';
toggleBtn.textContent = visible ? 'Hide Form' : 'Add Useful Link';
if (visible && options.focusTitle) {
document.getElementById('link-title')?.focus();
}
}
function normalizeLink(raw) {
return {
id: raw.id || Date.now(),
title: (raw.title || '').trim() || 'Untitled link',
url: (raw.url || '').trim(),
description: (raw.description || '').trim(),
createdAt: raw.createdAt || new Date().toISOString()
};
}
async function fetchUsefulLinks() {
try {
const resp = await fetch(LINKS_ENDPOINT, { cache: 'no-store' });
if (!resp.ok) throw new Error(`HTTP ${resp.status}`);
const data = await resp.json();
const items = Array.isArray(data.items) ? data.items.map(normalizeLink) : [];
usefulLinks = items;
return items;
} catch (e) {
console.error('Failed to load useful links:', e);
return usefulLinks || [];
}
}
async function saveUsefulLinks(list) {
try {
const resp = await fetch(LINKS_ENDPOINT, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ items: list })
});
if (!resp.ok) throw new Error(`HTTP ${resp.status}`);
usefulLinks = list;
return true;
} catch (e) {
console.error('Failed to save useful links:', e);
alert('Could not save useful links. Please ensure the server is running and try again.');
return false;
}
}
function formatDate(dateString) {
const d = new Date(dateString);
return isNaN(d.getTime()) ? 'Unknown' : d.toLocaleString();
}
async function addUsefulLink(event) {
event.preventDefault();
const titleInput = document.getElementById('link-title');
const urlInput = document.getElementById('link-url');
const descriptionInput = document.getElementById('link-description');
const title = (titleInput.value || '').trim();
const url = (urlInput.value || '').trim();
const description = (descriptionInput.value || '').trim();
if (!title || !url) {
alert('Please add both a title and a URL.');
return;
}
const newItem = {
id: Date.now(),
title,
url,
description,
createdAt: new Date().toISOString()
};
const list = [...usefulLinks, newItem];
const ok = await saveUsefulLinks(list);
if (!ok) return;
titleInput.value = '';
urlInput.value = '';
descriptionInput.value = '';
setLinkFormVisible(false);
renderUsefulLinks();
}
async function deleteUsefulLink(id) {
const list = usefulLinks.filter(item => item.id !== id);
const ok = await saveUsefulLinks(list);
if (ok) renderUsefulLinks();
}
function renderUsefulLinks(listOverride) {
const list = (listOverride || usefulLinks)
.slice()
.sort((a, b) => {
const aTime = new Date(a.createdAt || 0).getTime();
const bTime = new Date(b.createdAt || 0).getTime();
if (aTime !== bTime) return bTime - aTime;
return (a.title || '').localeCompare(b.title || '');
});
const container = document.getElementById('links-list');
if (!container) return;
container.innerHTML = '';
if (list.length === 0) {
container.innerHTML = '<div class="list-group-item text-muted">No useful links yet. Add one above.</div>';
return;
}
list.forEach(item => {
const row = document.createElement('div');
row.className = 'list-group-item';
const header = document.createElement('div');
header.className = 'd-flex justify-content-between align-items-center flex-wrap';
const info = document.createElement('div');
const titleEl = document.createElement('div');
const anchor = document.createElement('a');
anchor.href = item.url || '#';
anchor.target = '_blank';
anchor.rel = 'noopener';
anchor.textContent = item.title || item.url || 'Untitled link';
titleEl.appendChild(anchor);
const descriptionEl = document.createElement('div');
descriptionEl.className = 'text-muted';
descriptionEl.textContent = item.description || '';
const metaEl = document.createElement('div');
metaEl.className = 'small text-muted';
metaEl.textContent = `Added: ${formatDate(item.createdAt)}`;
info.appendChild(titleEl);
if (item.description) info.appendChild(descriptionEl);
info.appendChild(metaEl);
const deleteBtn = document.createElement('button');
deleteBtn.className = 'btn btn-sm btn-outline-danger mt-2 mt-sm-0';
deleteBtn.textContent = 'Delete';
deleteBtn.onclick = () => deleteUsefulLink(item.id);
header.appendChild(info);
header.appendChild(deleteBtn);
row.appendChild(header);
container.appendChild(row);
});
}
document.getElementById('link-form-toggle').addEventListener('click', () => {
setLinkFormVisible(!isLinkFormVisible, { focusTitle: !isLinkFormVisible });
});
document.getElementById('link-form').addEventListener('submit', addUsefulLink);
fetchUsefulLinks().then(renderUsefulLinks);
</script>
</body>
</html>