From 5a45c61d1f95c5bab29c6331920167caf11e35ce Mon Sep 17 00:00:00 2001 From: kiosion Date: Sat, 3 Aug 2024 00:12:23 -0400 Subject: [PATCH 01/30] temp: Design updates --- elixir-api/lib/utils/translate.ex | 301 +++++++++++------- svelte-app/src/components/code-block.svelte | 73 +++-- .../components/controls/arrow-button.svelte | 50 ++- .../components/controls/lang-toggle.svelte | 118 +++++++ .../controls/language-toggle.svelte | 126 -------- .../components/controls/theme-toggle.svelte | 112 ++++++- svelte-app/src/components/divider.svelte | 2 +- .../document/content/content.svelte | 18 +- .../components/document/content/footer.svelte | 46 +-- .../components/document/content/header.svelte | 296 +++++++++++------ .../src/components/experiments/gol.svelte | 27 +- .../src/components/experiments/toru.svelte | 15 +- .../components/headings/headed-block.svelte | 11 +- svelte-app/src/components/images/image.svelte | 16 +- .../components/layouts/page-content.svelte | 3 - .../layouts/scroll-container.svelte | 152 --------- .../src/components/lists/document-list.svelte | 15 +- .../src/components/lists/list-item.svelte | 52 +-- .../src/components/nav/header-link.svelte | 50 ++- .../portable-text/serializers/image.svelte | 2 +- svelte-app/src/components/sidebar.svelte | 108 +++++++ .../components/sidebar/sidebar-block.svelte | 122 +++++++ svelte-app/src/languages/en.json | 11 + svelte-app/src/languages/fr.json | 11 + svelte-app/src/lib/i18n.ts | 13 +- svelte-app/src/lib/route-trie.ts | 6 +- svelte-app/src/lib/sidebar.ts | 11 + svelte-app/src/lib/utils.ts | 2 +- svelte-app/src/routes/+error.svelte | 55 ++-- svelte-app/src/routes/+layout.svelte | 42 ++- .../src/routes/[[lang=lang]]/+page.svelte | 105 +++--- .../src/routes/[[lang=lang]]/etc/+page.svelte | 27 +- .../[[lang=lang]]/experiments/+page.svelte | 20 +- .../[[lang=lang]]/thoughts/+/+page.svelte | 99 +++--- .../thoughts/+/[slug]/+page.svelte | 39 +-- .../[[lang=lang]]/thoughts/+page.svelte | 80 ++--- .../thoughts/[slug]/+page.svelte | 2 +- .../routes/[[lang=lang]]/work/+page.svelte | 46 ++- svelte-app/src/styles/_tw.scss | 2 +- svelte-app/static/assets/tmp_avi.png | Bin 0 -> 2049 bytes svelte-app/types/generated/index.ts | 9 + 41 files changed, 1395 insertions(+), 900 deletions(-) create mode 100644 svelte-app/src/components/controls/lang-toggle.svelte delete mode 100644 svelte-app/src/components/controls/language-toggle.svelte delete mode 100644 svelte-app/src/components/layouts/page-content.svelte delete mode 100644 svelte-app/src/components/layouts/scroll-container.svelte create mode 100644 svelte-app/src/components/sidebar.svelte create mode 100644 svelte-app/src/components/sidebar/sidebar-block.svelte create mode 100644 svelte-app/src/lib/sidebar.ts create mode 100644 svelte-app/static/assets/tmp_avi.png diff --git a/elixir-api/lib/utils/translate.ex b/elixir-api/lib/utils/translate.ex index 57dd31e3c..1143e97f7 100644 --- a/elixir-api/lib/utils/translate.ex +++ b/elixir-api/lib/utils/translate.ex @@ -30,7 +30,21 @@ defmodule Hexerei.Translate do {:error, "Error parsing response"} end else - {:ok, %HTTPoison.Response{status_code: status_code, body: _body}} -> + {:ok, %HTTPoison.Response{status_code: status_code, body: body}} -> + # try to parse the body to get the error message + parsed_body = + try do + {:ok, parsed_body} = Poison.decode(body) + parsed_body + rescue + _ -> + %{error: %{message: "Unknown error sending request"}} + end + + Logger.error( + "Error sending request - #{status_code} - #{inspect(parsed_body["error"]["message"])}" + ) + {:error, "Error sending request - #{status_code}"} {:error, %HTTPoison.Error{reason: reason}} -> @@ -52,7 +66,8 @@ defmodule Hexerei.Translate do {"q", text_array}, {"source", source_lang}, {"target", target_lang}, - {"format", "text"} + {"format", "text"}, + {"key", Hexerei.Env.get!(:gcloud_key)} ] } end @@ -129,9 +144,9 @@ defmodule Hexerei.Translate do {:ok, translated_blocks} -> translated_blocks - # {:error, reason} -> - # Logger.error("Error translating: #{reason}") - # text_blocks + {:error, errors} -> + Logger.error("Error translating: #{inspect(errors)}") + text_blocks end {:ok, @@ -152,19 +167,32 @@ defmodule Hexerei.Translate do translated_content_arrays = Enum.map(content_arrays, fn name -> - translate_content_list(Map.get(document, name), target_lang, source_lang) + case translate_content_list(Map.get(document, name), target_lang, source_lang) do + {:ok, translated_content} -> translated_content + {:error, errors} -> {:error, errors} + end end) - translated_blocks = - Enum.reduce(content_arrays, %{}, fn key, acc -> - idx = content_arrays |> Enum.find_index(fn k -> k == key end) - Map.put(acc, key, Enum.at(translated_content_arrays, idx)) + collected_errors = + Enum.flat_map(translated_content_arrays, fn + {:error, errors} -> errors + _ -> [] end) - {:ok, - %{ - "result" => document |> Map.merge(translated_blocks |> Enum.into(%{})) - }} + if not Enum.empty?(collected_errors) do + {:error, sanity_response, collected_errors} + else + translated_blocks = + Enum.reduce(content_arrays, %{}, fn key, acc -> + idx = content_arrays |> Enum.find_index(fn k -> k == key end) + Map.put(acc, key, Enum.at(translated_content_arrays, idx)) + end) + + {:ok, + %{ + "result" => document |> Map.merge(translated_blocks |> Enum.into(%{})) + }} + end else _ -> Logger.error("Cannot translate invalid document: #{inspect(sanity_response)}") @@ -173,35 +201,53 @@ defmodule Hexerei.Translate do end defp translate_content_list(content_list, target_lang, source_lang) do - Enum.map(content_list, fn content -> - field_names = ["title"] - block_names = ["content"] + updated_list = + Enum.map(content_list, fn content -> + field_names = ["title"] + block_names = ["content"] + + fields = Enum.map(field_names, &Map.get(content, &1)) + blocks = Map.take(content, block_names) + + translated_title = + case translate_pt_fields(fields, target_lang, source_lang) do + {:ok, translated_fields} -> + translated_fields |> List.first() + + {:error, errors} -> + Logger.error("Error translating: #{inspect(errors |> List.first())}") + content["title"] + end - fields = Enum.map(field_names, &Map.get(content, &1)) - blocks = Map.take(content, block_names) + translated_content = + case translate_pt_blocks(blocks, target_lang, source_lang) do + {:ok, translated_blocks} -> + translated_blocks["content"] - translated_title = - case translate_pt_fields(fields, target_lang, source_lang) do - {:ok, translated_fields} -> - translated_fields |> List.first() + {:error, errors} -> + Logger.error( + "Error(s) translating config_content_list blocks: #{inspect(errors |> List.first())}" + ) - # {:error, reason} -> - # Logger.error("Error translating: #{reason}") - # content["title"] - end + {:error, errors} + end - translated_content = - case translate_pt_blocks(blocks, target_lang, source_lang) do - {:ok, translated_blocks} -> - translated_blocks["content"] + case translated_content do + {:error, errors} -> + %{"title" => translated_title, "content" => content["content"], "errors" => errors} - # {:error, reason} -> - # Logger.error("Error translating: #{reason}") - # content["content"] + _ -> + %{"title" => translated_title, "content" => translated_content, "errors" => []} end + end) - %{"title" => translated_title, "content" => translated_content} - end) + all_errors = Enum.flat_map(updated_list, fn content -> content["errors"] end) + + if Enum.empty?(all_errors) do + {:ok, updated_list |> Enum.map(fn content -> Map.delete(content, "errors") end)} + else + {:error, all_errors} + end end defp translate_post(sanity_response, target_lang, source_lang, ignore_blocks \\ false) do @@ -218,50 +264,55 @@ defmodule Hexerei.Translate do {:ok, translated_fields} -> translated_fields - # {:error, reason} -> - # Logger.error("Error translating: #{reason}") - # text_fields + {:error, errors} -> + {:error, errors} end - # Since field translations will be in reverse order we can just pop them off - updated_document = - Enum.reduce(field_names, document, fn key, acc -> - original_value = Map.get(acc, key) - index = field_names |> Enum.find_index(fn k -> k == key end) - translation = Enum.at(translated_fields, index) - - translation = - case translation do - [translation] -> translation - translation when is_binary(translation) -> translation - # Gracefully fall back to original value if translation is invalid - _ -> original_value - end - - Map.put(acc, key, translation) - end) + case translated_fields do + {:error, errors} -> + {:error, sanity_response, errors} - # For lists of posts we don't want to translate the blocks since they aren't visible anyways - if ignore_blocks do - {:ok, - %{ - "result" => updated_document - }} - else - translated_blocks = - case translate_pt_blocks(text_blocks, target_lang, source_lang) do - {:ok, translated_blocks} -> - translated_blocks + _ -> + # Since field translations will be in reverse order we can just pop them off + updated_document = + Enum.reduce(field_names, document, fn key, acc -> + original_value = Map.get(acc, key) + index = field_names |> Enum.find_index(fn k -> k == key end) + translation = Enum.at(translated_fields, index) + + translation = + case translation do + [translation] -> translation + translation when is_binary(translation) -> translation + # Gracefully fall back to original value if translation is invalid + _ -> original_value + end + + Map.put(acc, key, translation) + end) - # {:error, reason} -> - # Logger.error("Error translating post blocks: #{reason}") - # text_blocks - end + # For lists of posts we don't want to translate the blocks since they aren't visible anyways + if ignore_blocks do + {:ok, + %{ + "result" => updated_document + }} + else + translated_blocks = + case translate_pt_blocks(text_blocks, target_lang, source_lang) do + {:ok, translated_blocks} -> + translated_blocks + + {:error, errors} -> + Logger.error("Error(s) translating post blocks: #{inspect(errors)}") + text_blocks + end - {:ok, - %{ - "result" => updated_document |> Map.merge(translated_blocks |> Enum.into(%{})) - }} + {:ok, + %{ + "result" => updated_document |> Map.merge(translated_blocks |> Enum.into(%{})) + }} + end end else _ -> @@ -282,6 +333,7 @@ defmodule Hexerei.Translate do Enum.split_with(updated_docs, fn doc -> case doc do {:ok, _} -> true + {:error, _} -> false {:error, _, _} -> false end end) @@ -324,38 +376,43 @@ defmodule Hexerei.Translate do {:ok, translated_fields} -> translated_fields - # {:error, reason} -> - # Logger.error("Error translating: #{reason}") - # text_fields + {:error, errors} -> + {:error, errors} end - updated_document = - Enum.reduce(field_names, document, fn key, acc -> - index = field_names |> Enum.find_index(fn k -> k == key end) - {_list, [translation]} = translated_fields |> List.pop_at(index) - Map.put(acc, key, translation |> List.first()) - end) + case translated_fields do + {:error, errors} -> + {:error, sanity_response, errors} - if ignore_blocks do - {:ok, - %{ - "result" => updated_document - }} - else - translated_blocks = - case translate_pt_blocks(text_blocks, target_lang, source_lang) do - {:ok, translated_blocks} -> - translated_blocks + _ -> + updated_document = + Enum.reduce(field_names, document, fn key, acc -> + index = field_names |> Enum.find_index(fn k -> k == key end) + {_list, [translation]} = translated_fields |> List.pop_at(index) + Map.put(acc, key, translation |> List.first()) + end) - # {:error, reason} -> - # Logger.error("Error translating: #{reason}") - # text_blocks - end + if ignore_blocks do + {:ok, + %{ + "result" => updated_document + }} + else + translated_blocks = + case translate_pt_blocks(text_blocks, target_lang, source_lang) do + {:ok, translated_blocks} -> + translated_blocks + + {:error, errors} -> + Logger.error("Error(s) translating project blocks: #{inspect(errors)}") + text_blocks + end - {:ok, - %{ - "result" => updated_document |> Map.merge(translated_blocks |> Enum.into(%{})) - }} + {:ok, + %{ + "result" => updated_document |> Map.merge(translated_blocks |> Enum.into(%{})) + }} + end end else _ -> @@ -415,14 +472,25 @@ defmodule Hexerei.Translate do result {:ok, {:error, reason}} -> - {:error, "Error translating fields: #{inspect(reason)}"} + Logger.error("Error translating fields: #{inspect(reason)}") + {:error, reason} {:exit, reason} -> Logger.error("Error translating fields: #{inspect(reason)}") {:error, "Error translating fields"} end) - {:ok, translations} + collected_errors = + Enum.flat_map(translations, fn + {:error, reason} -> [reason] + _ -> [] + end) + + if not Enum.empty?(collected_errors) do + {:error, collected_errors} + else + {:ok, translations} + end end defp translate_pt_blocks(pt_blocks, target_lang, source_lang) do @@ -447,21 +515,38 @@ defmodule Hexerei.Translate do {:ok, {:ok, result}} -> result - {:ok, {:error, _}} -> - {:error, "Error translating block"} + {:ok, {:error, reason}} -> + {:error, "Error translating block: #{inspect(reason)}"} {:exit, reason} -> Logger.error("Error translating block: #{inspect(reason)}") {:error, "Error translating block"} end) + collected_errors = + Enum.flat_map(translations, fn + {:error, reason} -> [reason] + _ -> [] + end) + updated_blocks = replace_text_in_children(value, text_array, translations) - {key, updated_blocks} + {key, updated_blocks, collected_errors} + # {key, updated_blocks} end end) - {:ok, updated_fields |> Enum.into(%{})} + all_errors = Enum.flat_map(updated_fields, fn {_, _, errors} -> errors end) + + # updated_fields = Enum.map(updated_fields, fn {key, value, _} -> {key, value} end) |> Enum.into(%{}) + + if Enum.empty?(all_errors) do + {:ok, Enum.map(updated_fields, fn {key, value, _} -> {key, value} end) |> Enum.into(%{})} + else + {:error, all_errors} + end + + # {:ok, updated_fields |> Enum.into(%{})} end defp translate_field(text, target_lang, source_lang) do diff --git a/svelte-app/src/components/code-block.svelte b/svelte-app/src/components/code-block.svelte index 7d483ab86..b716400b1 100644 --- a/svelte-app/src/components/code-block.svelte +++ b/svelte-app/src/components/code-block.svelte @@ -1,7 +1,7 @@
{#if filename}
- {filename} + + + + + {filename}
{/if} - - - + + + + + + + + +