diff --git a/lib/nerves_hub/devices.ex b/lib/nerves_hub/devices.ex index 3216e594d..97b9052e4 100644 --- a/lib/nerves_hub/devices.ex +++ b/lib/nerves_hub/devices.ex @@ -106,14 +106,14 @@ defmodule NervesHub.Devices do |> Repo.one!() end - @spec filter(Product.t(), map()) :: %{ + @spec filter(Product.t(), User.t(), map()) :: %{ entries: list(Device.t()), current_page: non_neg_integer(), page_size: non_neg_integer(), total_pages: non_neg_integer(), total_count: non_neg_integer() } - def filter(product, opts) do + def filter(product, user, opts) do pagination = Map.get(opts, :pagination, %{}) sorting = Map.get(opts, :sort, {:asc, :identifier}) filters = Map.get(opts, :filters, %{}) @@ -125,12 +125,22 @@ defmodule NervesHub.Devices do |> Repo.exclude_deleted() |> join(:left, [d], dc in assoc(d, :latest_connection), as: :latest_connection) |> join(:left, [d, dc], dh in assoc(d, :latest_health), as: :latest_health) + |> join(:left, [d, dc, dh], pd in PinnedDevice, + on: pd.device_id == d.id and pd.user_id == ^user.id + ) |> preload([latest_connection: lc], latest_connection: lc) |> preload([latest_health: lh], latest_health: lh) + |> select([d, dc, dh, pd], device: d, pinned: not is_nil(pd.device_id)) + |> order_by([d, dc, dh, pd], desc_nulls_last: pd.device_id) |> Filtering.build_filters(filters) |> sort_devices(sorting) |> Flop.run(flop) |> then(fn {entries, meta} -> + entries = + Enum.map(entries, fn [device: device, pinned: pinned] -> + Map.put(device, :pinned, pinned) + end) + meta |> Map.take([ :current_page, diff --git a/lib/nerves_hub_web/live/devices/index-new.html.heex b/lib/nerves_hub_web/live/devices/index-new.html.heex index 6e625ee3a..5802762d5 100644 --- a/lib/nerves_hub_web/live/devices/index-new.html.heex +++ b/lib/nerves_hub_web/live/devices/index-new.html.heex @@ -153,6 +153,7 @@ <.link navigate={~p"/org/#{@org.name}/#{@product.name}/devices/#{device.identifier}"} class={"ff-m #{firmware_update_status(device)}"} title={firmware_update_title(device)}> {device.identifier} + <.icon :if={device.pinned} name="pinned" class="size-1 stroke-zinc-500" /> updating diff --git a/lib/nerves_hub_web/live/devices/index.ex b/lib/nerves_hub_web/live/devices/index.ex index 7e345865e..3d9f46cf6 100644 --- a/lib/nerves_hub_web/live/devices/index.ex +++ b/lib/nerves_hub_web/live/devices/index.ex @@ -446,7 +446,7 @@ defmodule NervesHubWeb.Live.Devices.Index do end defp assign_display_devices( - %{assigns: %{product: product, paginate_opts: paginate_opts}} = socket + %{assigns: %{product: product, user: user, paginate_opts: paginate_opts}} = socket ) do opts = %{ pagination: %{page: paginate_opts.page_number, page_size: paginate_opts.page_size}, @@ -456,7 +456,7 @@ defmodule NervesHubWeb.Live.Devices.Index do filters: socket.assigns.current_filters } - page = Devices.filter(product, opts) + page = Devices.filter(product, user, opts) statuses = Enum.into(page.entries, %{}, fn device ->