Skip to content

Commit

Permalink
Merge pull request #211 from aodn/bugs/5964-fix-abstract-formatting
Browse files Browse the repository at this point in the history
Fix abstract format issue on UI
  • Loading branch information
HavierD authored Oct 31, 2024
2 parents 7ebae36 + 56a4b8b commit a58c6f4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/components/list/listItem/subitem/ExpandableTextArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Button, Grid, Typography } from "@mui/material";
import {
decodeHtmlEntities,
truncateText,
enrichHTML,
} from "../../../../utils/StringUtils";

interface ExpandableTextAreaProps {
Expand All @@ -24,19 +25,20 @@ const ExpandableTextArea: React.FC<ExpandableTextAreaProps> = ({
showMoreStr = "Show More",
}) => {
const decodedText = decodeHtmlEntities(text);
const truncatedText = truncateText(decodedText, truncateCount);
const doesNeedTruncation = decodedText.length > truncateCount;

const [isExpanded, setIsExpanded] = useState(false);

const onButtonClick = useCallback(() => {
setIsExpanded((prev) => !prev);
}, []);

return (
<TextAreaBaseGrid>
<Grid item md={12}>
<Typography
variant="detailContent"
whiteSpace="pre-wrap"
sx={{
textAlign: "left",
...(isClickable && {
Expand All @@ -48,7 +50,7 @@ const ExpandableTextArea: React.FC<ExpandableTextAreaProps> = ({
}}
onClick={onClick}
>
{isExpanded ? decodedText : truncatedText}
{isExpanded ? decodedText : truncateText(decodedText, truncateCount)}
</Typography>
</Grid>
<Grid item md={12} display="flex" justifyContent="flex-end">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import Layers from "../../../../components/map/mapbox/layers/Layers";
import { StaticLayersDef } from "../../../../components/map/mapbox/layers/StaticLayer";
import { MapboxWorldLayersDef } from "../../../../components/map/mapbox/layers/MapboxWorldLayer";
import useScrollToSection from "../../../../hooks/useScrollToSection";
import { decodeHtmlEntities } from "../../../../utils/StringUtils";
import ExpandableTextArea from "../../../../components/list/listItem/subitem/ExpandableTextArea";
import DetailClusterLayer from "../../../../components/map/mapbox/layers/DetailClusterLayer";

Expand Down Expand Up @@ -156,7 +155,7 @@ const AbstractAndDownloadPanel: FC = () => {
<Grid item xs={12}>
<Stack direction="column">
<ExpandableTextArea
text={decodeHtmlEntities(abstract)}
text={abstract}
showMoreStr={"Show All"}
truncateCount={TRUNCATE_COUNT}
/>
Expand Down
13 changes: 13 additions & 0 deletions src/utils/StringUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// e.g capitalize, concatenate, ...

import { decode } from "he";
import React from "react";

/**
* Capitalizes the first letter of a given string.
Expand Down Expand Up @@ -46,3 +47,15 @@ export const decodeHtmlEntities = (str: string): string => {
return str;
}
};
// TODO: Keep it for now, we want to enhance the url later with http
export const enrichHTML = (text: string): string => {
// Regular expression to match URLs with http:// or https://
const urlPattern = /\bhttps?:\/\/[^\s/$.?#].[^\s]*\b/g;

// Replace URLs in the text with anchor tags
const h = text.replace(urlPattern, (url) => {
return `<a href="${url}" target="_blank" rel="noopener noreferrer">${url}</a>`;
});

return h.replace(/\n/g, "<br>");
};

0 comments on commit a58c6f4

Please sign in to comment.