refactor: remove unnecessary useMemo from applesauce helper calls

Phase 1 of applesauce helpers refactoring plan.

Removed useMemo from direct applesauce helper calls since these helpers
cache their results internally using symbol-based caching. This improves
code clarity without sacrificing performance.

Changes:
- ArticleRenderer.tsx: removed useMemo from getArticleTitle, getArticleSummary
- HighlightRenderer.tsx: removed useMemo from 6 highlight helpers
- HighlightDetailRenderer.tsx: removed useMemo from 6 highlight helpers
- CodeSnippetDetailRenderer.tsx: removed useMemo from 8 NIP-C0 helpers
- ChatView.tsx: removed useMemo from getNip10References, getTagValue

Kept useMemo in LiveActivityRenderer.tsx for parseLiveActivity and related
functions since these don't implement their own caching (noted for future
optimization).

All tests pass (607 tests).
This commit is contained in:
Claude
2025-12-22 13:18:44 +00:00
parent 2189d5a969
commit 4b7148510a
5 changed files with 32 additions and 44 deletions

View File

@@ -91,9 +91,10 @@ export function ChatView({ events, className }: ChatViewProps) {
}
function ChatMessage({ event }: { event: NostrEvent }) {
const threadRefs = useMemo(() => getNip10References(event), [event]);
// Both helpers cache internally, no useMemo needed
const threadRefs = getNip10References(event);
const replyToId = threadRefs.reply?.e?.id;
const qTagValue = useMemo(() => getTagValue(event, "q"), [event]);
const qTagValue = getTagValue(event, "q");
return (
<div className="flex flex-col gap-0.5">

View File

@@ -1,4 +1,3 @@
import { useMemo } from "react";
import {
BaseEventContainer,
BaseEventProps,
@@ -12,10 +11,11 @@ import {
/**
* Renderer for Kind 30023 - Long-form Article
* Displays article title and summary in feed
* Note: getArticleTitle and getArticleSummary cache internally, no useMemo needed
*/
export function Kind30023Renderer({ event }: BaseEventProps) {
const title = useMemo(() => getArticleTitle(event), [event]);
const summary = useMemo(() => getArticleSummary(event), [event]);
const title = getArticleTitle(event);
const summary = getArticleSummary(event);
return (
<BaseEventContainer event={event}>

View File

@@ -29,19 +29,21 @@ interface Kind1337DetailRendererProps {
/**
* Detail renderer for Kind 1337 - Code Snippet (NIP-C0)
* Full view with all metadata and complete code
* Note: NIP-C0 helpers wrap getTagValue which caches internally
*/
export function Kind1337DetailRenderer({ event }: Kind1337DetailRendererProps) {
const { addWindow } = useGrimoire();
const { copy, copied } = useCopy();
const name = useMemo(() => getCodeName(event), [event]);
const language = useMemo(() => getCodeLanguage(event), [event]);
const extension = useMemo(() => getCodeExtension(event), [event]);
const description = useMemo(() => getCodeDescription(event), [event]);
const runtime = useMemo(() => getCodeRuntime(event), [event]);
const licenses = useMemo(() => getCodeLicenses(event), [event]);
const dependencies = useMemo(() => getCodeDependencies(event), [event]);
const repo = useMemo(() => getCodeRepo(event), [event]);
// All these helpers wrap getTagValue, which caches internally
const name = getCodeName(event);
const language = getCodeLanguage(event);
const extension = getCodeExtension(event);
const description = getCodeDescription(event);
const runtime = getCodeRuntime(event);
const licenses = getCodeLicenses(event);
const dependencies = getCodeDependencies(event);
const repo = getCodeRepo(event);
// Parse NIP-34 repository address if present
const repoPointer = useMemo(() => {

View File

@@ -1,4 +1,3 @@
import { useMemo } from "react";
import { ExternalLink } from "lucide-react";
import type { NostrEvent } from "@/types/nostr";
import {
@@ -16,23 +15,18 @@ import { useGrimoire } from "@/core/state";
/**
* Detail renderer for Kind 9802 - Highlight
* Shows highlighted text, comment, context, and embedded source event
* Note: All applesauce helpers cache internally, no useMemo needed
*/
export function Kind9802DetailRenderer({ event }: { event: NostrEvent }) {
const { addWindow } = useGrimoire();
const highlightText = useMemo(() => getHighlightText(event), [event]);
const comment = useMemo(() => getHighlightComment(event), [event]);
const context = useMemo(() => getHighlightContext(event), [event]);
const sourceUrl = useMemo(() => getHighlightSourceUrl(event), [event]);
const highlightText = getHighlightText(event);
const comment = getHighlightComment(event);
const context = getHighlightContext(event);
const sourceUrl = getHighlightSourceUrl(event);
// Get source event pointer (e tag) or address pointer (a tag)
const eventPointer = useMemo(
() => getHighlightSourceEventPointer(event),
[event],
);
const addressPointer = useMemo(
() => getHighlightSourceAddressPointer(event),
[event],
);
const eventPointer = getHighlightSourceEventPointer(event);
const addressPointer = getHighlightSourceAddressPointer(event);
// Format created date
const createdDate = new Date(event.created_at * 1000).toLocaleDateString(

View File

@@ -1,4 +1,3 @@
import { useMemo } from "react";
import { BaseEventContainer, BaseEventProps } from "./BaseEventRenderer";
import { ExternalLink } from "lucide-react";
import {
@@ -18,36 +17,28 @@ import { KindBadge } from "@/components/KindBadge";
/**
* Renderer for Kind 9802 - Highlight
* Displays highlighted text with optional comment, compact source event preview, and source URL
* Note: All applesauce helpers cache internally, no useMemo needed
*/
export function Kind9802Renderer({ event }: BaseEventProps) {
const { addWindow } = useGrimoire();
const highlightText = useMemo(() => getHighlightText(event), [event]);
const sourceUrl = useMemo(() => getHighlightSourceUrl(event), [event]);
const comment = useMemo(() => getHighlightComment(event), [event]);
const highlightText = getHighlightText(event);
const sourceUrl = getHighlightSourceUrl(event);
const comment = getHighlightComment(event);
// Get source event pointer (e tag) or address pointer (a tag) for Nostr event references
const eventPointer = useMemo(
() => getHighlightSourceEventPointer(event),
[event],
);
const addressPointer = useMemo(
() => getHighlightSourceAddressPointer(event),
[event],
);
const eventPointer = getHighlightSourceEventPointer(event);
const addressPointer = getHighlightSourceAddressPointer(event);
// Load the source event for preview
const sourceEvent = useNostrEvent(eventPointer || addressPointer);
// Extract title or content preview from source event
const sourcePreview = useMemo(() => {
// Extract title or content preview from source event (getArticleTitle caches internally)
const sourcePreview = (() => {
if (!sourceEvent) return null;
const title = getArticleTitle(sourceEvent);
if (title) return title;
// Fall back to content
return sourceEvent.content || null;
}, [sourceEvent]);
})();
// Handle click to open source event
const handleOpenEvent = () => {