@@ -29,12 +29,33 @@ type PreferencesStore = {
29
29
} ;
30
30
} & { hideAiDisclaimer ?: boolean } ;
31
31
32
+ async function updateConsolePreferences ( store : PreferencesStore ) : Promise < void > {
33
+ const currentPreferences = await sdk . forConsole . account . getPrefs ( ) ;
34
+ if ( ! currentPreferences ?. console || Array . isArray ( currentPreferences . console ) ) {
35
+ currentPreferences . console = { } ;
36
+ }
37
+
38
+ currentPreferences . console = {
39
+ ...( currentPreferences [ 'console' ] ?? { } ) ,
40
+ ...store
41
+ } ;
42
+
43
+ await sdk . forConsole . account . updatePrefs ( currentPreferences ) ;
44
+ }
45
+
32
46
function createPreferences ( ) {
33
47
const { subscribe, set, update } = writable < PreferencesStore > ( { } ) ;
34
48
let preferences : PreferencesStore = { } ;
35
49
36
50
if ( browser ) {
37
- set ( JSON . parse ( globalThis . localStorage . getItem ( 'preferences' ) ?? '{}' ) ) ;
51
+ // fresh fetch.
52
+ sdk . forConsole . account . getPrefs ( ) . then ( ( userPreferences ) => {
53
+ if ( ! userPreferences ?. console || Array . isArray ( userPreferences . console ) ) {
54
+ userPreferences . console = { } ;
55
+ }
56
+
57
+ set ( userPreferences . console ) ;
58
+ } ) ;
38
59
}
39
60
40
61
subscribe ( ( v ) => {
@@ -44,6 +65,30 @@ function createPreferences() {
44
65
}
45
66
} ) ;
46
67
68
+ /**
69
+ * Update the local store and then synchronizes them on user prefs.
70
+ */
71
+ function updateAndSync ( callback : ( prefs : PreferencesStore ) => void ) {
72
+ let oldPrefsSnapshot : string ;
73
+ let newPrefsSnapshot : PreferencesStore ;
74
+
75
+ update ( ( currentPrefs ) => {
76
+ oldPrefsSnapshot = JSON . stringify ( currentPrefs ) ;
77
+ callback ( currentPrefs ) ;
78
+ newPrefsSnapshot = currentPrefs ;
79
+ return currentPrefs ;
80
+ } ) ;
81
+
82
+ // Skip API if no changes (sufficient for simple objects).
83
+ // The key order seemed to be maintained during local tests.
84
+ if ( oldPrefsSnapshot === JSON . stringify ( newPrefsSnapshot ) ) {
85
+ return ;
86
+ }
87
+
88
+ // sync the preferences.
89
+ updateConsolePreferences ( newPrefsSnapshot ) . then ( ) ;
90
+ }
91
+
47
92
return {
48
93
subscribe,
49
94
set,
@@ -65,7 +110,7 @@ function createPreferences() {
65
110
) ;
66
111
} ,
67
112
setLimit : ( limit : Preferences [ 'limit' ] ) =>
68
- update ( ( n ) => {
113
+ updateAndSync ( ( n ) => {
69
114
const path = get ( page ) . route . id ;
70
115
const project = sdk . forProject . client . config . project ;
71
116
if ( ! n [ project ] ?. [ path ] ) {
@@ -78,7 +123,7 @@ function createPreferences() {
78
123
return n ;
79
124
} ) ,
80
125
setView : ( view : Preferences [ 'view' ] ) =>
81
- update ( ( n ) => {
126
+ updateAndSync ( ( n ) => {
82
127
const path = get ( page ) . route . id ;
83
128
const project = sdk . forProject . client . config . project ;
84
129
if ( ! n [ project ] ?. [ path ] ) {
@@ -91,7 +136,7 @@ function createPreferences() {
91
136
return n ;
92
137
} ) ,
93
138
setColumns : ( columns : Preferences [ 'columns' ] ) =>
94
- update ( ( n ) => {
139
+ updateAndSync ( ( n ) => {
95
140
const path = get ( page ) . route . id ;
96
141
const project = sdk . forProject . client . config . project ;
97
142
if ( ! n [ project ] ?. [ path ] ) {
@@ -104,7 +149,7 @@ function createPreferences() {
104
149
return n ;
105
150
} ) ,
106
151
setCustomCollectionColumns : ( columns : Preferences [ 'columns' ] ) =>
107
- update ( ( n ) => {
152
+ updateAndSync ( ( n ) => {
108
153
const current = get ( page ) ;
109
154
const project = sdk . forProject . client . config . project ;
110
155
const collection = current . params . collection ;
0 commit comments