@@ -19,7 +19,7 @@ const mock = process.mock
19
19
20
20
const users : Ref < UserLogin [ ] > | RemovableRef < UserLogin [ ] > = import . meta. server ? ref < UserLogin [ ] > ( [ ] ) : ref < UserLogin [ ] > ( [ ] ) as RemovableRef < UserLogin [ ] >
21
21
const nodes = useLocalStorage < Record < string , any > > ( STORAGE_KEY_NODES , { } , { deep : true } )
22
- const currentUserHandle = useLocalStorage < string > ( STORAGE_KEY_CURRENT_USER_HANDLE , mock ? mock . user . account . id : '' )
22
+ export const currentUserHandle = useLocalStorage < string > ( STORAGE_KEY_CURRENT_USER_HANDLE , mock ? mock . user . account . id : '' )
23
23
export const instanceStorage = useLocalStorage < Record < string , mastodon . v1 . Instance > > ( STORAGE_KEY_SERVERS , mock ? mock . server : { } , { deep : true } )
24
24
25
25
export type ElkInstance = Partial < mastodon . v1 . Instance > & {
@@ -32,17 +32,24 @@ export function getInstanceCache(server: string): mastodon.v1.Instance | undefin
32
32
}
33
33
34
34
export const currentUser = computed < UserLogin | undefined > ( ( ) => {
35
- if ( currentUserHandle . value ) {
36
- const user = users . value . find ( user => user . account ?. acct === currentUserHandle . value )
35
+ const handle = currentUserHandle . value
36
+ const currentUsers = users . value
37
+ if ( handle ) {
38
+ const user = currentUsers . find ( user => user . account ?. acct === handle )
37
39
if ( user )
38
40
return user
39
41
}
40
42
// Fallback to the first account
41
- return users . value [ 0 ]
43
+ return currentUsers . length ? currentUsers [ 0 ] : undefined
42
44
} )
43
45
44
46
const publicInstance = ref < ElkInstance | null > ( null )
45
- export const currentInstance = computed < null | ElkInstance > ( ( ) => currentUser . value ? instanceStorage . value [ currentUser . value . server ] ?? null : publicInstance . value )
47
+ export const currentInstance = computed < null | ElkInstance > ( ( ) => {
48
+ const user = currentUser . value
49
+ const storage = instanceStorage . value
50
+ const instance = publicInstance . value
51
+ return user ? storage [ user . server ] ?? null : instance
52
+ } )
46
53
47
54
export function getInstanceDomain ( instance : ElkInstance ) {
48
55
return instance . accountDomain || withoutProtocol ( instance . uri )
@@ -55,44 +62,6 @@ export const currentNodeInfo = computed<null | Record<string, any>>(() => nodes.
55
62
export const isGotoSocial = computed ( ( ) => currentNodeInfo . value ?. software ?. name === 'gotosocial' )
56
63
export const isGlitchEdition = computed ( ( ) => currentInstance . value ?. version ?. includes ( '+glitch' ) )
57
64
58
- // when multiple tabs: we need to reload window when sign in, switch account or sign out
59
- if ( import . meta. client ) {
60
- // fix #2972: now users loaded from idb, we need to wait for it
61
- const initialLoad = ref ( true )
62
- watchOnce ( users , ( ) => {
63
- initialLoad . value = false
64
- } , { immediate : true , flush : 'sync' } )
65
-
66
- const windowReload = ( ) => {
67
- if ( document . visibilityState === 'visible' && ! initialLoad . value )
68
- window . location . reload ( )
69
- }
70
- watch ( currentUserHandle , async ( handle , oldHandle ) => {
71
- // when sign in or switch account
72
- if ( handle ) {
73
- if ( handle === currentUser . value ?. account ?. acct ) {
74
- // when sign in, the other tab will not have the user, idb is not reactive
75
- const newUser = users . value . find ( user => user . account ?. acct === handle )
76
- // if the user is there, then we are switching account
77
- if ( newUser ) {
78
- // check if the change is on current tab: if so, don't reload
79
- if ( document . hasFocus ( ) || document . visibilityState === 'visible' )
80
- return
81
- }
82
- }
83
-
84
- window . addEventListener ( 'visibilitychange' , windowReload , { capture : true } )
85
- }
86
- // when sign out
87
- else if ( oldHandle ) {
88
- const oldUser = users . value . find ( user => user . account ?. acct === oldHandle )
89
- // when sign out, the other tab will not have the user, idb is not reactive
90
- if ( oldUser )
91
- window . addEventListener ( 'visibilitychange' , windowReload , { capture : true } )
92
- }
93
- } , { immediate : true , flush : 'post' } )
94
- }
95
-
96
65
export function useUsers ( ) {
97
66
return users
98
67
}
@@ -102,7 +71,10 @@ export function useSelfAccount(user: MaybeRefOrGetter<mastodon.v1.Account | unde
102
71
103
72
export const characterLimit = computed ( ( ) => currentInstance . value ?. configuration ?. statuses . maxCharacters ?? DEFAULT_POST_CHARS_LIMIT )
104
73
105
- export async function loginTo ( masto : ElkMasto , user : Overwrite < UserLogin , { account ?: mastodon . v1 . AccountCredentials } > ) {
74
+ export async function loginTo (
75
+ masto : ElkMasto ,
76
+ user : Overwrite < UserLogin , { account ?: mastodon . v1 . AccountCredentials } > ,
77
+ ) {
106
78
const { client } = masto
107
79
const instance = mastoLogin ( masto , user )
108
80
@@ -303,7 +275,7 @@ export async function signOut() {
303
275
if ( ! currentUserHandle . value )
304
276
await useRouter ( ) . push ( '/' )
305
277
306
- loginTo ( masto , currentUser . value || { server : publicServer . value } )
278
+ await loginTo ( masto , currentUser . value || { server : publicServer . value } )
307
279
}
308
280
309
281
export function checkLogin ( ) {
0 commit comments