Migrate to Vue 3 and Vite

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2022-07-12 10:55:28 +02:00
parent 8f4099ee33
commit ee20e03cc2
464 changed files with 31515 additions and 32758 deletions

View File

@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang={assigns.locale} dir={language_direction(assigns)}>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="apple-touch-icon" href="/img/icons/apple-touch-icon-152x152.png" sizes="152x152">
<link rel="mask-icon" href="/img/icons/safari-pinned-tab.svg" color={theme_color()}>
<meta name="theme-color" content={theme_color()}>
<%= tags(assigns) || assigns.tags %>
<%= Vite.inlined_phx_manifest() %>
<%= Vite.vite_client() %>
<%= Vite.vite_snippet("src/main.ts") %>
</head>
<body>
<noscript>
<strong>
We're sorry but Mobilizon doesn't work properly without JavaScript enabled. Please enable it to continue.
</strong>
</noscript>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>

View File

@@ -6,15 +6,22 @@ defmodule Mobilizon.Web.AuthView do
use Mobilizon.Web, :view
alias Mobilizon.Service.Metadata.Instance
alias Phoenix.HTML.Tag
import Mobilizon.Web.Views.Utils
@spec render(String.t(), map()) :: String.t() | Plug.Conn.t()
def render("callback.html", %{
conn: conn,
access_token: access_token,
refresh_token: refresh_token,
user: %{id: user_id, email: user_email, role: user_role, default_actor_id: user_actor_id}
}) do
def render(
"callback.html",
%{
conn: _conn,
access_token: access_token,
refresh_token: refresh_token,
user: %{
id: user_id,
email: user_email,
role: user_role,
default_actor_id: user_actor_id
}
} = assigns
) do
info_tags = [
Tag.tag(:meta, name: "auth-access-token", content: access_token),
Tag.tag(:meta, name: "auth-refresh-token", content: refresh_token),
@@ -25,11 +32,8 @@ defmodule Mobilizon.Web.AuthView do
]
with tags <- Instance.build_tags() ++ info_tags,
{:ok, html} <- inject_tags(tags, get_locale(conn)) do
html
else
{:error, error} ->
return_error(conn, error)
assigns <- Map.put(assigns, :tags, tags) do
render("index.html", assigns)
end
end
end

View File

@@ -3,18 +3,10 @@ defmodule Mobilizon.Web.ErrorView do
View for errors
"""
use Mobilizon.Web, :view
alias Mobilizon.Service.Metadata.Instance
import Mobilizon.Web.Views.Utils
@spec render(String.t(), map()) :: map() | String.t() | Plug.Conn.t()
def render("404.html", %{conn: conn}) do
with tags <- Instance.build_tags(),
{:ok, html} <- inject_tags(tags, get_locale(conn)) do
html
else
{:error, error} ->
return_error(conn, error)
end
def render("404.html", _assigns) do
render("index.html")
end
def render("404.json", _assigns) do

View File

@@ -63,28 +63,27 @@ defmodule Mobilizon.Web.PageView do
|> Map.merge(Utils.make_json_ld_header())
end
def render(page, %{object: object, conn: conn} = _assigns)
def render(page, %{object: object, conn: conn} = assigns)
when page in ["actor.html", "event.html", "comment.html", "post.html"] do
with locale <- get_locale(conn),
tags <- object |> Metadata.build_tags(locale),
{:ok, html} <- inject_tags(tags, locale) do
html
else
{:error, error} ->
return_error(conn, error)
assigns <- Map.put(assigns, :tags, tags) do
render("index.html", assigns)
end
end
# Discussions are private, no need to embed metadata
def render("discussion.html", params), do: render("index.html", params)
def render("index.html", %{conn: conn}) do
with tags <- Instance.build_tags(),
{:ok, html} <- inject_tags(tags, get_locale(conn)) do
html
else
{:error, error} ->
return_error(conn, error)
end
def tags(assigns) do
Map.get(assigns, :tags, Instance.build_tags())
end
def theme_color do
"#ffd599"
end
def language_direction(assigns) do
get_language_direction(assigns.locale)
end
end

View File

@@ -3,58 +3,6 @@ defmodule Mobilizon.Web.Views.Utils do
Utils for views
"""
alias Mobilizon.Service.Metadata.Utils, as: MetadataUtils
import Mobilizon.Web.Gettext, only: [dgettext: 2]
import Plug.Conn, only: [put_status: 2, halt: 1]
# sobelow_skip ["Traversal.FileModule"]
@spec inject_tags(Enum.t(), String.t()) :: {:ok, {:safe, String.t()}} | {:error, atom()}
def inject_tags(tags, locale \\ "en") do
with path <- Path.join(Application.app_dir(:mobilizon, "priv/static"), "index.html"),
{:exists, true} <- {:exists, File.exists?(path)},
{:ok, index_content} <- File.read(path),
safe <- do_replacements(index_content, MetadataUtils.stringify_tags(tags), locale) do
{:ok, {:safe, safe}}
else
{:exists, false} -> {:error, :index_not_found}
{:error, error} when is_atom(error) -> {:error, error}
end
end
@spec return_error(Plug.Conn.t(), atom()) :: Plug.Conn.t()
def return_error(conn, error) do
conn
|> put_status(500)
|> Phoenix.Controller.put_view(Mobilizon.Web.ErrorView)
|> Phoenix.Controller.render("500.html", %{details: details(error)})
|> halt()
end
defp details(:index_not_found) do
[dgettext("errors", "Index file not found. You need to recompile the front-end.")]
end
defp details(_) do
[]
end
@spec replace_meta(String.t(), String.t()) :: String.t()
defp replace_meta(index_content, tags) do
index_content
|> String.replace("<meta name=\"server-injected-data\"/>", tags)
|> String.replace("<meta name=\"server-injected-data\" />", tags)
end
@spec do_replacements(String.t(), String.t(), String.t()) :: String.t()
defp do_replacements(index_content, tags, locale) do
index_content
|> replace_meta(tags)
|> String.replace(
~s(<html lang="en" dir="auto">),
~s(<html lang="#{locale}" dir="#{get_language_direction(locale)}">)
)
end
@spec get_locale(Plug.Conn.t()) :: String.t()
def get_locale(%Plug.Conn{assigns: assigns}) do
Map.get(assigns, :locale)
@@ -63,6 +11,6 @@ defmodule Mobilizon.Web.Views.Utils do
@ltr_languages ["ar", "ae", "he", "fa", "ku", "ur"]
@spec get_language_direction(String.t()) :: String.t()
defp get_language_direction(locale) when locale in @ltr_languages, do: "rtl"
defp get_language_direction(_locale), do: "ltr"
def get_language_direction(locale) when locale in @ltr_languages, do: "rtl"
def get_language_direction(_locale), do: "ltr"
end