import { commitLocalUpdate , Environment , graphql } from "relay-runtime" ; import { StaticConfig } from "coral-common/config" ; import { parseQuery } from "coral-common/utils" ; import { isStoryMode } from "coral-framework/helpers" ; import { parseAccessToken } from "coral-framework/lib/auth" ; import { InitLocalState } from "coral-framework/lib/bootstrap/createManaged" ; import { getExternalConfig } from "coral-framework/lib/externalConfig" ; import { createAndRetain , fetchQuery , initLocalBaseState , } from "coral-framework/lib/relay" ; import { GQLFEATURE_FLAG } from "coral-framework/schema" ; import { initLocalStateQuery } from "coral-stream/__generated__/initLocalStateQuery.graphql" ; import { COMMENTS_ORDER_BY } from "../constants" ; import { AUTH_POPUP_ID , AUTH_POPUP_TYPE } from "./constants" ; interface ResolvedConfig { readonly featureFlags : string [ ] ; readonly flattenReplies ? : boolean | null ; } async function resolveConfig ( environment , staticConfig ? ) { if ( staticConfig ) { return staticConfig as ResolvedConfig ; } if ( process . env . NODE_ENV === "development" ) { const data = await fetchQuery < initLocalStateQuery > ( environment , graphql `
        query initLocalStateQuery {
          settings {
            flattenReplies
            featureFlags
          }
        }
      ` , { } ) ; return data . settings as ResolvedConfig ; } return { featureFlags : [ ] , flattenReplies : false , } ; } /**
 * Initializes the local state, before we start the App.
 */ const initLocalState = async ( { environment , context , auth = null , staticConfig , ... rest } ) => { const config = await getExternalConfig ( context . window , context . pym ) ; if ( config ) { if ( config . accessToken ) { auth = parseAccessToken ( config . accessToken ) ; } if ( config . bodyClassName ) { context . window . document . body . classList . add ( config . bodyClassName ) ; } } await initLocalBaseState ( { environment , context , auth , staticConfig , ... rest , } ) ; const { featureFlags , flattenReplies } = await resolveConfig ( environment , staticConfig ) ; const commentsOrderBy = ( await context . localStorage . getItem ( COMMENTS_ORDER_BY ) ) || "CREATED_AT_DESC" ; commitLocalUpdate ( environment , ( s ) => { const root = s . getRoot ( ) ; const localRecord = root . getLinkedRecord ( "local" ) ! ; const query = parseQuery ( location . search ) ; if ( query . storyID ) { localRecord . setValue ( query . storyID , "storyID" ) ; } if ( query . storyURL ) { localRecord . setValue ( query . storyURL , "storyURL" ) ; } if ( query . storyMode && isStoryMode ( query . storyMode ) ) { localRecord . setValue ( query . storyMode , "storyMode" ) ; } if ( query . commentID ) { localRecord . setValue ( query . commentID , "commentID" ) ; } localRecord . setValue ( commentsOrderBy , "commentsOrderBy" ) ; const authPopupRecord = createAndRetain ( environment , s , AUTH_POPUP_ID , AUTH_POPUP_TYPE ) ; authPopupRecord . setValue ( false , "open" ) ; authPopupRecord . setValue ( false , "focus" ) ; authPopupRecord . setValue ( "" , "href" ) ; localRecord . setLinkedRecord ( authPopupRecord , "authPopup" ) ; localRecord . setValue ( "COMMENTS" , "activeTab" ) ; localRecord . setValue ( "MY_COMMENTS" , "profileTab" ) ; localRecord . setValue ( "NONE" , "commentsTab" ) ; localRecord . setValue ( featureFlags . includes ( GQLFEATURE_FLAG . COMMENT_SEEN ) , "enableCommentSeen" ) ; localRecord . setValue ( ! ! flattenReplies , "flattenReplies" ) ; localRecord . setValue ( featureFlags . includes ( GQLFEATURE_FLAG . Z_KEY ) , "enableZKey" ) ; localRecord . setValue ( config ?. version , "embedVersion" ) ; localRecord . setValue ( Boolean ( config ?. amp ) , "amp" ) ; } ) ; } ; export default initLocalState ;