Skip to content

QueryPreferencesService for User View Preferences #443

Closed

Description

@malibio

Overview

Implement QueryPreferencesService for storing and retrieving per-user, per-query view preferences. Supports last-used view selection, view-specific configurations, and caching for performance.

Architecture References

  • Service Patterns: See existing services in packages/desktop-app/src/lib/services/
  • SharedNodeStore: src/lib/services/shared-node-store.ts - Similar caching pattern

Problem Statement

Users need their view preferences (last selected view, table columns, kanban groupBy, etc.) to persist across sessions. Each query should remember the user's preferred view independently.

Dependencies

Proposed Solution

Storage Strategy

Phase 1 (This Issue): localStorage with in-memory cache

  • Simple, no backend changes needed
  • Works immediately
  • Per-browser storage (not synced)

Phase 2 (Future): Database storage via Tauri command

  • Cross-device sync
  • Multi-user support
  • Backend persistence

Interface remains same, just swap implementation.

Implementation

QueryPreferences Interface

File: packages/desktop-app/src/lib/types/query-preferences.ts

import type { ListViewConfig, TableViewConfig, KanbanViewConfig } from './query';

export interface QueryPreferences {
  lastView: 'list' | 'table' | 'kanban';
  viewConfigs: {
    list?: ListViewConfig;
    table?: TableViewConfig;
    kanban?: KanbanViewConfig;
  };
}

export const DEFAULT_QUERY_PREFERENCES: QueryPreferences = {
  lastView: 'list',
  viewConfigs: {}
};

QueryPreferencesService

File: packages/desktop-app/src/lib/services/query-preferences-service.ts

import type { QueryPreferences } from '$lib/types/query-preferences';
import type { QueryViewConfig } from '$lib/types/query';
import { DEFAULT_QUERY_PREFERENCES } from '$lib/types/query-preferences';

class QueryPreferencesService {
  private cache = new Map<string, QueryPreferences>();
  private STORAGE_PREFIX = 'query-prefs-';

  async getPreferences(queryNodeId: string): Promise<QueryPreferences> {
    // Check cache first
    if (this.cache.has(queryNodeId)) {
      return this.cache.get(queryNodeId)\!;
    }

    // Load from storage
    const prefs = this.loadFromStorage(queryNodeId);
    
    // Cache and return
    this.cache.set(queryNodeId, prefs);
    return prefs;
  }

  async saveViewConfig(
    queryNodeId: string,
    view: 'list' | 'table' | 'kanban',
    config: Partial<QueryViewConfig>
  ): Promise<void> {
    const prefs = await this.getPreferences(queryNodeId);
    
    prefs.lastView = view;
    prefs.viewConfigs[view] = {
      ...prefs.viewConfigs[view],
      ...config
    } as any;
    
    this.saveToStorage(queryNodeId, prefs);
    this.cache.set(queryNodeId, prefs);
  }

  clearPreferences(queryNodeId: string): void {
    this.cache.delete(queryNodeId);
    localStorage.removeItem(this.getStorageKey(queryNodeId));
  }

  invalidateCache(queryNodeId: string): void {
    this.cache.delete(queryNodeId);
  }

  private loadFromStorage(queryNodeId: string): QueryPreferences {
    try {
      const stored = localStorage.getItem(this.getStorageKey(queryNodeId));
      if (\!stored) return { ...DEFAULT_QUERY_PREFERENCES };
      
      const parsed = JSON.parse(stored);
      if (\!parsed.lastView || \!parsed.viewConfigs) {
        return { ...DEFAULT_QUERY_PREFERENCES };
      }
      return parsed;
    } catch {
      return { ...DEFAULT_QUERY_PREFERENCES };
    }
  }

  private saveToStorage(queryNodeId: string, prefs: QueryPreferences): void {
    localStorage.setItem(this.getStorageKey(queryNodeId), JSON.stringify(prefs));
  }

  private getStorageKey(queryNodeId: string): string {
    return `${this.STORAGE_PREFIX}${queryNodeId}`;
  }
}

export const queryPreferencesService = new QueryPreferencesService();

Usage in QueryNodeViewer

onMount(async () => {
  const prefs = await queryPreferencesService.getPreferences(nodeId);
  activeView = prefs.lastView;
});

async function switchView(view: 'list' | 'table' | 'kanban') {
  activeView = view;
  await queryPreferencesService.saveViewConfig(nodeId, view, {});
}

Acceptance Criteria

  • QueryPreferences interface defined
  • QueryPreferencesService class implemented
  • getPreferences() loads with defaults if not found
  • saveViewConfig() persists preferences
  • In-memory cache reduces localStorage reads
  • Cache invalidation works correctly
  • Tests for load/save/cache operations
  • Tests pass (bun run test:all)
  • Code passes bun run quality:fix

Design Notes

Cache Invalidation:

  • On query node deletion: call invalidateCache(queryNodeId)
  • On preferences update: automatic (handled by saveViewConfig)
  • On storage error: fallback to defaults

Future Enhancements:

  • Export/import preferences
  • Global default preferences
  • Preference reset functionality
  • Database storage for cross-device sync

Testing Requirements

  • Unit tests for cache operations
  • Unit tests for localStorage save/load
  • Unit tests for default fallbacks
  • Integration tests with QueryNodeViewer
  • Tests for error handling (corrupt localStorage data)

Non-Goals

  • Database persistence (future, when needed for multi-device sync)
  • Export/import functionality (future enhancement)
  • Multi-user support (future, when needed)

Activity

  1. malibio commented on Apr 5, 2026

    @malibio
    CollaboratorAuthor

    Implemented in PR #1044 — QueryPreferencesService with write-through cache, localStorage persistence, and view tab UI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions