Skip to content

Commit

Permalink
Action/Server: fall back to the original target for result
Browse files Browse the repository at this point in the history
Sometimes all targets in the target lists are filtered out by the
mapMaybe in `showFromsLogic`, in that case we just fall back to the
head of the original result.
  • Loading branch information
Profpatsch committed Oct 11, 2022
1 parent a1ccb7d commit 7ee31dd
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/Action/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ import Data.Function ((&))
import Data.Functor ((<&>))
import Data.Traversable (for)
import Control.Category ((>>>))
import Data.List.NonEmpty (nonEmpty)
import qualified Data.List.NonEmpty as NonEmpty

actionServer :: CmdLine -> IO ()
actionServer cmd@Server{..} = do
Expand Down Expand Up @@ -211,8 +213,14 @@ showResults urlOpts links args query results = do
when (null results) $ H.p "No results found"
forM_ results $ \result -> do
let dat = showFromsLogic result
-- TODO: this crashes if there’s no targets
let Target{..} : _ = dat <&> showsFromFirstTarget
let Target{..} =
((dat <&> showsFromFirstTarget)
-- In case showsFromLogic filters out all targets because they are missing fields,
-- fall back to the original first target in the target list.
<|> result)
& nonEmpty & \case
Nothing -> error "showResults: The search result had an empty target list, this should not happen."
Just tgt -> NonEmpty.head tgt
H.div ! H.class_ "result" $ do
H.div ! H.class_ "ans" $ do
H.a ! H.href (H.stringValue $ showURL urlOpts targetURL) $
Expand Down

0 comments on commit 7ee31dd

Please sign in to comment.