|
| 1 | +defmodule AlgoraWeb.Components.Activity do |
| 2 | + @moduledoc false |
| 3 | + use AlgoraWeb.Component |
| 4 | + |
| 5 | + import AlgoraWeb.CoreComponents |
| 6 | + |
| 7 | + attr :activities, :list, required: true |
| 8 | + |
| 9 | + def activities_timeline(assigns) do |
| 10 | + ~H""" |
| 11 | + <div class="space-y-2 h-[400px] overflow-y-auto"> |
| 12 | + <%= for {_id, activity} <- assigns[:activities] do %> |
| 13 | + <div class="flex-grow hover:bg-accent"> |
| 14 | + <.activity_card activity={activity} /> |
| 15 | + </div> |
| 16 | + <% end %> |
| 17 | + </div> |
| 18 | + """ |
| 19 | + end |
| 20 | + |
| 21 | + attr :activity, :map, required: true |
| 22 | + |
| 23 | + def activity_card(assigns) do |
| 24 | + ~H""" |
| 25 | + <.link |
| 26 | + href={url_for_activity(assigns[:activity])} |
| 27 | + class="flex flex-grow items-center gap-4 mt-4 mb-4 p-4 pb-4 border-b w-full last:border-none first:border-t first:mt-0 first:pt-4" |
| 28 | + tabindex="-1" |
| 29 | + > |
| 30 | + <div class={[ |
| 31 | + "flex h-9 w-9 items-center justify-center rounded-full", |
| 32 | + activity_background_class(@activity.type) |
| 33 | + ]}> |
| 34 | + <.icon name={activity_icon(to_string(@activity.type))} class="h-5 w-5" /> |
| 35 | + </div> |
| 36 | + <div class="flex-1"> |
| 37 | + <div class="font-medium"> |
| 38 | + <.activity_name type={assigns.activity.type} /> |
| 39 | + </div> |
| 40 | + <div class="text-sm text-muted-foreground"> |
| 41 | + {Calendar.strftime(assigns.activity.inserted_at, "%b %d, %Y, %H:%M:%S")} |
| 42 | + </div> |
| 43 | + </div> |
| 44 | + </.link> |
| 45 | + """ |
| 46 | + end |
| 47 | + |
| 48 | + attr :type, :atom, required: true |
| 49 | + |
| 50 | + def activity_name(%{type: type} = assigns) do |
| 51 | + assigns = assign(assigns, :name, activity_type_to_name(type)) |
| 52 | + |
| 53 | + ~H""" |
| 54 | + <div class="">{@name}</div> |
| 55 | + """ |
| 56 | + end |
| 57 | + |
| 58 | + attr :id, :string, required: true |
| 59 | + attr :class, :string, default: nil |
| 60 | + attr :activities, :list, required: true |
| 61 | + |
| 62 | + def dropdown_activities(assigns) do |
| 63 | + ~H""" |
| 64 | + <div class={classes(["relative w-full text-left", @class])}> |
| 65 | + <div> |
| 66 | + <button |
| 67 | + id={@id} |
| 68 | + type="button" |
| 69 | + class="group w-full rounded-md px-3.5 py-2 text-left text-sm font-medium text-foreground hover:bg-accent focus:outline-none focus:ring-2 focus:ring-ring" |
| 70 | + phx-click={show_dropdown("##{@id}-dropdown")} |
| 71 | + phx-hook="Menu" |
| 72 | + data-active-class="bg-accent" |
| 73 | + aria-haspopup="true" |
| 74 | + > |
| 75 | + <span class="flex w-full items-center justify-between"> |
| 76 | + <span class="flex min-w-0 items-center justify-between space-x-3"> |
| 77 | + <.icon name="tabler-activity" class="h-15 w-15 shrink-0" /> |
| 78 | + </span> |
| 79 | + <.icon |
| 80 | + name="tabler-selector" |
| 81 | + class="ml-2 h-5 w-5 flex-shrink-0 text-gray-500 group-hover:text-gray-400" |
| 82 | + /> |
| 83 | + </span> |
| 84 | + </button> |
| 85 | + </div> |
| 86 | + <div |
| 87 | + id={"#{@id}-dropdown"} |
| 88 | + phx-click-away={hide_dropdown("##{@id}-dropdown")} |
| 89 | + class="absolute mt-4 p-0 right-[200px] left-[-160px] z-10 mt-1 hidden origin-top w-[400px] divide-border rounded-md bg-popover shadow-lg ring-1 ring-border shadow-[0_0_1px_rgba(255,255,255,0.5)]" |
| 90 | + role="menu" |
| 91 | + aria-labelledby={@id} |
| 92 | + > |
| 93 | + <div class="py-1 w-full overflow-y-auto" role="none"> |
| 94 | + <.activities_timeline activities={@activities} /> |
| 95 | + </div> |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + """ |
| 99 | + end |
| 100 | + |
| 101 | + defp url_for_activity(%{type: :identity_created, assoc: assoc}), do: "/@/#{assoc.provider_login}" |
| 102 | + |
| 103 | + defp url_for_activity(activity) do |
| 104 | + slug = activity.assoc_name |> to_string() |> String.replace("_activities", "") |
| 105 | + "/a/#{slug}/#{activity.id}" |
| 106 | + end |
| 107 | + |
| 108 | + defp activity_type_to_name(type) do |
| 109 | + type |
| 110 | + |> to_string() |
| 111 | + |> String.split("_") |
| 112 | + |> Enum.map_join(" ", &String.capitalize(&1)) |
| 113 | + end |
| 114 | + |
| 115 | + defp activity_icon(type) do |
| 116 | + "tabler-file-check" |
| 117 | + end |
| 118 | + |
| 119 | + defp activity_background_class(type) do |
| 120 | + "bg-primary/20" |
| 121 | + end |
| 122 | +end |
0 commit comments