1
1
import { useEffect , useState } from "react"
2
2
import { useIntl } from "react-intl"
3
3
import { Container , Text , Badge } from "@dataesr/dsfr-plus"
4
- import { MatchResults } from "../../types"
4
+ import { MatchIds , MatchResults } from "../../types"
5
5
import Error from "../error"
6
6
import Result from "./result"
7
7
import Fetching from "../fetching"
@@ -10,6 +10,81 @@ import useUrl from "../../hooks/useUrl"
10
10
import Info from "../info"
11
11
import useMatch from "../../hooks/useMatch"
12
12
13
+ type MatcherResultsArgs = {
14
+ matchIds : MatchIds
15
+ matchResults : MatchResults
16
+ setTitle : any
17
+ }
18
+
19
+ function MatcherResults ( { matchIds, matchResults, setTitle } : MatcherResultsArgs ) {
20
+ const intl = useIntl ( )
21
+ return (
22
+ < Container fluid >
23
+ < Container className = "fr-mt-3w" >
24
+ < Text size = "md" > { intl . formatMessage ( { id : "match.count" } , { count : matchIds . length } ) } </ Text >
25
+ </ Container >
26
+ < Container fluid className = "fr-mt-3w" >
27
+ { matchIds . map ( ( id , index ) => {
28
+ return < Result key = { index } resultData = { matchResults } resultId = { id } setTitle = { setTitle } />
29
+ } ) }
30
+ </ Container >
31
+ </ Container >
32
+ )
33
+ }
34
+
35
+ function PaysageResults ( { matchIds, matchResults, setTitle } : MatcherResultsArgs ) {
36
+ const intl = useIntl ( )
37
+
38
+ const DEFAULT_CATEGORY = "Others"
39
+ const DEFAULT_PRIORITY = 99
40
+
41
+ const categories = matchResults ?. enriched_results ?. reduce ( ( acc , res ) => {
42
+ if ( res ?. paysage_categories ) {
43
+ const lowestPriority = Math . min ( ...res . paysage_categories . map ( ( category ) => category ?. priority || DEFAULT_PRIORITY ) )
44
+ res . paysage_categories
45
+ . filter ( ( category ) => ( category ?. priority || DEFAULT_PRIORITY ) === lowestPriority )
46
+ . forEach ( ( category ) => {
47
+ const label = category ?. label || DEFAULT_CATEGORY
48
+ const priority = category ?. priority || DEFAULT_PRIORITY
49
+ acc [ label ] = acc ?. [ label ]
50
+ ? { ...acc [ label ] , ids : [ ...acc [ label ] . ids , res . id ] }
51
+ : { ids : [ res . id ] , priority : priority }
52
+ } )
53
+ } else {
54
+ acc [ DEFAULT_CATEGORY ] = acc ?. [ DEFAULT_CATEGORY ]
55
+ ? { ...acc [ DEFAULT_CATEGORY ] , ids : [ ...acc [ DEFAULT_CATEGORY ] . ids , res . id ] }
56
+ : { ids : [ res . id ] , priority : DEFAULT_PRIORITY }
57
+ }
58
+ return acc
59
+ } , { } as Record < string , { ids : MatchIds ; priority : number } > )
60
+
61
+ if ( ! categories || ( Object . keys ( categories ) ?. length === 1 && Object . keys ( categories ) [ 0 ] === DEFAULT_CATEGORY ) )
62
+ return < MatcherResults matchIds = { matchIds } matchResults = { matchResults } setTitle = { setTitle } />
63
+
64
+ return (
65
+ < Container fluid >
66
+ { Object . entries ( categories )
67
+ . sort ( ( a , b ) => a [ 1 ] . priority - b [ 1 ] . priority )
68
+ . map ( ( [ key , values ] ) => (
69
+ < Container fluid >
70
+ < Container className = "fr-mt-3w" >
71
+ < Text size = "md" >
72
+ { key }
73
+ { " : " }
74
+ { intl . formatMessage ( { id : "match.count" } , { count : values . ids . length } ) }
75
+ </ Text >
76
+ </ Container >
77
+ < Container fluid className = "fr-mt-3w" >
78
+ { values . ids . map ( ( id , index ) => {
79
+ return < Result key = { index } resultData = { matchResults } resultId = { id } setTitle = { setTitle } />
80
+ } ) }
81
+ </ Container >
82
+ </ Container >
83
+ ) ) }
84
+ </ Container >
85
+ )
86
+ }
87
+
13
88
export default function Results ( ) {
14
89
const intl = useIntl ( )
15
90
const { currentQuery, currentMatcher, currentYear } = useUrl ( )
@@ -42,7 +117,7 @@ export default function Results() {
42
117
< Container className = "fr-mt-3w" >
43
118
< Badge color = "error" > { `${ currentMatcher } : ${ intl . formatMessage ( { id : "match.count" } , { count : 0 } ) } ` } </ Badge >
44
119
</ Container >
45
- < ResultsDebug resultsDebug = { matchResults ?. debug } />
120
+ < ResultsDebug resultsDebug = { matchResults ?. debug } resultsLogs = { matchResults ?. logs } />
46
121
</ Container >
47
122
)
48
123
@@ -51,16 +126,12 @@ export default function Results() {
51
126
< Container className = "sticky card" >
52
127
< Text size = "lead" > { currentTitle } </ Text >
53
128
</ Container >
54
- < Container className = "fr-mt-3w" >
55
- < Text size = "md" > { intl . formatMessage ( { id : "match.count" } , { count : matchIds . length } ) } </ Text >
56
- </ Container >
57
- < Container fluid className = "fr-mt-3w" >
58
- { matchIds . map ( ( id , index ) => {
59
- return < Result key = { index } resultData = { matchResults } resultId = { id } setTitle = { setTitle } />
60
- } ) }
61
- </ Container >
62
- < ResultsDebug resultsDebug = { matchResults ?. debug } />
63
- { /* <div dangerouslySetInnerHTML={{ __html: matchResults?.logs }} /> */ }
129
+ { currentMatcher === "paysage" ? (
130
+ < PaysageResults matchIds = { matchIds } matchResults = { matchResults } setTitle = { setTitle } />
131
+ ) : (
132
+ < MatcherResults matchIds = { matchIds } matchResults = { matchResults } setTitle = { setTitle } />
133
+ ) }
134
+ < ResultsDebug resultsDebug = { matchResults ?. debug } resultsLogs = { matchResults ?. logs } />
64
135
</ Container >
65
136
)
66
137
}
0 commit comments