Skip to content

Commit 907cacb

Browse files
committed
Show byte size for icons
1 parent 95d60e3 commit 907cacb

File tree

6 files changed

+72
-13
lines changed

6 files changed

+72
-13
lines changed

apps/components_guide/lib/components_guide/research/source.ex

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ defmodule ComponentsGuide.Research.Source do
6565
%{query: query, path: path} -> path <> "?" <> query
6666
end
6767

68-
IO.puts("fetching URL #{uri} #{uri.host} #{path}")
68+
IO.puts("fetching URL #{uri} #{uri.host} #{path}")
6969
{:ok, conn, request_ref} = Mint.HTTP.request(conn, "GET", path, [], nil)
7070

7171
receive_mint_response(%Fetch{}, conn, request_ref)
@@ -82,37 +82,46 @@ defmodule ComponentsGuide.Research.Source do
8282
# Fetch.get(url)
8383

8484
with {:ok, response} <- Mojito.request(method: :get, url: url, timeout: 50000) do
85-
{:ok, response.body}
85+
{:ok, response}
8686
else
8787
_ -> :err
8888
end
8989
end
9090

9191
defp body({:html_document, url}) do
92-
with {:ok, html} <- read({:fetch, url}),
93-
{:ok, document} <- Floki.parse_document(html) do
92+
with {:ok, response} <- read({:fetch, url}),
93+
{:ok, document} <- Floki.parse_document(response.body) do
9494
{:ok, document}
9595
else
9696
_ -> :err
9797
end
9898
end
9999

100100
defp body({:fetch_json, url}) do
101-
with {:ok, encoded} <- read({:fetch, url}),
102-
{:ok, data} <- Jason.decode(encoded) do
101+
with {:ok, response} <- read({:fetch, url}),
102+
{:ok, data} <- Jason.decode(response.body) do
103103
{:ok, data}
104104
else
105105
_ -> :err
106106
end
107107
end
108108

109+
defp body({:content_length, url}) do
110+
with {:ok, response} <- read({:fetch, url}) do
111+
{:ok, Mojito.Headers.get(response.headers, "content-length")}
112+
else
113+
_ -> :err
114+
end
115+
end
116+
109117
defp run(key) do
110118
with {:ok, value} <- body(key) do
111119
write_cache(key, value)
112120
{:ok, value}
113121
else
114-
_ -> write_cache(key, :err)
115-
:err
122+
_ ->
123+
write_cache(key, :err)
124+
:err
116125
end
117126
end
118127

@@ -131,6 +140,7 @@ defmodule ComponentsGuide.Research.Source do
131140

132141
def html_document_at(url), do: read({:html_document, url})
133142
def json_at(url), do: read({:fetch_json, url})
143+
def content_length(url), do: read({:content_length, url})
134144

135145
def clear_cache() do
136146
Cachex.clear(@cache_name)

apps/components_guide_web/lib/components_guide_web.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,12 @@ defmodule ComponentsGuideWeb do
9696

9797
def markdown!(markdown) do
9898
Earmark.as_html!(markdown)
99-
|> raw()
99+
|> raw()
100100
end
101+
102+
def turbo_frame(id, src, do: block), do: content_tag("turbo-frame", block, id: id, src: src)
103+
def turbo_frame(id, do: block), do: content_tag("turbo-frame", block, id: id)
104+
def turbo_frame(id, src), do: content_tag("turbo-frame", [], id: id, src: src)
101105
end
102106
end
103107

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
defmodule ComponentsGuideWeb.ContentLengthController do
2+
use ComponentsGuideWeb, :controller
3+
alias ComponentsGuide.Research.Source
4+
alias ComponentsGuideWeb.ResearchView, as: View
5+
6+
def index(conn, _params) do
7+
url = "https://unpkg.com/[email protected]/images/svg/twitter.svg"
8+
9+
case Source.content_length(url) do
10+
{:ok, content_length} ->
11+
conn
12+
|> put_root_layout(false)
13+
|> put_layout(false)
14+
|> render("index.html", content_length: content_length)
15+
16+
_ ->
17+
html(conn, "")
18+
end
19+
end
20+
end
21+
22+
defmodule ComponentsGuideWeb.ContentLengthView do
23+
use ComponentsGuideWeb, :view
24+
25+
def render("index.html", assigns) do
26+
turbo_frame("content-length") do
27+
content_tag(:data, "#{assigns.content_length}", value: assigns.content_length)
28+
end
29+
end
30+
end

apps/components_guide_web/lib/components_guide_web/controllers/research_controller.ex

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,8 @@ defmodule ComponentsGuideWeb.ResearchView do
334334
|> Stream.filter(fn {_title, value} -> value != nil end)
335335
|> Enum.map(fn {title, value} ->
336336
content_tag(:div, [
337-
content_tag(:dt, title, class: "font-bold"),
338-
content_tag(:dd, value, class: "pl-4")
337+
content_tag(:dt, title, class: "text-base font-bold"),
338+
content_tag(:dd, value, class: "text-base pl-4")
339339
])
340340
end)
341341

@@ -344,6 +344,8 @@ defmodule ComponentsGuideWeb.ResearchView do
344344
end
345345

346346
defmodule Static do
347+
# use ComponentsGuideWeb, :view
348+
347349
def render(:http_status, {name, description}) do
348350
Section.card([
349351
content_tag(:h3, "HTTP Status: #{name}", class: "text-2xl font-bold"),
@@ -367,8 +369,18 @@ defmodule ComponentsGuideWeb.ResearchView do
367369
def render(:super_tiny_icon, %{name: name, url: url}) do
368370
Section.card([
369371
content_tag(:h3, "#{name |> String.capitalize()} Icon", class: "text-2xl font-bold"),
370-
link(url, to: url, class: "text-base"),
371-
tag(:img, src: url, width: 80, height: 80)
372+
content_tag(
373+
:div,
374+
[
375+
tag(:img, src: url, width: 80, height: 80),
376+
Section.description_list([
377+
{"URL", link(url, to: url, class: "text-base")},
378+
{"Size",
379+
ComponentsGuideWeb.ResearchView.turbo_frame("content-length", "/~/content-length")}
380+
])
381+
],
382+
class: "flex flex-row space-x-4"
383+
)
372384
])
373385
end
374386

apps/components_guide_web/lib/components_guide_web/router.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ defmodule ComponentsGuideWeb.Router do
1818
pipe_through :browser
1919

2020
get "/elements/:element_id", ElementsController, :index
21+
get "/~/content-length", ContentLengthController, :index
2122

2223
get "/", LandingController, :index
2324

apps/components_guide_web/lib/components_guide_web/templates/research/turbo-initial.html.eex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@
1111
</div>
1212

1313
</turbo-frame>
14+
15+
<%= turbo_frame("content-length", "/~/content-length") %>

0 commit comments

Comments
 (0)