Skip to content

Commit 8a974ef

Browse files
authored
Collapse Nothing and empty string for the search query. They were already handled the same way (#981)
* Collapse Nothing and empty string for the search query. They were already handled the same way * Simplify code
1 parent 291086c commit 8a974ef

File tree

3 files changed

+76
-88
lines changed

3 files changed

+76
-88
lines changed

frontend/src/Main.elm

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ attemptQuery (( model, _ ) as pair) =
152152
model.nixosChannels
153153
searchModel.searchType
154154
searchModel.channel
155-
(Maybe.withDefault "" searchModel.query)
155+
searchModel.query
156156
searchModel.from
157157
searchModel.size
158158
searchModel.buckets
@@ -351,33 +351,28 @@ view :
351351
}
352352
view model =
353353
let
354-
maybeQuery m =
355-
case m.query of
356-
Nothing ->
357-
""
354+
maybeQuery q =
355+
if String.isEmpty q then
356+
""
358357

359-
Just q ->
360-
if String.isEmpty q then
361-
""
362-
363-
else
364-
" - " ++ q
358+
else
359+
" - " ++ q
365360

366361
maybeFlakeQuery m =
367362
case m of
368363
OptionModel m_ ->
369-
maybeQuery m_
364+
maybeQuery m_.query
370365

371366
PackagesModel m_ ->
372-
maybeQuery m_
367+
maybeQuery m_.query
373368

374369
title =
375370
case model.page of
376371
Packages m ->
377-
"NixOS Search - Packages" ++ maybeQuery m
372+
"NixOS Search - Packages" ++ maybeQuery m.query
378373

379374
Options m ->
380-
"NixOS Search - Options" ++ maybeQuery m
375+
"NixOS Search - Options" ++ maybeQuery m.query
381376

382377
Flakes m ->
383378
"NixOS Search - Flakes (Experimental)" ++ maybeFlakeQuery m
@@ -467,7 +462,8 @@ viewNavigation route =
467462
[ ( Route.Packages searchArgs, text "Packages" )
468463
, ( Route.Options searchArgs, text "NixOS options" )
469464
, ( Route.Flakes searchArgs, span [] [ text "Flakes", sup [] [ span [ class "label label-info" ] [ small [] [ text "Experimental" ] ] ] ] )
470-
] ++ [ li [] [ a [ href "https://wiki.nixos.org" ] [ text "NixOS Wiki" ] ] ]
465+
]
466+
++ [ li [] [ a [ href "https://wiki.nixos.org" ] [ text "NixOS Wiki" ] ] ]
471467

472468

473469
viewNavigationItem :

