11import Ape from "../ape" ;
22import * as Notifications from "../elements/notifications" ;
33import Config , * as UpdateConfig from "../config" ;
4- import * as AccountButton from "../elements/account-button" ;
54import * as Misc from "../utils/misc" ;
6- import * as Settings from "../pages/settings" ;
75import * as DB from "../db" ;
8- import * as TestLogic from "../test/test-logic" ;
96import * as Loader from "../elements/loader" ;
107import * as PageTransition from "../states/page-transition" ;
118import * as ActivePage from "../states/active-page" ;
129import * as LoadingPage from "../pages/loading" ;
1310import * as LoginPage from "../pages/login" ;
14- import * as ResultFilters from "../elements/account/result-filters" ;
15- import * as TagController from "./tag-controller" ;
1611import * as RegisterCaptchaModal from "../modals/register-captcha" ;
17- import * as LastSignedOutResultModal from "../modals/last-signed-out-result" ;
18- import * as URLHandler from "../utils/url-handler" ;
1912import * as Account from "../pages/account" ;
20- import * as Alerts from "../elements/alerts" ;
21- import * as AccountSettings from "../pages/account-settings" ;
2213import {
2314 GoogleAuthProvider ,
2415 GithubAuthProvider ,
@@ -37,17 +28,12 @@ import {
3728 signInWithPopup ,
3829 resetIgnoreAuthCallback ,
3930} from "../firebase" ;
40- import {
41- hideFavoriteQuoteLength ,
42- showFavoriteQuoteLength ,
43- } from "../test/test-config" ;
4431import * as ConnectionState from "../states/connection" ;
4532import { navigate } from "./route-controller" ;
46- import * as PSA from "../elements/psa" ;
4733import { getActiveFunboxesWithFunction } from "../test/funbox/list" ;
48- import { Snapshot } from "../constants/default-snapshot" ;
4934import * as Sentry from "../sentry" ;
5035import { tryCatch } from "@monkeytype/util/trycatch" ;
36+ import * as AuthEvent from "../observables/auth-event" ;
5137
5238export const gmailProvider = new GoogleAuthProvider ( ) ;
5339export const githubProvider = new GithubAuthProvider ( ) ;
@@ -87,12 +73,58 @@ async function getDataAndInit(): Promise<boolean> {
8773 LoadingPage . updateText ( "Downloading user data..." ) ;
8874 await LoadingPage . showBar ( ) ;
8975 const snapshot = await DB . initSnapshot ( ) ;
90- if ( snapshot !== false ) {
91- Sentry . setUser ( snapshot . uid , snapshot . name ) ;
76+
77+ if ( snapshot === false ) {
78+ throw new Error (
79+ "Snapshot didn't initialize due to lacking authentication even though user is authenticated"
80+ ) ;
81+ }
82+
83+ Sentry . setUser ( snapshot . uid , snapshot . name ) ;
84+ if ( snapshot . needsToChangeName ) {
85+ Notifications . addPSA (
86+ "You need to update your account name. <a class='openNameChange'>Click here</a> to change it and learn more about why." ,
87+ - 1 ,
88+ undefined ,
89+ true ,
90+ undefined ,
91+ true
92+ ) ;
93+ }
94+ if ( ActivePage . get ( ) === "loading" ) {
95+ LoadingPage . updateBar ( 100 ) ;
96+ } else {
97+ LoadingPage . updateBar ( 45 ) ;
98+ }
99+ LoadingPage . updateText ( "Applying settings..." ) ;
100+
101+ const areConfigsEqual =
102+ JSON . stringify ( Config ) === JSON . stringify ( snapshot . config ) ;
103+
104+ if ( Config === undefined || ! areConfigsEqual ) {
105+ console . log (
106+ "no local config or local and db configs are different - applying db"
107+ ) ;
108+ await UpdateConfig . apply ( snapshot . config ) ;
109+ UpdateConfig . saveFullConfigToLocalStorage ( true ) ;
110+
111+ //funboxes might be different and they wont activate on the account page
112+ for ( const fb of getActiveFunboxesWithFunction ( "applyGlobalCSS" ) ) {
113+ fb . functions . applyGlobalCSS ( ) ;
114+ }
115+ }
116+ if ( window . location . pathname === "/account" ) {
117+ LoadingPage . updateBar ( 90 ) ;
118+ await Account . downloadResults ( ) ;
119+ }
120+ if ( window . location . pathname === "/login" ) {
121+ navigate ( "/account" ) ;
122+ } else {
123+ navigate ( ) ;
92124 }
125+ return true ;
93126 } catch ( error ) {
94127 console . error ( error ) ;
95- AccountButton . loading ( false ) ;
96128 LoginPage . enableInputs ( ) ;
97129 $ ( "header nav .view-account" ) . css ( "opacity" , 1 ) ;
98130 if ( error instanceof DB . SnapshotInitError ) {
@@ -120,70 +152,17 @@ async function getDataAndInit(): Promise<boolean> {
120152 }
121153 return false ;
122154 }
123- if ( ActivePage . get ( ) === "loading" ) {
124- LoadingPage . updateBar ( 100 ) ;
125- } else {
126- LoadingPage . updateBar ( 45 ) ;
127- }
128- LoadingPage . updateText ( "Applying settings..." ) ;
129- const snapshot = DB . getSnapshot ( ) as Snapshot ;
130- AccountButton . update ( snapshot ) ;
131- Alerts . setNotificationBubbleVisible ( snapshot . inboxUnreadSize > 0 ) ;
132- showFavoriteQuoteLength ( ) ;
133-
134- ResultFilters . loadTags ( snapshot . tags ) ;
135-
136- // filters = defaultResultFilters;
137- void ResultFilters . load ( ) ;
138-
139- if ( snapshot . needsToChangeName ) {
140- Notifications . addPSA (
141- "You need to update your account name. <a class='openNameChange'>Click here</a> to change it and learn more about why." ,
142- - 1 ,
143- undefined ,
144- true ,
145- undefined ,
146- true
147- ) ;
148- }
149-
150- const areConfigsEqual =
151- JSON . stringify ( Config ) === JSON . stringify ( snapshot . config ) ;
152-
153- if ( Config === undefined || ! areConfigsEqual ) {
154- console . log (
155- "no local config or local and db configs are different - applying db"
156- ) ;
157- await UpdateConfig . apply ( snapshot . config ) ;
158- UpdateConfig . saveFullConfigToLocalStorage ( true ) ;
159-
160- //funboxes might be different and they wont activate on the account page
161- for ( const fb of getActiveFunboxesWithFunction ( "applyGlobalCSS" ) ) {
162- fb . functions . applyGlobalCSS ( ) ;
163- }
164- }
165- AccountButton . loading ( false ) ;
166- TagController . loadActiveFromLocalStorage ( ) ;
167- if ( window . location . pathname === "/account" ) {
168- LoadingPage . updateBar ( 90 ) ;
169- await Account . downloadResults ( ) ;
170- }
171- if ( window . location . pathname === "/login" ) {
172- navigate ( "/account" ) ;
173- } else {
174- navigate ( ) ;
175- }
176- return true ;
177155}
178156
179157export async function loadUser ( _user : UserType ) : Promise < void > {
180158 // User is signed in.
181159 PageTransition . set ( false ) ;
182- AccountButton . loading ( true ) ;
183160 if ( ! ( await getDataAndInit ( ) ) ) {
184161 signOut ( ) ;
185162 }
186163
164+ AuthEvent . dispatch ( { type : "snapshotUpdated" , data : { isInitial : true } } ) ;
165+
187166 // var displayName = user.displayName;
188167 // var email = user.email;
189168 // var emailVerified = user.emailVerified;
@@ -194,21 +173,14 @@ export async function loadUser(_user: UserType): Promise<void> {
194173 LoginPage . hidePreloader ( ) ;
195174
196175 // showFavouriteThemesAtTheTop();
197-
198- if ( TestLogic . notSignedInLastResult !== null ) {
199- LastSignedOutResultModal . show ( ) ;
200- }
201176}
202177
203178export async function onAuthStateChanged (
204179 authInitialisedAndConnected : boolean ,
205180 user : UserType | null
206181) : Promise < void > {
207- const search = window . location . search ;
208- const hash = window . location . hash ;
209182 console . debug ( `account controller ready` ) ;
210183 if ( authInitialisedAndConnected ) {
211- void PSA . show ( ) ;
212184 console . debug ( `auth state changed, user ${ user ? "true" : "false" } ` ) ;
213185 console . debug ( user ) ;
214186 if ( user ) {
@@ -218,13 +190,8 @@ export async function onAuthStateChanged(
218190 window . history . replaceState ( "" , "" , "/login" ) ;
219191 }
220192
221- Settings . hideAccountSection ( ) ;
222- AccountButton . update ( undefined ) ;
223193 DB . setSnapshot ( undefined ) ;
224194 Sentry . clearUser ( ) ;
225- setTimeout ( ( ) => {
226- hideFavoriteQuoteLength ( ) ;
227- } , 125 ) ;
228195 PageTransition . set ( false ) ;
229196 navigate ( ) ;
230197 }
@@ -238,12 +205,10 @@ export async function onAuthStateChanged(
238205 navigate ( ) ;
239206 }
240207
241- URLHandler . loadCustomThemeFromUrl ( search ) ;
242- URLHandler . loadTestSettingsFromUrl ( search ) ;
243- URLHandler . loadChallengeFromUrl ( search ) ;
244- void URLHandler . linkDiscord ( hash ) ;
245-
246- AccountSettings . updateUI ( ) ;
208+ AuthEvent . dispatch ( {
209+ type : "authStateChanged" ,
210+ data : { isUserSignedIn : user !== null } ,
211+ } ) ;
247212}
248213
249214export async function signIn ( email : string , password : string ) : Promise < void > {
@@ -360,7 +325,7 @@ async function addAuthProvider(
360325 await linkWithPopup ( user , provider ) ;
361326 Loader . hide ( ) ;
362327 Notifications . add ( `${ providerName } authentication added` , 1 ) ;
363- AccountSettings . updateUI ( ) ;
328+ AuthEvent . dispatch ( { type : "authConfigUpdated" } ) ;
364329 } catch ( error ) {
365330 Loader . hide ( ) ;
366331 const message = Misc . createErrorMessage (
0 commit comments