Skip to content

Commit 22efb88

Browse files
committed
Show byte size of icons
1 parent 907cacb commit 22efb88

File tree

6 files changed

+88
-72
lines changed

6 files changed

+88
-72
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,10 @@ defmodule ComponentsGuide.Research.Source do
107107
end
108108

109109
defp body({:content_length, url}) do
110-
with {:ok, response} <- read({:fetch, url}) do
111-
{:ok, Mojito.Headers.get(response.headers, "content-length")}
110+
with {:ok, response} <- Mojito.request(method: :head, url: url, timeout: 50000),
111+
s when is_binary(s) <- Mojito.Headers.get(response.headers, "content-length"),
112+
{n, _} <- Integer.parse(s) do
113+
{:ok, n}
112114
else
113115
_ -> :err
114116
end

apps/components_guide_web/lib/components_guide_web.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ defmodule ComponentsGuideWeb do
9393

9494
import Paredown
9595
alias ComponentsGuideWeb.StylingHelpers, as: Styling
96+
alias ComponentsGuideWeb.FormattingHelpers, as: Format
9697

9798
def markdown!(markdown) do
9899
Earmark.as_html!(markdown)

apps/components_guide_web/lib/components_guide_web/controllers/content_length_controller.ex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ defmodule ComponentsGuideWeb.ContentLengthController do
33
alias ComponentsGuide.Research.Source
44
alias ComponentsGuideWeb.ResearchView, as: View
55

6-
def index(conn, _params) do
7-
url = "https://unpkg.com/[email protected]/images/svg/twitter.svg"
8-
6+
def index(conn, %{"url" => "https://unpkg.com/" <> _ = url}) do
97
case Source.content_length(url) do
108
{:ok, content_length} ->
119
conn
@@ -14,7 +12,7 @@ defmodule ComponentsGuideWeb.ContentLengthController do
1412
|> render("index.html", content_length: content_length)
1513

1614
_ ->
17-
html(conn, "")
15+
html(conn, "")
1816
end
1917
end
2018
end
@@ -24,7 +22,9 @@ defmodule ComponentsGuideWeb.ContentLengthView do
2422

