import { useMemo } from "react"; import { Scores } from "@local-logic/client"; const calcScoreVal = (value?: string, round = false) => { if (!value) { return null; } const num = parseFloat(value); const outOf10 = num * 2; return round ? Math.round(outOf10) : Math.round(outOf10 * 10) / 10; }; const useHelpers = ({ scoresData, score, round = false, }: { scoresData?: Partial; score: Partial; round?: boolean; }) => { const scoreVal = useMemo(() => { const value = scoresData?.location?.[score]?.value; return calcScoreVal(value, round); }, [scoresData, score, round]); const textVal = useMemo( () => scoresData?.location?.[score]?.text, [scoresData, score] ); const cityGeography = useMemo( () => scoresData?.geographies?.find( (g) => g.geog_level_type === "municipality" ), [scoresData] ); const neighbourhoodGeography = useMemo( () => scoresData?.geographies?.find( (g) => g.geog_level_type === "macro neighbourhood" ), [scoresData] ); const cityScoreVal = useMemo(() => { const value = cityGeography?.scores[score]?.value; return calcScoreVal(value, round); }, [cityGeography, score, round]); const neighbourhoodScoreVal = useMemo(() => { const value = neighbourhoodGeography?.scores[score]?.value; return calcScoreVal(value, round); }, [neighbourhoodGeography, score, round]); const cityName = useMemo(() => cityGeography?.name, [cityGeography]); const neighbourhoodName = useMemo( () => neighbourhoodGeography?.name, [neighbourhoodGeography] ); return { textVal, scoreVal, cityName, cityScoreVal, neighbourhoodName, neighbourhoodScoreVal, }; }; export default useHelpers;