Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix([issue-719]): skip re-render when media annotation socket update is unchanged

[._.]/ Adam Eivyed6eab86ab94
diff --git a/client/src/hooks/useMediaAnnotations.js b/client/src/hooks/useMediaAnnotations.js
index b8db53ef63..66f693b892 100644
--- a/client/src/hooks/useMediaAnnotations.js
+++ b/client/src/hooks/useMediaAnnotations.js
@@ -2,6 +2,7 @@ import { useCallback, useEffect, useRef, useState } from 'react';
import { listMediaAnnotations, setMediaAnnotation } from '../services/api';
import socket from '../services/socket';
import toast from '../components/ui/Toast';
+import { sameJsonShape } from '../lib/sameJsonShape';
// Per-entry: `own`/`others` from the server, plus back-compat aliases
// (`starred`/`note`/`updatedAt` = local author) and `anyNote` for the
@@ -56,8 +57,17 @@ export function useMediaAnnotations() {
const onUpdate = ({ key, entry }) => {
if (!key) return;
setAnnotations((prev) => {
- const next = { ...prev };
const enriched = enrich(entry);
+ // Bail out when the broadcast carries nothing new: an identical entry
+ // (the originator re-receives its own write) or a delete of a key we
+ // never held. Returning `prev` lets React skip the re-render and the
+ // downstream MediaCard churn instead of allocating a fresh map.
+ if (enriched) {
+ if (sameJsonShape(prev[key], enriched)) return prev;
+ } else if (!(key in prev)) {
+ return prev;
+ }
+ const next = { ...prev };
if (enriched) next[key] = enriched;
else delete next[key];
return next;
diff --git a/client/src/hooks/useMediaAnnotations.test.jsx b/client/src/hooks/useMediaAnnotations.test.jsx
new file mode 100644
index 0000000000..4a23d16ede
--- /dev/null
+++ b/client/src/hooks/useMediaAnnotations.test.jsx
@@ -0,0 +1,96 @@
+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
+import { renderHook, act, cleanup, waitFor } from '@testing-library/react';
+
+// Mock the socket module so the test can drive the 'media:annotation:updated'
+// handler that the hook subscribes to.
+const handlers = new Map();
+vi.mock('../services/socket', () => ({
+ default: {
+ emit: vi.fn(),
+ on: (event, fn) => { handlers.set(event, fn); },
+ off: (event, fn) => { if (handlers.get(event) === fn) handlers.delete(event); },
+ },
+}));
+
+// Mock the API so the initial fetch resolves to a known map.
+const listMediaAnnotations = vi.fn();
+const setMediaAnnotation = vi.fn();
+vi.mock('../services/api', () => ({
+ listMediaAnnotations: (...args) => listMediaAnnotations(...args),
+ setMediaAnnotation: (...args) => setMediaAnnotation(...args),
+}));
+
+vi.mock('../components/ui/Toast', () => ({ default: { error: vi.fn() } }));
+
+import { useMediaAnnotations } from './useMediaAnnotations.js';
+
+const emit = (payload) => act(() => { handlers.get('media:annotation:updated')(payload); });
+
+describe('useMediaAnnotations — socket bail-out guard', () => {
+ beforeEach(() => {
+ handlers.clear();
+ listMediaAnnotations.mockResolvedValue({ annotations: {} });
+ setMediaAnnotation.mockReset();
+ });
+
+ afterEach(() => {
+ cleanup();
+ });
+
+ it('applies a new entry from the socket broadcast', async () => {
+ const { result } = renderHook(() => useMediaAnnotations());
+ await waitFor(() => expect(handlers.has('media:annotation:updated')).toBe(true));
+
+ emit({ key: 'a.png', entry: { own: { starred: true, note: '', updatedAt: '2026-01-01T00:00:00Z' } } });
+
+ expect(result.current.annotations['a.png']?.starred).toBe(true);
+ });
+
+ it('returns the same annotations reference when an identical entry is rebroadcast', async () => {
+ const { result } = renderHook(() => useMediaAnnotations());
+ await waitFor(() => expect(handlers.has('media:annotation:updated')).toBe(true));
+
+ const entry = { own: { starred: true, note: 'hi', updatedAt: '2026-01-01T00:00:00Z' } };
+ emit({ key: 'a.png', entry });
+ const first = result.current.annotations;
+
+ // Re-broadcast the same entry (the originator re-receives its own write).
+ emit({ key: 'a.png', entry: { own: { starred: true, note: 'hi', updatedAt: '2026-01-01T00:00:00Z' } } });
+
+ expect(result.current.annotations).toBe(first);
+ });
+
+ it('still re-renders when a field changes (e.g. a newer updatedAt)', async () => {
+ const { result } = renderHook(() => useMediaAnnotations());
+ await waitFor(() => expect(handlers.has('media:annotation:updated')).toBe(true));
+
+ emit({ key: 'a.png', entry: { own: { starred: false, note: 'first', updatedAt: '2026-01-01T00:00:00Z' } } });
+ const first = result.current.annotations;
+
+ emit({ key: 'a.png', entry: { own: { starred: false, note: 'second', updatedAt: '2026-01-02T00:00:00Z' } } });
+
+ expect(result.current.annotations).not.toBe(first);
+ expect(result.current.annotations['a.png']?.note).toBe('second');
+ });
+
+ it('returns the same reference when deleting a key that was never present', async () => {
+ const { result } = renderHook(() => useMediaAnnotations());
+ await waitFor(() => expect(handlers.has('media:annotation:updated')).toBe(true));
+
+ const before = result.current.annotations;
+ emit({ key: 'missing.png', entry: null });
+
+ expect(result.current.annotations).toBe(before);
+ });
+
+ it('removes an existing key when the broadcast clears it', async () => {
+ const { result } = renderHook(() => useMediaAnnotations());
+ await waitFor(() => expect(handlers.has('media:annotation:updated')).toBe(true));
+
+ emit({ key: 'a.png', entry: { own: { starred: true, note: '', updatedAt: '2026-01-01T00:00:00Z' } } });
+ expect(result.current.annotations['a.png']).toBeTruthy();
+
+ emit({ key: 'a.png', entry: null });
+ expect('a.png' in result.current.annotations).toBe(false);
+ });
+});