You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.6 KiB
67 lines
1.6 KiB
import sanitizeHtml from "sanitize-html";
|
|
|
|
import { useMemo, useState } from "react";
|
|
import {
|
|
FiChevronUp as CollapseIcon,
|
|
FiChevronDown as ExpandIcon
|
|
} from "react-icons/fi";
|
|
|
|
import { Button } from "@nextui-org/button";
|
|
|
|
import { highlight } from "@/utils/highlight";
|
|
|
|
import { HighlightRenderer } from "./HighlightRenderer";
|
|
|
|
import { Component } from "@/typings/component";
|
|
|
|
const shortenedDescriptionLength = 200;
|
|
|
|
export const Description: Component<{ data: string }> = ({ data }) => {
|
|
const [expandedDescription, setExpandedDescription] = useState(false);
|
|
|
|
const sanitizedDescription = useMemo(
|
|
() =>
|
|
sanitizeHtml(data, {
|
|
allowedTags: ["a", "br"],
|
|
allowedAttributes: {
|
|
a: ["href"]
|
|
}
|
|
}),
|
|
[data]
|
|
);
|
|
|
|
const descriptionAlreadyShort =
|
|
sanitizedDescription.length <= shortenedDescriptionLength;
|
|
|
|
const descriptionCut = useMemo(() => {
|
|
if (descriptionAlreadyShort || expandedDescription)
|
|
return sanitizedDescription;
|
|
else
|
|
return (
|
|
sanitizedDescription.substring(0, shortenedDescriptionLength) + "..."
|
|
);
|
|
}, [sanitizedDescription, descriptionAlreadyShort, expandedDescription]);
|
|
|
|
const description = useMemo(
|
|
() => highlight(descriptionCut),
|
|
[descriptionCut]
|
|
);
|
|
|
|
return (
|
|
<div>
|
|
<h2 className="text-ellipsis overflow-y-hidden">
|
|
<HighlightRenderer highlighted={description} />
|
|
</h2>
|
|
{!descriptionAlreadyShort && (
|
|
<Button
|
|
startContent={expandedDescription ? <CollapseIcon /> : <ExpandIcon />}
|
|
variant="light"
|
|
onClick={() => setExpandedDescription((state) => !state)}
|
|
>
|
|
{expandedDescription ? "Show less" : "Show more"}
|
|
</Button>
|
|
)}
|
|
</div>
|
|
);
|
|
};
|