Skip to content

Commit bf31828

Browse files
srzeszutsrzeszut
authored andcommitted
Enhancement: Add :info flash (#727)
* add key to push_flash function * add flash_group component and extend flash component with :info kind * change colors for :info messages
1 parent 75cfc26 commit bf31828

File tree

10 files changed

+62
-18
lines changed

10 files changed

+62
-18
lines changed

assets/app/styles/themes/dark.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,11 @@
7171

7272
--search-highlight-bg: var(--swm-yellow-100);
7373
--search-highlight-text: var(--gray-900);
74+
75+
/* Info */
76+
--info-bg: var(--surface-0-bg);
77+
--info-border: var(--default-border);
78+
--info-icon: var(--primary-text);
79+
--info-text: var(--primary-text);
7480
}
7581
}

assets/app/styles/themes/light.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@
6565
--error-icon: var(--red-500);
6666
--error-text: var(--red-800);
6767

68+
/* Info */
69+
--info-bg: var(--surface-0-bg);
70+
--info-border: var(--default-border);
71+
--info-icon: var(--primary-text);
72+
--info-text: var(--primary-text);
73+
6874
/* Warning */
6975
--warning-text: #9a7e00;
7076

assets/app/tailwind.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ module.exports = {
5757
'error-border': 'var(--error-border)',
5858
'error-icon': 'var(--error-icon)',
5959
'error-text': 'var(--error-text)',
60+
'info-bg': 'var(--info-bg)',
61+
'info-border': 'var(--info-border)',
62+
'info-icon': 'var(--info-icon)',
63+
'info-text': 'var(--info-text)',
6064
'warning-text': 'var(--warning-text)',
6165
},
6266
screens: { xs: '380px' },

lib/live_debugger/app/debugger/callback_tracing/web/hook_components/trace_wrapper.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ defmodule LiveDebugger.App.Debugger.CallbackTracing.Web.HookComponents.TraceWrap
1919

2020
use LiveDebugger.App.Web, :hook_component
2121

22-
import LiveDebugger.App.Web.Hooks.Flash, only: [push_flash: 3]
22+
import LiveDebugger.App.Web.Hooks.Flash, only: [push_flash: 4]
2323

2424
alias LiveDebugger.API.TracesStorage
2525
alias LiveDebugger.App.Debugger.CallbackTracing.Structs.TraceDisplay
@@ -105,7 +105,7 @@ defmodule LiveDebugger.App.Debugger.CallbackTracing.Web.HookComponents.TraceWrap
105105
|> case do
106106
nil ->
107107
socket
108-
|> push_flash("Trace has been removed.", socket.assigns.parent_pid)
108+
|> push_flash(:error, "Trace has been removed.", socket.assigns.parent_pid)
109109

110110
trace ->
111111
socket
@@ -133,7 +133,7 @@ defmodule LiveDebugger.App.Debugger.CallbackTracing.Web.HookComponents.TraceWrap
133133

134134
defp handle_info({:trace_wrapper_not_found, string_trace_id}, socket) do
135135
socket
136-
|> push_flash("Trace has been removed.", socket.assigns.parent_pid)
136+
|> push_flash(:error, "Trace has been removed.", socket.assigns.parent_pid)
137137
|> push_event("existing_traces-#{string_trace_id}-collapsible", %{action: "close"})
138138
|> halt()
139139
end

lib/live_debugger/app/debugger/callback_tracing/web/hooks/tracing_fuse.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ defmodule LiveDebugger.App.Debugger.CallbackTracing.Web.Hooks.TracingFuse do
106106

107107
socket
108108
|> Flash.push_flash(
109+
:error,
109110
"Callback tracer stopped: Too many callbacks in a short time. Current limit is #{limit} callbacks in #{period}.",
110111
socket.assigns.parent_pid
111112
)

lib/live_debugger/app/settings/web/settings_live.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ defmodule LiveDebugger.App.Settings.Web.SettingsLive do
111111
assign(socket, settings: new_settings)
112112

113113
{:error, _} ->
114-
push_flash(socket, "Failed to update setting")
114+
push_flash(socket, :error, "Failed to update setting")
115115
end
116116
|> noreply()
117117
end

lib/live_debugger/app/web.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ defmodule LiveDebugger.App.Web do
1313
import Phoenix.HTML
1414
import LiveDebugger.Helpers
1515
import unquote(__MODULE__).Components
16-
import unquote(__MODULE__).Hooks.Flash, only: [push_flash: 2, push_flash: 3]
16+
import unquote(__MODULE__).Hooks.Flash, only: [push_flash: 3, push_flash: 4]
1717
end
1818
end
1919

@@ -24,7 +24,7 @@ defmodule LiveDebugger.App.Web do
2424
import Phoenix.HTML
2525
import LiveDebugger.Helpers
2626
import unquote(__MODULE__).Components
27-
import unquote(__MODULE__).Hooks.Flash, only: [push_flash: 2, push_flash: 3]
27+
import unquote(__MODULE__).Hooks.Flash, only: [push_flash: 3, push_flash: 4]
2828
end
2929
end
3030

