<%= if session = session_by_file(@file, @sessions) do %>
<.link navigate={~p"/sessions/#{session.id}"} class="button-base button-blue">
Join session
<% else %>
<% end %>
"""
end
defp open_button_tooltip_attrs(file, file_info) do
if regular?(file, file_info) and not writable?(file_info) do
[class: "tooltip top", "data-tooltip": "This file is write-protected, please fork instead"]
else
[]
end
end
@impl true
def handle_event("fork", %{}, socket) do
send(self(), {:fork, socket.assigns.file})
{:noreply, socket}
end
def handle_event("open", %{}, socket) do
send(self(), {:open, socket.assigns.file})
{:noreply, socket}
end
defp path_forkable?(file, file_info) do
regular?(file, file_info)
end
defp path_openable?(file, file_info, sessions) do
regular?(file, file_info) and not file_running?(file, sessions) and
writable?(file_info)
end
defp regular?(file, file_info) do
file_info.exists and not FileSystem.File.dir?(file)
end
defp writable?(file_info) do
file_info.access in [:read_write, :write]
end
defp file_access(file) do
case FileSystem.File.access(file) do
{:ok, access} -> access
{:error, _} -> :none
end
end
defp file_running?(file, sessions) do
Enum.any?(sessions, &(&1.file == file))
end
defp files(sessions) do
Enum.map(sessions, & &1.file)
end
defp session_by_file(file, sessions) do
Enum.find(sessions, &(&1.file == file))
end
end