Skip to content

Commit

Permalink
fix: validate soil id urls before allowing users to navigate to them
Browse files Browse the repository at this point in the history
  • Loading branch information
tm-ruxandra committed Jul 5, 2024
1 parent 49b0984 commit de997cd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -27,6 +28,7 @@ import {
Heading,
Text,
} from 'terraso-mobile-client/components/NativeBaseAdapters';
import {validateUrl} from 'terraso-mobile-client/util';

type SoilInfoDisplayProps = {
dataSource: string;
Expand All @@ -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 (
<Column space={3}>
<Heading variant="h6" fontStyle="italic" pb="10px">
{soilInfo.soilSeries.taxonomySubgroup}
</Heading>
<Text variant="body1">{soilInfo.soilSeries.description}</Text>
<InternalLink
label={t('site.soil_id.soil_info.series_descr_url')}
url={soilInfo.soilSeries.fullDescriptionUrl}
/>
{hasValidUrl && (
<InternalLink
label={t('site.soil_id.soil_info.series_descr_url')}
url={soilInfo.soilSeries.fullDescriptionUrl}
/>
)}
{soilInfo.ecologicalSite && (
<>
<Box>
Expand All @@ -61,10 +75,12 @@ export function SoilInfoDisplay({dataSource, soilInfo}: SoilInfoDisplayProps) {
}}
/>
</Box>
<InternalLink
label={t('site.soil_id.soil_info.eco_descr_url')}
url={soilInfo.ecologicalSite.url}
/>
{hasValidEsUrl && (
<InternalLink
label={t('site.soil_id.soil_info.eco_descr_url')}
url={soilInfo.ecologicalSite.url}
/>
)}
</>
)}
<Box>
Expand Down
8 changes: 8 additions & 0 deletions dev-client/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};

0 comments on commit de997cd

Please sign in to comment.