Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Guard comment emoji segmentation on older browsers

Anand Baburajan2b853bcb06e5
diff --git a/web/apps/space/src/utils/comments.ts b/web/apps/space/src/utils/comments.ts
index b605757e7a..a3bad2ad2f 100644
--- a/web/apps/space/src/utils/comments.ts
+++ b/web/apps/space/src/utils/comments.ts
@@ -96,18 +96,23 @@ export const commentReactionGroups = (
}));
};
-const commentEmojiSegmenter = new Intl.Segmenter(undefined, {
- granularity: "grapheme",
-});
+const commentEmojiSegmenter =
+ typeof Intl !== "undefined" && typeof Intl.Segmenter == "function"
+ ? new Intl.Segmenter(undefined, { granularity: "grapheme" })
+ : null;
export const isEmojiOnlyComment = (text: string) => {
- const segments = [
- ...commentEmojiSegmenter.segment(text.replace(/\s/g, "")),
- ];
+ const compact = text.replace(/\s/g, "");
+ const segments = commentEmojiSegmenter
+ ? Array.from(
+ commentEmojiSegmenter.segment(compact),
+ ({ segment }) => segment,
+ )
+ : Array.from(compact);
return (
segments.length > 0 &&
segments.length <= 3 &&
- segments.every(({ segment }) =>
+ segments.every((segment) =>
/\p{Extended_Pictographic}|\p{Regional_Indicator}|\u20E3/u.test(
segment,
),
diff --git a/web/apps/space/tests/comments.test.ts b/web/apps/space/tests/comments.test.ts
index f07a2cb334..2de6b59991 100644
--- a/web/apps/space/tests/comments.test.ts
+++ b/web/apps/space/tests/comments.test.ts
@@ -1,4 +1,4 @@
-import { expect, test } from "vitest";
+import { expect, test, vi } from "vitest";
import type { SpaceComment, SpaceNotification } from "../src/services/space";
import {
commentNotificationText,
@@ -84,6 +84,22 @@ test.each(["", " ", "123", "Nice πŸ˜‚", "πŸ˜‚πŸ˜‚πŸ˜‚πŸ˜‚", "á", "hello"])(
(text) => expect(isEmojiOnlyComment(text)).toBe(false),
);
+test("comments load and classify simple emojis without Intl.Segmenter", async () => {
+ const descriptor = Object.getOwnPropertyDescriptor(Intl, "Segmenter")!;
+ Reflect.deleteProperty(Intl, "Segmenter");
+ vi.resetModules();
+ try {
+ const comments = await import("../src/utils/comments");
+ expect(comments.isEmojiOnlyComment("πŸ˜‚ πŸ₯ͺ")).toBe(true);
+ expect(comments.isEmojiOnlyComment("Nice πŸ˜‚")).toBe(false);
+ expect(comments.isEmojiOnlyComment("πŸ˜‚πŸ˜‚πŸ˜‚πŸ˜‚")).toBe(false);
+ expect(comments.isEmojiOnlyComment("πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦")).toBe(false);
+ } finally {
+ Object.defineProperty(Intl, "Segmenter", descriptor);
+ vi.resetModules();
+ }
+});
+
test("reactions stay grouped by emoji with your group and your row first", () => {
const reactions = [
reaction("alice", "❀️"),