Skip to content

Commit

Permalink
typing indicators
Browse files Browse the repository at this point in the history
  • Loading branch information
michelson committed Nov 15, 2023
1 parent 194ec7f commit f154763
Show file tree
Hide file tree
Showing 15 changed files with 116 additions and 6 deletions.
2 changes: 2 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ APP PACKAGES:
+ notify typing
+ hide text chat on wait package

+ articles frame are not in iframe, iframeize it

improvements UI
5 changes: 5 additions & 0 deletions app/controllers/apps/conversation_messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ def create
})
end

def notify_typing
@conversation = @app.conversations.find_by(key: params[:conversation_id])
@conversation.notify_typing_to_user
end

def update
if params[:read]
@conversation_part = @app.conversation_parts.find_by(key: params[:id])
Expand Down
8 changes: 7 additions & 1 deletion app/controllers/messenger_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def events
when "track_tour_finished" then track_tour_finished(params["messenger"])
when "track_tour_skipped" then track_tour_skipped(params["messenger"])
when "update_conversation_state" then render_conversation_state(params)
when "notify_typing" then notify_typing

end

Expand Down Expand Up @@ -96,6 +97,11 @@ def home_apps

def render_conversation_state; end

def notify_typing
conversation = @app.conversations.find_by(key: params[:conversation])
conversation.notify_typing_to_agents
end

private

def get_messages_for_user
Expand Down Expand Up @@ -153,7 +159,7 @@ def resolve_contents

@home_apps = home_apps

@latest_conversations = @app_user.conversations.limit(4)
@latest_conversations = @app_user.conversations.order("updated_at desc").limit(4)

@needs_privacy_consent = needs_privacy_consent
end
Expand Down
15 changes: 15 additions & 0 deletions app/javascript/controllers/chat_editor_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,21 @@ export default class extends Controller {
overflow.scrollTop = overflow.scrollHeight;
}

notifyTyping() {
this.sendPost(
`${this.actionPath}/notify_typing`,
{ type: 'typing' },
'post'
);
}

typingNotifier() {
console.log('NOTIFY TYPING');

clearTimeout(this.delayTimer);
this.delayTimer = setTimeout(() => {
this.notifyTyping();
}, 400);
}

