Skip to content

Commit 8b57d95

Browse files
authored
Merge pull request #82 from amatsuda/show_with_threads
messages#show with threads
2 parents 1c24a24 + d2ac64a commit 8b57d95

File tree

5 files changed

+53
-19
lines changed

5 files changed

+53
-19
lines changed

app/controllers/messages_controller.rb

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,7 @@ def index(list_name: nil, yyyymm: nil, q: nil, page: nil)
66
if list_name
77
@list = List.find_by_name list_name
88

9-
@yyyymms = Message.where(list_id: @list).order('yyyymm').pluck(Arel.sql "distinct to_char(published_at, 'YYYYMM') as yyyymm")
10-
@yyyymm = yyyymm || @yyyymms.last
11-
12-
root_query = Message.where(list_id: @list, parent_id: nil).where("to_char(published_at, 'YYYYMM') = ?", @yyyymm).order(:id)
13-
messages = Message.with_recursive(parent_and_children: [root_query, Message.joins('inner join parent_and_children on messages.parent_id = parent_and_children.id')])
14-
.joins('inner join parent_and_children on parent_and_children.id = messages.id')
15-
16-
@messages = compose_tree(messages)
9+
render_threads yyyymm: yyyymm
1710
elsif q
1811
search q, page
1912

@@ -29,10 +22,28 @@ def index(list_name: nil, yyyymm: nil, q: nil, page: nil)
2922
def show(list_name:, list_seq:)
3023
@list = List.find_by_name(list_name)
3124
@message = Message.find_by!(list_id: @list, list_seq: list_seq)
25+
26+
# If this is a turbo frame request, just render the message
27+
return if turbo_frame_request?
28+
29+
render_threads yyyymm: @message.published_at.strftime('%Y%m')
3230
end
3331

3432
private
3533

34+
def render_threads(yyyymm: nil)
35+
@yyyymms = Message.where(list_id: @list).order('yyyymm').pluck(Arel.sql "distinct to_char(published_at, 'YYYYMM') as yyyymm")
36+
@yyyymm = yyyymm || @yyyymms.last
37+
38+
root_query = Message.where(list_id: @list, parent_id: nil).where("to_char(published_at, 'YYYYMM') = ?", @yyyymm).order(:id)
39+
messages = Message.with_recursive(parent_and_children: [root_query, Message.joins('inner join parent_and_children on messages.parent_id = parent_and_children.id')])
40+
.joins('inner join parent_and_children on parent_and_children.id = messages.id')
41+
42+
@messages = compose_tree(messages)
43+
44+
render :index
45+
end
46+
3647
def get_list_ids(params)
3748
list_ids = []
3849
['ruby-talk', 'ruby-core', 'ruby-list', 'ruby-dev'].each do |name|

app/javascript/controllers/message_list_controller.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import { Controller } from "@hotwired/stimulus"
22

33
export default class extends Controller {
4+
connect() {
5+
// Scroll to selected message on page load, with a delay to ensure threads are expanded
6+
setTimeout(() => {
7+
const selected = this.element.querySelector('.message-selected')
8+
if (selected) {
9+
selected.scrollIntoView({behavior: 'smooth', block: 'center'})
10+
}
11+
}, 100)
12+
}
13+
414
select(event) {
515
// Remove highlight from previously selected message
616
const previousSelected = this.element.querySelector('.message-selected')

app/javascript/controllers/thread_controller.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,17 @@ export default class extends Controller {
55
static targets = ["children", "icon"]
66

77
connect() {
8-
// Initially hide children
8+
// Initially hide children unless one is selected
99
if (this.hasChildrenTarget) {
10-
this.childrenTarget.classList.add("hidden")
10+
const hasSelectedChild = this.childrenTarget.querySelector('.message-selected')
11+
if (hasSelectedChild) {
12+
// Keep expanded and rotate icon
13+
if (this.hasIconTarget) {
14+
this.iconTarget.classList.add("rotate-90")
15+
}
16+
} else {
17+
this.childrenTarget.classList.add("hidden")
18+
}
1119
}
1220
}
1321

app/views/messages/_thread.html.erb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<div class="thread-message" style="margin-left: <%= depth * 6 %>px;" <% if (depth == 0) && message.children&.any? %>data-controller="thread"<% end %>>
2+
<% selected = defined?(@message) && @message&.id == message.id %>
23
<% if depth == 0 %>
3-
<%= link_to [list, message], class: 'message-item block bg-white dark:bg-gray-800 rounded-lg shadow-md border border-gray-200 dark:border-gray-700 overflow-hidden hover:shadow-lg transition-shadow', data: {turbo_frame: 'message_content', turbo_action: 'advance', action: 'click->message-list#select'} do %>
4+
<%= link_to [list, message], class: "message-item block bg-white dark:bg-gray-800 rounded-lg shadow-md border border-gray-200 dark:border-gray-700 overflow-hidden hover:shadow-lg transition-shadow#{' message-selected' if selected}", data: {turbo_frame: 'message_content', turbo_action: 'advance', action: 'click->message-list#select'} do %>
45
<div class="p-5">
56
<div class="flex items-start justify-between gap-4">
67
<div class="flex-1 min-w-0">
@@ -35,7 +36,7 @@
3536
</div>
3637
<% end %>
3738
<% else %>
38-
<%= link_to [list, message], class: 'message-item block py-2 border-l-2 border-gray-200 dark:border-gray-700 pl-4 hover:border-red-400 dark:hover:border-red-500 transition-colors', data: {turbo_frame: 'message_content', turbo_action: 'advance', action: 'click->message-list#select'} do %>
39+
<%= link_to [list, message], class: "message-item block py-2 border-l-2 border-gray-200 dark:border-gray-700 pl-4 hover:border-red-400 dark:hover:border-red-500 transition-colors#{' message-selected' if selected}", data: {turbo_frame: 'message_content', turbo_action: 'advance', action: 'click->message-list#select'} do %>
3940
<div class="flex items-start gap-2 text-sm">
4041
<svg class="w-4 h-4 text-gray-400 dark:text-gray-500 mt-0.5 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
4142
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 10h10a8 8 0 018 8v2M3 10l6 6m-6-6l6-6"></path>

app/views/messages/index.html.erb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,18 @@
4242

4343
<div class="col-span-3 overflow-y-auto">
4444
<%= turbo_frame_tag "message_content", class: "block" do %>
45-
<div class="flex items-center justify-center h-full text-gray-400 dark:text-gray-500">
46-
<div class="text-center">
47-
<svg class="w-16 h-16 mx-auto mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
48-
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"></path>
49-
</svg>
50-
<p>Select a message to view</p>
45+
<% if @message %>
46+
<%= render @message %>
47+
<% else %>
48+
<div class="flex items-center justify-center h-full text-gray-400 dark:text-gray-500">
49+
<div class="text-center">
50+
<svg class="w-16 h-16 mx-auto mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
51+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"></path>
52+
</svg>
53+
<p>Select a message to view</p>
54+
</div>
5155
</div>
52-
</div>
56+
<% end %>
5357
<% end %>
5458
</div>
5559
</div>

0 commit comments

Comments
 (0)