fix: quick action translate and validate apply now use tasks
This commit is contained in:
@@ -5,7 +5,7 @@ defmodule BDS.Desktop.ShellLive.Bridges do
|
||||
import Phoenix.LiveView, only: [connected?: 1, send_update: 2]
|
||||
|
||||
alias BDS.Desktop.ShellData
|
||||
alias BDS.Desktop.ShellLive.{ChatEditor, PostEditor}
|
||||
alias BDS.Desktop.ShellLive.{ChatEditor, MediaEditor, PostEditor}
|
||||
alias BDS.Desktop.ShellLive.{CliSync, SessionUtil}
|
||||
alias BDS.UI.Workbench
|
||||
|
||||
@@ -216,6 +216,26 @@ defmodule BDS.Desktop.ShellLive.Bridges do
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
def handle_info({:editor_translation_completed, :post, post_id, language}, socket, _callbacks) do
|
||||
send_update(PostEditor,
|
||||
id: "post-editor-#{post_id}",
|
||||
action: :translation_completed,
|
||||
language: language
|
||||
)
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
def handle_info({:editor_translation_completed, :media, media_id, language}, socket, _callbacks) do
|
||||
send_update(MediaEditor,
|
||||
id: "media-editor-#{media_id}",
|
||||
action: :translation_completed,
|
||||
language: language
|
||||
)
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
def handle_info({:post_editor_apply_ai_suggestions, post_id, fields}, socket, _callbacks) do
|
||||
send_update(PostEditor,
|
||||
id: "post-editor-#{post_id}",
|
||||
|
||||
@@ -7,7 +7,7 @@ defmodule BDS.Desktop.ShellLive.MediaEditor do
|
||||
|
||||
alias BDS.Desktop.{FilePicker}
|
||||
alias BDS.Desktop.ShellLive.Notify
|
||||
alias BDS.{AI, I18n, Media}
|
||||
alias BDS.{AI, I18n, Media, Tasks}
|
||||
alias BDS.Media.Media, as: MediaRecord
|
||||
alias BDS.Media.Translation
|
||||
alias BDS.Posts.Post
|
||||
@@ -57,6 +57,17 @@ defmodule BDS.Desktop.ShellLive.MediaEditor do
|
||||
{:ok, socket}
|
||||
end
|
||||
|
||||
def update(%{action: :translation_completed} = assigns, socket) do
|
||||
socket =
|
||||
socket
|
||||
|> assign(Map.drop(assigns, [:action, :language]))
|
||||
|> assign(:quick_actions_open?, false)
|
||||
|> assign(:editing_translation, nil)
|
||||
|> build_data()
|
||||
|
||||
{:ok, socket}
|
||||
end
|
||||
|
||||
def update(assigns, socket) do
|
||||
socket =
|
||||
socket
|
||||
@@ -552,25 +563,34 @@ defmodule BDS.Desktop.ShellLive.MediaEditor do
|
||||
media = socket.assigns.media
|
||||
normalized_language = normalize_language(language)
|
||||
source_language = normalize_language(media.language)
|
||||
parent = self()
|
||||
|
||||
case AI.translate_media(media.id, normalized_language, source_language: source_language) do
|
||||
{:ok, translation} ->
|
||||
case Media.upsert_media_translation(media.id, normalized_language, translation) do
|
||||
{:ok, _saved_translation} ->
|
||||
socket
|
||||
|> assign(:quick_actions_open?, false)
|
||||
|> assign(:editing_translation, nil)
|
||||
|> build_data()
|
||||
{:ok, _task} =
|
||||
Tasks.submit_task(
|
||||
"Translate Media to #{normalized_language}",
|
||||
fn report ->
|
||||
report.(0.1, "Translating media to #{normalized_language}")
|
||||
|
||||
{:error, reason} ->
|
||||
notify_output(socket, dgettext("ui", "Translate"), inspect(reason), "error")
|
||||
|> build_data()
|
||||
end
|
||||
with {:ok, translation} <-
|
||||
AI.translate_media(media.id, normalized_language,
|
||||
source_language: source_language
|
||||
),
|
||||
{:ok, _saved_translation} <-
|
||||
Media.upsert_media_translation(media.id, normalized_language, translation) do
|
||||
report.(1.0, "Media translation complete")
|
||||
send(parent, {:editor_translation_completed, :media, media.id, normalized_language})
|
||||
%{media_id: media.id, language: normalized_language}
|
||||
else
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end,
|
||||
%{group_id: media.project_id, group_name: "AI"}
|
||||
)
|
||||
|
||||
{:error, reason} ->
|
||||
notify_output(socket, dgettext("ui", "Translate"), inspect(reason), "error")
|
||||
|> build_data()
|
||||
end
|
||||
socket
|
||||
|> assign(:quick_actions_open?, false)
|
||||
|> assign(:editing_translation, nil)
|
||||
|> build_data()
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ defmodule BDS.Desktop.ShellLive.MiscEditor do
|
||||
import Phoenix.HTML, only: [raw: 1]
|
||||
import Ecto.Query
|
||||
|
||||
alias BDS.{Embeddings, Generation, Git, HelpDocs, Posts, Repo}
|
||||
alias BDS.{Embeddings, Git, HelpDocs, Posts, Repo}
|
||||
alias BDS.Desktop.ShellLive.Notify
|
||||
alias BDS.MapUtils
|
||||
alias BDS.Settings.Setting
|
||||
@@ -77,7 +77,6 @@ defmodule BDS.Desktop.ShellLive.MiscEditor do
|
||||
def handle_event("apply_site_validation", _params, socket) do
|
||||
meta = meta(socket.assigns)
|
||||
payload = Map.get(meta, :payload, %{})
|
||||
project_id = Map.get(meta, :project_id, socket.assigns.project_id)
|
||||
|
||||
report = %{
|
||||
sitemap_path: Map.get(payload, :sitemap_path),
|
||||
@@ -89,21 +88,8 @@ defmodule BDS.Desktop.ShellLive.MiscEditor do
|
||||
existing_html_url_count: Map.get(payload, :existing_html_url_count, 0)
|
||||
}
|
||||
|
||||
case Generation.apply_validation(project_id, report) do
|
||||
{:ok, result} ->
|
||||
notify_output(
|
||||
dgettext("ui", "Site Validation"),
|
||||
dgettext("ui", "Validation changes applied"),
|
||||
inspect(result)
|
||||
)
|
||||
|
||||
notify_command("validate_site")
|
||||
{:noreply, socket}
|
||||
end
|
||||
rescue
|
||||
error ->
|
||||
notify_output(dgettext("ui", "Site Validation"), inspect(error), nil, "error")
|
||||
{:noreply, socket}
|
||||
notify_command("apply_site_validation", %{report: report})
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
def handle_event("fix_translation_validation", _params, socket) do
|
||||
|
||||
@@ -3,7 +3,7 @@ defmodule BDS.Desktop.ShellLive.PostEditor do
|
||||
|
||||
use Phoenix.LiveComponent
|
||||
|
||||
alias BDS.{AI, Embeddings, Metadata, Posts, Preview}
|
||||
alias BDS.{AI, Embeddings, Metadata, Posts, Preview, Tasks}
|
||||
alias BDS.Desktop.ShellData
|
||||
alias BDS.Desktop.ShellLive.{EditorImageDrop, Notify}
|
||||
alias BDS.Desktop.ShellLive.PostEditor.{DraftManagement, ListValues, Persistence, PostMetadata}
|
||||
@@ -131,6 +131,15 @@ defmodule BDS.Desktop.ShellLive.PostEditor do
|
||||
{:ok, socket}
|
||||
end
|
||||
|
||||
def update(%{action: :translation_completed, language: language} = assigns, socket) do
|
||||
socket =
|
||||
socket
|
||||
|> assign(Map.drop(assigns, [:action, :language]))
|
||||
|> apply_translation_completed(language)
|
||||
|
||||
{:ok, socket}
|
||||
end
|
||||
|
||||
def update(%{action: :apply_ai_suggestions, fields: fields} = assigns, socket) do
|
||||
socket =
|
||||
socket
|
||||
@@ -808,34 +817,56 @@ defmodule BDS.Desktop.ShellLive.PostEditor do
|
||||
post_id = socket.assigns.post_id
|
||||
normalized_language = normalize_language(language, "")
|
||||
source_language = socket.assigns.canonical_language
|
||||
project_id = socket.assigns.post.project_id
|
||||
parent = self()
|
||||
|
||||
case AI.translate_post(post_id, normalized_language, source_language: source_language) do
|
||||
{:ok, translation} ->
|
||||
with {:ok, _saved_translation} <-
|
||||
Posts.upsert_post_translation(post_id, normalized_language, %{
|
||||
title: translation.title,
|
||||
excerpt: translation.excerpt,
|
||||
content: translation.content
|
||||
}) do
|
||||
socket =
|
||||
socket
|
||||
|> assign(:active_language, normalized_language)
|
||||
|> assign(:drafts, Map.delete(socket.assigns.drafts, normalized_language))
|
||||
|> assign(:quick_actions_open?, false)
|
||||
|> build_data()
|
||||
{:ok, _task} =
|
||||
Tasks.submit_task(
|
||||
"Translate Post to #{normalized_language}",
|
||||
fn report ->
|
||||
report.(0.1, "Translating post to #{normalized_language}")
|
||||
|
||||
Notify.dirty(:post, post_id, false)
|
||||
socket
|
||||
else
|
||||
{:error, reason} ->
|
||||
notify_output(socket, dgettext("ui", "Translate"), inspect(reason), "error")
|
||||
|> build_data()
|
||||
end
|
||||
with {:ok, translation} <-
|
||||
AI.translate_post(post_id, normalized_language,
|
||||
source_language: source_language
|
||||
),
|
||||
{:ok, _saved_translation} <-
|
||||
Posts.upsert_post_translation(post_id, normalized_language, %{
|
||||
title: translation.title,
|
||||
excerpt: translation.excerpt,
|
||||
content: translation.content
|
||||
}) do
|
||||
report.(1.0, "Post translation complete")
|
||||
send(parent, {:editor_translation_completed, :post, post_id, normalized_language})
|
||||
%{post_id: post_id, language: normalized_language}
|
||||
else
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end,
|
||||
%{group_id: project_id, group_name: "AI"}
|
||||
)
|
||||
|
||||
{:error, reason} ->
|
||||
notify_output(socket, dgettext("ui", "Translate"), inspect(reason), "error")
|
||||
socket
|
||||
|> assign(:quick_actions_open?, false)
|
||||
|> build_data()
|
||||
end
|
||||
end
|
||||
|
||||
defp apply_translation_completed(socket, language) do
|
||||
case Posts.get_post(socket.assigns.post_id) do
|
||||
nil ->
|
||||
build_data(socket)
|
||||
|
||||
%Post{} = post ->
|
||||
socket =
|
||||
socket
|
||||
|> assign(:post, post)
|
||||
|> assign(:active_language, language)
|
||||
|> assign(:drafts, Map.delete(socket.assigns.drafts, language))
|
||||
|> build_data()
|
||||
end
|
||||
|
||||
Notify.dirty(:post, post.id, false)
|
||||
socket
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user