1- import { useEffect , useState , useRef } from 'react'
2- import { useIsAuthenticated } from '@azure/msal-react'
3- import { useMsal } from '@azure/msal-react'
4- import { loginRequest } from 'api/AuthConfig'
5- import { Autocomplete , Button , CircularProgress , Typography } from '@equinor/eds-core-react'
6- import { InteractionStatus , IPublicClientApplication } from '@azure/msal-browser'
1+ import { useEffect , useState } from 'react'
2+ import { Autocomplete , Button } from '@equinor/eds-core-react'
73import styled from 'styled-components'
84import { useLanguageContext } from 'components/Contexts/LanguageContext'
95import { Header } from 'components/Header/Header'
@@ -13,12 +9,6 @@ import { useNavigate } from 'react-router-dom'
139import { phone_width } from '../../utils/constants'
1410import { useAssetContext } from 'components/Contexts/AssetContext'
1511
16- const Centered = styled . div `
17- display: flex;
18- flex-direction: column;
19- align-items: center;
20- padding-top: 10px;
21- `
2212const StyledAssetSelection = styled . div `
2313 display: flex;
2414 flex-direction: column;
@@ -44,87 +34,44 @@ const StyledContent = styled.div`
4434 gap: 80px;
4535`
4636
47- const handleLogin = ( instance : IPublicClientApplication ) => {
48- const accounts = instance . getAllAccounts ( )
49- if ( accounts . length > 0 ) {
50- console . log ( 'Active account found, skipping login' )
51- return
52- }
53-
54- instance . loginRedirect ( loginRequest ) . catch ( ( e ) => {
55- console . error ( 'Login error:' , e )
56- } )
57- }
58-
59- export const AssetSelectionPage = ( ) => {
60- const isAuthenticated = useIsAuthenticated ( )
61- const { instance, inProgress } = useMsal ( )
62- const loginInitiatedRef = useRef ( false )
63-
64- useEffect ( ( ) => {
65- // Trigger login when not authenticated and no interaction is in progress
66- // Use ref to prevent double-triggering in StrictMode
67- if ( ! isAuthenticated && inProgress === InteractionStatus . None && ! loginInitiatedRef . current ) {
68- console . log ( 'Not authenticated, initiating login...' )
69- loginInitiatedRef . current = true
70- handleLogin ( instance )
71- }
72- } , [ isAuthenticated , inProgress , instance ] )
73-
74- // Show loading while authentication is in progress
75- if ( inProgress !== InteractionStatus . None ) {
76- return (
77- < Centered >
78- < Typography variant = "body_long_bold" color = "primary" >
79- Authentication in progress...
80- </ Typography >
81- < CircularProgress size = { 48 } />
82- </ Centered >
83- )
84- }
85-
86- return (
87- < >
88- { isAuthenticated ? (
89- < >
90- < Header page = { 'root' } />
91- < StyledContent >
92- < InstallationPicker />
93- < StyledImage src = { assetImage } />
94- </ StyledContent >
95- </ >
96- ) : (
97- < Centered >
98- < Typography variant = "body_long_bold" color = "primary" >
99- Redirecting to login...
100- </ Typography >
101- < CircularProgress size = { 48 } />
102- </ Centered >
103- ) }
104- </ >
105- )
106- }
37+ export const AssetSelectionPage = ( ) => (
38+ < >
39+ < Header page = { 'root' } />
40+ < StyledContent >
41+ < InstallationPicker />
42+ < StyledImage src = { assetImage } />
43+ </ StyledContent >
44+ </ >
45+ )
10746
10847export const findNavigationPage = ( installationCode : string ) => {
10948 if ( window . innerWidth <= 600 ) {
110- return `${ config . FRONTEND_BASE_ROUTE } /${ installationCode } / mission-control`
49+ return `${ config . FRONTEND_BASE_ROUTE } /${ installationCode } : mission-control`
11150 } else {
112- return `${ config . FRONTEND_BASE_ROUTE } /${ installationCode } / front-page`
51+ return `${ config . FRONTEND_BASE_ROUTE } /${ installationCode } : front-page`
11352 }
11453}
11554
11655const InstallationPicker = ( ) => {
11756 const { installationName, switchInstallation, activeInstallations, installationCode } = useAssetContext ( )
11857 const { TranslateText } = useLanguageContext ( )
11958 const [ selectedInstallation , setSelectedInstallation ] = useState < string > ( installationName )
59+ const [ shouldNavigate , setShouldNavigate ] = useState < boolean > ( false )
12060 const navigate = useNavigate ( )
12161
12262 const handleClick = ( ) => {
12363 switchInstallation ( selectedInstallation )
124- const target = findNavigationPage ( installationCode )
125- navigate ( target )
64+ setShouldNavigate ( true )
12665 }
12766
67+ useEffect ( ( ) => {
68+ if ( shouldNavigate ) {
69+ setShouldNavigate ( false )
70+ const target = findNavigationPage ( installationCode )
71+ navigate ( target )
72+ }
73+ } , [ shouldNavigate , installationCode ] )
74+
12875 const installationNames = activeInstallations . map ( ( i ) => i . name )
12976
13077 return (
0 commit comments