From 2db909d7af261f95f0f3605ff52ef1ae068cd9da Mon Sep 17 00:00:00 2001
From: Lim Chee Aun <cheeaun@gmail.com>
Date: Tue, 20 Aug 2024 19:58:50 +0800
Subject: [PATCH] Try/catch all intl stuff

---
 src/components/name-text.jsx | 16 ++++++++++------
 src/utils/lang.js            |  8 ++++++--
 src/utils/nice-date-time.js  |  9 +++++++--
 src/utils/shorten-number.jsx | 12 ++++++++----
 4 files changed, 31 insertions(+), 14 deletions(-)

diff --git a/src/components/name-text.jsx b/src/components/name-text.jsx
index b159348d..1a1425e4 100644
--- a/src/components/name-text.jsx
+++ b/src/components/name-text.jsx
@@ -10,12 +10,16 @@ import states from '../utils/states';
 import Avatar from './avatar';
 import EmojiText from './emoji-text';
 
-const nameCollator = mem(
-  (locale) =>
-    new Intl.Collator(locale || undefined, {
-      sensitivity: 'base',
-    }),
-);
+const nameCollator = mem((locale) => {
+  const options = {
+    sensitivity: 'base',
+  };
+  try {
+    return new Intl.Collator(locale || undefined, options);
+  } catch (e) {
+    return new Intl.Collator(undefined, options);
+  }
+});
 
 function NameText({
   account,
diff --git a/src/utils/lang.js b/src/utils/lang.js
index 9bc06e2b..b17e252d 100644
--- a/src/utils/lang.js
+++ b/src/utils/lang.js
@@ -20,8 +20,12 @@ i18n.on('change', () => {
     // lang
     document.documentElement.lang = lang;
     // LTR or RTL
-    const { direction } = new Locale(lang).textInfo;
-    document.documentElement.dir = direction;
+    try {
+      const { direction } = new Locale(lang).textInfo;
+      document.documentElement.dir = direction;
+    } catch (e) {
+      console.error(e);
+    }
   }
 });
 
diff --git a/src/utils/nice-date-time.js b/src/utils/nice-date-time.js
index 585c1c65..f1376aab 100644
--- a/src/utils/nice-date-time.js
+++ b/src/utils/nice-date-time.js
@@ -8,7 +8,7 @@ const _DateTimeFormat = (opts) => {
   const { locale, dateYear, hideTime, formatOpts } = opts || {};
   const loc = locale && !/pseudo/i.test(locale) ? locale : defaultLocale;
   const currentYear = new Date().getFullYear();
-  return Intl.DateTimeFormat(loc, {
+  const options = {
     // Show year if not current year
     year: dateYear === currentYear ? undefined : 'numeric',
     month: 'short',
@@ -17,7 +17,12 @@ const _DateTimeFormat = (opts) => {
     hour: hideTime ? undefined : 'numeric',
     minute: hideTime ? undefined : 'numeric',
     ...formatOpts,
-  });
+  };
+  try {
+    return Intl.DateTimeFormat(loc, opts);
+  } catch (e) {
+    return Intl.DateTimeFormat(undefined, opts);
+  }
 };
 const DateTimeFormat = mem(_DateTimeFormat);
 
diff --git a/src/utils/shorten-number.jsx b/src/utils/shorten-number.jsx
index 1316ad74..5dc32350 100644
--- a/src/utils/shorten-number.jsx
+++ b/src/utils/shorten-number.jsx
@@ -1,8 +1,12 @@
 import { i18n } from '@lingui/core';
 
 export default function shortenNumber(num) {
-  return i18n.number(num, {
-    notation: 'compact',
-    roundingMode: 'floor',
-  });
+  try {
+    return i18n.number(num, {
+      notation: 'compact',
+      roundingMode: 'floor',
+    });
+  } catch (e) {
+    return num;
+  }
 }