From bb0bce31a86da561a4b1fc8989093e145d53af58 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 9 Jan 2026 10:00:15 +0000 Subject: [PATCH] Refactor: extract emoji shortcode regex to constant Move the emoji shortcode matching pattern to EMOJI_SHORTCODE_REGEX constant in emoji-helpers.ts for reuse across reaction renderers. --- src/components/nostr/compact/ReactionCompactPreview.tsx | 3 ++- src/components/nostr/kinds/ReactionRenderer.tsx | 3 ++- src/lib/emoji-helpers.ts | 6 ++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/components/nostr/compact/ReactionCompactPreview.tsx b/src/components/nostr/compact/ReactionCompactPreview.tsx index cb39993..fc0faca 100644 --- a/src/components/nostr/compact/ReactionCompactPreview.tsx +++ b/src/components/nostr/compact/ReactionCompactPreview.tsx @@ -4,6 +4,7 @@ import { Heart, ThumbsUp, ThumbsDown, Flame, Smile } from "lucide-react"; import { useNostrEvent } from "@/hooks/useNostrEvent"; import { UserName } from "../UserName"; import { RichText } from "../RichText"; +import { EMOJI_SHORTCODE_REGEX } from "@/lib/emoji-helpers"; /** * Compact preview for Kind 7 (Reaction) @@ -26,7 +27,7 @@ export function ReactionCompactPreview({ event }: { event: NostrEvent }) { // Parse reaction content for custom emoji const parsedReaction = useMemo(() => { - const match = reaction.match(/^:([a-zA-Z0-9_-]+):$/); + const match = reaction.match(EMOJI_SHORTCODE_REGEX); if (match && customEmojis[match[1]]) { return { type: "custom" as const, diff --git a/src/components/nostr/kinds/ReactionRenderer.tsx b/src/components/nostr/kinds/ReactionRenderer.tsx index 25f5c37..10a4c3f 100644 --- a/src/components/nostr/kinds/ReactionRenderer.tsx +++ b/src/components/nostr/kinds/ReactionRenderer.tsx @@ -6,6 +6,7 @@ import { NostrEvent } from "@/types/nostr"; import { KindRenderer } from "./index"; import { EventCardSkeleton } from "@/components/ui/skeleton"; import { parseReplaceableAddress } from "applesauce-core/helpers/pointers"; +import { EMOJI_SHORTCODE_REGEX } from "@/lib/emoji-helpers"; /** * Renderer for Kind 7 - Reactions @@ -32,7 +33,7 @@ export function Kind7Renderer({ event }: BaseEventProps) { // Parse reaction content to detect custom emoji shortcodes // Format: :shortcode: in the content const parsedReaction = useMemo(() => { - const match = reaction.match(/^:([a-zA-Z0-9_-]+):$/); + const match = reaction.match(EMOJI_SHORTCODE_REGEX); if (match && customEmojis[match[1]]) { return { type: "custom" as const, diff --git a/src/lib/emoji-helpers.ts b/src/lib/emoji-helpers.ts index c5a299a..709dcd9 100644 --- a/src/lib/emoji-helpers.ts +++ b/src/lib/emoji-helpers.ts @@ -1,6 +1,12 @@ import { getOrComputeCachedValue } from "applesauce-core/helpers"; import type { NostrEvent } from "@/types/nostr"; +/** + * Regex pattern to match NIP-30 custom emoji shortcodes like :shortcode: + * Supports alphanumeric characters, underscores, and dashes + */ +export const EMOJI_SHORTCODE_REGEX = /^:([a-zA-Z0-9_-]+):$/; + /** * Represents a parsed emoji tag from NIP-30 */