From de997cd5306979ec878887a5667280fc0c0b1336 Mon Sep 17 00:00:00 2001 From: Ruxandra Machedon Date: Fri, 5 Jul 2024 16:44:47 -0400 Subject: [PATCH] fix: validate soil id urls before allowing users to navigate to them --- .../components/soilInfo/SoilInfoDisplay.tsx | 32 ++++++++++++++----- dev-client/src/util.ts | 8 +++++ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/dev-client/src/screens/LocationScreens/components/soilInfo/SoilInfoDisplay.tsx b/dev-client/src/screens/LocationScreens/components/soilInfo/SoilInfoDisplay.tsx index 2e81a9efe..114effcfa 100644 --- a/dev-client/src/screens/LocationScreens/components/soilInfo/SoilInfoDisplay.tsx +++ b/dev-client/src/screens/LocationScreens/components/soilInfo/SoilInfoDisplay.tsx @@ -15,6 +15,7 @@ * along with this program. If not, see https://www.gnu.org/licenses/. */ +import {useMemo} from 'react'; import {useTranslation} from 'react-i18next'; import {SoilInfo} from 'terraso-client-shared/graphqlSchema/graphql'; @@ -27,6 +28,7 @@ import { Heading, Text, } from 'terraso-mobile-client/components/NativeBaseAdapters'; +import {validateUrl} from 'terraso-mobile-client/util'; type SoilInfoDisplayProps = { dataSource: string; @@ -35,16 +37,28 @@ type SoilInfoDisplayProps = { export function SoilInfoDisplay({dataSource, soilInfo}: SoilInfoDisplayProps) { const {t} = useTranslation(); + + const hasValidUrl = useMemo( + () => validateUrl(soilInfo.soilSeries.fullDescriptionUrl), + [soilInfo], + ); + const hasValidEsUrl = useMemo( + () => validateUrl(soilInfo.ecologicalSite?.url), + [soilInfo], + ); + return ( {soilInfo.soilSeries.taxonomySubgroup} {soilInfo.soilSeries.description} - + {hasValidUrl && ( + + )} {soilInfo.ecologicalSite && ( <> @@ -61,10 +75,12 @@ export function SoilInfoDisplay({dataSource, soilInfo}: SoilInfoDisplayProps) { }} /> - + {hasValidEsUrl && ( + + )} )} diff --git a/dev-client/src/util.ts b/dev-client/src/util.ts index e1e5afcfc..3b1dac914 100644 --- a/dev-client/src/util.ts +++ b/dev-client/src/util.ts @@ -164,3 +164,11 @@ export const isSiteManager = matchesRole([ export const getSoilWebUrl = (coords: Coords) => { return `https://casoilresource.lawr.ucdavis.edu/gmap/?loc=${coords.latitude},${coords.longitude}`; }; + +export const validateUrl = (url?: string): URL | false => { + try { + return url ? new URL(url) : false; + } catch { + return false; + } +};