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
This commit is contained in:
Jason
2025-11-05 21:40:06 +08:00
parent 21d29b9c2d
commit a428e618d2
7 changed files with 56 additions and 144 deletions

View File

@@ -108,8 +108,9 @@ pub fn import_default_config(state: State<'_, AppState>, app: String) -> Result<
}
/// 查询供应商用量
#[allow(non_snake_case)]
#[tauri::command]
pub async fn query_provider_usage(
pub async fn queryProviderUsage(
state: State<'_, AppState>,
#[allow(non_snake_case)]
providerId: String, // 使用 camelCase 匹配前端
@@ -122,8 +123,9 @@ pub async fn query_provider_usage(
}
/// 测试用量脚本(使用当前编辑器中的脚本,不保存)
#[allow(non_snake_case)]
#[tauri::command]
pub async fn test_usage_script(
pub async fn testUsageScript(
state: State<'_, AppState>,
#[allow(non_snake_case)]
providerId: String,

View File

@@ -539,8 +539,8 @@ pub fn run() {
commands::delete_claude_mcp_server,
commands::validate_mcp_command,
// usage query
commands::query_provider_usage,
commands::test_usage_script,
commands::queryProviderUsage,
commands::testUsageScript,
// New MCP via config.json (SSOT)
commands::get_mcp_config,
commands::upsert_mcp_server_in_config,