@@ -5,7 +5,7 @@ defmodule ComponentsGuideWeb.LatencyComparisonLive do
55 alias ComponentsGuide.Fetch
66
77 defmodule State do
8- defstruct responses: nil
8+ defstruct responses: nil , form_values: % { }
99
1010 def default ( ) do
1111 % __MODULE__ { }
@@ -20,24 +20,31 @@ defmodule ComponentsGuideWeb.LatencyComparisonLive do
2020 | responses: responses
2121 }
2222 end
23+
24+ def set_form_values ( % __MODULE__ { } = state , form_values = % { } ) do
25+ % __MODULE__ {
26+ state
27+ | form_values: form_values
28+ }
29+ end
2330 end
2431
2532 @ impl true
2633 def render ( assigns ) do
2734 ~H"""
2835 < . form
29- let = { f }
3036 for = { :editor }
31- id = "view_source_form "
37+ id = "latency_comparison_form "
3238 phx-submit = "submitted "
3339 class = "max-w-2xl mt-12 mx-auto space-y-2 "
3440 >
35- < div class = "flex justify- center " >
41+ < div class = "flex flex-col items- center gap-4 " >
3642 < button type = "submit " class = "px-3 py-1 text-xl text-blue-900 bg-blue-200 rounded " autofocus > Load</ button >
43+ < input type = "url " id = "user_url " name = "user_url " value = { @ state . form_values [ "user_url" ] } class = "w-80 text-sm text-white bg-black border border-gray-700 rounded " placeholder = "Compare your URL (optional) " aria-label = "" >
3744 </ div >
3845 </ .form >
3946
40- < output form = "view_source_form " class = "flex flex-col gap-4 pt-4 max-w-none text-center " >
47+ < output form = "latency_comparison_form " class = "flex flex-col gap-4 pt-4 max-w-none text-center " >
4148 <%= for response <- @ state . responses || [ ] , response != nil do %>
4249 < div class = "p-4 text-white bg-black font-mono " >
4350 < pre class = "bg-transparent " > <%= response . url %> </ pre >
@@ -46,10 +53,11 @@ defmodule ComponentsGuideWeb.LatencyComparisonLive do
4653 < div class = "h-1 bg-green-500 " style = { "width: #{ System . convert_time_unit ( response . timings . received_status - response . timings . connected , :native , :millisecond ) } px" } > </ div >
4754 < div class = "h-1 bg-purple-200 " style = { "width: #{ System . convert_time_unit ( response . timings . duration - response . timings . received_status , :native , :millisecond ) } px" } > </ div >
4855 </ div >
49- < p class = "text-lg " >
50- 🤝 < data class = "text-yellow-200 " > <%= System . convert_time_unit ( response . timings . connected , :native , :millisecond ) %> ms</ data >
51- | < span class = "py-1 px-1 text-green-100 bg-green-900/50 rounded " > <%= response . status %> </ span > < data class = "text-green-200 " > +<%= System . convert_time_unit ( response . timings . received_status - response . timings . connected , :native , :millisecond ) %> ms</ data >
52- | < span class = "py-1 px-1 text-purple-100 bg-purple-900/50 rounded " > <%= Format . humanize_bytes ( byte_size ( response . body ) ) %> </ span > body < data class = "text-purple-200 " > +<%= System . convert_time_unit ( response . timings . duration - response . timings . received_status , :native , :millisecond ) %> ms</ data >
56+ < p class = "flex justify-center items-center gap-3 text-lg " >
57+ < data class = "text-yellow-200 " > <%= System . convert_time_unit ( response . timings . connected , :native , :millisecond ) %> ms</ data > < span > 🤝</ span >
58+ < data class = "text-green-200 " > <%= System . convert_time_unit ( response . timings . received_status - response . timings . connected , :native , :millisecond ) %> ms</ data > < span class = "px-1 text-sm text-green-900 bg-green-100 border border-green-200 rounded-full " > <%= response . status %> </ span >
59+ < data class = "text-purple-200 " > <%= System . convert_time_unit ( response . timings . duration - response . timings . received_status , :native , :millisecond ) %> ms</ data > < span class = "px-1 text-sm text-purple-900 bg-purple-100 border border-purple-200 rounded-full " > <%= Format . humanize_bytes ( byte_size ( response . body ) ) %> </ span >
60+ < span class = "text-blue-200/50 " > |</ span > < data class = "text-blue-200 " > <%= System . convert_time_unit ( response . timings . duration , :native , :millisecond ) %> ms</ data >
5361 </ p >
5462 </ div >
5563 <% end %>
@@ -72,22 +80,33 @@ defmodule ComponentsGuideWeb.LatencyComparisonLive do
7280 end
7381
7482 @ impl true
75- def handle_event ( "submitted" , _form_values , socket ) do
83+ def handle_event ( "submitted" , form_values = % { "user_url" => user_url } , socket ) do
7684 urls = [
7785 "https://workers.cloudflare.com/cf.json" ,
86+ "https://components-guide-deno.deno.dev/cf.json" ,
7887 "https://api.github.com/rate_limit" ,
7988 "https://unpkg.com/robots.txt" ,
8089 "https://api.npmjs.org/downloads/point/last-month/react" ,
8190 "https://cdn.jsdelivr.net/npm/underscore@1.13.6/underscore-esm-min.js" ,
91+ "https://unpkg.com/underscore@1.13.6/underscore-esm-min.js" ,
8292 # "https://aws.amazon.com/blogs/aws/",
8393 # "https://aws.amazon.com/blogs/aws/feed/",
8494 # "https://cloud.google.com/blog/",
95+ "https://github.blog/" ,
96+ "https://github.blog/feed/" ,
8597 "https://blog.cloudflare.com/" ,
8698 "https://blog.cloudflare.com/rss/" ,
8799 "https://vercel.com/blog" ,
88- "https://vercel.com/atom" ,
100+ "https://vercel.com/atom"
89101 ]
90102
103+ urls =
104+ case URI . new ( user_url ) do
105+ { :ok , % URI { host: "" } } -> urls
106+ { :ok , uri = % URI { host: host , port: 443 } } when is_binary ( host ) -> [ to_string ( uri ) | urls ]
107+ _ -> urls
108+ end
109+
91110 responses =
92111 for url <- urls do
93112 req = Fetch.Request . new! ( url )
@@ -106,7 +125,11 @@ defmodule ComponentsGuideWeb.LatencyComparisonLive do
106125 # redis_res = Fetch.Response.new("redis:") |> Fetch.Response.add_timings(redis_timings)
107126 # responses = [redis_res | responses]
108127
109- state = socket . assigns . state |> State . add_responses ( responses )
128+ state =
129+ socket . assigns . state
130+ |> State . add_responses ( responses )
131+ |> State . set_form_values ( form_values )
132+
110133 socket = socket |> assign_state ( state )
111134 { :noreply , socket }
112135 end
0 commit comments