frontend/src/Page/Packages.elm

Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ viewResultItem nixosChannels channel showInstallDetails show item =
377377
[ text "Name: "
378378
, code [ class "package-name" ] [ text item.source.pname ]
379379
]
380-
:: (optionals (item.source.pversion /= "")
380+
:: (optionals (not (String.isEmpty item.source.pversion))
381381
[ li []
382382
[ text "Version: "
383383
, strong [] [ text item.source.pversion ]
@@ -414,31 +414,26 @@ viewResultItem nixosChannels channel showInstallDetails show item =
414414
item.source.licenses
415415
|> List.filterMap
416416
(\license ->
417-
case ( license.fullName, license.url ) of
418-
( Nothing, Nothing ) ->
419-
Nothing
420-
421-
( Just fullName, Nothing ) ->
422-
Just (text fullName)
423-
424-
( Nothing, Just url ) ->
425-
Just (createShortDetailsItem "Unknown" url)
426-
427-
( Just fullName, Just url ) ->
428-
Just (createShortDetailsItem fullName url)
417+
case license.url of
418+
Nothing ->
419+
Maybe.map text license.fullName
420+
421+
Just url ->
422+
Just
423+
(createShortDetailsItem
424+
(Maybe.withDefault "Unknown" license.fullName)
425+
url
426+
)
429427
)
430428
in
431429
optionals (not (List.isEmpty licenses))
432430
[ li []
433431
(text
434-
("License"
435-
++ (if List.length licenses == 1 then
436-
""
437-
438-
else
439-
"s"
440-
)
441-
++ ": "
432+
(if List.length licenses == 1 then
433+
"License: "
434+
435+
else
436+
"Licenses: "
442437
)
443438
:: List.intersperse (text "") licenses
444439
)
@@ -510,15 +505,15 @@ viewResultItem nixosChannels channel showInstallDetails show item =
510505
]
511506
in
512507
li [] <|
513-
case team.githubTeams of
514-
Just ((_ :: _) as nonEmptyList) ->
508+
case Maybe.withDefault [] team.githubTeams of
509+
[] ->
510+
text team.shortName :: scope
511+
512+
githubTeams ->
515513
text (team.shortName ++ ":")
516-
:: List.concatMap showTeamEntry nonEmptyList
514+
:: List.concatMap showTeamEntry githubTeams
517515
++ scope
518516

519-
_ ->
520-
text team.shortName :: scope
521-
522517
mailtoAllMaintainers maintainers =
523518
let
524519
maintainerMails =
@@ -547,38 +542,31 @@ viewResultItem nixosChannels channel showInstallDetails show item =
547542
maintainersTeamsAndPlatforms =
548543
div []
549544
[ div []
550-
(List.append [ h4 [] [ text "Maintainers" ] ]
551-
(if List.isEmpty item.source.maintainers then
552-
[ p [] [ text "This package has no maintainers. If you find it useful, please consider becoming a maintainer!" ] ]
553-
554-
else
555-
[ ul []
556-
(List.map showMaintainer item.source.maintainers
557-
++ mailtoAllMaintainers item.source.maintainers
558-
++ linkAllMaintainers item.source.maintainers
559-
)
560-
]
561-
)
562-
)
545+
[ h4 [] [ text "Maintainers" ]
546+
, if List.isEmpty item.source.maintainers then
547+
p [] [ text "This package has no maintainers. If you find it useful, please consider becoming a maintainer!" ]
548+
549+
else
550+
ul []
551+
(List.map showMaintainer item.source.maintainers
552+
++ mailtoAllMaintainers item.source.maintainers
553+
++ linkAllMaintainers item.source.maintainers
554+
)
555+
]
563556
, div []
564-
(if not (List.isEmpty item.source.teams) then
557+
(optionals (not (List.isEmpty item.source.teams))
565558
[ h4 [] [ text "Teams" ]
566-
, ul []
567-
(List.map showTeam item.source.teams)
559+
, ul [] (List.map showTeam item.source.teams)
568560
]
569-
570-
else
571-
[]
572561
)
573562
, div []
574-
(List.append [ h4 [] [ text "Platforms" ] ]
575-
(if List.isEmpty item.source.platforms then
576-
[ p [] [ text "This package does not list its available platforms." ] ]
563+
[ h4 [] [ text "Platforms" ]
564+
, if List.isEmpty item.source.platforms then
565+
p [] [ text "This package does not list its available platforms." ]
577566

578-
else
579-
[ ul [] (List.map showPlatform (List.sort item.source.platforms)) ]
580-
)
581-
)
567+
else
568+
ul [] (List.map showPlatform (List.sort item.source.platforms))
569+
]
582570
]
583571

584572
programs =

frontend/src/Search.elm

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ import Task
9494
type alias Model a b =
9595
{ channel : String
9696
, flake : String
97-
, query : Maybe String
97+
, query : String
9898
, result : RemoteData.WebData (SearchResult a b)
9999
, show : Maybe String
100100
, from : Int
@@ -303,16 +303,15 @@ init args defaultNixOSChannel nixosChannels maybeModel =
303303
|> Maybe.withDefault modelChannel
304304
, flake = defaultFlakeId
305305
, query =
306-
args.query
307-
|> Maybe.andThen Route.SearchQuery.searchQueryToString
308-
|> (\x ->
309-
case x of
310-
Just q ->
311-
Just q
312-
313-
Nothing ->
314-
args.show
315-
)
306+
case
307+
args.query
308+
|> Maybe.andThen Route.SearchQuery.searchQueryToString
309+
of
310+
Just q ->
311+
q
312+
313+
Nothing ->
314+
args.show |> Maybe.withDefault ""
316315
, result = getField .result RemoteData.NotAsked
317316
, show = args.show
318317
, from =
@@ -353,7 +352,7 @@ ensureLoading nixosChannels model =
353352
channels =
354353
List.map .id nixosChannels
355354
in
356-
if model.query /= Nothing && model.query /= Just "" && List.member model.channel channels then
355+
if not (String.isEmpty model.query) && List.member model.channel channels then
357356
{ model | result = RemoteData.Loading }
358357

359358
else
@@ -440,7 +439,7 @@ update toRoute navKey msg model nixosChannels =
440439
BucketsChange buckets ->
441440
{ model
442441
| buckets =
443-
if buckets == "" then
442+
if String.isEmpty buckets then
444443
Nothing
445444

446445
else
@@ -472,7 +471,7 @@ update toRoute navKey msg model nixosChannels =
472471
|> pushUrl toRoute navKey
473472

474473
QueryInput query ->
475-
( { model | query = Just query }
474+
( { model | query = query }
476475
, Cmd.none
477476
)
478477

@@ -520,7 +519,7 @@ pushUrl :
520519
-> ( Model a b, Cmd msg )
521520
pushUrl toRoute navKey model =
522521
( model
523-
, if model.query == Nothing || model.query == Just "" then
522+
, if String.isEmpty model.query then
524523
Cmd.none
525524

526525
else
@@ -536,7 +535,12 @@ createUrl toRoute model =
536535
Route.routeToString <|
537536
toRoute
538537
{ channel = Just model.channel
539-
, query = Maybe.map Route.SearchQuery.toSearchQuery model.query
538+
, query =
539+
if String.isEmpty model.query then
540+
Nothing
541+
542+
else
543+
Just (Route.SearchQuery.toSearchQuery model.query)
540544
, show = model.show
541545
, from = Just model.from
542546
, size = Just model.size
@@ -820,7 +824,7 @@ viewResult nixosChannels outMsg toRoute categoryName model viewSuccess viewBucke
820824
if result.hits.total.value == 0 && List.isEmpty buckets then
821825
div [ class "search-results" ]
822826
[ ul [ class "search-sidebar" ] searchBuckets
823-
, viewNoResults categoryName (Maybe.withDefault "" model.query)
827+
, viewNoResults categoryName model.query
824828
]
825829

826830
else if not (List.isEmpty buckets) then
@@ -937,7 +941,7 @@ viewSearchInput :
937941
-> (Msg a b -> c)
938942
-> String
939943
-> Maybe String
940-
-> Maybe String
944+
-> String
941945
-> Html c
942946
viewSearchInput nixosChannels outMsg categoryName selectedChannel searchQuery =
943947
form
@@ -952,7 +956,7 @@ viewSearchInput nixosChannels outMsg categoryName selectedChannel searchQuery =
952956
, autofocus True
953957
, placeholder <| "Search for " ++ categoryName
954958
, onInput (outMsg << QueryInput)
955-
, value <| Maybe.withDefault "" searchQuery
959+
, value searchQuery
956960
]
957961
[]
958962
]

0 commit comments

Comments
 (0)