Skip to content

Reactive collections sidebar updates on member_of changes - #833

Merged
malibio merged 2 commits into
mainfrom
feature/issue-832-reactive-collection-sidebar
Jan 26, 2026
Merged

malibio merged 2 commits into
mainfrom
feature/issue-832-reactive-collection-sidebar

Conversation

@malibio

@malibio malibio commented Jan 26, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Collections sidebar now automatically refreshes when collection nodes are created or member_of relationships change
  • Added node_type to node:created domain events to enable reactive UI updates without fetching full node data
  • Debounced refresh (300ms) to handle bulk operations efficiently

Changes

Backend:

  • Added node_type field to DomainEvent::NodeCreated
  • Updated domain event forwarder (Tauri) and dev-proxy (SSE) to include node_type

Frontend:

  • Both sync services now react to collection node creation and member_of relationship changes
  • Debounced collection refresh prevents excessive API calls during bulk imports

Test plan

  • All existing tests pass (3468 frontend, 722 Rust)
  • Quality checks pass (eslint, svelte-check, clippy)
  • Manual test: Create collection via MCP, verify sidebar updates
  • Manual test: Add node to collection via MCP, verify member count updates
  • Manual test: Bulk import collections, verify sidebar updates after debounce

Closes #832

🤖 Generated with Claude Code

The collections sidebar now automatically refreshes when:
- New collection nodes are created (via node:created event with nodeType)
- Nodes are added to collections (via member_of relationship created)
- Nodes are removed from collections (via member_of relationship deleted)

## Backend Changes
- Added `node_type` field to `DomainEvent::NodeCreated` to enable reactive
  UI updates without requiring the frontend to fetch full node data
- Updated domain event forwarder (Tauri) to include node_type in payload
- Updated dev-proxy SSE events (browser mode) to include node_type

## Frontend Changes
- Both sync services (tauri-sync-listener, browser-sync-service) now:
  - React to collection node creation by refreshing collections
  - React to member_of relationship changes by refreshing collections
  - Use debounced refresh (300ms) to handle bulk operations efficiently
  - Refresh selected collection's members if affected

## Design Decisions
- node_type is only included in node:created events (not updated/deleted)
  since that's the only case where the frontend needs to know the type
  without already having the node in its store
