Skip to content

Commit 804a92b

Browse files
committed
Preserve nav group state when navigating to non-resource pages
Track open groups in a MapSet in the component assigns. When navigating to a resource, recompute which groups are open. When navigating to a non-resource page, keep the existing open groups unchanged.
1 parent 85c1b5a commit 804a92b

1 file changed

Lines changed: 17 additions & 16 deletions

File tree

  • lib/live_admin/components

lib/live_admin/components/nav.ex

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,23 @@ defmodule LiveAdmin.Components.Nav do
2424
!String.match?(r.helper, ~r/(home|session)/)
2525
end)
2626

27+
open_groups =
28+
case assigns[:resource] do
29+
nil ->
30+
socket.assigns[:open_groups] || MapSet.new()
31+
32+
resource ->
33+
resource.__live_admin_config__()
34+
|> Keyword.fetch!(:schema)
35+
|> Module.split()
36+
|> Enum.drop(-1)
37+
|> MapSet.new()
38+
end
39+
2740
socket =
2841
socket
2942
|> assign(assigns)
30-
|> assign(extra_pages: extra_pages)
43+
|> assign(extra_pages: extra_pages, open_groups: open_groups)
3144

3245
{:ok, socket}
3346
end
@@ -88,6 +101,7 @@ defmodule LiveAdmin.Components.Nav do
88101
items={@nested_resources}
89102
base_path={@base_path}
90103
current_resource={assigns[:resource]}
104+
open_groups={@open_groups}
91105
config={@config}
92106
/>
93107
</div>
@@ -133,7 +147,7 @@ defmodule LiveAdmin.Components.Nav do
133147
type="checkbox"
134148
id={"#{parent}-toggle"}
135149
class="nav-toggle-input"
136-
checked={open?(assigns, parent)}
150+
checked={MapSet.member?(@open_groups, parent)}
137151
/>
138152
<label for={"#{parent}-toggle"} class="nav-toggle-label">
139153
<span class="nav-icon">
@@ -195,6 +209,7 @@ defmodule LiveAdmin.Components.Nav do
195209
items={children}
196210
base_path={@base_path}
197211
current_resource={@current_resource}
212+
open_groups={@open_groups}
198213
config={@config}
199214
/>
200215
</div>
@@ -204,18 +219,4 @@ defmodule LiveAdmin.Components.Nav do
204219
"""
205220
end
206221

207-
defp open?(assigns, schema) do
208-
assigns.current_resource
209-
|> case do
210-
nil ->
211-
false
212-
213-
resource ->
214-
resource.__live_admin_config__()
215-
|> Keyword.fetch!(:schema)
216-
|> Module.split()
217-
|> Enum.drop(-1)
218-
|> Enum.member?(schema)
219-
end
220-
end
221222
end

0 commit comments

Comments
 (0)