livebook/lib/livebook_web/live/learn_helpers.ex
Frank Hunleth 3131610ad8
Fix error when specifying custom learn notebooks (#1570)
This fixes the following error:

```
07:18:26.178 [error] #PID<0.2236.0> running LivebookWeb.Endpoint (connection #PID<0.2226.0>, stream id 6) terminated
Server: localhost:8080 (http)
Request: GET /
** (exit) an exception was raised:
    ** (ArgumentError) expected a path starting with a single / but got "data:image/svg+xml;base64,PD94bWwgdmVyc2lv..."
        (phoenix 1.6.15) lib/phoenix/endpoint/supervisor.ex:311: Phoenix.Endpoint.Supervisor.raise_invalid_path/1
        (phoenix 1.6.15) lib/phoenix/config.ex:65: Phoenix.Config.cache/3
        (livebook 0.8.0) lib/phoenix/endpoint.ex:597: LivebookWeb.Endpoint.static_path/1
        (livebook 0.8.0) lib/livebook_web/live/learn_helpers.ex:15: anonymous fn/2 in LivebookWeb.LearnHelpers.notebook_card/1
        (livebook 0.8.0) /Users/fhunleth/git/livebook-dev/livebook/lib/livebook_web/live/home_live.ex:119: LivebookWeb.HomeLive.render/1
        (elixir 1.14.2) lib/enum.ex:1780: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
```
2022-12-10 12:46:56 +01:00

32 lines
1.1 KiB
Elixir

defmodule LivebookWeb.LearnHelpers do
use Phoenix.Component
alias LivebookWeb.Router.Helpers, as: Routes
@doc """
Renders an learn notebook card.
"""
def notebook_card(assigns) do
~H"""
<%= live_redirect to: Routes.learn_path(@socket, :notebook, @notebook_info.slug),
class: "flex flex-col border-2 border-gray-100 hover:border-gray-200 rounded-2xl" do %>
<div class="flex items-center justify-center p-6 border-b-2 border-gray-100 rounded-t-2xl h-[150px]">
<img
src={img_src(@socket, @notebook_info.details.cover_url)}
class="max-h-full max-w-[75%]"
alt={"#{@notebook_info.title} logo"}
/>
</div>
<div class="px-6 py-4 bg-gray-100 rounded-b-2xl grow">
<span class="text-gray-800 font-semibold"><%= @notebook_info.title %></span>
<p class="mt-2 text-sm text-gray-600">
<%= @notebook_info.details.description %>
</p>
</div>
<% end %>
"""
end
defp img_src(_socket, "data:" <> _ = url), do: url
defp img_src(socket, url), do: Routes.static_path(socket, url)
end