@@ -5,7 +5,7 @@ defmodule ComponentsGuideWeb.LatencyComparisonLive do
5
5
alias ComponentsGuide.Fetch
6
6
7
7
defmodule State do
8
- defstruct responses: nil
8
+ defstruct responses: nil , form_values: % { }
9
9
10
10
def default ( ) do
11
11
% __MODULE__ { }
@@ -20,24 +20,31 @@ defmodule ComponentsGuideWeb.LatencyComparisonLive do
20
20
| responses: responses
21
21
}
22
22
end
23
+
24
+ def set_form_values ( % __MODULE__ { } = state , form_values = % { } ) do
25
+ % __MODULE__ {
26
+ state
27
+ | form_values: form_values
28
+ }
29
+ end
23
30
end
24
31
25
32
@ impl true
26
33
def render ( assigns ) do
27
34
~H"""
28
35
< . form
29
- let = { f }
30
36
for = { :editor }
31
- id = "view_source_form "
37
+ id = "latency_comparison_form "
32
38
phx-submit = "submitted "
33
39
class = "max-w-2xl mt-12 mx-auto space-y-2 "
34
40
>
35
- < div class = "flex justify- center " >
41
+ < div class = "flex flex-col items- center gap-4 " >
36
42
< 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 = "" >
37
44
</ div >
38
45
</ .form >
39
46
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 " >
41
48
<%= for response <- @ state . responses || [ ] , response != nil do %>
42
49
< div class = "p-4 text-white bg-black font-mono " >
43
50
< pre class = "bg-transparent " > <%= response . url %> </ pre >
@@ -46,10 +53,11 @@ defmodule ComponentsGuideWeb.LatencyComparisonLive do
46
53
< div class = "h-1 bg-green-500 " style = { "width: #{ System . convert_time_unit ( response . timings . received_status - response . timings . connected , :native , :millisecond ) } px" } > </ div >
47
54
< div class = "h-1 bg-purple-200 " style = { "width: #{ System . convert_time_unit ( response . timings . duration - response . timings . received_status , :native , :millisecond ) } px" } > </ div >
48
55
</ 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 >
53
61
</ p >
54
62
</ div >
55
63
<% end %>
@@ -72,22 +80,33 @@ defmodule ComponentsGuideWeb.LatencyComparisonLive do
72
80
end
73
81
74
82
@ impl true
75
- def handle_event ( "submitted" , _form_values , socket ) do
83
+ def handle_event ( "submitted" , form_values = % { "user_url" => user_url } , socket ) do
76
84
urls = [
77
85
"https://workers.cloudflare.com/cf.json" ,
86
+ "https://components-guide-deno.deno.dev/cf.json" ,
78
87
"https://api.github.com/rate_limit" ,
79
88
"https://unpkg.com/robots.txt" ,
80
89
"https://api.npmjs.org/downloads/point/last-month/react" ,
81
90
"https://cdn.jsdelivr.net/npm/[email protected] /underscore-esm-min.js" ,
91
+ "https://unpkg.com/[email protected] /underscore-esm-min.js" ,
82
92
# "https://aws.amazon.com/blogs/aws/",
83
93
# "https://aws.amazon.com/blogs/aws/feed/",
84
94
# "https://cloud.google.com/blog/",
95
+ "https://github.blog/" ,
96
+ "https://github.blog/feed/" ,
85
97
"https://blog.cloudflare.com/" ,
86
98
"https://blog.cloudflare.com/rss/" ,
87
99
"https://vercel.com/blog" ,
88
- "https://vercel.com/atom" ,
100
+ "https://vercel.com/atom"
89
101
]
90
102
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
+
91
110
responses =
92
111
for url <- urls do
93
112
req = Fetch.Request . new! ( url )
@@ -106,7 +125,11 @@ defmodule ComponentsGuideWeb.LatencyComparisonLive do
106
125
# redis_res = Fetch.Response.new("redis:") |> Fetch.Response.add_timings(redis_timings)
107
126
# responses = [redis_res | responses]
108
127
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
+
110
133
socket = socket |> assign_state ( state )
111
134
{ :noreply , socket }
112
135
end
0 commit comments