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:
19
src/utils/postChangeSync.ts
Normal file
19
src/utils/postChangeSync.ts
Normal 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 };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user