Text with & hexadecimal entities
"; + expect(decodeHtmlEntities(input)).toBe(expected); + }); + + test("should handle text without HTML entities", () => { + const input = "Text without HTML entities"; + const expected = "Text without HTML entities"; + expect(decodeHtmlEntities(input)).toBe(expected); + }); + + test("should handle empty text", () => { + const input = ""; + const expected = ""; + expect(decodeHtmlEntities(input)).toBe(expected); + }); + + test("should decode entities for accented characters", () => { + const input = "Text with accentção"; + const expected = "Text with accentção"; + expect(decodeHtmlEntities(input)).toBe(expected); + }); +}); diff --git a/ui/src/utils/decodeHtml.ts b/ui/src/utils/decodeHtml.ts new file mode 100644 index 000000000..84d4cac14 --- /dev/null +++ b/ui/src/utils/decodeHtml.ts @@ -0,0 +1,5 @@ +export default function decodeHtmlEntities(text: string) { + const parser = new DOMParser(); + const doc = parser.parseFromString(text, "text/html"); + return doc.documentElement.textContent || text; +} diff --git a/ui/src/utils/formatDate.ts b/ui/src/utils/formatDate.ts new file mode 100644 index 000000000..43d106944 --- /dev/null +++ b/ui/src/utils/formatDate.ts @@ -0,0 +1,8 @@ +export default function formatDate(timestamp: string) { + try { + const date = new Date(timestamp); + return date.toLocaleString(); + } catch { + return timestamp; + } +} diff --git a/ui/src/utils/getErrorMessage.ts b/ui/src/utils/getErrorMessage.ts new file mode 100644 index 000000000..5ba42ba4b --- /dev/null +++ b/ui/src/utils/getErrorMessage.ts @@ -0,0 +1,7 @@ +export default function getErrorMessage(error: unknown): string { + console.error(error); + + if (error instanceof Error) return error.message; + if (typeof error === "string") return error; + return "An unknown error occurred"; +} diff --git a/ui/src/utils/truncateText.ts b/ui/src/utils/truncateText.ts new file mode 100644 index 000000000..d7aa58cf7 --- /dev/null +++ b/ui/src/utils/truncateText.ts @@ -0,0 +1,4 @@ +export default function truncateText(text: string, maxLength: number = 100) { + if (text.length <= maxLength) return text; + return text.substring(0, maxLength) + "..."; +}