@@ -2,6 +2,7 @@ module Project exposing (DisplayType(..), Language(..), Project, getProjects, la
2
2
3
3
-- Represents a single "project"
4
4
5
+ import Ansi.Log exposing (LineDiscipline (..) )
5
6
import BackendTask exposing (BackendTask )
6
7
import BackendTask.Env
7
8
import BackendTask.Glob as Glob
@@ -41,6 +42,10 @@ type alias Project =
41
42
type alias ProcessedData =
42
43
{ imgPath : String
43
44
, 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
44
49
}
45
50
46
51
@@ -120,6 +125,7 @@ addProcessedData decodedProj =
120
125
BackendTask . succeed ProcessedData
121
126
|> BackendTask . andMap ( addImagePath decodedProj)
122
127
|> BackendTask . andMap ( addYear decodedProj)
128
+ |> BackendTask . andMap ( addStargazers decodedProj)
123
129
|> BackendTask . map
124
130
( \ processed ->
125
131
{ raw = decodedProj
@@ -139,6 +145,46 @@ getEnvs =
139
145
|> BackendTask . andMap ( BackendTask . Env . get " GITHUB_TOKEN" )
140
146
141
147
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
+
142
188
143
189
-- if the project doesn't have a "year" field, scrape the github API
144
190
-- 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 =
169
215
Json . Decode . map2 Range
170
216
( Json . Decode . field " created_at" dateDecoder)
171
217
( 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
- []
180
218
in
181
219
BackendTask . Http . getWithOptions
182
220
{ url = " https://api.github.com/repos/" ++ repoName
183
221
, expect = BackendTask . Http . expectJson yearDecoder
184
- , headers = headers
222
+ , headers = githubApiAuthHeader env
185
223
, cacheStrategy = Nothing
186
224
, retries = Nothing
187
225
, timeoutInMs = Nothing
@@ -421,7 +459,11 @@ view proj =
421
459
]
422
460
]
423
461
[ 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
+ ]
425
467
, Html . p [] [ Html . text proj. raw. blurb ]
426
468
, languagesAndConcepts Util . Row { languages = proj. raw. languages, concepts = proj. raw. concepts }
427
469
]
@@ -446,7 +488,11 @@ viewFeatured proj =
446
488
}
447
489
[ padding2 ( px 0 ) ( em 1 ) ]
448
490
, 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
+ ]
450
496
, Html . p [] [ Html . text proj. raw. blurb ]
451
497
452
498
-- height-filling empty div to align the languages/concepts and links to the bottom
@@ -499,6 +545,18 @@ projectTitle raw =
499
545
[ Html . text raw. name ]
500
546
501
547
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
+
502
560
projectYear : Year -> Html msg
503
561
projectYear year =
504
562
let
@@ -528,10 +586,8 @@ projectYear year =
528
586
Range start end ->
529
587
displayRange start end
530
588
in
531
- Html . p
532
- [ css
533
- [ fontSize ( em 0.75 ) ]
534
- , case year of
589
+ Html . span
590
+ [ case year of
535
591
Manual _ ->
536
592
Html . Styled . Attributes . attribute " data-datetype" " manual"
537
593
0 commit comments