import { commitLocalUpdate } from "relay-runtime" ; import { ADMIN_REDIRECT_PATH_KEY , MOD_QUEUE_SORT_ORDER , } from "coral-admin/constants" ; import { clearHash , getParamsFromHash } from "coral-framework/helpers" ; import { parseAccessToken } from "coral-framework/lib/auth" ; import { InitLocalState } from "coral-framework/lib/bootstrap/createManaged" ; import { initLocalBaseState , LOCAL_ID } from "coral-framework/lib/relay" ; import { GQLCOMMENT_SORT } from "coral-framework/schema" ; /**
 * Initializes the local state, before we start the App.
 */ const initLocalState = async ( { environment , context , auth = null , staticConfig , ... rest } ) => { let redirectPath = await context . localStorage . getItem ( ADMIN_REDIRECT_PATH_KEY ) ; if ( redirectPath && /^(\/\/|http:\/\/)/ . test ( redirectPath ) ) { redirectPath = null ; } let error = null ; const params = getParamsFromHash ( context . window ) ; if ( params && ( params . accessToken || params . error ) ) { clearHash ( context . window ) ; if ( params . error ) { error = params . error ; } if ( params . accessToken ) { auth = parseAccessToken ( params . accessToken ) ; } } await initLocalBaseState ( { environment , context , auth , staticConfig , ... rest , } ) ; const modQueueSortOrder = await context . localStorage . getItem ( MOD_QUEUE_SORT_ORDER ) ; commitLocalUpdate ( environment , ( s ) => { const localRecord = s . get ( LOCAL_ID ) ! ; localRecord . setValue ( redirectPath || null , "redirectPath" ) ; localRecord . setValue ( "SIGN_IN" , "authView" ) ; localRecord . setValue ( error , "authError" ) ; localRecord . setValue ( modQueueSortOrder ? modQueueSortOrder : GQLCOMMENT_SORT . CREATED_AT_DESC , "moderationQueueSort" ) ; localRecord . setValue ( staticConfig ?. forceAdminLocalAuth ?? false , "forceAdminLocalAuth" ) ; } ) ; } ; export default initLocalState ;