2021-05-04 02:03:19 +08:00
|
|
|
defmodule LivebookWeb.UserHelpers do
|
2023-02-23 02:34:54 +08:00
|
|
|
use LivebookWeb, :html
|
2022-03-02 07:43:06 +08:00
|
|
|
|
2021-05-04 02:03:19 +08:00
|
|
|
@doc """
|
2021-07-07 20:32:49 +08:00
|
|
|
Renders user avatar.
|
2021-05-04 02:03:19 +08:00
|
|
|
|
2021-07-07 20:32:49 +08:00
|
|
|
## Examples
|
2021-05-04 02:03:19 +08:00
|
|
|
|
2021-07-07 20:32:49 +08:00
|
|
|
<.user_avatar user={@user} class="h-20 w-20" text_class="text-3xl" />
|
2023-02-23 02:34:54 +08:00
|
|
|
|
2021-05-04 02:03:19 +08:00
|
|
|
"""
|
2023-02-23 02:34:54 +08:00
|
|
|
attr :user, Livebook.Users.User, required: true
|
|
|
|
attr :class, :string, default: "w-full h-full"
|
|
|
|
attr :text_class, :string, default: nil
|
2021-07-07 20:32:49 +08:00
|
|
|
|
2023-02-23 02:34:54 +08:00
|
|
|
def user_avatar(assigns) do
|
2021-07-07 20:32:49 +08:00
|
|
|
~H"""
|
2022-08-02 21:51:02 +08:00
|
|
|
<div
|
2023-02-23 02:34:54 +08:00
|
|
|
class={["rounded-full flex items-center justify-center", @class]}
|
2021-11-02 01:20:56 +08:00
|
|
|
style={"background-color: #{@user.hex_color}"}
|
2022-08-02 21:51:02 +08:00
|
|
|
aria-hidden="true"
|
|
|
|
>
|
2023-02-23 02:34:54 +08:00
|
|
|
<div class={["text-gray-100 font-semibold", @text_class]}>
|
2021-07-07 20:32:49 +08:00
|
|
|
<%= avatar_text(@user.name) %>
|
2021-05-04 02:03:19 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
|
|
|
defp avatar_text(nil), do: "?"
|
|
|
|
|
|
|
|
defp avatar_text(name) do
|
|
|
|
name
|
|
|
|
|> String.split()
|
|
|
|
|> Enum.map(&String.at(&1, 0))
|
|
|
|
|> Enum.map(&String.upcase/1)
|
|
|
|
|> case do
|
|
|
|
[initial] -> initial
|
|
|
|
initials -> List.first(initials) <> List.last(initials)
|
|
|
|
end
|
|
|
|
end
|
2021-11-03 05:34:44 +08:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Renders the current user edit form in a modal.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2022-03-02 07:43:06 +08:00
|
|
|
<.current_user_modal current_user={@current_user} />
|
2023-02-23 02:34:54 +08:00
|
|
|
|
2021-11-03 05:34:44 +08:00
|
|
|
"""
|
2023-02-23 02:34:54 +08:00
|
|
|
attr :current_user, Livebook.Users.User, required: true
|
|
|
|
|
2021-11-03 05:34:44 +08:00
|
|
|
def current_user_modal(assigns) do
|
|
|
|
~H"""
|
2023-03-08 05:19:16 +08:00
|
|
|
<.modal id="user-modal" width={:small}>
|
2022-08-02 21:51:02 +08:00
|
|
|
<.live_component
|
|
|
|
module={LivebookWeb.UserComponent}
|
2021-11-03 05:34:44 +08:00
|
|
|
id="user"
|
2022-03-02 07:43:06 +08:00
|
|
|
user={@current_user}
|
2022-08-02 21:51:02 +08:00
|
|
|
on_save={hide_current_user_modal()}
|
|
|
|
/>
|
2022-03-02 07:43:06 +08:00
|
|
|
</.modal>
|
2021-11-03 05:34:44 +08:00
|
|
|
"""
|
|
|
|
end
|
2022-03-02 07:43:06 +08:00
|
|
|
|
|
|
|
def show_current_user_modal(js \\ %JS{}), do: show_modal(js, "user-modal")
|
|
|
|
|
|
|
|
def hide_current_user_modal(js \\ %JS{}), do: hide_modal(js, "user-modal")
|
2021-05-04 02:03:19 +08:00
|
|
|
end
|