- Debouncing prevents excessive API calls during bulk imports (like the
  30+ collections created during documentation import in #770)

Closes #832

Co-Authored-By: Claude <[email protected]>
@malibio

malibio commented Jan 26, 2026

Copy link
Copy Markdown
Collaborator Author

Code Review: Reactive Collections Sidebar (#832)

Review Type: Initial Review


Requirements Check

From Issue #832:

  • When a member_of relationship is created/deleted, the collections sidebar automatically updates
  • New collections appear immediately (via node:created event with nodeType)
  • Member counts update in real-time (via relationship events)
  • Debouncing implemented for bulk operations (300ms)
  • Both sync services updated (tauri-sync-listener.ts and browser-sync-service.ts)

All acceptance criteria have been met.


Code Review Findings

Architecture & Design

Net Positive Assessment: This PR cleanly extends the existing event-driven architecture to support reactive collection updates. The implementation follows established patterns (ID-only events, debouncing for bulk operations) and maintains consistency between Tauri and browser sync services.

Design Decision - Well Justified:
The choice to include node_type only in node:created events (not updated/deleted) is sound. The commit message explains the rationale clearly: created events are the only case where the frontend needs type information without already having the node cached.


Suggested Improvements

1. packages/desktop-app/src/lib/services/browser-sync-service.ts:99-132 and packages/desktop-app/src/lib/services/tauri-sync-listener.ts:37-70

[Improvement] Code Duplication - DRY Principle

The scheduleCollectionRefresh function is duplicated verbatim in both sync services (browser-sync-service.ts and tauri-sync-listener.ts). This violates the DRY (Don't Repeat Yourself) principle.

Recommendation: Extract to a shared utility module:

// packages/desktop-app/src/lib/utils/collection-refresh.ts
export function scheduleCollectionRefresh(affectedCollectionId?: string): void {
  // ... shared implementation
}

Both services could then import and use this shared function. This would:

  • Reduce maintenance burden when the debounce logic changes
  • Ensure consistent behavior across both sync modes
  • Make testing easier (single source of truth)

2. packages/desktop-app/src/lib/services/browser-sync-service.ts:99-100

[Nit] Module-level mutable state

let collectionRefreshTimer: ReturnType<typeof setTimeout> | null = null;
const COLLECTION_REFRESH_DEBOUNCE_MS = 300;

Module-level let variables can cause issues with hot module replacement (HMR) and make the code harder to test in isolation. Consider encapsulating this state within the BrowserSyncService class as private instance properties. However, since the service is a singleton, this is a minor concern.


3. packages/desktop-app/src/lib/services/browser-sync-service.ts:290-296

[Nit] Comment notes potential improvement for nodeDeleted

The comment correctly identifies that deleted nodes may be collections, but the current implementation relies on "UI to handle stale data gracefully." This is acceptable for now, but consider a future enhancement: track node types in the collections store or include node_type in delete events for more precise reactivity.


Security

No security concerns identified. The changes:

  • Do not introduce new user input handling
  • Follow existing event filtering patterns to prevent feedback loops
  • Use established backend-to-frontend communication channels

Testing

Test Updates: All existing tests have been updated to include the new nodeType field in nodeCreated events. The test file changes are comprehensive and ensure backward compatibility.

Test Coverage Assessment:

  • Unit tests for SSE event handling are updated appropriately
  • The PR notes that manual testing is required for full validation (collection creation via MCP, bulk imports)

[Suggestion] Consider adding a dedicated test case for the debounce behavior:

it('should debounce rapid collection refresh calls', async () => {
  // Create multiple member_of relationships rapidly
  // Verify loadCollections is called only once after debounce
});

Performance

Positive: The 300ms debounce is appropriate for bulk operations. During the documentation import mentioned in Issue #770 (30+ collections), this prevents 30+ API calls from being made, reducing them to a single call after the batch completes.

Observation: The implementation fetches ALL collections on any change (collectionsData.loadCollections()). For large collection counts, consider future optimization to update only the affected collection(s). However, for the current scale, this approach is pragmatic and correct.


Summary

This is a well-implemented feature that cleanly addresses the issue requirements. The code follows existing architectural patterns, includes appropriate documentation (issue references, JSDoc comments), and maintains consistency between the two sync services.

Minor improvements suggested:

  1. Extract duplicated scheduleCollectionRefresh to shared utility (DRY)
  2. Consider future test coverage for debounce behavior

Recommendation: APPROVE

The changes are a clear net positive. The suggested improvements are minor and can be addressed in future PRs if desired.

@malibio malibio left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review complete - see detailed findings above. This is a net positive change ready for merge. (Note: Cannot auto-approve own PR)

Addresses reviewer recommendation from PR #833:
- Created shared collection-refresh.ts utility module
- Updated browser-sync-service.ts to import from shared utility
- Updated tauri-sync-listener.ts to import from shared utility

This follows the DRY (Don't Repeat Yourself) principle by consolidating
the duplicated debounce logic into a single source of truth.

Reviewer suggestions NOT addressed (with justification):
- Module-level mutable state: Reviewer noted this is "minor concern" for
  singleton services. Current implementation works correctly.
- Test coverage for debounce: Valid future improvement but outside scope
  of addressing immediate review feedback. Simple timeout logic.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@malibio

malibio commented Jan 26, 2026

Copy link
Copy Markdown
Collaborator Author

Review Feedback Addressed

Summary

Recommendation Severity Status Justification
Extract duplicated scheduleCollectionRefresh to shared utility 🟢 Suggestion ✅ Addressed Created collection-refresh.ts utility
Encapsulate module-level mutable state 🟢 Nit ⏭️ Skipped Reviewer noted "minor concern" for singletons
Add test coverage for debounce behavior 🟢 Suggestion ⏭️ Skipped Outside scope; simple timeout logic

Changes Made

Commit: 5144ac77 - "Address review: Extract scheduleCollectionRefresh to shared utility"

  • Created packages/desktop-app/src/lib/utils/collection-refresh.ts
    • Single source of truth for debounced collection refresh logic
    • Exported scheduleCollectionRefresh() and clearCollectionRefreshTimer()
  • Updated browser-sync-service.ts to import from shared utility
  • Updated tauri-sync-listener.ts to import from shared utility

Verification

  • ✅ All 3468 tests passing
  • ✅ Quality checks pass (bun run quality:fix)
  • ✅ Pre-commit hooks pass

Skipped Recommendations (Justification)

  1. Module-level mutable state: The reviewer explicitly noted this is a "minor concern" since the services are singletons. Encapsulating within the class would require significant refactoring for marginal benefit.

  2. Test coverage for debounce: While valid for future improvement, this extends beyond the scope of addressing review feedback. The debounce logic is simple and well-understood timeout behavior.

@malibio
malibio merged commit 7bbbbd9 into main Jan 26, 2026
@malibio
malibio deleted the feature/issue-832-reactive-collection-sidebar branch January 26, 2026 16:51
malibio added a commit that referenced this pull request Feb 4, 2026
* Reactive collections sidebar updates on member_of changes (#832)

The collections sidebar now automatically refreshes when:
- New collection nodes are created (via node:created event with nodeType)
- Nodes are added to collections (via member_of relationship created)
- Nodes are removed from collections (via member_of relationship deleted)

## Backend Changes
- Added `node_type` field to `DomainEvent::NodeCreated` to enable reactive
  UI updates without requiring the frontend to fetch full node data
- Updated domain event forwarder (Tauri) to include node_type in payload
- Updated dev-proxy SSE events (browser mode) to include node_type

## Frontend Changes
- Both sync services (tauri-sync-listener, browser-sync-service) now:
  - React to collection node creation by refreshing collections
  - React to member_of relationship changes by refreshing collections
  - Use debounced refresh (300ms) to handle bulk operations efficiently
  - Refresh selected collection's members if affected

## Design Decisions
- node_type is only included in node:created events (not updated/deleted)
  since that's the only case where the frontend needs to know the type
  without already having the node in its store
- Debouncing prevents excessive API calls during bulk imports (like the
  30+ collections created during documentation import in #770)

Closes #832

Co-Authored-By: Claude <[email protected]>

* Address review: Extract scheduleCollectionRefresh to shared utility

Addresses reviewer recommendation from PR #833:
- Created shared collection-refresh.ts utility module
- Updated browser-sync-service.ts to import from shared utility
- Updated tauri-sync-listener.ts to import from shared utility

This follows the DRY (Don't Repeat Yourself) principle by consolidating
the duplicated debounce logic into a single source of truth.

Reviewer suggestions NOT addressed (with justification):
- Module-level mutable state: Reviewer noted this is "minor concern" for
  singleton services. Current implementation works correctly.
- Test coverage for debounce: Valid future improvement but outside scope
  of addressing immediate review feedback. Simple timeout logic.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

---------

Co-authored-by: Claude <[email protected]>
malibio added a commit that referenced this pull request Feb 5, 2026
* Reactive collections sidebar updates on member_of changes (#832)

The collections sidebar now automatically refreshes when:
- New collection nodes are created (via node:created event with nodeType)
- Nodes are added to collections (via member_of relationship created)
- Nodes are removed from collections (via member_of relationship deleted)

## Backend Changes
- Added `node_type` field to `DomainEvent::NodeCreated` to enable reactive
  UI updates without requiring the frontend to fetch full node data
- Updated domain event forwarder (Tauri) to include node_type in payload
- Updated dev-proxy SSE events (browser mode) to include node_type

## Frontend Changes
- Both sync services (tauri-sync-listener, browser-sync-service) now:
  - React to collection node creation by refreshing collections
  - React to member_of relationship changes by refreshing collections
  - Use debounced refresh (300ms) to handle bulk operations efficiently
  - Refresh selected collection's members if affected

## Design Decisions
- node_type is only included in node:created events (not updated/deleted)
  since that's the only case where the frontend needs to know the type
  without already having the node in its store
- Debouncing prevents excessive API calls during bulk imports (like the
  30+ collections created during documentation import in #770)

Closes #832

Co-Authored-By: Claude <[email protected]>

* Address review: Extract scheduleCollectionRefresh to shared utility

Addresses reviewer recommendation from PR #833:
- Created shared collection-refresh.ts utility module
- Updated browser-sync-service.ts to import from shared utility
- Updated tauri-sync-listener.ts to import from shared utility

This follows the DRY (Don't Repeat Yourself) principle by consolidating
the duplicated debounce logic into a single source of truth.

Reviewer suggestions NOT addressed (with justification):
- Module-level mutable state: Reviewer noted this is "minor concern" for
  singleton services. Current implementation works correctly.
- Test coverage for debounce: Valid future improvement but outside scope
  of addressing immediate review feedback. Simple timeout logic.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

---------

Co-authored-by: Claude <[email protected]>
malibio added a commit that referenced this pull request Feb 26, 2026
* Reactive collections sidebar updates on member_of changes (#832)

The collections sidebar now automatically refreshes when:
- New collection nodes are created (via node:created event with nodeType)
- Nodes are added to collections (via member_of relationship created)
- Nodes are removed from collections (via member_of relationship deleted)

## Backend Changes
- Added `node_type` field to `DomainEvent::NodeCreated` to enable reactive
  UI updates without requiring the frontend to fetch full node data
- Updated domain event forwarder (Tauri) to include node_type in payload
- Updated dev-proxy SSE events (browser mode) to include node_type

## Frontend Changes
- Both sync services (tauri-sync-listener, browser-sync-service) now:
  - React to collection node creation by refreshing collections
  - React to member_of relationship changes by refreshing collections
  - Use debounced refresh (300ms) to handle bulk operations efficiently
  - Refresh selected collection's members if affected

## Design Decisions
- node_type is only included in node:created events (not updated/deleted)
  since that's the only case where the frontend needs to know the type
  without already having the node in its store
- Debouncing prevents excessive API calls during bulk imports (like the
  30+ collections created during documentation import in #770)

Closes #832

Co-Authored-By: Claude <[email protected]>

* Address review: Extract scheduleCollectionRefresh to shared utility

Addresses reviewer recommendation from PR #833:
- Created shared collection-refresh.ts utility module
- Updated browser-sync-service.ts to import from shared utility
- Updated tauri-sync-listener.ts to import from shared utility

This follows the DRY (Don't Repeat Yourself) principle by consolidating
the duplicated debounce logic into a single source of truth.

Reviewer suggestions NOT addressed (with justification):
- Module-level mutable state: Reviewer noted this is "minor concern" for
  singleton services. Current implementation works correctly.
- Test coverage for debounce: Valid future improvement but outside scope
  of addressing immediate review feedback. Simple timeout logic.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

---------

Co-authored-by: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Collections sidebar should reactively update when member_of relationships change

1 participant