ash_hq/lib/ash_hq_web/redirect_to_hex.ex

98 lines
3 KiB
Elixir
Raw Normal View History

2023-09-28 02:56:53 +13:00
defmodule AshHqWeb.RedirectToHex do
@moduledoc "Sets the platform being used with liveview"
import Phoenix.LiveView
require Ash.Query
2024-09-06 11:04:00 +12:00
@behaviour Plug
2023-09-28 02:56:53 +13:00
2024-09-06 11:04:00 +12:00
def init(opts), do: opts
2023-09-28 02:56:53 +13:00
2024-09-06 11:04:00 +12:00
def call(conn, _) do
case conn.params do
%{"dsl_target" => dsl_target} ->
to_load = AshHq.Docs.Extensions.Search.load_for_search(AshHq.Docs.Module)
2023-09-28 02:56:53 +13:00
2024-09-06 11:04:00 +12:00
AshHq.Docs.Module
|> Ash.Query.filter(name == ^dsl_target or sanitized_name == ^dsl_target)
|> Ash.Query.load(to_load)
|> Ash.read_one()
|> case do
{:ok, module} when not is_nil(module) ->
redirect_to_hex(conn, AshHqWeb.DocRoutes.doc_link(module))
2023-09-28 02:56:53 +13:00
2024-09-06 11:04:00 +12:00
_ ->
Phoenix.Controller.redirect(conn, to: "/")
end
2023-09-28 02:56:53 +13:00
2024-09-06 11:04:00 +12:00
%{"mix_task" => mix_task} ->
to_load = AshHq.Docs.Extensions.Search.load_for_search(AshHq.Docs.MixTask)
2023-09-28 02:56:53 +13:00
2024-09-06 11:04:00 +12:00
AshHq.Docs.MixTask
|> Ash.Query.filter(name == ^mix_task or sanitized_name == ^mix_task)
|> Ash.Query.load(to_load)
|> Ash.read_one()
|> case do
{:ok, module} when not is_nil(module) ->
redirect_to_hex(conn, AshHqWeb.DocRoutes.doc_link(module))
2023-09-28 02:56:53 +13:00
2024-09-06 11:04:00 +12:00
_ ->
to_load = AshHq.Docs.Extensions.Search.load_for_search(AshHq.Docs.Module)
2023-09-28 02:56:53 +13:00
2024-09-06 11:04:00 +12:00
AshHq.Docs.Module
|> Ash.Query.filter(name == ^mix_task or sanitized_name == ^mix_task)
|> Ash.Query.load(to_load)
|> Ash.read_one()
|> case do
{:ok, module} when not is_nil(module) ->
redirect_to_hex(conn, AshHqWeb.DocRoutes.doc_link(module))
_ ->
Phoenix.Controller.redirect(conn, to: "/")
end
end
%{"module" => module} ->
to_load = AshHq.Docs.Extensions.Search.load_for_search(AshHq.Docs.Module)
AshHq.Docs.Module
|> Ash.Query.filter(name == ^module or sanitized_name == ^module)
|> Ash.Query.load(to_load)
|> Ash.read_one()
|> case do
{:ok, module} when not is_nil(module) ->
redirect_to_hex(conn, AshHqWeb.DocRoutes.doc_link(module))
_ ->
Phoenix.Controller.redirect(conn, to: "/")
end
%{"extension" => module} ->
to_load = AshHq.Docs.Extensions.Search.load_for_search(AshHq.Docs.Module)
AshHq.Docs.Module
|> Ash.Query.filter(name == ^module or sanitized_name == ^module)
|> Ash.Query.load(to_load)
|> Ash.read_one()
|> case do
{:ok, module} when not is_nil(module) ->
redirect_to_hex(conn, AshHqWeb.DocRoutes.doc_link(module))
2023-09-28 02:56:53 +13:00
2024-09-06 11:04:00 +12:00
_ ->
Phoenix.Controller.redirect(conn, to: "/")
end
2023-09-28 02:56:53 +13:00
2024-09-06 11:04:00 +12:00
%{"guide" => path, "library" => library} ->
redirect_to_hex(conn, "https://hexdocs.pm/#{library}/#{List.last(path)}.html")
2023-09-28 02:56:53 +13:00
_ ->
2024-09-06 11:04:00 +12:00
conn
2023-09-28 02:56:53 +13:00
end
end
2024-09-06 11:04:00 +12:00
defp redirect_to_hex(conn, to) do
conn
|> Plug.Conn.put_status(302)
|> Phoenix.Controller.redirect(external: to)
|> Plug.Conn.halt()
2023-09-28 02:56:53 +13:00
end
end