@@ -35,13 +35,17 @@ if Code.ensure_loaded?(Phoenix.LiveView) do
3535 end
3636
3737 @ impl true
38- def handle_params ( params , _uri , socket ) do
38+ def handle_params ( params , uri , socket ) do
3939 time_range = params [ "range" ] || "7d"
4040 event_type = params [ "type" ] || ""
4141 event_id = params [ "id" ]
4242
43+ # Extract route prefix from URI
44+ route_prefix = extract_route_prefix_from_uri ( uri )
45+
4346 socket =
4447 socket
48+ |> assign ( :route_prefix , route_prefix )
4549 |> assign ( :time_range , time_range )
4650 |> assign ( :event_type , event_type )
4751 |> load_event_types ( )
@@ -71,24 +75,39 @@ if Code.ensure_loaded?(Phoenix.LiveView) do
7175
7276 @ impl true
7377 def handle_event ( "time_range" , % { "range" => range } , socket ) do
74- { :noreply , push_patch ( socket , to: build_url ( range , socket . assigns . event_type ) ) }
78+ { :noreply , push_patch ( socket , to: build_url ( socket , range , socket . assigns . event_type ) ) }
7579 end
7680
7781 @ impl true
7882 def handle_event ( "event_type" , % { "type" => type } , socket ) do
79- { :noreply , push_patch ( socket , to: build_url ( socket . assigns . time_range , type ) ) }
83+ { :noreply , push_patch ( socket , to: build_url ( socket , socket . assigns . time_range , type ) ) }
8084 end
8185
8286 @ impl true
8387 def handle_event ( "close_detail" , _ , socket ) do
8488 { :noreply ,
85- push_patch ( socket , to: build_url ( socket . assigns . time_range , socket . assigns . event_type ) ) }
89+ push_patch ( socket ,
90+ to: build_url ( socket , socket . assigns . time_range , socket . assigns . event_type )
91+ ) }
92+ end
93+
94+ @ doc false
95+ def extract_route_prefix_from_uri ( uri ) do
96+ # Extract the route prefix from the URI
97+ # For example: "http://localhost:4000/admin/fyi?range=7d" -> "/admin/fyi"
98+ # "http://localhost:4000/fyi/events/123?range=7d" -> "/fyi"
99+ uri
100+ |> URI . parse ( )
101+ |> Map . get ( :path , "/fyi" )
102+ |> String . split ( "/events/" )
103+ |> List . first ( )
86104 end
87105
88- defp build_url ( range , type ) do
106+ @ doc false
107+ def build_url ( socket , range , type ) do
89108 params = [ { "range" , range } ]
90109 params = if type != "" , do: params ++ [ { "type" , type } ] , else: params
91- "/fyi ?" <> URI . encode_query ( params )
110+ "#{ socket . assigns . route_prefix } ?" <> URI . encode_query ( params )
92111 end
93112
94113 @ impl true
@@ -697,7 +716,7 @@ if Code.ensure_loaded?(Phoenix.LiveView) do
697716 </ div >
698717 <% else %>
699718 <%= for event <- @ events do %>
700- < . link patch = { event_url ( event . id , @ time_range , @ event_type ) } class = "fyi-event-row " >
719+ < . link patch = { event_url ( assigns , event . id , @ time_range , @ event_type ) } class = "fyi-event-row " >
701720 < svg class = "fyi-event-icon " width = "14 " height = "14 " viewBox = "0 0 24 24 " fill = "none " stroke = "currentColor " stroke-width = "2 " >
702721 < polygon points = "13 2 3 14 12 14 11 22 21 10 12 10 13 2 " > </ polygon >
703722 </ svg >
@@ -849,10 +868,18 @@ if Code.ensure_loaded?(Phoenix.LiveView) do
849868 end
850869 end
851870
852- defp event_url ( event_id , range , type ) do
871+ @ doc false
872+ def event_url ( socket_or_assigns , event_id , range , type ) do
873+ route_prefix =
874+ case socket_or_assigns do
875+ % { assigns: % { route_prefix: prefix } } -> prefix
876+ % { route_prefix: prefix } -> prefix
877+ _ -> "/fyi"
878+ end
879+
853880 params = [ { "range" , range } ]
854881 params = if type != "" , do: params ++ [ { "type" , type } ] , else: params
855- "/fyi /events/#{ event_id } ?" <> URI . encode_query ( params )
882+ "#{ route_prefix } /events/#{ event_id } ?" <> URI . encode_query ( params )
856883 end
857884
858885 defp time_range_since ( range ) do
0 commit comments