toggleClass() {
Expand All @@ -202,6 +215,7 @@ export default class extends Controller {
}

initialize() {
this.delayTimer = null;
this.actionPath = this.element.dataset.editorActionPath;
this.messageMode = 'public';

Expand All @@ -221,6 +235,7 @@ export default class extends Controller {
this.valueNotified = val;
this.editorRef = editorRef;
console.log(this.valueNotified);
this.typingNotifier();
}}
handleReturn={(e, isEmptyDraft, value) => {
console.log(e);
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/controllers/custom_event_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default class extends Controller {
sendCustomEvent() {
const eventData = this.element.dataset.values;
const eventName = this.element.dataset.eventName || 'ChaskiqEvent';
console.log('SENDING CUSTOM EVENT TO MESSENGER', eventName, eventData);
// console.log('SENDING CUSTOM EVENT TO MESSENGER', eventName, eventData);
const event = new CustomEvent(eventName, {
detail: { data: eventData },
bubbles: true,
Expand Down
13 changes: 13 additions & 0 deletions app/javascript/controllers/messenger_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default class extends Controller {
};

initialize() {
this.delayTimer = null;
// Bind the debounced version to the instance
this.debouncedHandleGiphySeach = debounce(
this.handleGiphySeach.bind(this),
Expand Down Expand Up @@ -566,6 +567,18 @@ export default class extends Controller {
handleChatInput(e) {
console.log('HANDLE typing');
console.log(e.type);

clearTimeout(this.delayTimer);
this.delayTimer = setTimeout(() => {
this.notifyTyping();
}, 400);
}

notifyTyping() {
console.log('NOTIFY TYPING');
this.pushEvent('notify_typing', {
conversation: this.currentConversationKey(),
});
}

setHeaderStyles(element, styles) {
Expand Down
23 changes: 23 additions & 0 deletions app/javascript/controllers/typing_indicator_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Controller } from '@hotwired/stimulus';

export default class extends Controller {
static targets = ['indicator'];

initialize() {
this.showIndicator();
this.debounce(this.hideIndicator, 1000);
}

showIndicator() {
this.indicatorTarget.classList.remove('hidden');
}

hideIndicator = () => {
this.indicatorTarget.classList.add('hidden');
};

debounce(func, delay) {
clearTimeout(this.timeout);
this.timeout = setTimeout(func, delay);
}
}
16 changes: 16 additions & 0 deletions app/models/conversation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,22 @@ def notify_typing(author = nil)
locals: { data: data }
end

def notify_typing_to_agents
broadcast_update_to app, :conversations,
target: nil,
targets: ".typing-#{key}",
partial: "apps/conversations/typing",
locals: { key: key }
end

def notify_typing_to_user
broadcast_update_to app, main_participant.id,
target: nil,
targets: ".typing-#{key}",
partial: "messenger/conversations/typing",
locals: { key: key }
end

private

def handle_part_details(part, opts)
Expand Down
4 changes: 4 additions & 0 deletions app/views/apps/conversations/_conversation.erb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@
<% end %>
</div>
</div>
<div class="typing-<%= conversation.key %>" class=" bottom-0">
</div>
</div>
</div>
<% end %>
8 changes: 8 additions & 0 deletions app/views/apps/conversations/_typing.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

<div data-controller="typing-indicator">
<div data-typing-indicator-target="indicator" class="typing-indicator mx-auto w-full pl-4">
<span class="text-xs ">
User is typing...
</span>
</div>
</div>
8 changes: 6 additions & 2 deletions app/views/apps/conversations/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
data: { controller: 'conversation' },
class: "flex w-full" do %>

<div class="md:w-0 md:flex-grow w-full bg-gray-200 dark:bg-gray-900 h-screen border-r dark:border-black">
<div class="relative md:w-0 md:flex-grow w-full bg-gray-200 dark:bg-gray-900 h-screen border-r dark:border-black">
<div
class="w-full flex-1 flex flex-col h-screen conversation-bg">
<div class="h-[60px] border-b flex px-6 py-3 items-center flex-none bg-white dark:bg-gray-900 dark:border-gray-800">
Expand Down Expand Up @@ -71,7 +71,7 @@

<div
id="conversation-overflow"
class="overflow-y-scroll"
class="overflow-y-scroll relative"
style="height: calc(100vh - 199px);">
<%= turbo_frame_tag 'conversation-messages',
src: app_conversation_conversation_messages_path(
Expand All @@ -82,6 +82,10 @@
<% end %>
</div>


<div class="typing-<%= @conversation.key %>" class=" bottom-0">
</div>

<div class="bg-gray-100 mb-2 mx-2 rounded-sm shadow-md">
<%= render 'editor' %>
</div>
Expand Down
3 changes: 3 additions & 0 deletions app/views/messenger/conversations/_conversation.erb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
<% end %>
</div>

<div class="typing-<%= conversation.key %> absolute top-[167px] left-[20px]">
</div>

<%= render "text_editor", app: app, conversation: conversation, user: user %>

</div>
Expand Down
8 changes: 8 additions & 0 deletions app/views/messenger/conversations/_typing.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

<div data-controller="typing-indicator">
<div data-typing-indicator-target="indicator" class="typing-indicator mx-auto w-full">
<span class="text-xs">
User is typing...
</span>
</div>
</div>
1 change: 0 additions & 1 deletion app/views/messenger/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<% #= Time.now.to_i %>
<%= turbo_stream_from @app, @app_user.id %>
<div style="display:none;" id="chaskiq-custom-events">
<% #= render "messenger/custom_event", data: {a: 1}.to_json %>
</div>
</div>
Expand Down
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@
member do
get :sidebar
end
resources :conversation_messages, controller: "apps/conversation_messages"
resources :conversation_messages, controller: "apps/conversation_messages" do
collection do
post :notify_typing
end
end
end

resources :editor_quick_replies, controller: "apps/editor_quick_replies"
Expand Down

0 comments on commit f154763

Please sign in to comment.