cannery/lib/cannery_web/views/view_helpers.ex

49 lines
1.2 KiB
Elixir
Raw Permalink Normal View History

2022-02-12 02:28:04 -05:00
defmodule CanneryWeb.ViewHelpers do
@moduledoc """
Contains common helpers that can be used in liveviews and regular views. These
are automatically imported into any Phoenix View using `use CanneryWeb,
:view`
"""
2023-01-26 00:39:16 -05:00
use Phoenix.Component
2022-03-04 22:05:08 -05:00
2022-02-15 22:41:07 -05:00
@doc """
Displays emoji as text emoji if SHIBAO_MODE is set to true :)
"""
@spec display_emoji(String.t()) :: String.t()
2023-01-26 00:39:16 -05:00
def display_emoji("😔") do
if Application.get_env(:cannery, CanneryWeb.ViewHelpers)[:shibao_mode], do: "q_q", else: "😔"
end
2022-02-15 23:52:44 -05:00
2022-02-15 22:41:07 -05:00
def display_emoji(other_emoji), do: other_emoji
2023-01-26 00:46:25 -05:00
@doc """
Displays content in a QR code as a base64 encoded PNG
"""
@spec qr_code_image(String.t()) :: String.t()
@spec qr_code_image(String.t(), width :: non_neg_integer()) :: String.t()
def qr_code_image(content, width \\ 384) do
img_data =
content
|> EQRCode.encode()
|> EQRCode.png(width: width)
|> Base.encode64()
"data:image/png;base64," <> img_data
end
2022-11-09 21:04:57 -05:00
@doc """
Get a random color in `#ffffff` hex format
## Examples
iex> random_color()
"#cc0066"
"""
@spec random_color() :: <<_::7>>
def random_color do
["#cc0066", "#ff6699", "#6666ff", "#0066cc", "#00cc66", "#669900", "#ff9900", "#996633"]
|> Enum.random()
end
2022-02-12 02:28:04 -05:00
end