@@ -3,7 +3,7 @@ import { Item, ItemMedia, ItemContent, ItemTitle, ItemDescription, ItemActions }
33import { FileJson } from "lucide-react" ;
44import { open , save } from "@tauri-apps/plugin-dialog" ;
55import { useToast } from "@/hooks/use-toast" ;
6- import { BaseDirectory , copyFile , readTextFile } from "@tauri-apps/plugin-fs" ;
6+ import { BaseDirectory , copyFile , readTextFile , writeTextFile } from "@tauri-apps/plugin-fs" ;
77import { Store } from "@tauri-apps/plugin-store" ;
88import { isMobileDevice } from "@/lib/check" ;
99import { relaunch } from "@tauri-apps/plugin-process" ;
@@ -13,23 +13,41 @@ export default function SetConfig() {
1313 const t = useTranslations ( 'settings.dev' ) ;
1414 const { toast } = useToast ( )
1515 async function handleImport ( ) {
16- const file = await open ( {
17- title : t ( 'importConfigTitle' ) ,
18- } )
19- if ( file ) {
20- const content = await readTextFile ( file , { baseDir : BaseDirectory . AppData } )
21- const jsonContent = JSON . parse ( content )
22- const store = await Store . load ( 'store.json' ) ;
23- Object . keys ( jsonContent ) . forEach ( ( key : string ) => {
24- store . set ( key , jsonContent [ key ] )
16+ try {
17+ const file = await open ( {
18+ title : t ( 'importConfigTitle' ) ,
2519 } )
26- if ( isMobileDevice ( ) ) {
27- toast ( {
28- description : t ( 'importConfigSuccessMobile' ) ,
29- } )
30- } else {
31- relaunch ( )
20+ if ( file ) {
21+ // 验证 JSON 格式
22+ const content = await readTextFile ( file )
23+ JSON . parse ( content )
24+
25+ // 直接将文件写入 store.json 位置
26+ await writeTextFile ( 'store.json' , content , { baseDir : BaseDirectory . AppData } )
27+
28+ // 关闭已加载的 store 实例(如果有)
29+ const existingStore = await Store . get ( 'store.json' )
30+ if ( existingStore ) {
31+ await existingStore . close ( )
32+ }
33+
34+ // 重新加载 store,会自动从磁盘读取新写入的文件
35+ await Store . load ( 'store.json' )
36+
37+ if ( isMobileDevice ( ) ) {
38+ toast ( {
39+ description : t ( 'importConfigSuccessMobile' ) ,
40+ } )
41+ } else {
42+ relaunch ( )
43+ }
3244 }
45+ } catch ( error ) {
46+ toast ( {
47+ title : '导入失败' ,
48+ description : error instanceof Error ? error . message : String ( error ) ,
49+ variant : 'destructive'
50+ } )
3351 }
3452 }
3553 async function handleExport ( ) {
0 commit comments