1
1
import { defineStore , storeToRefs } from 'pinia' ;
2
- import { computed , reactive } from 'vue' ;
2
+ import { computed , reactive , ref } from 'vue' ;
3
3
4
4
import type { TmdbClientAuthentication } from '~/models/tmdb/tmdb-client.model' ;
5
5
import type { TraktClientAuthentication } from '~/models/trakt/trakt-authentication.model' ;
6
6
7
+ import type { TraktUserSettings } from '~/models/trakt/trakt-user.model' ;
7
8
import type { TvdbClientAuthentication } from '~/models/tvdb/tvdb-client.model' ;
8
9
9
10
import { storage } from '~/utils/browser/browser-storage.utils' ;
10
11
12
+ type UserSetting = TraktUserSettings | Record < string , never > ;
13
+ type UserSettings = Record < string , UserSetting > ;
14
+
15
+ export const useUserSettingsStore = defineStore ( 'settings.user' , ( ) => {
16
+ const userSettings = reactive < UserSettings > ( { } ) ;
17
+ const user = ref < string > ( 'default' ) ;
18
+ const userSetting = computed ( ( ) => userSettings [ user . value ] ) ;
19
+
20
+ const syncSetUser = ( _settings : UserSetting = userSetting . value , account : string = _settings ?. user ?. username ?? user . value ) => {
21
+ const _lastUser = storage . sync . set ( `settings.last-user` , account ) ;
22
+ const _setting = storage . sync . set ( `settings.user.${ encodeURIComponent ( account ) } ` , _settings ) ;
23
+ console . info ( 'settings-store' , 'Saving user' , account , _settings ) ;
24
+ return Promise . all ( [ _lastUser , _setting ] ) ;
25
+ } ;
26
+ const syncClearUser = ( account ?: string ) => {
27
+ console . info ( 'settings-store' , 'Clearing user' , account ) ;
28
+ return storage . sync . remove ( `settings.user${ account ? `.${ encodeURIComponent ( account ) } ` : '' } ` ) ;
29
+ } ;
30
+ const syncRestoreUser = async ( account : string = user . value ) => {
31
+ if ( account === 'default' ) account = ( await storage . sync . get < string > ( `settings.last-user` ) ) || account ;
32
+ user . value = account ;
33
+ console . info ( 'settings-store' , 'Restoring user' , account ) ;
34
+ const _setting = await storage . sync . get < UserSetting > ( `settings.user.${ encodeURIComponent ( account ) } ` ) ;
35
+ if ( ! userSettings [ account ] ) userSettings [ account ] = { } ;
36
+ if ( _setting ) Object . assign ( userSettings [ account ] , _setting ) ;
37
+ return _setting ;
38
+ } ;
39
+
40
+ const setUserSetting = ( _settings : UserSetting = { } , account : string = _settings ?. user ?. username ?? user . value ) => {
41
+ if ( ! userSettings [ account ] ) userSettings [ account ] = { } ;
42
+ Object . assign ( userSettings [ account ] , _settings ) ;
43
+ console . info ( 'settings-store' , 'User changed' , account , { ..._settings } ) ;
44
+ if ( _settings ) {
45
+ user . value = account ;
46
+ return syncSetUser ( _settings , account ) ;
47
+ }
48
+ user . value = 'default' ;
49
+ return syncClearUser ( account ) ;
50
+ } ;
51
+
52
+ return { user, userSetting, setUserSetting, syncSetUser, syncClearUser, syncRestoreUser } ;
53
+ } ) ;
54
+
55
+ export const useUserSettingsStoreRefs = ( ) => storeToRefs ( useUserSettingsStore ( ) ) ;
56
+
11
57
type SettingsAuth = {
12
58
trakt ?: TraktClientAuthentication ;
13
59
tvdb ?: TvdbClientAuthentication ;
@@ -20,40 +66,56 @@ type SettingsAuthenticated = {
20
66
tmdb ?: boolean ;
21
67
} ;
22
68
23
- export const useSettingsStore = defineStore ( 'settings' , ( ) => {
24
- const auth = reactive < SettingsAuth > ( { } ) ;
69
+ type SettingsAuths = Record < string , SettingsAuth > ;
70
+
71
+ export const useAuthSettingsStore = defineStore ( 'settings.auth' , ( ) => {
72
+ const { user } = useUserSettingsStoreRefs ( ) ;
73
+
74
+ const auths = reactive < SettingsAuths > ( { } ) ;
75
+ const auth = computed ( ( ) => auths [ user . value ] ) ;
76
+
25
77
const authenticated = reactive < SettingsAuthenticated > ( { } ) ;
26
- const isAuthenticated = computed ( ( ) => Object . values ( authenticated ) . every ( Boolean ) ) ;
78
+ const isAuthenticated = computed ( ( ) => {
79
+ const values = Object . values ( authenticated ) ;
80
+ return values . length > 0 ? values . every ( Boolean ) : false ;
81
+ } ) ;
27
82
28
83
const setAuthenticated = ( { trakt, tvdb, tmdb } : SettingsAuth = { } ) => {
29
84
if ( trakt ) authenticated . trakt = ! ! trakt . access_token ;
30
85
if ( tvdb ) authenticated . tvdb = ! ! tvdb . accessToken ;
31
86
if ( tmdb ) authenticated . tmdb = ! ! tmdb . accessToken ;
87
+ return authenticated ;
88
+ } ;
89
+
90
+ const syncSetAuth = ( _auth : SettingsAuth = auth . value , account : string = user . value ) => {
91
+ console . info ( 'settings-store' , 'Saving auth' , account , _auth ) ;
92
+ return storage . sync . set ( `settings.auth.${ encodeURIComponent ( account ) } ` , _auth ) ;
93
+ } ;
94
+ const syncClearAuth = ( account ?: string ) => {
95
+ console . info ( 'settings-store' , 'Clearing auth' , account ) ;
96
+ return storage . sync . remove ( `settings.auth${ account ? `.${ encodeURIComponent ( account ) } ` : '' } ` ) ;
97
+ } ;
98
+ const syncRestoreAuth = async ( account : string = user . value ) => {
99
+ console . info ( 'settings-store' , 'Restoring auth' , account ) ;
100
+ const _auth = await storage . sync . get < SettingsAuth > ( `settings.auth.${ encodeURIComponent ( account ) } ` ) ;
101
+ if ( ! auths [ account ] ) auths [ account ] = { } ;
102
+ if ( _auth ) Object . assign ( auths [ account ] , _auth ) ;
103
+ setAuthenticated ( _auth ) ;
104
+ return auths [ account ] ;
32
105
} ;
33
106
34
- const syncSetAuth = ( _auth : SettingsAuth ) => storage . sync . set ( 'settings.auth' , _auth ) ;
35
- const syncClearAuth = ( ) => storage . sync . remove ( 'settings.auth' ) ;
36
- const syncRestoreAuth = ( ) =>
37
- storage . sync . get < SettingsAuth > ( 'settings.auth' ) . then ( _auth => {
38
- Object . assign ( auth , _auth ) ;
39
- setAuthenticated ( _auth ) ;
40
- return _auth ;
41
- } ) ;
42
-
43
- const setAuth = ( _auth : SettingsAuth = { } ) => {
44
- if ( _auth . trakt ) auth . trakt = _auth . trakt ;
45
- if ( _auth . tvdb ) auth . tvdb = _auth . tvdb ;
46
- if ( _auth . tmdb ) auth . tmdb = _auth . tmdb ;
47
- console . info ( 'settings-store' , 'Auth changed' , { ...auth } ) ;
107
+ const setAuth = async ( _auth : SettingsAuth = { } , account : string = user . value ) => {
108
+ if ( ! auths [ account ] ) auths [ account ] = { } ;
109
+ if ( _auth . trakt ) auths [ account ] . trakt = _auth . trakt ;
110
+ if ( _auth . tvdb ) auths [ account ] . tvdb = _auth . tvdb ;
111
+ if ( _auth . tmdb ) auths [ account ] . tmdb = _auth . tmdb ;
112
+ console . info ( 'settings-store' , 'Auth changed' , account , { ...auths } ) ;
48
113
setAuthenticated ( _auth ) ;
49
- if ( Object . keys ( _auth ) . length > 0 ) {
50
- syncSetAuth ( auth ) . then ( ( ) => console . info ( 'settings-store' , 'Auth saved' , { ...auth } ) ) ;
51
- } else {
52
- syncClearAuth ( ) . then ( ( ) => console . info ( 'settings-store' , 'Auth cleared' ) ) ;
53
- }
114
+ if ( Object . keys ( _auth ) . length > 0 ) return syncSetAuth ( _auth , account ) ;
115
+ return syncClearAuth ( account ) ;
54
116
} ;
55
117
56
- return { auth, setAuth, authenticated, isAuthenticated, syncRestoreAuth } ;
118
+ return { auth, setAuth, authenticated, isAuthenticated, syncRestoreAuth, syncSetAuth } ;
57
119
} ) ;
58
120
59
- export const useSettingsStoreRefs = ( ) => storeToRefs ( useSettingsStore ( ) ) ;
121
+ export const useAuthSettingsStoreRefs = ( ) => storeToRefs ( useAuthSettingsStore ( ) ) ;
0 commit comments