|
1 | 1 | defmodule AlgoraWeb.Org.PreviewNav do |
2 | 2 | @moduledoc false |
3 | 3 | use Phoenix.Component |
| 4 | + use AlgoraWeb, :verified_routes |
4 | 5 |
|
5 | 6 | import Phoenix.LiveView |
6 | 7 |
|
7 | | - alias Algora.Accounts.User |
8 | | - alias Algora.Github.TokenPool |
9 | | - alias Algora.Workspace |
| 8 | + alias Algora.Organizations |
10 | 9 |
|
11 | 10 | def on_mount(:default, %{"repo_owner" => repo_owner, "repo_name" => repo_name}, _session, socket) do |
12 | | - token = TokenPool.get_token() |
13 | | - {:ok, _repo} = Workspace.ensure_repository(token, repo_owner, repo_name) |
14 | | - {:ok, user} = Workspace.ensure_user(token, repo_owner) |
15 | | - |
16 | | - current_org = %User{ |
17 | | - id: Ecto.UUID.generate(), |
18 | | - provider: "github", |
19 | | - provider_login: repo_owner, |
20 | | - name: user.name, |
21 | | - handle: user.handle, |
22 | | - avatar_url: user.avatar_url |
23 | | - } |
24 | | - |
25 | | - {:cont, |
26 | | - socket |
27 | | - |> assign(:current_context, current_org) |
28 | | - |> assign(:all_contexts, [current_org]) |
29 | | - |> assign(:new_bounty_form, to_form(%{"github_issue_url" => "", "amount" => ""})) |
30 | | - |> assign(:current_org, current_org) |
31 | | - |> assign(:current_user_role, :admin) |
32 | | - |> assign(:nav, nav_items(repo_owner, repo_name)) |
33 | | - |> assign(:contacts, []) |
34 | | - |> attach_hook(:active_tab, :handle_params, &handle_active_tab_params/3)} |
| 11 | + current_context = socket.assigns[:current_context] |
| 12 | + |
| 13 | + if current_context && current_context.last_context == "repo/#{repo_owner}/#{repo_name}" do |
| 14 | + {:cont, |
| 15 | + socket |
| 16 | + |> assign(:new_bounty_form, to_form(%{"github_issue_url" => "", "amount" => ""})) |
| 17 | + |> assign(:current_org, current_context) |
| 18 | + |> assign(:current_user_role, :admin) |
| 19 | + |> assign(:nav, nav_items(repo_owner, repo_name)) |
| 20 | + |> assign(:contacts, []) |
| 21 | + |> attach_hook(:active_tab, :handle_params, &handle_active_tab_params/3)} |
| 22 | + else |
| 23 | + case Organizations.init_preview(repo_owner, repo_name) do |
| 24 | + {:ok, %{user: user, org: _org}} -> |
| 25 | + token = AlgoraWeb.UserAuth.sign_preview_code(user.id) |
| 26 | + path = AlgoraWeb.UserAuth.preview_path(user.id, token, ~p"/go/#{repo_owner}/#{repo_name}") |
| 27 | + |
| 28 | + {:halt, redirect(socket, to: path)} |
| 29 | + |
| 30 | + {:error, reason} -> |
| 31 | + {:cont, put_flash(socket, :error, "Failed to initialize preview: #{inspect(reason)}")} |
| 32 | + end |
| 33 | + end |
35 | 34 | end |
36 | 35 |
|
37 | 36 | defp handle_active_tab_params(_params, _url, socket) do |
|
0 commit comments