11-- | This module contains a Halogen component for search results.
22module Docs.Search.App.SearchResults where
33
4- import Prelude
5-
64import Docs.Search.App.SearchField (SearchFieldMessage (..))
5+ import Docs.Search.BrowserEngine (PartialIndex , browserSearchEngine )
76import Docs.Search.Config (config )
87import Docs.Search.Declarations (DeclLevel (..), declLevelToHashAnchor )
98import Docs.Search.DocsJson (DataDeclType (..))
10- import Docs.Search.Extra ((>#>), homePageFromRepository )
9+ import Docs.Search.Extra (homePageFromRepository , (>#>))
10+ import Docs.Search.PackageIndex (PackageResult )
11+ import Docs.Search.Engine (Result (..))
12+ import Docs.Search.Engine as Engine
1113import Docs.Search.SearchResult (ResultInfo (..), SearchResult (..))
1214import Docs.Search.TypeDecoder (Constraint (..), FunDep (..), FunDeps (..), Kind (..), QualifiedName (..), Type (..), TypeArgument (..), joinForAlls , joinRows )
13- import Docs.Search.Engine as SearchEngine
14- import Docs.Search.Engine (ResultsType (..))
15+ import Docs.Search.TypeIndex (TypeIndex )
16+
17+ import Prelude
1518
1619import Data.Array ((!!))
1720import Data.Array as Array
1821import Data.List as List
19- import Data.Maybe (Maybe (..), isJust )
22+ import Data.Maybe (Maybe (..), isJust , fromMaybe )
2023import Data.Newtype (wrap )
2124import Data.String.CodeUnits (stripSuffix ) as String
2225import Data.String.Common (null , trim ) as String
@@ -40,9 +43,12 @@ data Mode = Off | Loading | Active
4043derive instance eqMode :: Eq Mode
4144
4245
43- type State = { searchEngineState :: SearchEngine.State
44- , results :: Array SearchResult
45- , resultsType :: ResultsType
46+ type EngineState =
47+ Engine.EngineState PartialIndex TypeIndex
48+
49+
50+ type State = { engineState :: EngineState
51+ , results :: Array Result
4652 , input :: String
4753 , contents :: Element
4854 , resultsCount :: Int
@@ -62,14 +68,14 @@ data Action
6268
6369mkComponent
6470 :: forall o i
65- . Element
71+ . EngineState
72+ -> Element
6673 -> MD.MarkdownIt
6774 -> H.Component HH.HTML Query i o Aff
68- mkComponent contents markdownIt =
75+ mkComponent initialEngineState contents markdownIt =
6976 H .mkComponent
70- { initialState: const { searchEngineState: mempty
77+ { initialState: const { engineState: initialEngineState
7178 , results: []
72- , resultsType: DeclResults
7379 , input: " "
7480 , contents
7581 , resultsCount: config.resultsCount
@@ -107,12 +113,14 @@ handleQuery (MessageFromSearchField (InputUpdated input_) next) = do
107113 H .modify_ (_ { mode = Loading , resultsCount = config.resultsCount })
108114
109115 void $ H .fork do
110- { searchEngineState, results, resultsType } <- H .liftAff $
111- SearchEngine .query state.searchEngineState state.input
116+ { index, results } <- H .liftAff $
117+ Engine .query browserSearchEngine state.engineState state.input
118+
112119 H .modify_ (_ { results = results
113120 , mode = Active
114- , searchEngineState = searchEngineState
115- , resultsType = resultsType })
121+ , engineState = index
122+ }
123+ )
116124
117125 hidePageContents
118126
@@ -186,15 +194,6 @@ render state@{ mode: Active } =
186194 renderContainer $
187195 [ HH .h1_ [ HH .text " Search results" ]
188196
189- , HH .div [ HP .classes [ wrap " result" ] ] $
190- [ HH .text " Found "
191- , HH .strong_ [ HH .text $ show $ Array .length state.results ]
192- , HH .text $
193- case state.resultsType of
194- DeclResults -> " definitions."
195- TypeResults _ -> " definitions with similar types."
196- ]
197-
198197 , HH .div_ $
199198 Array .concat $ shownResults <#> renderResult state.markdownIt
200199
@@ -227,11 +226,49 @@ renderSummary text =
227226
228227
229228renderResult
229+ :: forall a
230+ . MD.MarkdownIt
231+ -> Result
232+ -> Array (HH.HTML a Action )
233+ renderResult markdownIt (DeclResult sr) = renderSearchResult markdownIt sr
234+ renderResult markdownIt (TypeResult sr) = renderSearchResult markdownIt sr
235+ renderResult markdownIt (PackResult sr) = renderPackageResult sr
236+
237+
238+ renderPackageResult
239+ :: forall a
240+ . PackageResult
241+ -> Array (HH.HTML a Action )
242+ renderPackageResult { name, description, repository } =
243+ [ HH .div [ HP .class_ (wrap " result" ) ]
244+ [ HH .h3 [ HP .class_ (wrap " result__title" ) ]
245+ [ HH .span [ HP .classes [ wrap " result__badge"
246+ , wrap " badge"
247+ , wrap " badge--package" ]
248+ , HP .title " Package"
249+ ]
250+ [ HH .text " P" ]
251+
252+ , HH .a [ HP .class_ (wrap " result__link" )
253+ , HP .href $ fromMaybe " " repository # homePageFromRepository
254+ ]
255+ [ HH .text name ]
256+ ]
257+ ]
258+ ] <>
259+
260+ description >#> \descriptionText ->
261+ [ HH .div [ HP .class_ (wrap " result__body" ) ]
262+ [ HH .text descriptionText ]
263+ ]
264+
265+
266+ renderSearchResult
230267 :: forall a
231268 . MD.MarkdownIt
232269 -> SearchResult
233270 -> Array (HH.HTML a Action )
234- renderResult markdownIt (SearchResult result) =
271+ renderSearchResult markdownIt (SearchResult result) =
235272 -- class names here and below are from Pursuit.
236273 [ HH .div [ HP .class_ (wrap " result" ) ]
237274 [ HH .h3 [ HP .class_ (wrap " result__title" ) ]
@@ -274,30 +311,6 @@ renderResult markdownIt (SearchResult result) =
274311 ]
275312 ]
276313
277- renderResult _markdownIt (PackageResult { name, description, repository }) =
278- [ HH .div [ HP .class_ (wrap " result" ) ]
279- [ HH .h3 [ HP .class_ (wrap " result__title" ) ]
280- [ HH .span [ HP .classes [ wrap " result__badge"
281- , wrap " badge"
282- , wrap " badge--package" ]
283- , HP .title " Package"
284- ]
285- [ HH .text " P" ]
286-
287- , HH .a [ HP .class_ (wrap " result__link" )
288- , HP .href $ homePageFromRepository repository
289- ]
290- [ HH .text name ]
291- ]
292- ]
293- ] <> (
294- description >#>
295- \descriptionText ->
296- [ HH .div [ HP .class_ (wrap " result__body" ) ]
297- [ HH .text descriptionText ]
298- ]
299- )
300-
301314
302315renderResultType
303316 :: forall a rest
@@ -476,7 +489,7 @@ renderType
476489 -> HH.HTML a Action
477490renderType = case _ of
478491 TypeVar str -> HH .text str
479- TypeLevelString str -> HH .text $ " (Text \" " <> str <> " \" ) " -- TODO: add escaping
492+ TypeLevelString str -> HH .text $ " \" " <> str <> " \" " -- TODO: add escaping
480493 TypeWildcard -> HH .text " _"
481494 TypeConstructor qname -> renderQualifiedName false TypeLevel qname
482495 TypeOp qname -> renderQualifiedName true TypeLevel qname
@@ -569,7 +582,17 @@ renderRow asRow =
569582
570583 if List .null rows
571584 then
572- [ HH .text $ if asRow then " ()" else " {}" ]
585+ [ if asRow
586+ then HH .text " ()"
587+ else
588+ fromMaybe (HH .text " {}" ) $
589+ ty <#> \ty' ->
590+ HH .span_
591+ [ renderQualifiedName false TypeLevel primRecord
592+ , HH .text " "
593+ , renderType ty'
594+ ]
595+ ]
573596 else
574597 [ HH .text opening ] <>
575598
@@ -579,9 +602,9 @@ renderRow asRow =
579602 , renderType entry.ty ] ]
580603 ) <>
581604
582- case ty of
583- Just ty' -> [ HH .text " | " , renderType ty', HH .text closing ]
584- Nothing -> [ HH .text closing ]
605+ (ty >#> \ty' -> [ HH .text " | " , renderType ty' ]) <>
606+
607+ [ HH .text closing ]
585608
586609 where
587610 opening = if asRow then " ( " else " { "
@@ -643,6 +666,10 @@ makeHref level isInfix moduleName name =
643666 if isInfix then " type (" <> name <> " )" else name
644667
645668
669+ primRecord :: QualifiedName
670+ primRecord = QualifiedName { moduleName: [ " Prim" ], name: " Record" }
671+
672+
646673keyword
647674 :: forall a
648675 . String
0 commit comments