2022-08-18 21:34:27 +08:00
|
|
|
defmodule LivebookWeb.SidebarHook do
|
2022-10-04 14:46:55 +08:00
|
|
|
import Phoenix.Component
|
2022-08-18 21:34:27 +08:00
|
|
|
import Phoenix.LiveView
|
|
|
|
|
|
|
|
def on_mount(:default, _params, _session, socket) do
|
|
|
|
if connected?(socket) do
|
|
|
|
Livebook.Hubs.subscribe()
|
|
|
|
end
|
|
|
|
|
|
|
|
socket =
|
|
|
|
socket
|
|
|
|
|> assign(saved_hubs: Livebook.Hubs.fetch_metadatas())
|
|
|
|
|> attach_hook(:hubs, :handle_info, &handle_info/2)
|
|
|
|
|> attach_hook(:shutdown, :handle_event, &handle_event/3)
|
|
|
|
|
|
|
|
{:cont, socket}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp handle_info({:hubs_metadata_changed, hubs}, socket) do
|
|
|
|
{:halt, assign(socket, :saved_hubs, hubs)}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp handle_info(_event, socket), do: {:cont, socket}
|
|
|
|
|
|
|
|
defp handle_event("shutdown", _params, socket) do
|
2022-11-11 01:09:01 +08:00
|
|
|
case Livebook.Config.shutdown_callback() do
|
|
|
|
{m, f, a} ->
|
|
|
|
apply(m, f, a)
|
|
|
|
|
|
|
|
{:halt, put_flash(socket, :info, "Livebook is shutting down. You can close this page.")}
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
socket
|
2022-08-18 21:34:27 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp handle_event(_event, _params, socket), do: {:cont, socket}
|
|
|
|
end
|