@@ -8,27 +8,30 @@ import { page } from '$app/state';
8
8
import { user } from '$lib/stores/user' ;
9
9
import deepEqual from 'deep-equal' ;
10
10
11
- type Preferences = {
11
+ type ConsolePreferences = {
12
12
limit ?: number ;
13
13
view ?: View ;
14
14
columns ?: string [ ] ;
15
- } ;
15
+ } /* support a strict + flexible preference type for TS compatibility */ & Record <
16
+ string ,
17
+ string | number | boolean | object | null | unknown
18
+ > ;
16
19
17
20
type TeamPreferences = {
18
21
names ?: string [ ] ;
19
22
} ;
20
23
21
- type PreferencesStore = {
22
- [ key : string ] : Preferences ;
24
+ type ConsolePreferencesStore = {
25
+ [ key : string ] : ConsolePreferences ;
23
26
collections ?: {
24
- [ key : string ] : Preferences [ 'columns' ] ;
27
+ [ key : string ] : ConsolePreferences [ 'columns' ] ;
25
28
} ;
26
29
displayNames ?: {
27
30
[ key : string ] : TeamPreferences [ 'names' ] ;
28
31
} ;
29
32
} & { hideAiDisclaimer ?: boolean } ;
30
33
31
- async function updateConsolePreferences ( store : PreferencesStore ) : Promise < void > {
34
+ async function updateConsolePreferences ( store : ConsolePreferencesStore ) : Promise < void > {
32
35
const currentPreferences = get ( user ) ?. prefs ?? ( await sdk . forConsole . account . getPrefs ( ) ) ;
33
36
if ( ! currentPreferences ?. console || Array . isArray ( currentPreferences . console ) ) {
34
37
currentPreferences . console = { } ;
@@ -43,8 +46,8 @@ async function updateConsolePreferences(store: PreferencesStore): Promise<void>
43
46
}
44
47
45
48
function createPreferences ( ) {
46
- const { subscribe, set, update } = writable < PreferencesStore > ( { } ) ;
47
- let preferences : PreferencesStore = { } ;
49
+ const { subscribe, set, update } = writable < ConsolePreferencesStore > ( { } ) ;
50
+ let preferences : ConsolePreferencesStore = { } ;
48
51
49
52
if ( browser ) {
50
53
// fresh fetch.
@@ -73,9 +76,9 @@ function createPreferences() {
73
76
/**
74
77
* Update the local store and then synchronizes them on user prefs.
75
78
*/
76
- function updateAndSync ( callback : ( prefs : PreferencesStore ) => void ) : Promise < void > {
77
- let oldPrefsSnapshot : PreferencesStore ;
78
- let newPrefsSnapshot : PreferencesStore ;
79
+ function updateAndSync ( callback : ( prefs : ConsolePreferencesStore ) => void ) : Promise < void > {
80
+ let oldPrefsSnapshot : ConsolePreferencesStore ;
81
+ let newPrefsSnapshot : ConsolePreferencesStore ;
79
82
80
83
update ( ( currentPrefs ) => {
81
84
oldPrefsSnapshot = structuredClone ( currentPrefs ) ;
@@ -96,7 +99,7 @@ function createPreferences() {
96
99
subscribe,
97
100
set,
98
101
update,
99
- get : ( route ?: Page [ 'route' ] ) : Preferences => {
102
+ get : ( route ?: Page [ 'route' ] ) : ConsolePreferences => {
100
103
const parsedRoute = route ?? page . route ;
101
104
return (
102
105
preferences ?. [ parsedRoute . id ] ?? {
@@ -106,10 +109,10 @@ function createPreferences() {
106
109
}
107
110
) ;
108
111
} ,
109
- getCustomCollectionColumns : ( collectionId : string ) : Preferences [ 'columns' ] => {
112
+ getCustomCollectionColumns : ( collectionId : string ) : ConsolePreferences [ 'columns' ] => {
110
113
return preferences ?. collections ?. [ collectionId ] ?? [ ] ;
111
114
} ,
112
- setLimit : ( limit : Preferences [ 'limit' ] ) =>
115
+ setLimit : ( limit : ConsolePreferences [ 'limit' ] ) =>
113
116
updateAndSync ( ( n ) => {
114
117
const path = page . route . id ;
115
118
@@ -122,7 +125,7 @@ function createPreferences() {
122
125
123
126
return n ;
124
127
} ) ,
125
- setView : ( view : Preferences [ 'view' ] ) =>
128
+ setView : ( view : ConsolePreferences [ 'view' ] ) =>
126
129
updateAndSync ( ( n ) => {
127
130
const path = page . route . id ;
128
131
@@ -135,7 +138,7 @@ function createPreferences() {
135
138
136
139
return n ;
137
140
} ) ,
138
- setColumns : ( columns : Preferences [ 'columns' ] ) =>
141
+ setColumns : ( columns : ConsolePreferences [ 'columns' ] ) =>
139
142
updateAndSync ( ( n ) => {
140
143
const path = page . route . id ;
141
144
@@ -148,7 +151,10 @@ function createPreferences() {
148
151
149
152
return n ;
150
153
} ) ,
151
- setCustomCollectionColumns : ( collectionId : string , columns : Preferences [ 'columns' ] ) =>
154
+ setCustomCollectionColumns : (
155
+ collectionId : string ,
156
+ columns : ConsolePreferences [ 'columns' ]
157
+ ) =>
152
158
updateAndSync ( ( n ) => {
153
159
if ( ! n ?. collections ?. [ collectionId ] ) {
154
160
n ??= { } ;
@@ -159,7 +165,7 @@ function createPreferences() {
159
165
return n ;
160
166
} ) ,
161
167
loadTeamPrefs : async ( id : string ) => {
162
- const teamPrefs : Preferences = await sdk . forConsole . teams . getPrefs ( id ) ;
168
+ const teamPrefs = await sdk . forConsole . teams . getPrefs ( id ) ;
163
169
update ( ( n ) => {
164
170
n [ id ] = teamPrefs ;
165
171
return n ;
0 commit comments