refactor(hooks): introduce unified post-change sync utility

- Add postChangeSync.ts utility with Result pattern for graceful error handling
- Replace try-catch with syncCurrentProvidersLiveSafe in useImportExport
- Add directory-change-triggered sync in useSettings to maintain SSOT
- Introduce partial-success status to distinguish import success from sync failures
- Add test coverage for sync behavior in different scenarios

This refactoring ensures config.json changes are reliably synced to live
files while providing better user feedback for edge cases.
This commit is contained in:
Jason
2025-11-01 23:58:29 +08:00
parent 87f408c163
commit 2ebe34810c
4 changed files with 52 additions and 4 deletions

View File

@@ -0,0 +1,19 @@
import { settingsApi } from "@/lib/api";
/**
* 统一的“后置同步”工具:将当前使用的供应商写回对应应用的 live 配置。
* 不抛出异常,由调用方根据返回值决定提示策略。
*/
export async function syncCurrentProvidersLiveSafe(): Promise<{
ok: boolean;
error?: Error;
}> {
try {
await settingsApi.syncCurrentProvidersLive();
return { ok: true };
} catch (err) {
const error = err instanceof Error ? err : new Error(String(err ?? ""));
return { ok: false, error };
}
}