feat(ui): add drag-and-drop sorting for provider list (#126)

* feat(ui): add drag-and-drop sorting for provider list

Implement drag-and-drop functionality to allow users to reorder providers with custom sort indices.

Features:
- Install @dnd-kit libraries for drag-and-drop support
- Add sortIndex field to Provider type (frontend & backend)
- Implement SortableProviderItem component with drag handle
- Add update_providers_sort_order Tauri command
- Sync tray menu order with provider list sorting
- Add i18n support for drag-related UI text

Technical details:
- Use @dnd-kit/core and @dnd-kit/sortable for smooth drag interactions
- Disable animations for immediate response after drop
- Update tray menu immediately after reordering
- Sort priority: sortIndex → createdAt → name

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* fix(ui): remove unused transition variable in ProviderList

Remove unused 'transition' destructured variable from useSortable hook
to fix TypeScript error TS6133. The transition property is hardcoded
as 'none' in the style object to prevent conflicts with drag operations.

---------

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
ZyphrZero
2025-10-15 22:21:06 +08:00
committed by GitHub
parent 3b6048b1e8
commit 9eb991d087
11 changed files with 482 additions and 140 deletions

View File

@@ -1491,5 +1491,52 @@ pub async fn set_app_config_dir_override(
path: Option<String>,
) -> Result<bool, String> {
crate::app_store::set_app_config_dir_to_store(&app, path.as_deref())?;
Ok(true)
}
// =====================
// Provider Sort Order Management
// =====================
#[derive(serde::Deserialize)]
pub struct ProviderSortUpdate {
pub id: String,
#[serde(rename = "sortIndex")]
pub sort_index: usize,
}
/// Update sort order for multiple providers
#[tauri::command]
pub async fn update_providers_sort_order(
state: State<'_, AppState>,
app_type: Option<AppType>,
app: Option<String>,
appType: Option<String>,
updates: Vec<ProviderSortUpdate>,
) -> Result<bool, String> {
let app_type = app_type
.or_else(|| app.as_deref().map(|s| s.into()))
.or_else(|| appType.as_deref().map(|s| s.into()))
.unwrap_or(AppType::Claude);
let mut config = state
.config
.lock()
.map_err(|e| format!("获取锁失败: {}", e))?;
let manager = config
.get_manager_mut(&app_type)
.ok_or_else(|| format!("应用类型不存在: {:?}", app_type))?;
// Update sort_index for each provider
for update in updates {
if let Some(provider) = manager.providers.get_mut(&update.id) {
provider.sort_index = Some(update.sort_index);
}
}
drop(config);
state.save()?;
Ok(true)
}

View File

@@ -49,7 +49,28 @@ fn create_tray_menu(
menu_builder = menu_builder.item(&claude_header);
if !claude_manager.providers.is_empty() {
for (id, provider) in &claude_manager.providers {
// Sort providers by sortIndex, then by createdAt, then by name
let mut sorted_providers: Vec<_> = claude_manager.providers.iter().collect();
sorted_providers.sort_by(|(_, a), (_, b)| {
// Priority 1: sortIndex
match (a.sort_index, b.sort_index) {
(Some(idx_a), Some(idx_b)) => return idx_a.cmp(&idx_b),
(Some(_), None) => return std::cmp::Ordering::Less,
(None, Some(_)) => return std::cmp::Ordering::Greater,
_ => {}
}
// Priority 2: createdAt
match (a.created_at, b.created_at) {
(Some(time_a), Some(time_b)) => return time_a.cmp(&time_b),
(Some(_), None) => return std::cmp::Ordering::Greater,
(None, Some(_)) => return std::cmp::Ordering::Less,
_ => {}
}
// Priority 3: name
a.name.cmp(&b.name)
});
for (id, provider) in sorted_providers {
let is_current = claude_manager.current == *id;
let item = CheckMenuItem::with_id(
app,
@@ -84,7 +105,28 @@ fn create_tray_menu(
menu_builder = menu_builder.item(&codex_header);
if !codex_manager.providers.is_empty() {
for (id, provider) in &codex_manager.providers {
// Sort providers by sortIndex, then by createdAt, then by name
let mut sorted_providers: Vec<_> = codex_manager.providers.iter().collect();
sorted_providers.sort_by(|(_, a), (_, b)| {
// Priority 1: sortIndex
match (a.sort_index, b.sort_index) {
(Some(idx_a), Some(idx_b)) => return idx_a.cmp(&idx_b),
(Some(_), None) => return std::cmp::Ordering::Less,
(None, Some(_)) => return std::cmp::Ordering::Greater,
_ => {}
}
// Priority 2: createdAt
match (a.created_at, b.created_at) {
(Some(time_a), Some(time_b)) => return time_a.cmp(&time_b),
(Some(_), None) => return std::cmp::Ordering::Greater,
(None, Some(_)) => return std::cmp::Ordering::Less,
_ => {}
}
// Priority 3: name
a.name.cmp(&b.name)
});
for (id, provider) in sorted_providers {
let is_current = codex_manager.current == *id;
let item = CheckMenuItem::with_id(
app,
@@ -460,6 +502,8 @@ pub fn run() {
// app_config_dir override via Store
commands::get_app_config_dir_override,
commands::set_app_config_dir_override,
// provider sort order management
commands::update_providers_sort_order,
// theirs: config import/export and dialogs
import_export::export_config_to_file,
import_export::import_config_from_file,

View File

@@ -19,6 +19,9 @@ pub struct Provider {
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "createdAt")]
pub created_at: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "sortIndex")]
pub sort_index: Option<usize>,
/// 供应商元数据(不写入 live 配置,仅存于 ~/.cc-switch/config.json
#[serde(skip_serializing_if = "Option::is_none")]
pub meta: Option<ProviderMeta>,
@@ -39,6 +42,7 @@ impl Provider {
website_url,
category: None,
created_at: None,
sort_index: None,
meta: None,
}
}