Commit Graph

12 Commits

  • feat(ui): enhance provider switch error notification with copy action
    - Split error message into title and description for better UX
    - Add one-click copy button to easily share error details
    - Extend toast duration to 6s for improved readability
    - Use extractErrorMessage utility for consistent error handling
    - Add i18n keys: common.copy, notifications.switchFailedTitle
    
    This improves debugging experience when provider switching fails,
    allowing users to quickly copy and share error messages.
  • fix(error-handling): isolate tray menu update failures from main operations
    Previously, if updateTrayMenu() failed after a successful main operation
    (like sorting, adding, or updating providers), the entire operation would
    appear to fail with a misleading error message, even though the core
    functionality had already succeeded.
    
    This resulted in false negative feedback where:
    - Backend data was successfully updated
    - Frontend UI was successfully refreshed
    - Tray menu failed to update
    - User saw "operation failed" message (incorrect)
    
    Changes:
    - Wrap updateTrayMenu() calls in nested try-catch blocks
    - Log tray menu failures separately with descriptive messages
    - Ensure main operation success is reported accurately
    - Prevent tray menu failures from triggering main operation error handlers
    
    Files modified:
    - src/hooks/useDragSort.ts (drag-and-drop sorting)
    - src/lib/query/mutations.ts (add/delete/switch mutations)
    - src/hooks/useProviderActions.ts (update provider)
    
    This fixes the bug introduced in PR #179 and prevents similar issues
    across all provider operations.
  • feat(usage): enable background query for usage polling
    - Add `refetchIntervalInBackground: true` to usage query configuration
    - Allows usage queries to continue running when app window is minimized or unfocused
    - Existing safety mechanisms remain in place (timeout limits, minimum interval, no retry)
    - Users have full control through autoQueryInterval setting
  • fix(usage): resolve auto-query interval timing issue
    Problem:
    - User configured 5-minute auto-query interval
    - Actual queries executed every 10 minutes instead of 5
    
    Root Cause:
    - staleTime (5 min) conflicted with refetchInterval (5 min)
    - React Query skipped refetch when data was still within staleTime
    - First interval trigger at T+5min: data considered "fresh", skipped
    - Second interval trigger at T+10min: data "stale", executed
    
    Solution:
    - Set staleTime to 0 for usage queries
    - Ensures refetchInterval executes precisely as configured
    - Auto-query is meant to fetch fresh data periodically, not use cache
    
    Technical Details:
    - Modified useUsageQuery in src/lib/query/queries.ts
    - Changed: staleTime: 5 * 60 * 1000 → staleTime: 0
    - Added explanatory comment in Chinese
    - Manual queries still work via refetch() button
  • refactor(usage): consolidate query logic to eliminate DRY violations
    Breaking Changes:
    - Removed useAutoUsageQuery hook (119 lines)
    - Unified all usage queries into single useUsageQuery hook
    
    Technical Improvements:
    - Eliminated duplicate state management (React Query + manual useState)
    - Fixed single source of truth principle violation
    - Replaced manual setInterval with React Query's built-in refetchInterval
    - Reduced UsageFooter complexity by 28% (54 → 39 lines)
    
    New Features:
    - useUsageQuery now accepts autoQueryInterval option
    - Automatic query interval control (0 = disabled, min 1 minute)
    - Built-in lastQueriedAt timestamp from dataUpdatedAt
    - Auto-query only enabled for currently active provider
    
    Architecture Benefits:
    - Single data source: manual and auto queries share same cache
    - No more state inconsistency between manual/auto query results
    - Leverages React Query's caching, deduplication, and background updates
    - Cleaner separation of concerns
    
    Code Changes:
    - src/lib/query/queries.ts: Enhanced useUsageQuery with auto-query support
    - src/components/UsageFooter.tsx: Simplified to use single query hook
    - src/hooks/useAutoUsageQuery.ts: Deleted (redundant)
    - All type checks passed
  • refine(usage): enhance query robustness and error handling
    Backend improvements:
    - Add InvalidHttpMethod error enum for better error semantics
    - Clamp HTTP timeout to 2-30s to prevent config abuse
    - Strict HTTP method validation instead of silent fallback to GET
    
    Frontend improvements:
    - Add i18n support for usage query errors (en/zh)
    - Improve error handling with type-safe unknown instead of any
    - Optimize i18n import (direct import instead of dynamic)
    - Disable auto-retry for usage queries to avoid API stampede
    
    Additional changes:
    - Apply prettier formatting to affected files
    
    Files changed:
    - src-tauri/src/error.rs (+2)
    - src-tauri/src/usage_script.rs (+8 -2)
    - src/i18n/locales/{en,zh}.json (+4 -1 each)
    - src/lib/api/usage.ts (+21 -4)
    - src/lib/query/queries.ts (+1)
    - style: prettier formatting on 6 other files
  • refactor(types): rename AppType to AppId for semantic clarity
    Rename `AppType` to `AppId` across the entire frontend codebase to better
    reflect its purpose as an application identifier rather than a type category.
    This aligns frontend naming with backend command parameter conventions.
    
    Changes:
    - Rename type `AppType` to `AppId` in src/lib/api/types.ts
    - Remove `AppType` export from src/lib/api/index.ts
    - Update all component props from `appType` to `appId` (43 files)
    - Update all variable names from `appType` to `appId`
    - Synchronize documentation (CHANGELOG, refactoring plans)
    - Update test files and MSW mocks
    
    BREAKING CHANGE: `AppType` type is no longer exported. Use `AppId` instead.
    All component props have been renamed from `appType` to `appId`.
  • fix: unify dialog layout and fix content padding issues
    - Fix negative margin overflow in all dialog content areas
    - Standardize dialog structure with flex-col layout
    - Add consistent py-4 spacing to all content areas
    - Ensure proper spacing between header, content, and footer
    
    Affected components:
    - AddProviderDialog, EditProviderDialog
    - McpFormModal, McpPanel
    - UsageScriptModal
    - SettingsDialog
    
    All dialogs now follow unified layout pattern:
    - DialogContent: flex flex-col max-h-[90vh]
    - Content area: flex-1 overflow-y-auto px-6 py-4
    - No negative margins that cause content overflow
  • fix: eliminate layout shift when switching between Claude and Codex
    Fixed dual-source jitter issue:
    1. Horizontal shift: Use overflow-y-scroll to force scrollbar gutter
    2. Vertical jump: Use keepPreviousData to maintain content during app switch
  • refactor: improve code quality and consistency
    Changes:
    1. Remove unused variable in useSettings.ts (readPersistedLanguage)
    2. Replace manual state management with React Query in UsageFooter
       - Create useUsageQuery hook with 5-minute cache
       - Simplify component from 227 lines to 81 lines (-64%)
       - Improve consistency with project's React Query pattern
       - Enable automatic refetch and error handling
  • feat: complete stage 4 cleanup and code formatting
    This commit completes stage 4 of the refactoring plan, focusing on cleanup
    and optimization of the modernized codebase.
    
    ## Key Changes
    
    ### Code Cleanup
    - Remove legacy `src/lib/styles.ts` (no longer needed)
    - Remove old modal components (`ImportProgressModal.tsx`, `ProviderList.tsx`)
    - Streamline `src/lib/tauri-api.ts` from 712 lines to 17 lines (-97.6%)
      - Remove global `window.api` pollution
      - Keep only event listeners (`tauriEvents.onProviderSwitched`)
      - All API calls now use modular `@/lib/api/*` layer
    
    ### Type System
    - Clean up `src/vite-env.d.ts` (remove 156 lines of outdated types)
    - Remove obsolete global type declarations
    - All TypeScript checks pass with zero errors
    
    ### Code Formatting
    - Format all source files with Prettier (82 files)
    - Fix formatting issues in 15 files:
      - App.tsx and core components
      - MCP management components
      - Settings module components
      - Provider management components
      - UI components
    
    ### Documentation Updates
    - Update `REFACTORING_CHECKLIST.md` with stage 4 progress
    - Mark completed tasks in `REFACTORING_MASTER_PLAN.md`
    
    ## Impact
    
    **Code Reduction:**
    - Total: -1,753 lines, +384 lines (net -1,369 lines)
    - tauri-api.ts: 712 → 17 lines (-97.6%)
    - Removed styles.ts: -82 lines
    - Removed vite-env.d.ts declarations: -156 lines
    
    **Quality Improvements:**
    -  Zero TypeScript errors
    -  Zero TODO/FIXME comments
    -  100% Prettier compliant
    -  Zero `window.api` references
    -  Fully modular API layer
    
    ## Testing
    - [x] TypeScript compilation passes
    - [x] Code formatting validated
    - [x] No linting errors
    
    Stage 4 completion: 100%
    Ready for stage 5 (testing and bug fixes)