返回 DeepSeek-Reasonix
remote_prefs.go
根目录 / desktop / remote_prefs.go
1 package main
2
3 import (
4 "encoding/json"
5 "os"
6 "path/filepath"
7 "sync"
8
9 "reasonix/internal/config"
10 "reasonix/internal/fileutil"
11 )
12
13 var remotePrefsMu sync.Mutex
14
15 // remotePrefs is desktop-only remote UI state, stored beside the other desktop
16 // JSON prefs (desktop-workspaces.json, desktop-tabs.json). All fields are
17 // optional so an older file decodes cleanly.
18 type remotePrefs struct {
19 LastHostID string `json:"lastHostId,omitempty"`
20 LastWorkspaceByHost map[string]string `json:"lastWorkspaceByHost,omitempty"`
21 ExplorerTab string `json:"explorerTab,omitempty"`
22 }
23
24 func remotePrefsPath() string {
25 dir := config.MemoryUserDir()
26 if dir == "" {
27 return ""
28 }
29 return filepath.Join(dir, "desktop-remote.json")
30 }
31
32 func loadRemotePrefs() remotePrefs {
33 var p remotePrefs
34 path := remotePrefsPath()
35 if path == "" {
36 return p
37 }
38 data, err := os.ReadFile(path)
39 if err != nil {
40 return p
41 }
42 _ = json.Unmarshal(data, &p)
43 if p.LastWorkspaceByHost == nil {
44 p.LastWorkspaceByHost = map[string]string{}
45 }
46 return p
47 }
48
49 func saveRemotePrefs(p remotePrefs) {
50 path := remotePrefsPath()
51 if path == "" {
52 return
53 }
54 data, err := json.MarshalIndent(p, "", " ")
55 if err != nil {
56 return
57 }
58 _ = fileutil.AtomicWriteFile(path, data, 0o600)
59 }
60
61 func (a *App) saveLastRemoteWorkspace(hostID, workspace string) {
62 remotePrefsMu.Lock()
63 defer remotePrefsMu.Unlock()
64 p := loadRemotePrefs()
65 if p.LastWorkspaceByHost == nil {
66 p.LastWorkspaceByHost = map[string]string{}
67 }
68 p.LastHostID = hostID
69 p.LastWorkspaceByHost[hostID] = workspace
70 saveRemotePrefs(p)
71 }
72
73 // RemoteLastWorkspace returns the last opened workspace for hostID (bound so
74 // the frontend can prefill the server card).
75 func (a *App) RemoteLastWorkspace(hostID string) string {
76 remotePrefsMu.Lock()
77 defer remotePrefsMu.Unlock()
78 return loadRemotePrefs().LastWorkspaceByHost[hostID]
79 }
80
80 lines GO