Skip to content

Commit a4d2867

Browse files
committed
Add Simple Icons to search
1 parent 6c92c39 commit a4d2867

4 files changed

Lines changed: 104 additions & 7 deletions

File tree

lib/components_guide/application.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ defmodule ComponentsGuide.Application do
2727
start: {Cachex, :start_link, [:content_cache, []]}
2828
},
2929
%{
30-
id: :research_spec_cache,
31-
start: {Cachex, :start_link, [:research_spec_cache, []]}
30+
id: ComponentsGuide.Research.Source.cache_name(),
31+
start: {Cachex, :start_link, [ComponentsGuide.Research.Source.cache_name(), []]}
32+
},
33+
%{
34+
id: ComponentsGuide.Research.Static.Sources.cache_name(),
35+
start: {Cachex, :start_link, [ComponentsGuide.Research.Static.Sources.cache_name(), []]}
3236
},
3337
{Redix, {redis_url_string, [name: :redix_cache]}}
3438
# ComponentsGuide.Worker

lib/components_guide/research/source.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ defmodule ComponentsGuide.Research.Source do
66

77
@cache_name :research_spec_cache
88

9+
def cache_name(), do: @cache_name
10+
911
defp read_cache(key) do
1012
if @cache_enabled do
1113
tuple = {:ok, value} = Cachex.get(@cache_name, key)

lib/components_guide/research/static.ex

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
defmodule ComponentsGuide.Research.Static do
2+
alias ComponentsGuide.Fetch
3+
24
@http_statuses_list [
35
{101, "Switching Protocols", ""},
46
{200, "OK", ""},
@@ -338,6 +340,50 @@ defmodule ComponentsGuide.Research.Static do
338340
]
339341
|> MapSet.new()
340342

343+
defmodule Sources do
344+
@cache_enabled true
345+
@cache_name :static_sources_cache
346+
347+
def cache_name(), do: @cache_name
348+
349+
defp read_cache(key) do
350+
if @cache_enabled do
351+
{:ok, value} = Cachex.get(@cache_name, key)
352+
value
353+
else
354+
nil
355+
end
356+
end
357+
358+
defp write_cache(key, value) do
359+
if @cache_enabled do
360+
Cachex.put(@cache_name, key, value)
361+
end
362+
end
363+
364+
def fetch_simple_icon_names() do
365+
url = "https://unpkg.com/browse/simple-icons@8.5.0/icons/"
366+
367+
case cached = read_cache(url) do
368+
nil ->
369+
link_els =
370+
Fetch.get!(url).body
371+
|> Floki.parse_document!()
372+
|> Floki.find("table tbody td a")
373+
374+
names = for {"a", _attrs, [text]} <- link_els, text != "..", do: text
375+
names = MapSet.new(names)
376+
write_cache(url, names)
377+
names
378+
379+
names ->
380+
names
381+
end
382+
end
383+
end
384+
385+
@simple_icons ComponentsGuide.Research.Static.Sources.fetch_simple_icon_names()
386+
341387
@aliases %{
342388
"redirect" => ["301", "302"],
343389
"invalid" => ["412", "422"],
@@ -348,7 +394,8 @@ defmodule ComponentsGuide.Research.Static do
348394
[
349395
search_for(:http_status, query),
350396
search_for(:rfc, query),
351-
search_for(:super_tiny_icon, query)
397+
search_for(:super_tiny_icon, query),
398+
search_for(:simple_icons, query)
352399
]
353400
|> List.flatten()
354401
end
@@ -395,7 +442,31 @@ defmodule ComponentsGuide.Research.Static do
395442
url: "https://cdn.jsdelivr.net/npm/super-tiny-icons@0.4.0/images/svg/#{query}.svg",
396443
urls: [
397444
"https://cdn.jsdelivr.net/npm/super-tiny-icons@0.4.0/images/svg/#{query}.svg",
398-
"https://unpkg.com/super-tiny-icons@0.4.0/images/svg/#{query}.svg",
445+
"https://unpkg.com/super-tiny-icons@0.4.0/images/svg/#{query}.svg"
446+
]
447+
}}
448+
]
449+
450+
false ->
451+
[]
452+
end
453+
end
454+
455+
defp search_for(:simple_icons, query) when is_binary(query) do
456+
query = query |> String.downcase() |> String.trim()
457+
names = ComponentsGuide.Research.Static.Sources.fetch_simple_icon_names()
458+
# names = @simple_icons
459+
460+
case MapSet.member?(names, query <> ".svg") do
461+
true ->
462+
[
463+
{:simple_icon,
464+
%{
465+
name: query,
466+
url: "https://cdn.jsdelivr.net/npm/simple-icons@8.5.0/icons/#{query}.svg",
467+
urls: [
468+
"https://cdn.jsdelivr.net/npm/simple-icons@8.5.0/icons/#{query}.svg",
469+
"https://unpkg.com/simple-icons@8.5.0/icons/#{query}.svg"
399470
]
400471
}}
401472
]

