@@ -6,6 +6,8 @@ import { sdk } from './sdk';
6
6
import type { Models } from '@appwrite.io/console' ;
7
7
import { organization } from './organization' ;
8
8
import { page } from '$app/state' ;
9
+ import { user } from '$lib/stores/user' ;
10
+ import deepEqual from 'deep-equal' ;
9
11
10
12
type Preferences = {
11
13
limit ?: number ;
@@ -27,12 +29,39 @@ type PreferencesStore = {
27
29
} ;
28
30
} & { hideAiDisclaimer ?: boolean } ;
29
31
32
+ async function updateConsolePreferences ( store : PreferencesStore ) : Promise < void > {
33
+ const currentPreferences = get ( user ) . prefs ?? ( 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
+
30
46
function createPreferences ( ) {
31
47
const { subscribe, set, update } = writable < PreferencesStore > ( { } ) ;
32
48
let preferences : PreferencesStore = { } ;
33
49
34
50
if ( browser ) {
35
- set ( JSON . parse ( globalThis . localStorage . getItem ( 'preferences' ) ?? '{}' ) ) ;
51
+ // fresh fetch.
52
+ sdk . forConsole . account
53
+ . getPrefs ( )
54
+ . then ( ( userPreferences ) => {
55
+ if ( ! userPreferences ?. console || Array . isArray ( userPreferences . console ) ) {
56
+ userPreferences . console = { } ;
57
+ }
58
+
59
+ set ( userPreferences . console ) ;
60
+ } )
61
+ . catch ( ( ) => {
62
+ // exception is thrown if there's no session; in that case - fallback!
63
+ set ( JSON . parse ( globalThis . localStorage . getItem ( 'preferences' ) ?? '{}' ) ) ;
64
+ } ) ;
36
65
}
37
66
38
67
subscribe ( ( v ) => {
@@ -42,6 +71,28 @@ function createPreferences() {
42
71
}
43
72
} ) ;
44
73
74
+ /**
75
+ * Update the local store and then synchronizes them on user prefs.
76
+ */
77
+ function updateAndSync ( callback : ( prefs : PreferencesStore ) => void ) : Promise < void > {
78
+ let oldPrefsSnapshot : PreferencesStore ;
79
+ let newPrefsSnapshot : PreferencesStore ;
80
+
81
+ update ( ( currentPrefs ) => {
82
+ oldPrefsSnapshot = currentPrefs ;
83
+ callback ( currentPrefs ) ;
84
+ newPrefsSnapshot = currentPrefs ;
85
+ return currentPrefs ;
86
+ } ) ;
87
+
88
+ if ( deepEqual ( oldPrefsSnapshot , newPrefsSnapshot ) ) {
89
+ return ;
90
+ }
91
+
92
+ // sync the preferences.
93
+ return updateConsolePreferences ( newPrefsSnapshot ) ;
94
+ }
95
+
45
96
return {
46
97
subscribe,
47
98
set,
@@ -61,7 +112,7 @@ function createPreferences() {
61
112
return preferences ?. collections ?. [ collectionId ] ?? [ ] ;
62
113
} ,
63
114
setLimit : ( limit : Preferences [ 'limit' ] ) =>
64
- update ( ( n ) => {
115
+ updateAndSync ( ( n ) => {
65
116
const path = page . route . id ;
66
117
67
118
if ( ! n ?. [ path ] ) {
@@ -74,7 +125,7 @@ function createPreferences() {
74
125
return n ;
75
126
} ) ,
76
127
setView : ( view : Preferences [ 'view' ] ) =>
77
- update ( ( n ) => {
128
+ updateAndSync ( ( n ) => {
78
129
const path = page . route . id ;
79
130
80
131
if ( ! n ?. [ path ] ) {
@@ -87,7 +138,7 @@ function createPreferences() {
87
138
return n ;
88
139
} ) ,
89
140
setColumns : ( columns : Preferences [ 'columns' ] ) =>
90
- update ( ( n ) => {
141
+ updateAndSync ( ( n ) => {
91
142
const path = page . route . id ;
92
143
93
144
if ( ! n ?. [ path ] ) {
@@ -100,10 +151,8 @@ function createPreferences() {
100
151
return n ;
101
152
} ) ,
102
153
setCustomCollectionColumns : ( columns : Preferences [ 'columns' ] ) =>
103
- update ( ( n ) => {
104
- const current = page ;
105
-
106
- const collection = current . params . collection ;
154
+ updateAndSync ( ( n ) => {
155
+ const collection = page . params . collection ;
107
156
if ( ! n ?. collections ?. [ collection ] ) {
108
157
n ??= { } ;
109
158
n . collections ??= { } ;
0 commit comments