Skip to content

Refactor: Extract autocomplete filter logic to service class #204

Closed

Description

@malibio

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:

  1. Testable independently - Unit tests for filter rules
  2. Reusable across components - Other components may need similar filtering
  3. 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.ts in /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.svelte to use the new service
  • Ensure all existing autocomplete tests still pass
  • Document the service with JSDoc comments

Related Issues

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

  1. self-assigned this
    on Oct 30, 2025
  2. added 2 commits that reference this issue on Oct 30, 2025
    b129134
    2d6be5c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions