slax/lib/slax_web/live/chat_room_live.ex

98 lines
2.8 KiB
Elixir
Raw Normal View History

2024-10-07 23:21:49 +00:00
defmodule SlaxWeb.ChatRoomLive do
@moduledoc false
use SlaxWeb, :live_view
alias Slax.Chat
2024-10-09 00:12:59 +00:00
alias Slax.Chat.Room
@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">
<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>
<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>
</div>
</div>
2024-10-07 23:21:49 +00:00
"""
end
2024-10-09 00:12:59 +00:00
@impl Phoenix.LiveView
def mount(_params, _session, socket) do
rooms = Chat.list_rooms()
{:ok, assign(socket, hide_topic?: false, rooms: rooms)}
end
@impl Phoenix.LiveView
def handle_params(params, _session, socket) do
room =
case Map.fetch(params, "id") do
{:ok, id} ->
Chat.get_room!(id)
:error ->
Chat.get_first_room!()
end
{:noreply, assign(socket, hide_topic?: false, page_title: "# #{room.name}", room: room)}
end
@impl Phoenix.LiveView
def handle_event("toggle-topic", _params, socket) do
{: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
@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"""
<.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"
]}
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>
</.link>
2024-10-10 00:30:46 +00:00
"""
end
2024-10-07 23:21:49 +00:00
end