defmodule PlausibleWeb.Live.Sites do @moduledoc """ LiveView for sites index. """ use Phoenix.LiveView use PlausibleWeb.Live.Flash alias Phoenix.LiveView.JS use Phoenix.HTML import PlausibleWeb.Components.Generic import PlausibleWeb.Live.Components.Pagination alias Plausible.Auth alias Plausible.Repo alias Plausible.Site alias Plausible.Sites def mount(params, %{"current_user_id" => user_id}, socket) do uri = ("/sites?" <> URI.encode_query(Map.take(params, ["filter_text"]))) |> URI.new!() socket = socket |> assign(:uri, uri) |> assign(:filter_text, params["filter_text"] || "") |> assign(:user, Repo.get!(Auth.User, user_id)) {:ok, socket} end def handle_params(params, _uri, socket) do socket = socket |> assign(:params, params) |> load_sites() |> assign_new(:has_sites?, fn %{user: user} -> Site.Memberships.any_or_pending?(user) end) |> assign_new(:needs_to_upgrade, fn %{user: user, sites: sites} -> user_owns_sites = Enum.any?(sites.entries, fn site -> List.first(site.memberships ++ site.invitations).role == :owner end) || Auth.user_owns_sites?(user) user_owns_sites && Plausible.Billing.check_needs_to_upgrade(user) end) {:noreply, socket} end def render(assigns) do invitations = assigns.sites.entries |> Enum.filter(&(&1.entry_type == "invitation")) |> Enum.flat_map(& &1.invitations) assigns = assign(assigns, :invitations, invitations) ~H""" <.flash_messages flash={@flash} />
Enum.into(%{}) |> Jason.encode!}}"} x-on:keydown.escape.window="invitationOpen = false" class="container pt-6" > <.upgrade_nag_screen :if={@needs_to_upgrade == {:needs_to_upgrade, :no_active_subscription}} />

My Sites

<.search_form :if={@has_sites?} filter_text={@filter_text} uri={@uri} />

You don't have any sites yet.

No sites found. Please search for something else.

    <%= for site <- @sites.entries do %> <.site :if={site.entry_type in ["pinned_site", "site"]} site={site} hourly_stats={@hourly_stats[site.domain]} /> <.invitation :if={site.entry_type == "invitation"} site={site} invitation={hd(site.invitations)} hourly_stats={@hourly_stats[site.domain]} /> <% end %>
<.pagination :if={@sites.total_pages > 1} id="sites-pagination" uri={@uri} page_number={@sites.page_number} total_pages={@sites.total_pages} > Total of <%= @sites.total_entries %> sites <.invitation_modal :if={Enum.any?(@sites.entries, &(&1.entry_type == "invitation"))} user={@user} />
""" end def upgrade_nag_screen(assigns) do ~H"""

Payment required

To access the sites you own, you need to subscribe to a monthly or yearly payment plan. <%= link( "Upgrade now →", to: "/settings", class: "text-sm font-medium text-yellow-800" ) %>

""" end attr :site, Plausible.Site, required: true attr :invitation, Plausible.Auth.Invitation, required: true attr :hourly_stats, :map, required: true def invitation(assigns) do ~H"""
  • <%= @site.domain %>

    Pending invitation
    <.site_stats hourly_stats={@hourly_stats} />
  • """ end attr :site, Plausible.Site, required: true attr :hourly_stats, :map, required: true def site(assigns) do ~H""" """ end def ellipsis_menu(assigns) do ~H"""
    """ end attr :rest, :global def icon_pin(assigns) do ~H""" """ end attr :hourly_stats, :map, required: true def site_stats(assigns) do ~H"""
    """ end attr :change, :integer, required: true def percentage_change(assigns) do ~H"""

    0} class="font-semibold text-green-500">↑ <%= abs(@change) %>%

    """ end attr :user, Plausible.Auth.User, required: true def invitation_modal(assigns) do ~H""" """ end attr :filter_text, :string, default: "" attr :uri, URI, required: true def search_form(assigns) do ~H"""
    <.spinner class="hidden phx-change-loading:inline ml-2" />
    """ end def favicon(assigns) do src = "/favicon/sources/#{assigns.domain}" assigns = assign(assigns, :src, src) ~H""" """ end attr :class, :any, default: "" def spinner(assigns) do ~H""" """ end def handle_event("pin-toggle", %{"domain" => domain}, socket) do site = Enum.find(socket.assigns.sites.entries, &(&1.domain == domain)) if site do socket = case Sites.toggle_pin(socket.assigns.user, site) do {:ok, preference} -> flash_message = if preference.pinned_at do "Site pinned" else "Site unpinned" end socket |> put_live_flash(:success, flash_message) |> load_sites() |> push_event("js-exec", %{ to: "#site-card-#{hash_domain(site.domain)}", attr: "data-pin-toggled" }) {:error, :too_many_pins} -> flash_message = "Looks like you've hit the pinned sites limit! " <> "Please unpin one of your pinned sites to make room for new pins" socket |> put_live_flash(:error, flash_message) |> push_event("js-exec", %{ to: "#site-card-#{hash_domain(site.domain)}", attr: "data-pin-failed" }) end {:noreply, socket} else Sentry.capture_message("Attempting to toggle pin for invalid domain.", extra: %{domain: domain, user: socket.assigns.user.id} ) {:noreply, socket} end end def handle_event( "filter", %{"filter_text" => filter_text}, %{assigns: %{filter_text: filter_text}} = socket ) do {:noreply, socket} end def handle_event("filter", %{"filter_text" => filter_text}, socket) do socket = socket |> reset_pagination() |> set_filter_text(filter_text) {:noreply, socket} end def handle_event("reset-filter-text", _params, socket) do socket = socket |> reset_pagination() |> set_filter_text("") {:noreply, socket} end defp load_sites(%{assigns: assigns} = socket) do sites = Sites.list_with_invitations(assigns.user, assigns.params, filter_by_domain: assigns.filter_text ) hourly_stats = if connected?(socket) do Plausible.Stats.Clickhouse.last_24h_visitors_hourly_intervals(sites.entries) else sites.entries |> Enum.into(%{}, fn site -> {site.domain, :loading} end) end assign( socket, sites: sites, hourly_stats: hourly_stats ) end defp set_filter_text(socket, filter_text) do uri = socket.assigns.uri uri_params = uri.query |> URI.decode_query() |> Map.put("filter_text", filter_text) |> URI.encode_query() uri = %{uri | query: uri_params} socket |> assign(:filter_text, filter_text) |> assign(:uri, uri) |> push_patch(to: URI.to_string(uri), replace: true) end defp reset_pagination(socket) do pagination_fields = ["page"] uri = socket.assigns.uri uri_params = uri.query |> URI.decode_query() |> Map.drop(pagination_fields) |> URI.encode_query() assign(socket, uri: %{uri | query: uri_params}, params: Map.drop(socket.assigns.params, pagination_fields) ) end defp hash_domain(domain) do :sha |> :crypto.hash(domain) |> Base.encode16() end end