lib/live_debugger/app/web/components.ex

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,15 @@ defmodule LiveDebugger.App.Web.Components do
175175
"""
176176
attr(:id, :string, doc: "the optional id of flash container")
177177
attr(:flash, :map, default: %{}, doc: "the map of flash messages to display")
178+
attr(:kind, :atom, values: [:info, :error], doc: "used for styling and flash lookup")
178179
attr(:rest, :global, doc: "the arbitrary HTML attributes to add to the flash container")
179180

180181
def flash(assigns) do
181-
message = Phoenix.Flash.get(assigns.flash, :error)
182+
message = Phoenix.Flash.get(assigns.flash, assigns.kind)
182183

183184
assigns =
184185
assigns
185-
|> assign_new(:id, fn -> "flash" end)
186+
|> assign_new(:id, fn -> "flash-#{assigns.kind}" end)
186187
|> assign(:message, message)
187188

188189
~H"""
@@ -193,13 +194,16 @@ defmodule LiveDebugger.App.Web.Components do
193194
role="alert"
194195
class={[
195196
"fixed left-2 bottom-2 w-80 sm:w-96 z-50 rounded-sm p-4 flex justify-between items-center gap-3",
196-
"bg-error-bg text-error-text border-error-text border"
197+
@kind == :error && "bg-error-bg text-error-text border-error-text border",
198+
@kind == :info &&
199+
"bg-info-bg text-info-text border-info-border border"
197200
]}
198201
{@rest}
199202
>
200203
<div class="flex gap-3 items-start">
201204
<div>
202-
<.icon name="icon-x-circle" class="text-error-icon w-3 h-3" />
205+
<.icon :if={@kind == :error} name="icon-x-circle" class="text-error-icon w-3 h-3" />
206+
<.icon :if={@kind == :info} name="icon-info" class="text-info-icon w-3 h-3" />
203207
</div>
204208
<p>
205209
<%= @message %>
@@ -208,7 +212,7 @@ defmodule LiveDebugger.App.Web.Components do
208212
<button
209213
phx-click={
210214
"lv:clear-flash"
211-
|> JS.push(value: %{key: :error})
215+
|> JS.push(value: %{key: @kind})
212216
|> JS.hide(
213217
to: "##{@id}",
214218
time: 200,
@@ -223,6 +227,25 @@ defmodule LiveDebugger.App.Web.Components do
223227
"""
224228
end
225229

230+
@doc """
231+
Shows the flash group with standard titles and content.
232+
233+
## Examples
234+
235+
<.flash_group flash={@flash} />
236+
"""
237+
attr(:flash, :map, required: true, doc: "the map of flash messages")
238+
attr(:id, :string, default: "flash-group", doc: "the optional id of flash container")
239+
240+
def flash_group(assigns) do
241+
~H"""
242+
<div id={@id}>
243+
<.flash kind={:info} flash={@flash} />
244+
<.flash kind={:error} flash={@flash} />
245+
</div>
246+
"""
247+
end
248+
226249
attr(:id, :string, required: true)
227250
attr(:title, :string, required: true)
228251
attr(:class, :any, default: nil)

lib/live_debugger/app/web/hooks/flash.ex

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,31 @@ defmodule LiveDebugger.App.Web.Hooks.Flash do
1818
"""
1919
@spec push_flash(
2020
socket :: Phoenix.LiveView.Socket.t(),
21+
key :: String.t() | atom(),
2122
message :: String.t()
2223
) ::
2324
Phoenix.LiveView.Socket.t()
24-
def push_flash(socket, message) when is_binary(message) do
25-
push_flash(socket, message, self())
25+
def push_flash(socket, key, message)
26+
when is_binary(message) and key in [:error, :info] do
27+
push_flash(socket, key, message, self())
2628
end
2729

2830
@spec push_flash(
2931
socket :: Phoenix.LiveView.Socket.t(),
32+
key :: String.t() | atom(),
3033
message :: String.t(),
3134
pid :: pid()
3235
) ::
3336
Phoenix.LiveView.Socket.t()
34-
def push_flash(socket, message, pid) when is_pid(pid) and is_binary(message) do
35-
send(pid, {:put_flash, message})
37+
def push_flash(socket, key, message, pid)
38+
when is_pid(pid) and is_binary(message) and key in [:error, :info] do
39+
send(pid, {:put_flash, key, message})
3640

3741
socket
3842
end
3943

40-
defp maybe_receive_flash({:put_flash, message}, socket) do
41-
{:halt, put_flash(socket, :error, message)}
44+
defp maybe_receive_flash({:put_flash, key, message}, socket) do
45+
{:halt, put_flash(socket, key, message)}
4246
end
4347

4448
defp maybe_receive_flash(_, socket), do: {:cont, socket}

lib/live_debugger/app/web/layout.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ defmodule LiveDebugger.App.Web.Layout do
6363
def render("app.html", assigns) do
6464
~H"""
6565
<main class="h-screen w-screen max-w-full">
66-
<LiveDebugger.App.Web.Components.flash flash={@flash} />
66+
<LiveDebugger.App.Web.Components.flash_group flash={@flash} />
6767
<%= @inner_content %>
6868
</main>
6969
"""

0 commit comments

Comments
 (0)