2023-03-07 01:14:33 +08:00
|
|
|
defmodule LivebookWeb.OpenLive.UrlComponent do
|
2021-04-23 23:40:13 +08:00
|
|
|
use LivebookWeb, :live_component
|
|
|
|
|
2022-02-10 19:15:41 +08:00
|
|
|
alias Livebook.{Utils, Notebook}
|
2021-04-23 23:40:13 +08:00
|
|
|
|
|
|
|
@impl true
|
|
|
|
def mount(socket) do
|
|
|
|
{:ok, assign(socket, url: "", error_message: nil)}
|
|
|
|
end
|
|
|
|
|
2021-10-16 18:23:08 +08:00
|
|
|
@impl true
|
|
|
|
def update(assigns, socket) do
|
|
|
|
if url = assigns[:url] do
|
|
|
|
{:ok, do_import(socket, url)}
|
|
|
|
else
|
|
|
|
{:ok, socket}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-23 23:40:13 +08:00
|
|
|
@impl true
|
|
|
|
def render(assigns) do
|
2021-07-07 20:32:49 +08:00
|
|
|
~H"""
|
2021-04-23 23:40:13 +08:00
|
|
|
<div class="flex-col space-y-5">
|
2023-02-23 02:34:54 +08:00
|
|
|
<div :if={@error_message} class="error-box">
|
|
|
|
<%= @error_message %>
|
|
|
|
</div>
|
2022-02-05 00:05:38 +08:00
|
|
|
<p class="text-gray-700" id="import-from-url">
|
2022-09-01 05:53:23 +08:00
|
|
|
Paste the URL to a .livemd file, to a GitHub file, or to a Gist.
|
2021-04-23 23:40:13 +08:00
|
|
|
</p>
|
2022-08-02 21:51:02 +08:00
|
|
|
<.form
|
2022-10-04 14:46:55 +08:00
|
|
|
:let={f}
|
2023-02-23 02:34:54 +08:00
|
|
|
for={%{"url" => @url}}
|
2023-02-21 03:51:17 +08:00
|
|
|
as={:data}
|
2021-07-07 20:32:49 +08:00
|
|
|
phx-submit="import"
|
|
|
|
phx-change="validate"
|
|
|
|
phx-target={@myself}
|
2022-08-02 21:51:02 +08:00
|
|
|
autocomplete="off"
|
|
|
|
>
|
2023-02-23 02:34:54 +08:00
|
|
|
<.text_field
|
|
|
|
field={f[:url]}
|
|
|
|
label="Notebook URL"
|
|
|
|
autofocus
|
|
|
|
aria-labelledby="import-from-url"
|
|
|
|
spellcheck="false"
|
|
|
|
/>
|
2022-08-02 21:51:02 +08:00
|
|
|
<button
|
|
|
|
class="mt-5 button-base button-blue"
|
2021-07-07 20:32:49 +08:00
|
|
|
type="submit"
|
2022-08-02 21:51:02 +08:00
|
|
|
disabled={not Utils.valid_url?(@url)}
|
|
|
|
>
|
2021-07-07 20:32:49 +08:00
|
|
|
Import
|
|
|
|
</button>
|
|
|
|
</.form>
|
2021-04-23 23:40:13 +08:00
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_event("validate", %{"data" => %{"url" => url}}, socket) do
|
|
|
|
{:noreply, assign(socket, url: url)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_event("import", %{"data" => %{"url" => url}}, socket) do
|
2021-10-16 18:23:08 +08:00
|
|
|
{:noreply, do_import(socket, url)}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp do_import(socket, url) do
|
2022-02-10 19:15:41 +08:00
|
|
|
origin = Notebook.ContentLoader.url_to_location(url)
|
2023-07-06 02:01:12 +08:00
|
|
|
files_url = Livebook.Utils.expand_url(url, "files/")
|
2021-11-12 22:49:22 +08:00
|
|
|
|
|
|
|
origin
|
2022-02-10 19:15:41 +08:00
|
|
|
|> Notebook.ContentLoader.fetch_content_from_location()
|
2021-04-23 23:40:13 +08:00
|
|
|
|> case do
|
|
|
|
{:ok, content} ->
|
2023-07-06 02:01:12 +08:00
|
|
|
send(self(), {:import_source, content, [origin: origin, files_source: {:url, files_url}]})
|
2021-10-16 18:23:08 +08:00
|
|
|
socket
|
2021-04-23 23:40:13 +08:00
|
|
|
|
|
|
|
{:error, message} ->
|
2021-10-16 18:23:08 +08:00
|
|
|
assign(socket, url: url, error_message: Utils.upcase_first(message))
|
2021-04-23 23:40:13 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|