Skip to content

Commit c7fadc6

Browse files
committed
feat: Add sort options with links above comments section
1 parent b47ff21 commit c7fadc6

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

frontend/src/components/meh-comments/meh-comments.css

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,37 @@
55
color: var(--meh-error);
66
}
77

8+
div.sort-options {
9+
display: flex;
10+
align-items: center;
11+
margin-bottom: var(--meh-spacing);
12+
font-size: 0.9em;
13+
14+
.sort-label {
15+
margin-right: 0.5em;
16+
color: var(--meh-text-muted, #666);
17+
}
18+
19+
.sort-links {
20+
display: flex;
21+
gap: 1em;
22+
23+
a {
24+
color: var(--meh-link-color);
25+
text-decoration: none;
26+
27+
&.active {
28+
font-weight: bold;
29+
text-decoration: underline;
30+
}
31+
32+
&:hover {
33+
text-decoration: underline;
34+
}
35+
}
36+
}
37+
}
38+
839
> ul {
940
list-style-type: none;
1041
padding: 0;

frontend/src/components/meh-comments/meh-comments.tsx

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ export class MehComments {
7575
reply: 'Reply',
7676
confirmDelete: 'Are you sure you want to delete this comment?',
7777
inReplyTo: 'in reply to',
78+
sortOldest: 'Oldest',
79+
sortNewest: 'Newest',
80+
sortThreaded: 'Threaded',
81+
sortBy: 'Sort by:',
7882
};
7983

8084
// Translation manager instance
@@ -396,6 +400,55 @@ export class MehComments {
396400
}
397401
}
398402

403+
/**
404+
* Handle sort change
405+
*/
406+
private handleSortChange = (newSort: 'oldest' | 'newest' | 'threaded', e: Event) => {
407+
e.preventDefault();
408+
this.sort = newSort;
409+
410+
// Save the sort preference to localStorage
411+
try {
412+
localStorage.setItem('meh-sort', newSort);
413+
} catch (e) {
414+
console.warn('Could not save sort preference to localStorage:', e);
415+
}
416+
};
417+
418+
/**
419+
* Render sort options
420+
*/
421+
private renderSortOptions() {
422+
return (
423+
<div class="sort-options">
424+
<span class="sort-label">{this._('sortBy')}</span>
425+
<div class="sort-links">
426+
<a
427+
href="#"
428+
class={this.sort === 'oldest' ? 'active' : ''}
429+
onClick={(e) => this.handleSortChange('oldest', e)}
430+
>
431+
{this._('sortOldest')}
432+
</a>
433+
<a
434+
href="#"
435+
class={this.sort === 'newest' ? 'active' : ''}
436+
onClick={(e) => this.handleSortChange('newest', e)}
437+
>
438+
{this._('sortNewest')}
439+
</a>
440+
<a
441+
href="#"
442+
class={this.sort === 'threaded' ? 'active' : ''}
443+
onClick={(e) => this.handleSortChange('threaded', e)}
444+
>
445+
{this._('sortThreaded')}
446+
</a>
447+
</div>
448+
</div>
449+
);
450+
}
451+
399452
/**
400453
* Render comments in a threaded/nested structure
401454
*/
@@ -467,6 +520,8 @@ export class MehComments {
467520
} else if (this.comments.length === 0) {
468521
elements.push(this.renderNoComments());
469522
} else {
523+
// Add sort options above the comments
524+
elements.push(this.renderSortOptions());
470525
elements.push(this.renderCommentsList());
471526
}
472527

0 commit comments

Comments
 (0)