-
Notifications
You must be signed in to change notification settings - Fork 452
/
Copy pathhome_live.ex
281 lines (253 loc) · 9.38 KB
/
home_live.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
defmodule LivebookWeb.HomeLive do
use LivebookWeb, :live_view
import LivebookWeb.SessionHelpers
alias LivebookWeb.LayoutComponents
on_mount LivebookWeb.SidebarHook
@impl true
def mount(_params, _session, socket) do
if connected?(socket) do
Livebook.Sessions.subscribe()
Livebook.SystemResources.subscribe()
Livebook.NotebookManager.subscribe_starred_notebooks()
end
sessions = Livebook.Sessions.list_sessions() |> Enum.filter(&(&1.mode == :default))
notebook_infos = Livebook.Notebook.Learn.visible_notebook_infos() |> Enum.take(3)
starred_notebooks = Livebook.NotebookManager.starred_notebooks()
{:ok,
assign(socket,
self_path: ~p"/",
sessions: sessions,
starred_notebooks: starred_notebooks,
starred_expanded?: false,
notebook_infos: notebook_infos,
page_title: "Livebook",
new_version: Livebook.UpdateCheck.new_version(),
update_instructions_url: Livebook.Config.update_instructions_url(),
app_service_url: Livebook.Config.app_service_url(),
memory: Livebook.SystemResources.memory()
)}
end
@impl true
def render(assigns) do
~H"""
<LayoutComponents.layout
current_page={@self_path}
current_user={@current_user}
saved_hubs={@saved_hubs}
>
<:topbar_action>
<div class="flex space-x-2">
<.button color="gray" outlined navigate={~p"/open/storage"}>
Open
</.button>
<.button color="blue" patch={~p"/new"}>
<.remix_icon icon="add-line" />
<span>New notebook</span>
</.button>
</div>
</:topbar_action>
<.update_notification version={@new_version} instructions_url={@update_instructions_url} />
<.memory_notification memory={@memory} app_service_url={@app_service_url} />
<div class="p-4 md:px-12 md:py-6 max-w-screen-lg mx-auto">
<div class="flex flex-row space-y-0 items-center pb-4 justify-between">
<LayoutComponents.title text="Home" />
<div class="hidden md:flex space-x-2" role="navigation" aria-label="new notebook">
<.button color="gray" outlined navigate={~p"/open/storage"}>
Open
</.button>
<.button color="blue" patch={~p"/new"}>
<.remix_icon icon="add-line" />
<span>New notebook</span>
</.button>
</div>
</div>
<div id="starred-notebooks" role="region" aria-label="starred notebooks">
<div class="my-4 flex items-center md:items-end justify-between">
<h2 class="uppercase font-semibold text-gray-500 text-sm md:text-base">
Starred notebooks
</h2>
<button
:if={length(@starred_notebooks) > 6}
class="flex items-center text-blue-600"
phx-click="toggle_starred_expanded"
>
<%= if @starred_expanded? do %>
<span class="font-semibold">Show less</span>
<% else %>
<span class="font-semibold">Show more</span>
<% end %>
</button>
</div>
<%= if @starred_notebooks == [] do %>
<.no_entries>
Your starred notebooks will appear here. <br />
First time around? Check out the notebooks below to get started.
<:actions>
<.link navigate={~p"/learn"} class="flex items-center text-blue-600 pl-5">
<span class="font-semibold">Learn more</span>
<.remix_icon icon="arrow-right-line" class="align-middle ml-1" />
</.link>
</:actions>
</.no_entries>
<div class="mt-4 grid grid-cols-1 md:grid-cols-3 gap-4">
<%!--
Note: it's fine to use stateless components in this comprehension,
because @notebook_infos never change
--%>
<LivebookWeb.NotebookComponents.learn_notebook_card
:for={info <- @notebook_infos}
notebook_info={info}
/>
</div>
<% else %>
<.live_component
module={LivebookWeb.NotebookCardsComponent}
id="starred-notebook-list"
notebook_infos={visible_starred_notebooks(@starred_notebooks, @starred_expanded?)}
sessions={@sessions}
added_at_label="Starred"
>
<:card_icon :let={{_info, idx}}>
<span class="tooltip top" data-tooltip="Unstar">
<button
aria-label="unstar notebook"
phx-click={JS.push("unstar_notebook", value: %{idx: idx})}
>
<.remix_icon icon="star-fill" class="text-yellow-600" />
</button>
</span>
</:card_icon>
</.live_component>
<% end %>
</div>
<div id="running-sessions" class="py-20 mb-32" role="region" aria-label="running sessions">
<.live_component
module={LivebookWeb.HomeLive.SessionListComponent}
id="session-list"
sessions={@sessions}
starred_notebooks={@starred_notebooks}
memory={@memory}
/>
</div>
</div>
</LayoutComponents.layout>
<.modal :if={@live_action == :import} id="import-modal" show width="big" patch={@self_path}>
<.live_component
module={LivebookWeb.HomeLive.ImportComponent}
id="import"
tab={@tab}
import_opts={@import_opts}
/>
</.modal>
"""
end
defp update_notification(%{version: nil} = assigns), do: ~H""
defp update_notification(assigns) do
~H"""
<LayoutComponents.topbar>
<span>
Livebook v{@version} available!
<%= if @instructions_url do %>
Check out the news on
<a
class="font-medium border-b border-gray-900 hover:border-transparent"
href="https://livebook.dev/"
target="_blank"
>
livebook.dev
</a>
and follow the
<a
class="font-medium border-b border-gray-900 hover:border-transparent"
href={@instructions_url}
target="_blank"
>
update instructions
</a>
<% else %>
Check out the news and installation steps on
<a
class="font-medium border-b border-gray-900 hover:border-transparent"
href="https://livebook.dev/"
target="_blank"
>
livebook.dev
</a>
<% end %>
🚀
</span>
</LayoutComponents.topbar>
"""
end
defp memory_notification(assigns) do
~H"""
<LayoutComponents.topbar :if={@app_service_url && @memory.free < 30_000_000} variant="error">
<.remix_icon icon="alarm-warning-line" class="align-text-bottom mr-0.5" />
Less than 30 MB of memory left, consider
<a
class="font-medium border-b border-gray-900 hover:border-transparent"
href={@app_service_url}
target="_blank"
>
adding more resources to the instance
</a>
or closing
<a
class="font-medium border-b border-gray-900 hover:border-transparent"
href="#running-sessions"
>
running sessions
</a>
</LayoutComponents.topbar>
"""
end
@impl true
def handle_params(%{"session_id" => session_id}, _url, socket) do
session = Enum.find(socket.assigns.sessions, &(&1.id == session_id))
{:noreply, assign(socket, session: session)}
end
def handle_params(%{}, _url, socket) when socket.assigns.live_action == :public_new_notebook do
{:noreply, create_session(socket, connect_runtime: true)}
end
def handle_params(_params, _url, socket), do: {:noreply, socket}
@impl true
def handle_event("unstar_notebook", %{"idx" => idx}, socket) do
on_confirm = fn socket ->
%{file: file} = Enum.fetch!(socket.assigns.starred_notebooks, idx)
Livebook.NotebookManager.remove_starred_notebook(file)
socket
end
{:noreply,
confirm(socket, on_confirm,
title: "Unstar notebook",
description: "Once you unstar this notebook, you can always star it again.",
confirm_text: "Unstar",
opt_out_id: "unstar-notebook"
)}
end
def handle_event("toggle_starred_expanded", %{}, socket) do
{:noreply, update(socket, :starred_expanded?, ¬/1)}
end
@impl true
def handle_info({type, session} = event, socket)
when type in [:session_created, :session_updated, :session_closed] and
session.mode == :default do
{:noreply, update(socket, :sessions, &update_session_list(&1, event))}
end
def handle_info({:memory_update, memory}, socket) do
{:noreply, assign(socket, memory: memory)}
end
def handle_info({:starred_notebooks_updated, starred_notebooks}, socket) do
{:noreply, assign(socket, starred_notebooks: starred_notebooks)}
end
def handle_info({:fork, file}, socket) do
{:noreply, fork_notebook(socket, file)}
end
def handle_info({:open, file}, socket) do
{:noreply, open_notebook(socket, file)}
end
def handle_info(_message, socket), do: {:noreply, socket}
defp visible_starred_notebooks(notebooks, starred_expanded?)
defp visible_starred_notebooks(notebooks, true), do: notebooks
defp visible_starred_notebooks(notebooks, false), do: Enum.take(notebooks, 6)
end