2523
def render("index.html", assigns) do
2624
turbo_frame("content-length") do
27-
content_tag(:data, "#{assigns.content_length}", value: assigns.content_length)
25+
content_tag(:data, Format.humanize_bytes(assigns.content_length),
26+
value: assigns.content_length
27+
)
2828
end
2929
end
3030
end

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

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -279,35 +279,8 @@ defmodule ComponentsGuideWeb.ResearchView do
279279
"""
280280
end
281281

282-
defdelegate float_to_string(f, options), to: :erlang, as: :float_to_binary
283-
284-
def humanize_bytes(count) when count >= 1024 * 1024 do
285-
megabytes = count / (1024 * 1024)
286-
"#{float_to_string(megabytes, decimals: 1)} mB"
287-
end
288-
289-
def humanize_bytes(count) when count >= 1024 do
290-
kilobytes = count / 1024
291-
"#{float_to_string(kilobytes, decimals: 1)} kB"
292-
end
293-
294-
def humanize_bytes(count) when is_integer(count) do
295-
"#{count} B"
296-
end
297-
298-
def humanize_count(count) when count >= 1_000_000 do
299-
formatted = count / 1_000_000
300-
"#{float_to_string(formatted, decimals: 1)}M"
301-
end
302-
303-
def humanize_count(count) when count >= 1000 do
304-
formatted = count / 1000
305-
"#{float_to_string(formatted, decimals: 1)}K"
306-
end
307-
308-
def humanize_count(count) when is_integer(count) do
309-
"#{count}"
310-
end
282+
defdelegate humanize_bytes(count), to: Format
283+
defdelegate humanize_count(count), to: Format
311284

312285
defmodule Section do
313286
def card(children) do
@@ -376,7 +349,10 @@ defmodule ComponentsGuideWeb.ResearchView do
376349
Section.description_list([
377350
{"URL", link(url, to: url, class: "text-base")},
378351
{"Size",
379-
ComponentsGuideWeb.ResearchView.turbo_frame("content-length", "/~/content-length")}
352+
ComponentsGuideWeb.ResearchView.turbo_frame(
353+
"content-length",
354+
"/~/content-length?" <> URI.encode_query(url: url)
355+
)}
380356
])
381357
],
382358
class: "flex flex-row space-x-4"

apps/components_guide_web/lib/components_guide_web/router.ex

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,63 @@ defmodule ComponentsGuideWeb.Router do
22
use ComponentsGuideWeb, :router
33

44
pipeline :browser do
5-
plug :accepts, ["html"]
6-
plug :fetch_session
7-
plug :fetch_live_flash
8-
plug :put_root_layout, {ComponentsGuideWeb.LayoutView, :root}
9-
plug :protect_from_forgery
10-
plug :put_secure_browser_headers
5+
plug(:accepts, ["html"])
6+
plug(:fetch_session)
7+
plug(:fetch_live_flash)
8+
plug(:put_root_layout, {ComponentsGuideWeb.LayoutView, :root})
9+
plug(:protect_from_forgery)
10+
plug(:put_secure_browser_headers)
1111
end
1212

1313
pipeline :api do
14-
plug :accepts, ["json"]
14+
plug(:accepts, ["json"])
1515
end
1616

1717
scope "/", ComponentsGuideWeb do
18-
pipe_through :browser
19-
20-
get "/elements/:element_id", ElementsController, :index
21-
get "/~/content-length", ContentLengthController, :index
18+
pipe_through(:browser)
2219

23-
get "/", LandingController, :index
20+
get("/elements/:element_id", ElementsController, :index)
21+
get("/~/content-length", ContentLengthController, :index)
2422

25-
get "/research", ResearchController, :index
23+
get("/", LandingController, :index)
2624

27-
get "/concepts", ConceptsController, :index
25+
get("/research", ResearchController, :index)
2826

29-
get "/links", LinksController, :index
27+
get("/concepts", ConceptsController, :index)
3028

31-
resources "/accessibility-first",
32-
AccessibilityFirstController,
33-
only: [:index, :show]
29+
get("/links", LinksController, :index)
3430

35-
resources "/composable-systems",
36-
ComposableSystemsController,
37-
only: [:index, :show]
31+
resources(
32+
"/accessibility-first",
33+
AccessibilityFirstController,
34+
only: [:index, :show]
35+
)
3836

39-
resources "/web-standards",
40-
WebStandardsController,
41-
only: [:index, :show]
37+
resources(
38+
"/composable-systems",
39+
ComposableSystemsController,
40+
only: [:index, :show]
41+
)
4242

43-
live "/color", ColorLive, :index
44-
live "/color/:definition", ColorLive, :show
45-
live "/color/lab/:definition", ColorLive, :lab
43+
resources(
44+
"/web-standards",
45+
WebStandardsController,
46+
only: [:index, :show]
47+
)
4648

47-
get "/swiftui", SwiftUIController, :index
49+
live("/color", ColorLive, :index)
50+
live("/color/:definition", ColorLive, :show)
51+
live("/color/lab/:definition", ColorLive, :lab)
4852

49-
get "/react+typescript", ReactTypescriptController, :index
50-
get "/react+typescript/:article", ReactTypescriptController, :show
53+
get("/swiftui", SwiftUIController, :index)
5154

52-
resources "/text", TextController
53-
get "/text/:id/text/:format", TextController, :show_text_format
55+
get("/react+typescript", ReactTypescriptController, :index)
56+
get("/react+typescript/:article", ReactTypescriptController, :show)
5457

55-
get "/fake-search", FakeSearchController, :index
58+
resources("/text", TextController)
59+
get("/text/:id/text/:format", TextController, :show_text_format)
60+
61+
get("/fake-search", FakeSearchController, :index)
5662
end
5763

5864
# Other scopes may use custom stacks.
@@ -71,8 +77,8 @@ defmodule ComponentsGuideWeb.Router do
7177
import Phoenix.LiveDashboard.Router
7278

7379
scope "/" do
74-
pipe_through :browser
75-
live_dashboard "/dashboard", metrics: ComponentsGuideWeb.Telemetry
80+
pipe_through(:browser)
81+
live_dashboard("/dashboard", metrics: ComponentsGuideWeb.Telemetry)
7682
end
7783
end
7884
end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
defmodule ComponentsGuideWeb.FormattingHelpers do
2+
defdelegate float_to_string(f, options), to: :erlang, as: :float_to_binary
3+
4+
def humanize_bytes(count) when is_integer(count) and count >= 1024 * 1024 do
5+
megabytes = count / (1024 * 1024)
6+
"#{float_to_string(megabytes, decimals: 1)} mB"
7+
end
8+
9+
def humanize_bytes(count) when is_integer(count) and count >= 1024 do
10+
kilobytes = count / 1024
11+
"#{float_to_string(kilobytes, decimals: 1)} kB"
12+
end
13+
14+
def humanize_bytes(count) when is_integer(count) do
15+
"#{count} B"
16+
end
17+
18+
def humanize_count(count) when is_integer(count) and count >= 1_000_000 do
19+
formatted = count / 1_000_000
20+
"#{float_to_string(formatted, decimals: 1)}M"
21+
end
22+
23+
def humanize_count(count) when is_integer(count) and count >= 1000 do
24+
formatted = count / 1000
25+
"#{float_to_string(formatted, decimals: 1)}K"
26+
end
27+
28+
def humanize_count(count) when is_integer(count) do
29+
"#{count}"
30+
end
31+
end

0 commit comments

Comments
 (0)