@@ -4,17 +4,16 @@ export default function initDashboard(): void {
4
4
if ( ! document . location . href . startsWith ( 'https://localhost/dashboard/' ) ) {
5
5
return ;
6
6
}
7
- console . log ( 'Will init dashboard!' ) ;
8
- async function onNavigate ( event : Event ) {
9
- console . log ( `${ event . type } ! -> ${ location . href } ` ) ;
10
-
7
+ // Navigation API is only available in Chrome-derived browsers like Electron.
8
+ // https://developer.mozilla.org/en-US/docs/Web/API/Navigation
9
+ ( window as any ) . navigation . addEventListener ( 'navigate' , async function onNavigate ( ) {
11
10
const resp = await fetch ( 'https://localhost/v3/users?me=true' ) ;
11
+ let loginSuccessful = false ;
12
12
13
- console . log ( resp ) ;
14
13
if ( resp . status === 401 ) {
15
- // Need to login
16
14
const token = await ipcRenderer . invoke ( 'dashboard/get-csrf-token' ) ?? '' ;
17
- await fetch ( "https://localhost/v3-public/localProviders/local?action=login" , {
15
+ const loginURL = 'https://localhost/v3-public/localProviders/local?action=login' ;
16
+ const resp = await fetch ( loginURL , {
18
17
headers : {
19
18
'Accept' : "application/json" ,
20
19
'Content-Type' : "application/json" ,
@@ -29,25 +28,30 @@ export default function initDashboard(): void {
29
28
method : "POST" ,
30
29
credentials : "include"
31
30
} ) ;
31
+ loginSuccessful = resp . ok ;
32
32
}
33
33
34
- if ( location . pathname === '/dashboard/auth/login' ) {
35
- console . log ( 'Logging in!' ) ;
36
- /** Helper to evalute a singel XPath expression */
37
- function $x < T extends Element > ( expr : string ) {
38
- return document . evaluate (
39
- expr ,
40
- document ,
41
- null ,
42
- XPathResult . FIRST_ORDERED_NODE_TYPE
43
- ) . singleNodeValue as T ;
44
- }
45
- $x < HTMLInputElement > ( '//*[@id="username"]/descendant-or-self:input' ) . value = 'admin' ;
46
- $x < HTMLInputElement > ( '//*[@id="password"]/descendant-or-self:input' ) . value = 'password' ;
47
- $x < HTMLButtonElement > ( '//*[@id=submit]' ) . click ( ) ;
34
+ switch ( location . pathname ) {
35
+ case '/dashboard/auth/login' :
36
+ // If we logged in, return to the page before the login form.
37
+ if ( loginSuccessful ) {
38
+ history . back ( ) ;
39
+ }
40
+ return ;
41
+ case '/dashboard/home' :
42
+ // Whenever we go to home, replace with cluster explorer.
43
+ location . pathname = '/dashboard/c/local/explorer' ;
44
+ return ;
48
45
}
49
- }
50
- window . addEventListener ( 'hashchange' , onNavigate ) ;
51
- window . addEventListener ( 'pageshow' , onNavigate ) ;
52
- window . addEventListener ( 'popstate' , onNavigate ) ;
46
+ } ) ;
47
+ window . addEventListener ( 'load' , function ( ) {
48
+ const stylesheet = new CSSStyleSheet ( ) ;
49
+ // Hide the extensions navgation button.
50
+ stylesheet . insertRule ( `
51
+ .side-menu div:has(> a.option[href="/dashboard/c/local/uiplugins"]) {
52
+ display: none;
53
+ }
54
+ ` ) ;
55
+ document . adoptedStyleSheets . push ( stylesheet ) ;
56
+ } ) ;
53
57
}
0 commit comments