From 8f6b5d3df9b5a5bb61514497f3f25289513ef119 Mon Sep 17 00:00:00 2001
From: Ollie H <renahlee@outlook.com>
Date: Tue, 9 May 2023 10:13:23 -0700
Subject: Add avatar to mobile autocomplete and create grapheme hook (#602)

* Add avatar to mobile autocomplete and create grapheme hook

* Remove comment, update filename, cut out redundant logic
---
 .../com/composer/text-input/hooks/useGrapheme.tsx  | 36 ++++++++++++++++++++++
 1 file changed, 36 insertions(+)
 create mode 100644 src/view/com/composer/text-input/hooks/useGrapheme.tsx

(limited to 'src/view/com/composer/text-input/hooks/useGrapheme.tsx')

diff --git a/src/view/com/composer/text-input/hooks/useGrapheme.tsx b/src/view/com/composer/text-input/hooks/useGrapheme.tsx
new file mode 100644
index 000000000..25947c3ec
--- /dev/null
+++ b/src/view/com/composer/text-input/hooks/useGrapheme.tsx
@@ -0,0 +1,36 @@
+import Graphemer from 'graphemer'
+import {useCallback, useMemo} from 'react'
+
+export const useGrapheme = () => {
+  const splitter = useMemo(() => new Graphemer(), [])
+
+  const getGraphemeString = useCallback(
+    (name: string, length: number) => {
+      let remainingCharacters = 0
+
+      if (name.length > length) {
+        const graphemes = splitter.splitGraphemes(name)
+
+        if (graphemes.length > length) {
+          remainingCharacters = 0
+          name = `${graphemes.slice(0, length).join('')}...`
+        } else {
+          remainingCharacters = length - graphemes.length
+          name = graphemes.join('')
+        }
+      } else {
+        remainingCharacters = length - name.length
+      }
+
+      return {
+        name,
+        remainingCharacters,
+      }
+    },
+    [splitter],
+  )
+
+  return {
+    getGraphemeString,
+  }
+}
-- 
cgit 1.4.1

