Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,21 @@ def calculate_navigation_links
end

# Find previous/next thread (root messages)
@prev_thread = Message.where(list_id: @list, parent_id: nil).where('id < ?', root.id).order(id: :desc).first
@next_thread = Message.where(list_id: @list, parent_id: nil).where('id > ?', root.id).order(:id).first
@prev_thread_seq = Message.where(list_id: @list, parent_id: nil).where('id < ?', root.id).order(id: :desc).pick(:list_seq)
@next_thread_seq = Message.where(list_id: @list, parent_id: nil).where('id > ?', root.id).order(:id).pick(:list_seq)

# Get all messages in this thread
thread_messages = Message.with_recursive(
thread_msgs: [
Message.where(id: root.id),
Message.joins('inner join thread_msgs on messages.parent_id = thread_msgs.id')
]
).joins('inner join thread_msgs on thread_msgs.id = messages.id').order(:id).to_a
).joins('inner join thread_msgs on thread_msgs.id = messages.id').order(:id).pluck(:id, :list_seq)

# Find previous/next message in thread
current_index = thread_messages.index {|m| m.id == @message.id }
@prev_message_in_thread = thread_messages[current_index - 1] if current_index && current_index > 0
@next_message_in_thread = thread_messages[current_index + 1] if current_index
current_index = thread_messages.index {|(id, _)| id == @message.id }
@prev_message_in_thread_seq = thread_messages[current_index - 1]&.last if current_index && current_index > 0
@next_message_in_thread_seq = thread_messages[current_index + 1]&.last if current_index
end

def render_threads(yyyymm: nil, q: nil)
Expand Down
18 changes: 9 additions & 9 deletions app/views/messages/_message.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@
</div>
<% end %>

<% if defined?(@prev_thread) || defined?(@next_thread) || defined?(@prev_message_in_thread) || defined?(@next_message_in_thread) %>
<% if defined?(@prev_thread_seq) || defined?(@next_thread_seq) || defined?(@prev_message_in_thread_seq) || defined?(@next_message_in_thread_seq) %>
<div class="border-t border-gray-200 dark:border-gray-700 px-6 py-4 bg-gray-50 dark:bg-gray-900" data-controller="keyboard-nav">
<div class="grid grid-cols-2 gap-4">
<div>
<h3 class="text-xs font-medium text-gray-500 dark:text-gray-400 mb-2">Thread</h3>
<div class="flex gap-2">
<% if @prev_thread %>
<%= link_to [message.list, @prev_thread], class: "inline-flex items-center gap-1 px-3 py-2 text-sm font-medium rounded bg-gray-100 dark:bg-gray-800 text-gray-700 dark:text-gray-300 hover:bg-gray-200 dark:hover:bg-gray-700 transition-colors", data: {turbo_frame: 'message_content', turbo_action: 'advance', nav: 'prev-thread'} do %>
<% if @prev_thread_seq %>
<%= link_to list_message_path(message.list, @prev_thread_seq), class: "inline-flex items-center gap-1 px-3 py-2 text-sm font-medium rounded bg-gray-100 dark:bg-gray-800 text-gray-700 dark:text-gray-300 hover:bg-gray-200 dark:hover:bg-gray-700 transition-colors", data: {turbo_frame: 'message_content', turbo_action: 'advance', nav: 'prev-thread'} do %>
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"></path>
</svg>
Expand All @@ -54,8 +54,8 @@
Prev
</span>
<% end %>
<% if @next_thread %>
<%= link_to [message.list, @next_thread], class: "inline-flex items-center gap-1 px-3 py-2 text-sm font-medium rounded bg-gray-100 dark:bg-gray-800 text-gray-700 dark:text-gray-300 hover:bg-gray-200 dark:hover:bg-gray-700 transition-colors", data: {turbo_frame: 'message_content', turbo_action: 'advance', nav: 'next-thread'} do %>
<% if @next_thread_seq %>
<%= link_to list_message_path(message.list, @next_thread_seq), class: "inline-flex items-center gap-1 px-3 py-2 text-sm font-medium rounded bg-gray-100 dark:bg-gray-800 text-gray-700 dark:text-gray-300 hover:bg-gray-200 dark:hover:bg-gray-700 transition-colors", data: {turbo_frame: 'message_content', turbo_action: 'advance', nav: 'next-thread'} do %>
Next
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"></path>
Expand All @@ -74,8 +74,8 @@
<div>
<h3 class="text-xs font-medium text-gray-500 dark:text-gray-400 mb-2">In This Thread</h3>
<div class="flex gap-2">
<% if @prev_message_in_thread %>
<%= link_to [message.list, @prev_message_in_thread], class: "inline-flex items-center gap-1 px-3 py-2 text-sm font-medium rounded bg-gray-100 dark:bg-gray-800 text-gray-700 dark:text-gray-300 hover:bg-gray-200 dark:hover:bg-gray-700 transition-colors", data: {turbo_frame: 'message_content', turbo_action: 'advance', nav: 'prev-message'} do %>
<% if @prev_message_in_thread_seq %>
<%= link_to list_message_path(message.list, @prev_message_in_thread_seq), class: "inline-flex items-center gap-1 px-3 py-2 text-sm font-medium rounded bg-gray-100 dark:bg-gray-800 text-gray-700 dark:text-gray-300 hover:bg-gray-200 dark:hover:bg-gray-700 transition-colors", data: {turbo_frame: 'message_content', turbo_action: 'advance', nav: 'prev-message'} do %>
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"></path>
</svg>
Expand All @@ -89,8 +89,8 @@
Prev
</span>
<% end %>
<% if @next_message_in_thread %>
<%= link_to [message.list, @next_message_in_thread], class: "inline-flex items-center gap-1 px-3 py-2 text-sm font-medium rounded bg-gray-100 dark:bg-gray-800 text-gray-700 dark:text-gray-300 hover:bg-gray-200 dark:hover:bg-gray-700 transition-colors", data: {turbo_frame: 'message_content', turbo_action: 'advance', nav: 'next-message'} do %>
<% if @next_message_in_thread_seq %>
<%= link_to list_message_path(message.list, @next_message_in_thread_seq), class: "inline-flex items-center gap-1 px-3 py-2 text-sm font-medium rounded bg-gray-100 dark:bg-gray-800 text-gray-700 dark:text-gray-300 hover:bg-gray-200 dark:hover:bg-gray-700 transition-colors", data: {turbo_frame: 'message_content', turbo_action: 'advance', nav: 'next-message'} do %>
Next
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"></path>
Expand Down
Loading