Skip to content

Commit 91efe7b

Browse files
committed
display stargazers
1 parent 55971b6 commit 91efe7b

File tree

1 file changed

+71
-15
lines changed

1 file changed

+71
-15
lines changed

src/Project.elm

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Project exposing (DisplayType(..), Language(..), Project, getProjects, la
22

33
-- Represents a single "project"
44

5+
import Ansi.Log exposing (LineDiscipline(..))
56
import BackendTask exposing (BackendTask)
67
import BackendTask.Env
78
import BackendTask.Glob as Glob
@@ -41,6 +42,10 @@ type alias Project =
4142
type alias ProcessedData =
4243
{ imgPath : String
4344
, year : Year
45+
46+
-- only add github stars if the project has a github repo and it's above 5 stars (arbitrary)
47+
-- kinda embarassing to display star count if it's THAT low...
48+
, stargazers : Maybe Int
4449
}
4550

4651

@@ -120,6 +125,7 @@ addProcessedData decodedProj =
120125
BackendTask.succeed ProcessedData
121126
|> BackendTask.andMap (addImagePath decodedProj)
122127
|> BackendTask.andMap (addYear decodedProj)
128+
|> BackendTask.andMap (addStargazers decodedProj)
123129
|> BackendTask.map
124130
(\processed ->
125131
{ raw = decodedProj
@@ -139,6 +145,46 @@ getEnvs =
139145
|> BackendTask.andMap (BackendTask.Env.get "GITHUB_TOKEN")
140146

141147

148+
githubApiAuthHeader : Env -> List ( String, String )
149+
githubApiAuthHeader env =
150+
case env.githubToken of
151+
Just token ->
152+
[ ( "Authorization", "token " ++ token ) ]
153+
154+
Nothing ->
155+
[]
156+
157+
158+
159+
-- it's ok to re-fetch the github API since the get requests are cached
160+
161+
162+
addStargazers : RawData -> BackendTask FatalError (Maybe Int)
163+
addStargazers proj =
164+
case proj.githubRepo of
165+
Just repoName ->
166+
getEnvs
167+
|> BackendTask.andThen (getStargazers repoName)
168+
169+
Nothing ->
170+
BackendTask.succeed Nothing
171+
172+
173+
getStargazers : String -> Env -> BackendTask FatalError (Maybe Int)
174+
getStargazers repoName env =
175+
BackendTask.Http.getWithOptions
176+
{ url = "https://api.github.com/repos/" ++ repoName
177+
, expect = BackendTask.Http.expectJson (Json.Decode.field "stargazers_count" Json.Decode.int)
178+
, headers = githubApiAuthHeader env
179+
, cacheStrategy = Nothing
180+
, retries = Nothing
181+
, timeoutInMs = Nothing
182+
, cachePath = Nothing
183+
}
184+
|> BackendTask.allowFatal
185+
|> BackendTask.map Just
186+
187+
142188

143189
-- if the project doesn't have a "year" field, scrape the github API
144190
-- to get the date of the first commit and latest commit, and use that to create a range
@@ -169,19 +215,11 @@ getYearFromGithub repoName env =
169215
Json.Decode.map2 Range
170216
(Json.Decode.field "created_at" dateDecoder)
171217
(Json.Decode.field "pushed_at" dateDecoder)
172-
173-
headers =
174-
case env.githubToken of
175-
Just token ->
176-
[ ( "Authorization", "token " ++ token ) ]
177-
178-
Nothing ->
179-
[]
180218
in
181219
BackendTask.Http.getWithOptions
182220
{ url = "https://api.github.com/repos/" ++ repoName
183221
, expect = BackendTask.Http.expectJson yearDecoder
184-
, headers = headers
222+
, headers = githubApiAuthHeader env
185223
, cacheStrategy = Nothing
186224
, retries = Nothing
187225
, timeoutInMs = Nothing
@@ -421,7 +459,11 @@ view proj =
421459
]
422460
]
423461
[ projectTitle proj.raw
424-
, projectYear proj.processed.year
462+
, Html.p
463+
[ css [ fontSize (em 0.75) ] ]
464+
[ projectStars proj.processed.stargazers
465+
, projectYear proj.processed.year
466+
]
425467
, Html.p [] [ Html.text proj.raw.blurb ]
426468
, languagesAndConcepts Util.Row { languages = proj.raw.languages, concepts = proj.raw.concepts }
427469
]
@@ -446,7 +488,11 @@ viewFeatured proj =
446488
}
447489
[ padding2 (px 0) (em 1) ]
448490
, projectTitle proj.raw
449-
, projectYear proj.processed.year
491+
, Html.p
492+
[ css [ fontSize (em 0.75) ] ]
493+
[ projectStars proj.processed.stargazers
494+
, projectYear proj.processed.year
495+
]
450496
, Html.p [] [ Html.text proj.raw.blurb ]
451497

452498
-- height-filling empty div to align the languages/concepts and links to the bottom
@@ -499,6 +545,18 @@ projectTitle raw =
499545
[ Html.text raw.name ]
500546

501547

548+
projectStars : Maybe Int -> Html msg
549+
projectStars stargazers =
550+
case stargazers of
551+
Just stars ->
552+
Html.span
553+
[]
554+
[ Html.text <| String.fromInt stars ++ " ⭐ ⋅ " ]
555+
556+
Nothing ->
557+
Html.text ""
558+
559+
502560
projectYear : Year -> Html msg
503561
projectYear year =
504562
let
@@ -528,10 +586,8 @@ projectYear year =
528586
Range start end ->
529587
displayRange start end
530588
in
531-
Html.p
532-
[ css
533-
[ fontSize (em 0.75) ]
534-
, case year of
589+
Html.span
590+
[ case year of
535591
Manual _ ->
536592
Html.Styled.Attributes.attribute "data-datetype" "manual"
537593

0 commit comments

Comments
 (0)