Current section
Files
Jump to
Current section
Files
src/inlay@spotify.erl
-module(inlay@spotify).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/inlay/spotify.gleam").
-export([detect/1, render/2]).
-file("src/inlay/spotify.gleam", 80).
-spec validate_id(binary(), inlay@embed:spotify_media_type()) -> gleam@option:option(inlay@embed:embed()).
validate_id(Id, Media_type) ->
case string:length(Id) =:= 22 of
true ->
{some, {spotify_media, Media_type, Id}};
false ->
none
end.
-file("src/inlay/spotify.gleam", 68).
-spec detect_spotify(gleam@uri:uri()) -> gleam@option:option(inlay@embed:embed()).
detect_spotify(Url) ->
case gleam@uri:path_segments(erlang:element(6, Url)) of
[<<"track"/utf8>>, Id] ->
validate_id(Id, spotify_track);
[<<"album"/utf8>>, Id@1] ->
validate_id(Id@1, spotify_album);
[<<"playlist"/utf8>>, Id@2] ->
validate_id(Id@2, spotify_playlist);
[<<"artist"/utf8>>, Id@3] ->
validate_id(Id@3, spotify_artist);
[<<"episode"/utf8>>, Id@4] ->
validate_id(Id@4, spotify_episode);
[<<"show"/utf8>>, Id@5] ->
validate_id(Id@5, spotify_show);
_ ->
none
end.
-file("src/inlay/spotify.gleam", 12).
-spec detect(gleam@uri:uri()) -> gleam@option:option(inlay@embed:embed()).
detect(Url) ->
case erlang:element(4, Url) of
{some, <<"open.spotify.com"/utf8>>} ->
detect_spotify(Url);
_ ->
none
end.
-file("src/inlay/spotify.gleam", 90).
-spec media_type_to_string(inlay@embed:spotify_media_type()) -> binary().
media_type_to_string(Media_type) ->
case Media_type of
spotify_track ->
<<"track"/utf8>>;
spotify_album ->
<<"album"/utf8>>;
spotify_playlist ->
<<"playlist"/utf8>>;
spotify_artist ->
<<"artist"/utf8>>;
spotify_episode ->
<<"episode"/utf8>>;
spotify_show ->
<<"show"/utf8>>
end.
-file("src/inlay/spotify.gleam", 19).
-spec render(inlay@embed:embed(), inlay@embed:config()) -> {ok,
lustre@vdom@vnode:element(any())} |
{error, nil}.
render(Embed, Config) ->
case Embed of
{spotify_media, Media_type, Id} ->
Type_str = media_type_to_string(Media_type),
Src = <<<<<<"https://open.spotify.com/embed/"/utf8,
Type_str/binary>>/binary,
"/"/utf8>>/binary,
Id/binary>>,
{Width, Track_height, Other_height} = case erlang:element(4, Config) of
{some, {spotify_config, W, H, Th}} ->
{gleam@option:unwrap(W, 300),
gleam@option:unwrap(Th, 152),
gleam@option:unwrap(H, 352)};
none ->
{300, 152, 352}
end,
Height = case Media_type of
spotify_track ->
Track_height;
spotify_album ->
Other_height;
spotify_playlist ->
Other_height;
spotify_artist ->
Other_height;
spotify_episode ->
Other_height;
spotify_show ->
Other_height
end,
{ok,
lustre@element@html:'div'(
[lustre@attribute:styles(
[{<<"border-radius"/utf8>>, <<"12px"/utf8>>},
{<<"overflow"/utf8>>, <<"hidden"/utf8>>}]
)],
[lustre@element@html:iframe(
[lustre@attribute:src(Src),
lustre@attribute:width(Width),
lustre@attribute:height(Height),
lustre@attribute:attribute(
<<"frameborder"/utf8>>,
<<"0"/utf8>>
),
lustre@attribute:attribute(
<<"allow"/utf8>>,
<<"autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture"/utf8>>
),
lustre@attribute:attribute(
<<"loading"/utf8>>,
<<"lazy"/utf8>>
)]
)]
)};
_ ->
{error, nil}
end.