# Copyright 2024 Adobe. All rights reserved. # This file is licensed to you under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. You may obtain a copy # of the License at http://www.apache.org/licenses/LICENSE-2.0 # Unless required by applicable law or agreed to in writing, software distributed under # the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS # OF ANY KIND, either express or implied. See the License for the specific language # governing permissions and limitations under the License. defmodule <%= @web_module %>.DumperHTML do @moduledoc """ The view for dumper index and show pages. Put any value overrides here if you want special formatting of a certain field or data type. """ use Phoenix.Component use Phoenix.HTML use Phoenix.VerifiedRoutes, endpoint: <%= @web_module %>.Endpoint, router: <%= @web_module %>.Router, statics: <%= @web_module %>.static_paths() embed_templates "dumper_html/*" ################################################## ## Redacted trumps all else defp value(%{redacted: true} = assigns), do: ~H|redacted| ################################################## ## Nil, true, and false defp value(%{value: nil} = assigns), do: ~H|nil| defp value(%{value: true} = assigns), do: ~H|true| defp value(%{value: false} = assigns), do: ~H|false| ################################################## ## By field/column name # For example, the following turns all `user_id` columns into links # defp value(%{field: :user_id} = assigns) do # ~H""" # <%%= @value %> # """ # end ################################################## ## By Type defp value(%{type: :binary_id} = assigns), do: ~H|
<%%= @value %>| defp value(%{type: :date} = assigns) do ~H""" <%%= @value |> Calendar.strftime("%b %d, %Y") %> """ end defp value(%{type: type} = assigns) when type in ~w/utc_datetime_usec naive_datetime_usec utc_datetime naive_datetime/a do ~H""" <%%= @value |> Calendar.strftime("%b %d, %Y --- %I:%M:%S.%f %p") %> """ end ################################################## ## Default defp value(assigns), do: ~H|
<%%= inspect(@value, pretty: true) %>| ################################################## ## Helpers defp module_name(module), do: module |> to_string() |> String.replace(~r/^Elixir\./, "") defp humanize_association_name(module) do module |> Atom.to_string() |> String.split("_") |> Enum.map_join(" ", &String.capitalize/1) end defp fields([record | _]), do: record.__struct__.__schema__(:fields) defp embeds([record | _]), do: record.__struct__.__schema__(:embeds) defp redacted_fields(record), do: record.__struct__.__schema__(:redact_fields) attr :module, :atom, required: true defp docs(assigns) do doctext = case Code.fetch_docs(assigns.module) do {_, _, _, _, %{"en" => doctext} = _module_doc, _, _} -> String.trim(doctext) _ -> "" end markdown_html = doctext |> Earmark.as_html!(code_class_prefix: "lang- language-") |> Phoenix.HTML.raw() assigns = assign(assigns, markdown: markdown_html, doctext: doctext) ~H"""