From 2793de9d03603077c382d6058d95376e50499297 Mon Sep 17 00:00:00 2001 From: victor barbier Date: Mon, 9 Sep 2024 12:12:57 +0200 Subject: [PATCH] feat(client): rework --- .../src/components/results/debug/index.tsx | 37 +++++++++ .../components/results/highlights/index.tsx | 81 +++++++------------ .../client/src/components/results/index.tsx | 21 +++-- .../src/components/results/result/index.tsx | 11 +-- .../components/results/utils/highlights.tsx | 38 +-------- .../src/components/results/utils/url.tsx | 2 +- project/client/src/styles/index.scss | 9 +++ project/client/src/types/data.ts | 30 ------- project/client/src/types/functions.ts | 16 ---- project/client/src/types/index.ts | 43 ++++++++++ 10 files changed, 138 insertions(+), 150 deletions(-) create mode 100644 project/client/src/components/results/debug/index.tsx delete mode 100644 project/client/src/types/data.ts delete mode 100644 project/client/src/types/functions.ts create mode 100644 project/client/src/types/index.ts diff --git a/project/client/src/components/results/debug/index.tsx b/project/client/src/components/results/debug/index.tsx new file mode 100644 index 0000000..d1542b4 --- /dev/null +++ b/project/client/src/components/results/debug/index.tsx @@ -0,0 +1,37 @@ +import { Container, Text, Badge, BadgeGroup, Accordion } from "@dataesr/dsfr-plus" +import { MatchDebug } from "../../../types" + +type ResultsDebugArgs = { + resultsDebug: MatchDebug +} +export default function ResultsDebug({ resultsDebug }: ResultsDebugArgs) { + console.log("resultsDebug", resultsDebug) + + if (!resultsDebug) return null + + const criterionMatches = (criterion: string) => resultsDebug.criterion?.[criterion] ?? 0 + + return ( + + {resultsDebug.strategies.map((strategy, index) => ( + + {strategy.equivalent_strategies.map((equivalent) => ( + + {equivalent.map((criterion) => { + const matches: number = criterionMatches(criterion) + return ( + {`${criterion}: ${matches} match${ + matches == 1 ? "" : "es" + }`} + ) + })} + + ))} + {`${strategy.matches} ${ + strategy.matches == 1 ? "possibility" : "possibilities" + }`} + + ))} + + ) +} diff --git a/project/client/src/components/results/highlights/index.tsx b/project/client/src/components/results/highlights/index.tsx index 8a471f2..7b66133 100644 --- a/project/client/src/components/results/highlights/index.tsx +++ b/project/client/src/components/results/highlights/index.tsx @@ -1,65 +1,40 @@ import { IntlMessageFormat } from "intl-messageformat" -import { Container, Badge, Text, Row } from "@dataesr/dsfr-plus" -import { CriterionHighlightsArgs, ResultHighlightsArgs, StrategyHighlightsArgs } from "../../../types/functions" -import { getHighlightedText, getHighlightedQuery, strategyCriteria } from "../utils/highlights" +import { Container, Badge, BadgeGroup } from "@dataesr/dsfr-plus" +import { getHighlightedQuery } from "../utils/highlights" import useUrl from "../../../hooks/useUrl" +import { MatchHighlight } from "../../../types" -function CriterionHighlights({ criterion, criterionHighlights, setTitle }: CriterionHighlightsArgs) { - const { currentQuery } = useUrl() +export type ResultHighlightsArgs = { + resultHighlights: MatchHighlight + setTitle: Function +} - const highlightedData = criterionHighlights.map((criterionHighlight) => { - const highlightedText = getHighlightedText(criterionHighlight[0]) - return { - highlightedCriterion: highlightedText.join(" "), - highlightedQuery: getHighlightedQuery(highlightedText, currentQuery), - } - }) +export default function ResultHighlights({ resultHighlights, setTitle }: ResultHighlightsArgs) { + const { currentQuery } = useUrl() const onEnter = (highlightedQuery: string) => - setTitle(new IntlMessageFormat(highlightedQuery).format({ b: (chunks) => {chunks} })) + setTitle( + new IntlMessageFormat(highlightedQuery).format({ + b: (chunks) => {chunks}, + }) + ) const onLeave = () => setTitle(currentQuery) return ( - - {`${criterion} (${highlightedData.length} match${highlightedData.length > 1 ? "es" : ""}):`} - {highlightedData.map((data, index) => ( - onEnter(data.highlightedQuery)} - onMouseLeave={() => onLeave()} - > - {data.highlightedCriterion} - - ))} - - ) -} - -function StrategyHighlights({ strategy, strategyHighlights, setTitle }: StrategyHighlightsArgs) { - const criteria = strategyCriteria(strategy) - const color = criteria.length === Object.keys(strategyHighlights).length ? "success" : "error" - - return ( - - {`Strategy: ${criteria.join(", ")}`} - {Object.entries(strategyHighlights).map(([criterion, criterionHighlights], index) => ( - - ))} - - ) -} - -export default function ResultHighlights({ resultHighlights, setTitle }: ResultHighlightsArgs) { - return ( - - {Object.entries(resultHighlights).map(([strategy, criteriaHighlights], index) => ( - + + {Object.entries(resultHighlights.criterion).map(([criterion, highlights], groupIndex) => ( + + {criterion} + {highlights.map((highlight, badgeIndex) => ( + onEnter(getHighlightedQuery(highlight, currentQuery))} + onMouseLeave={() => onLeave()} + > + {highlight?.join(" ")} + + ))} + ))} ) diff --git a/project/client/src/components/results/index.tsx b/project/client/src/components/results/index.tsx index fdef0ba..3a8caa8 100644 --- a/project/client/src/components/results/index.tsx +++ b/project/client/src/components/results/index.tsx @@ -5,7 +5,8 @@ import useUrl from "../../hooks/useUrl" import Error from "../error" import Result from "./result" import Fetching from "../fetching" -import { MatchIds } from "../../types/data" +import { MatchIds } from "../../types" +import ResultsDebug from "./debug" export default function Results() { const { currentQuery, currentMatcher, currentYear } = useUrl() @@ -27,10 +28,14 @@ export default function Results() { const matchIds = data.results as MatchIds if (!matchIds.length) return ( - - {currentTitle} - {`${currentMatcher} : no results`} - + <> + + {currentTitle} + + + {`${currentMatcher} : 0 match`} + + ) return ( @@ -38,11 +43,15 @@ export default function Results() { {currentTitle} - + + {`${matchIds.length} match${matchIds.length > 1 ? "es" : ""}`} + + {matchIds.map((id, index) => { return })} + ) } diff --git a/project/client/src/components/results/result/index.tsx b/project/client/src/components/results/result/index.tsx index 94325f8..356f783 100644 --- a/project/client/src/components/results/result/index.tsx +++ b/project/client/src/components/results/result/index.tsx @@ -1,7 +1,6 @@ -import { Container, Badge, Row, BadgeGroup, Accordion } from "@dataesr/dsfr-plus" -import { MatchId, MatchResults } from "../../../types/data" +import { Container, Badge, Row, BadgeGroup } from "@dataesr/dsfr-plus" +import { MatchId, MatchResults } from "../../../types" import useUrl from "../../../hooks/useUrl" -import { getResultHighlights } from "../utils/highlights" import { getResultUrl } from "../utils/url" import ResultHighlights from "../highlights" @@ -19,7 +18,7 @@ export default function Result({ const { results: resultsIds, enriched_results: resultsEnriched, highlights: resultsHighlights } = resultData const resultIndex = resultsIds.findIndex((element) => element === resultId) const resultEnriched = resultsEnriched[resultIndex] - const resultHighlights = getResultHighlights(resultsHighlights, resultId) + const resultHighlights = resultsHighlights?.[resultId] const resultUrl = getResultUrl(resultId, currentMatcher) return ( @@ -42,9 +41,7 @@ export default function Result({ {resultEnriched?.country?.length && {resultEnriched.country[0]}} - - - + ) } diff --git a/project/client/src/components/results/utils/highlights.tsx b/project/client/src/components/results/utils/highlights.tsx index 46c6358..8e61694 100644 --- a/project/client/src/components/results/utils/highlights.tsx +++ b/project/client/src/components/results/utils/highlights.tsx @@ -1,40 +1,4 @@ -import { MatchHighlights, MatchId, ResultHighlights, TextHighlight } from "../../../types/data" - -export const getResultHighlights = (resultsHighlights: MatchHighlights, id: MatchId) => - Object.entries(resultsHighlights).reduce((acc: ResultHighlights, [strategy, strategyHighlights]) => { - Object.entries(strategyHighlights[id]).forEach(([criterion, criterionHiglights]) => { - if (strategy in acc) acc[strategy][criterion] = criterionHiglights - else acc[strategy] = { [criterion]: criterionHiglights } - }) - return acc - }, {}) - -export const strategyCriteria = (strategy: string) => strategy.split(";") - -const stringFindText = (str: string, text: string): Array => - [...str.matchAll(new RegExp(text, "gi"))].map((match) => match.index) - -export const getHighlightedText = (highlight: string): TextHighlight => { - const startTag = "" - const endTag = "" - const startTags = stringFindText(highlight, startTag) - const endTags = stringFindText(highlight, endTag) - - let highlightedArray = [] - let cursor = 0 - - for (let i = 0; i < startTags.length; i++) { - const startIndex = startTags[i] - const endIndex = endTags[i] - - const highlighted = highlight.slice(startIndex + startTag.length, endIndex) - highlightedArray.push(highlighted) - - cursor = endIndex + endTag.length - } - - return highlightedArray -} +import { TextHighlight } from "../../../types" export function getHighlightedQuery(highlightedArray: TextHighlight, query: string) { let highlightedQuery = query diff --git a/project/client/src/components/results/utils/url.tsx b/project/client/src/components/results/utils/url.tsx index b73fcee..38f70fc 100644 --- a/project/client/src/components/results/utils/url.tsx +++ b/project/client/src/components/results/utils/url.tsx @@ -1,4 +1,4 @@ -import { MatchId } from "../../../types/data" +import { MatchId } from "../../../types" export function getResultUrl(resultId: MatchId, currentMatcher: string) { if (currentMatcher === "ror") return `https://ror.org/${resultId}` diff --git a/project/client/src/styles/index.scss b/project/client/src/styles/index.scss index 3a18b74..280e747 100644 --- a/project/client/src/styles/index.scss +++ b/project/client/src/styles/index.scss @@ -14,3 +14,12 @@ padding-top: 15px; padding-bottom: 1px; } + +.debug-item { + padding-bottom: 1rem !important +} + +.debug-item:not(:first-child) { + border-top: 1px solid var(--border-default-grey); + padding-top: 1rem !important; +} \ No newline at end of file diff --git a/project/client/src/types/data.ts b/project/client/src/types/data.ts deleted file mode 100644 index 5ece45b..0000000 --- a/project/client/src/types/data.ts +++ /dev/null @@ -1,30 +0,0 @@ -export type MatchId = string -export type MatchIds = Array - -export type MatchEnrichedResult = { - id: MatchId - acronym: Array - name: Array - city: Array - country: Array -} -export type MatchEnrichedResults = Array - -export type CriterionHighlight = Array -export type CriterionHighlights = Array -export type CriteriaHighlights = Record -export type StrategyHighlights = Record - -export type ResultHighlights = Record -export type MatchHighlights = Record - -export type MatchResults = { - version: string - index_date: string - results: MatchIds - other_ids: MatchIds - enriched_results: MatchEnrichedResults - highlights: MatchHighlights -} - -export type TextHighlight = Array diff --git a/project/client/src/types/functions.ts b/project/client/src/types/functions.ts deleted file mode 100644 index e9cd79e..0000000 --- a/project/client/src/types/functions.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { CriterionHighlights, CriteriaHighlights, ResultHighlights } from "./data" - -export type CriterionHighlightsArgs = { - criterion: string - criterionHighlights: CriterionHighlights - setTitle: Function -} -export type StrategyHighlightsArgs = { - strategy: string - strategyHighlights: CriteriaHighlights - setTitle: Function -} -export type ResultHighlightsArgs = { - resultHighlights: ResultHighlights - setTitle: Function -} diff --git a/project/client/src/types/index.ts b/project/client/src/types/index.ts new file mode 100644 index 0000000..d101375 --- /dev/null +++ b/project/client/src/types/index.ts @@ -0,0 +1,43 @@ +export type MatchId = string +export type MatchIds = Array + +export type MatchEnrichedResult = { + id: MatchId + name: Array + acronym?: Array + city?: Array + country?: Array +} +export type MatchEnrichedResults = Array + +export type CriteriaHighlight = Array> +export type CriterionHighlight = Record +export type StrategyHighlight = Array +export type StrategiesHighlight = Array +export type MatchHighlight = { + criterion: CriterionHighlight + strategies: StrategiesHighlight +} +export type MatchHighlights = Record + +export type CriterionDebug = Record +export type StrategiesDebug = { + equivalent_strategies: Array> + matches: number +} +export type MatchDebug = { + criterion: CriterionDebug + strategies: Array +} + +export type MatchResults = { + version: string + index_date: string + results: MatchIds + other_ids: MatchIds + enriched_results: MatchEnrichedResults + highlights?: MatchHighlights + debug?: MatchDebug +} + +export type TextHighlight = Array