Repository navigation
Closed
Labels
Description
Refactor: Extract Autocomplete Filter Logic to Service Class
Context
From code review of PR #202 (Issue #199), suggestion #5 identifies that the @mention autocomplete filter logic in base-node.svelte should be extracted to a dedicated service class for better maintainability.
Current State
Location: /packages/desktop-app/src/lib/design/components/base-node.svelte:177-203
The filtering rules are complex and embedded directly in the component:
const filtered = allNodes.filter((node) => {
if (node.nodeType === 'date') return false; // Rule 1: Exclude dates
const matchesQuery = /* ... */; // Rule 2: Match query
if (!matchesQuery) return false;
if (node.nodeType !== 'text') return true; // Rule 3: Show non-text
const isContainer = node.containerNodeId === null; // Rule 4: Text containers only
return isContainer;
});Problem
This logic should be:
- Testable independently - Unit tests for filter rules
- Reusable across components - Other components may need similar filtering
- Easy to modify - When requirements change (e.g., Issue Add date picker and shortcuts to @mention system #201 adds date shortcuts)
Proposed Solution
Extract to a dedicated service class: /packages/desktop-app/src/lib/services/node-search-service.ts
export class NodeSearchService {
/**
* Filter nodes for @mention autocomplete
*
* Rules:
* 1. Exclude date nodes (accessible via date shortcuts - Issue #201)
* 2. Match query in title/content (case-insensitive)
* 3. Include ALL non-text nodes regardless of hierarchy
* 4. Include ONLY container text nodes (top-level)
*/
static filterMentionableNodes(
nodes: Node[],
query: string,
options: { excludeNodeTypes?: string[] } = {}
): Node[] {
const excludeTypes = options.excludeNodeTypes || ['date'];
return nodes
.filter(node => !excludeTypes.includes(node.nodeType))
.filter(node => this.matchesQuery(node, query))
.filter(node => this.isMentionable(node));
}
private static matchesQuery(node: Node, query: string): boolean {
const title = node.content.split('\n')[0] || '';
return title.toLowerCase().includes(query.toLowerCase()) ||
node.content.toLowerCase().includes(query.toLowerCase());
}
private static isMentionable(node: Node): boolean {
// Non-text nodes: always mentionable
if (node.nodeType !== 'text') return true;
// Text nodes: only containers
return node.containerNodeId === null;
}
}Benefits
- ✅ Unit testable - Can test filter rules independently of component
- ✅ Centralized business logic - Single source of truth for mentionable node rules
- ✅ Easy to extend - Issue Add date picker and shortcuts to @mention system #201 date shortcuts can be added cleanly
- ✅ Self-documenting - Clear rules with explicit methods
- ✅ Reusable - Other components can use the same filtering logic
Acceptance Criteria
- Create
node-search-service.tsin/packages/desktop-app/src/lib/services/ - Implement
filterMentionableNodes()static method - Implement
matchesQuery()private helper - Implement
isMentionable()private helper - Add comprehensive unit tests for all filter rules
- Update
base-node.svelteto use the new service - Ensure all existing autocomplete tests still pass
- Document the service with JSDoc comments
Related Issues
- Complete @mention system with container node refactoring #199 - @mention system implementation (where this pattern was introduced)
- Add date picker and shortcuts to @mention system #201 - Add date picker and shortcuts (will benefit from this refactor)
Priority
Low - This is a code quality improvement, not a functional bug. Can be done as part of Issue #201 or as a standalone refactoring task.
Labels
refactor, foundation
Activity
Address code review feedback from PR #202
Refactor: Extract autocomplete filter logic to NodeSearchService
Fix: Correct misleading test comment for whitespace query
Merge pull request: Extract autocomplete filter logic to NodeSearchSe…
Address code review feedback from PR #202
Refactor: Extract autocomplete filter logic to NodeSearchService
Fix: Correct misleading test comment for whitespace query
Merge pull request: Extract autocomplete filter logic to NodeSearchSe…
Address code review feedback from PR #202
Refactor: Extract autocomplete filter logic to NodeSearchService
Fix: Correct misleading test comment for whitespace query
Merge pull request: Extract autocomplete filter logic to NodeSearchSe…
Address code review feedback from PR #202
Refactor: Extract autocomplete filter logic to NodeSearchService
Fix: Correct misleading test comment for whitespace query
Merge pull request: Extract autocomplete filter logic to NodeSearchSe…