Skip to content

Commit e83b40b

Browse files
committed
allow sorting search results
1 parent 1ac7e72 commit e83b40b

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

render-html.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ if (rooms.length > 0) {
8585
function cpr(inDir, outDir) {
8686
fs.mkdirSync(outDir, { recursive: true });
8787
for (let file of fs.readdirSync(inDir)) {
88+
if (file.startsWith('.')) {
89+
continue;
90+
}
8891
let inFile = path.join(inDir, file);
8992
let outFile = path.join(outDir, file);
9093
if (fs.lstatSync(inFile).isDirectory()) {
@@ -277,7 +280,6 @@ function escapeForHtml(str) {
277280

278281

279282
function renderSearch(rooms, room) {
280-
// TODO fix the path to the sql to point to actual domain
281283
return `<!doctype html>
282284
283285
<title>Search ${room}</title>
@@ -310,6 +312,10 @@ ${renderSearchbar(room)}
310312
function renderSearchbar(room) {
311313
return `<div class="rhs-header">
312314
<span id="error" style="color: red; display:none">error</span>
315+
<select id="sort-order">
316+
<option value="oldest">Oldest first</option>
317+
<option value="newest">Newest first</option>
318+
</select>
313319
<input type="text" id="query" size=25 placeholder="Search ${room}">
314320
<a id="search-submit" class="button icon-link" title="Search">
315321
<svg class="icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M505 442.7L405.3 343c-4.5-4.5-10.6-7-17-7H372c27.6-35.3 44-79.7 44-128C416 93.1 322.9 0 208 0S0 93.1 0 208s93.1 208 208 208c48.3 0 92.7-16.4 128-44v16.3c0 6.4 2.5 12.5 7 17l99.7 99.7c9.4 9.4 24.6 9.4 33.9 0l28.3-28.3c9.4-9.4 9.4-24.6.1-34zM208 336c-70.7 0-128-57.2-128-128 0-70.7 57.2-128 128-128 70.7 0 128 57.2 128 128 0 70.7-57.2 128-128 128z"></path></svg>

scripts/search.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ async function load(configUrl) {
3333
let loadMore = document.getElementById('load-more');
3434
let thinking = document.getElementById('thinking');
3535
let errorOut = document.getElementById('error');
36+
let sort = document.getElementById('sort-order');
3637
let output = document.getElementById('search-output');
3738

3839
function more(isMore) {
@@ -55,6 +56,12 @@ async function load(configUrl) {
5556
}
5657
});
5758

59+
if (localStorage.getItem('sort-order') === 'newest') {
60+
// default for the element and therefore for this code is "oldest".
61+
sort.value = 'newest';
62+
}
63+
let lastSort = sort.value;
64+
5865
function startSearch() {
5966
search(query.value, 0);
6067
}
@@ -70,6 +77,12 @@ async function load(configUrl) {
7077
throw new Error('must be at least 3 characters');
7178
}
7279

80+
if (sort.value !== lastSort) {
81+
offset = 0;
82+
localStorage.setItem('sort-order', sort.value);
83+
lastSort = sort.value;
84+
}
85+
7386
if (offset === 0) {
7487
output.innerHTML = '';
7588
previousSearch = query;
@@ -83,7 +96,7 @@ async function load(configUrl) {
8396
more(false);
8497

8598
// we fetch an extra so we can tell if we're done
86-
let cmd = `select * from search where search match '${escapeForSql(query)}' limit ${perPage + 1} offset ${offset}`;
99+
let cmd = `select * from search where search match '${escapeForSql(query)}' order by rowid ${sort.value === 'newest' ? 'desc' : ''} limit ${perPage + 1} offset ${offset}`;
87100

88101
worker.worker.bytesRead = 0;
89102
let page = await worker.db.query(cmd);

utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ let root = path.join(__dirname, 'logs', 'json');
77
let historicalRoot = path.join(__dirname, 'logs', 'historical-json');
88

99
let rooms = [
10-
...fs.readdirSync(root).sort().map(room => ({ historical: false, room })),
11-
...fs.existsSync(historicalRoot) ? fs.readdirSync(historicalRoot).sort().map(room => ({ historical: true, room })) : [],
10+
...fs.readdirSync(root).filter(r => !r.startsWith('.')).sort().map(room => ({ historical: false, room })),
11+
...fs.existsSync(historicalRoot) ? fs.readdirSync(historicalRoot).filter(r => !r.startsWith('.')).sort().map(room => ({ historical: true, room })) : [],
1212
];
1313

1414

0 commit comments

Comments
 (0)