2024-10-07 23:21:49 +00:00
|
|
|
defmodule SlaxWeb.ChatRoomLive do
|
|
|
|
@moduledoc false
|
|
|
|
use SlaxWeb, :live_view
|
|
|
|
|
2024-10-15 00:27:18 +00:00
|
|
|
alias Slax.Chat
|
2024-10-23 22:33:47 +00:00
|
|
|
alias Slax.Chat.Message
|
2024-10-09 00:12:59 +00:00
|
|
|
alias Slax.Chat.Room
|
|
|
|
|
2024-10-10 23:40:44 +00:00
|
|
|
@impl Phoenix.LiveView
|
2024-10-07 23:21:49 +00:00
|
|
|
def render(assigns) do
|
|
|
|
~H"""
|
2024-10-10 00:30:46 +00:00
|
|
|
<div class="flex flex-col flex-shrink-0 w-64 bg-slate-100">
|
|
|
|
<div class="flex justify-between items-center flex-shrink-0 h-16 border-b border-slate-300 px-4">
|
|
|
|
<div class="flex flex-col gap-1.5">
|
|
|
|
<h1 class="text-lg font-bold text-gray-800">Slax</h1>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div clas="mt-4 overflow-auto">
|
|
|
|
<div class="flex items-center h-8 px-3 group">
|
|
|
|
<span class="ml-2 leading-none font-medium text-sm">Rooms</span>
|
|
|
|
</div>
|
|
|
|
<div id="rooms-list">
|
|
|
|
<.room_link :for={room <- @rooms} room={room} active={room.id == @room.id} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-10-09 00:12:59 +00:00
|
|
|
<div class="flex flex-col flex-grow shadow-lg">
|
|
|
|
<div class="flex justify-between items-center flex-shrink-0 h-16 bg-white border-b border-slate-300 px-4">
|
|
|
|
<div class="flex flex-col gap-1.5">
|
2024-10-16 01:04:23 +00:00
|
|
|
<h1 class="text-sm font-bold leading-none">
|
|
|
|
#<%= @room.name %>
|
|
|
|
<.link
|
|
|
|
class="font-normal text-xs text-blue-600 hover:text-blue-700"
|
|
|
|
navigate={~p"/rooms/#{@room}/edit"}
|
|
|
|
>
|
|
|
|
Edit
|
|
|
|
</.link>
|
|
|
|
</h1>
|
2024-10-09 23:50:50 +00:00
|
|
|
<div class="text-xs leading-none h-3.5" phx-click="toggle-topic">
|
|
|
|
<%= if @hide_topic? do %>
|
|
|
|
<span class="text-slate-600">[Topic hidden]</span>
|
|
|
|
<% else %>
|
|
|
|
<%= @room.topic %>
|
|
|
|
<% end %>
|
|
|
|
</div>
|
2024-10-09 00:12:59 +00:00
|
|
|
</div>
|
2024-10-22 00:19:26 +00:00
|
|
|
<ul class="relative z-10 flex items-center gap-4 px-4 sm:px-6 lg:px-8 justify-end">
|
|
|
|
<%= if @current_user do %>
|
|
|
|
<li class="text-[0.8125rem] leading-6 text-zinc-900">
|
|
|
|
<%= @current_user.email %>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<.link
|
|
|
|
href={~p"/users/settings"}
|
|
|
|
class="text-[0.8125rem] leading-6 text-zinc-900 font-semibold hover:text-zinc-700"
|
|
|
|
>
|
|
|
|
Settings
|
|
|
|
</.link>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<.link
|
|
|
|
href={~p"/users/log_out"}
|
|
|
|
method="delete"
|
|
|
|
class="text-[0.8125rem] leading-6 text-zinc-900 font-semibold hover:text-zinc-700"
|
|
|
|
>
|
|
|
|
Log out
|
|
|
|
</.link>
|
|
|
|
</li>
|
|
|
|
<% else %>
|
|
|
|
<li>
|
|
|
|
<.link
|
|
|
|
href={~p"/users/register"}
|
|
|
|
class="text-[0.8125rem] leading-6 text-zinc-900 font-semibold hover:text-zinc-700"
|
|
|
|
>
|
|
|
|
Register
|
|
|
|
</.link>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<.link
|
|
|
|
href={~p"/users/log_in"}
|
|
|
|
class="text-[0.8125rem] leading-6 text-zinc-900 font-semibold hover:text-zinc-700"
|
|
|
|
>
|
|
|
|
Log in
|
|
|
|
</.link>
|
|
|
|
</li>
|
|
|
|
<% end %>
|
|
|
|
</ul>
|
2024-10-09 00:12:59 +00:00
|
|
|
</div>
|
2024-10-23 22:33:47 +00:00
|
|
|
<div class="flex flex-col flex-grow overflow-auto">
|
|
|
|
<.message :for={message <- @messages} message={message} />
|
|
|
|
</div>
|
2024-10-09 00:12:59 +00:00
|
|
|
</div>
|
2024-10-07 23:21:49 +00:00
|
|
|
"""
|
|
|
|
end
|
2024-10-09 00:12:59 +00:00
|
|
|
|
2024-10-10 23:40:44 +00:00
|
|
|
@impl Phoenix.LiveView
|
|
|
|
def mount(_params, _session, socket) do
|
2024-10-15 00:27:18 +00:00
|
|
|
rooms = Chat.list_rooms()
|
2024-10-10 00:45:16 +00:00
|
|
|
|
2024-10-10 23:40:44 +00:00
|
|
|
{:ok, assign(socket, hide_topic?: false, rooms: rooms)}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl Phoenix.LiveView
|
|
|
|
def handle_params(params, _session, socket) do
|
2024-10-10 00:45:16 +00:00
|
|
|
room =
|
2024-10-10 23:40:44 +00:00
|
|
|
case Map.fetch(params, "id") do
|
|
|
|
{:ok, id} ->
|
2024-10-15 00:27:18 +00:00
|
|
|
Chat.get_room!(id)
|
2024-10-10 23:40:44 +00:00
|
|
|
|
|
|
|
:error ->
|
2024-10-15 00:27:18 +00:00
|
|
|
Chat.get_first_room!()
|
2024-10-10 23:40:44 +00:00
|
|
|
end
|
2024-10-10 00:45:16 +00:00
|
|
|
|
2024-10-23 22:33:47 +00:00
|
|
|
messages = Chat.list_messages_in_room(room)
|
|
|
|
|
|
|
|
{:noreply,
|
|
|
|
assign(socket,
|
|
|
|
hide_topic?: false,
|
|
|
|
messages: messages,
|
|
|
|
page_title: "# #{room.name}",
|
|
|
|
room: room
|
|
|
|
)}
|
2024-10-09 23:50:50 +00:00
|
|
|
end
|
|
|
|
|
2024-10-10 23:40:44 +00:00
|
|
|
@impl Phoenix.LiveView
|
2024-10-09 23:50:50 +00:00
|
|
|
def handle_event("toggle-topic", _params, socket) do
|
2024-10-09 23:53:20 +00:00
|
|
|
{:noreply, update(socket, :hide_topic?, &(!&1))}
|
2024-10-09 00:12:59 +00:00
|
|
|
end
|
2024-10-10 00:30:46 +00:00
|
|
|
|
|
|
|
attr :active, :boolean, required: true
|
|
|
|
attr :room, Room, required: true
|
|
|
|
|
2024-10-10 23:40:44 +00:00
|
|
|
@spec room_link(Phoenix.LiveView.Socket.assigns()) :: Phoenix.LiveView.Rendered.t()
|
2024-10-10 00:30:46 +00:00
|
|
|
defp room_link(assigns) do
|
|
|
|
~H"""
|
2024-10-10 23:40:44 +00:00
|
|
|
<.link
|
2024-10-10 00:30:46 +00:00
|
|
|
class={[
|
|
|
|
"flex items-center h-8 text-sm pl-8 pr-3",
|
|
|
|
(@active && "bg-slate-300") || "hover:bg-slate-300"
|
|
|
|
]}
|
2024-10-10 23:40:44 +00:00
|
|
|
patch={~p"/rooms/#{@room}"}
|
2024-10-10 00:30:46 +00:00
|
|
|
>
|
|
|
|
<.icon name="hero-hashtag" class="h-4 w-4" />
|
|
|
|
<span class={["ml-2 leading-none", @active && "font-bold"]}>
|
|
|
|
<%= @room.name %>
|
|
|
|
</span>
|
2024-10-10 23:40:44 +00:00
|
|
|
</.link>
|
2024-10-10 00:30:46 +00:00
|
|
|
"""
|
|
|
|
end
|
2024-10-23 22:33:47 +00:00
|
|
|
|
|
|
|
attr :message, Message, required: true
|
|
|
|
|
|
|
|
defp message(assigns) do
|
|
|
|
~H"""
|
|
|
|
<div class="relative flex px-4 py-3">
|
|
|
|
<div class="h-10 w-10 rounded flex-shrink-0 bg-slate-300"></div>
|
|
|
|
<div class="ml-2">
|
|
|
|
<div class="-mt-1">
|
|
|
|
<.link class="text-sm font-semibold hover:underline">
|
|
|
|
<span>User</span>
|
|
|
|
</.link>
|
|
|
|
<p class="text-sm"><%= @message.body %></p>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
end
|
2024-10-07 23:21:49 +00:00
|
|
|
end
|