lib/components_guide_web/controllers/research_controller.ex

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ defmodule ComponentsGuideWeb.ResearchView do
247247
~H"""
248248
<article class="relative mb-4 flex flex-col gap-4 p-4 text-xl text-white bg-violet-900/25 border border-violet-900 rounded shadow-lg">
249249
<h3 class="pr-16 text-2xl"><%= render_slot(@title) %></h3>
250-
<a href={@source_url} class="hover:underline absolute top-0 right-0 mt-4 mr-4 text-sm opacity-75"><%= @source %></a>
250+
<a href={@source_url} class="hover:underline absolute top-0 right-0 mt-4 mr-4 py-1 px-3 text-sm opacity-75 bg-white/10 rounded-full"><%= @source %></a>
251251
<%= render_slot(@inner_block) %>
252252
</article>
253253
"""
@@ -431,8 +431,8 @@ defmodule ComponentsGuideWeb.ResearchView do
431431
def super_tiny_icon(%{name: _, url: _, urls: _} = assigns) do
432432
~H"""
433433
<.card source="Super Tiny Icons" source_url="https://www.supertinyicons.org/">
434-
<:title><%= "#{@name |> String.capitalize()} Icon" %></:title>
435-
<div class="flex flex-row space-x-4">
434+
<:title><%= "#{@name |> String.capitalize()} icon" %></:title>
435+
<div class="flex flex-row space-x-8">
436436
<img src={@url} width={80} height={80}>
437437
<.description_list>
438438
<:item title="URL" data={for(url <- @urls, do: link(url, to: url))} />
@@ -445,6 +445,23 @@ defmodule ComponentsGuideWeb.ResearchView do
445445
"""
446446
end
447447

448+
def simple_icon(%{name: _, url: _, urls: _} = assigns) do
449+
~H"""
450+
<.card source="Simple Icons" source_url="https://simpleicons.org">
451+
<:title><%= "#{@name |> String.capitalize()} icon" %></:title>
452+
<div class="flex flex-row items-center space-x-8">
453+
<img src={@url} width={80} height={80} class="bg-white max-h-max p-2">
454+
<.description_list>
455+
<:item title="URL" data={for(url <- @urls, do: link(url, to: url))} />
456+
<:item title="Size">
457+
<%= client_include("/~/content-length?" <> URI.encode_query(url: @url), "loading…") %>
458+
</:item>
459+
</.description_list>
460+
</div>
461+
</.card>
462+
"""
463+
end
464+
448465
def render_static({type, info}) do
449466
case type do
450467
:http_status ->
@@ -457,6 +474,9 @@ defmodule ComponentsGuideWeb.ResearchView do
457474

458475
:super_tiny_icon ->
459476
super_tiny_icon(info)
477+
478+
:simple_icon ->
479+
simple_icon(info)
460480
end
461481
end
462482

0 commit comments

Comments
 (0)