Skip to content

Commit 399f574

Browse files
committed
feat: add avatar_group component for enhanced participant display
- Introduced a new `avatar_group` function in the Avatar module to display multiple user avatars in a grouped format. - Updated BountyLive to utilize the `avatar_group` component, improving the visual representation of participants in chat. - Enhanced the logic to limit the number of displayed avatars and show a count of additional participants if applicable.
1 parent f5f68f5 commit 399f574

File tree

3 files changed

+42
-31
lines changed

3 files changed

+42
-31
lines changed

lib/algora_web/components/core_components.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,7 @@ defmodule AlgoraWeb.CoreComponents do
12881288
defdelegate alert_title(assigns), to: Alert
12891289
defdelegate alert(assigns), to: Alert
12901290
defdelegate avatar_fallback(assigns), to: Avatar
1291+
defdelegate avatar_group(assigns), to: Avatar
12911292
defdelegate avatar_image(assigns), to: Avatar
12921293
defdelegate avatar(assigns), to: Avatar
12931294
defdelegate badge(assigns), to: AlgoraWeb.Components.UI.Badge

lib/algora_web/components/ui/avatar.ex

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ defmodule AlgoraWeb.Components.UI.Avatar do
66
attr :class, :string, default: nil
77
attr :rest, :global
88

9+
slot :inner_block, required: true
10+
911
def avatar(assigns) do
1012
~H"""
1113
<div class={classes(["relative h-10 w-10 shrink-0 overflow-hidden rounded-full", @class])} {@rest}>
@@ -48,4 +50,31 @@ defmodule AlgoraWeb.Components.UI.Avatar do
4850
</span>
4951
"""
5052
end
53+
54+
attr :class, :string, default: nil
55+
attr :rest, :global
56+
attr :srcs, :list, default: []
57+
attr :limit, :integer, default: 3
58+
59+
def avatar_group(assigns) do
60+
~H"""
61+
<div class="relative flex -space-x-1">
62+
<%= for src <- @srcs |> Enum.take(@limit) do %>
63+
<.avatar class={@class}>
64+
<.avatar_image src={src} />
65+
<.avatar_fallback>
66+
{Algora.Util.initials(src)}
67+
</.avatar_fallback>
68+
</.avatar>
69+
<% end %>
70+
<%= if length(@srcs) > @limit do %>
71+
<.avatar class={@class}>
72+
<.avatar_fallback>
73+
+{length(@srcs) - @limit}
74+
</.avatar_fallback>
75+
</.avatar>
76+
<% end %>
77+
</div>
78+
"""
79+
end
5180
end

lib/algora_web/live/bounty_live.ex

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,14 @@ defmodule AlgoraWeb.BountyLive do
143143
@impl true
144144
def handle_info(%Chat.MessageCreated{message: message, participant: participant}, socket) do
145145
socket =
146-
if message.id in Enum.map(socket.assigns.messages, & &1.id) do
147-
socket
148-
else
149-
Phoenix.Component.update(socket, :messages, &(&1 ++ [message]))
150-
end
146+
if message.id in Enum.map(socket.assigns.messages, & &1.id),
147+
do: socket,
148+
else: Phoenix.Component.update(socket, :messages, &(&1 ++ [message]))
151149

152150
socket =
153-
if participant.id in Enum.map(socket.assigns.participants, & &1.id) do
154-
socket
155-
else
156-
Phoenix.Component.update(socket, :participants, &(&1 ++ [participant]))
157-
end
151+
if participant.id in Enum.map(socket.assigns.participants, & &1.id),
152+
do: socket,
153+
else: Phoenix.Component.update(socket, :participants, &(&1 ++ [participant]))
158154

159155
{:noreply, socket}
160156
end
@@ -475,27 +471,12 @@ defmodule AlgoraWeb.BountyLive do
475471
<h2 class="text-lg font-semibold">
476472
Contributor chat
477473
</h2>
478-
<div class="relative flex -space-x-2">
479-
<%= for participant <- @participants |> Enum.take(3) do %>
480-
<.avatar class="ring-4 ring-background">
481-
<.avatar_image
482-
src={participant.user.avatar_url}
483-
alt="Developer avatar"
484-
class="ring-2 ring-background"
485-
/>
486-
<.avatar_fallback>
487-
{Algora.Util.initials(participant.user.name)}
488-
</.avatar_fallback>
489-
</.avatar>
490-
<% end %>
491-
<%= if length(@participants) > 3 do %>
492-
<.avatar class="ring-4 ring-background">
493-
<.avatar_fallback>
494-
+{length(@participants) - 3}
495-
</.avatar_fallback>
496-
</.avatar>
497-
<% end %>
498-
</div>
474+
475+
<.avatar_group
476+
srcs={Enum.map(@participants, & &1.user.avatar_url)}
477+
limit={4}
478+
class="ring-4 ring-background"
479+
/>
499480
</div>
500481
</div>
501482

0 commit comments

Comments
 (0)