1
- import { ComponentProps , DisplayComponent , EventBus , FSComponent , VNode } from "@microsoft/msfs-sdk" ;
2
- import { getDefaultAppDomain } from "@ navigraph/app" ;
3
- import { CancelToken , navigraphRequest } from "navigraph/auth" ;
4
- import { AuthService } from "../Services/AuthService" ;
5
- import "./NavigraphLogin.css" ;
6
-
1
+ import { ComponentProps , DisplayComponent , EventBus , FSComponent , VNode } from "@microsoft/msfs-sdk"
2
+ import { CancelToken , navigraphRequest } from "navigraph/auth"
3
+ import { packages } from "../Lib/navigraph"
4
+ import { AuthService } from "../Services/AuthService"
5
+ import "./NavigraphLogin.css"
6
+ import { Dropdown } from "./Dropdown"
7
7
8
8
interface NavigraphLoginProps extends ComponentProps {
9
9
bus : EventBus
@@ -12,8 +12,10 @@ interface NavigraphLoginProps extends ComponentProps {
12
12
export class NavigraphLogin extends DisplayComponent < NavigraphLoginProps > {
13
13
private readonly textRef = FSComponent . createRef < HTMLDivElement > ( )
14
14
private readonly navdataTextRef = FSComponent . createRef < HTMLDivElement > ( )
15
- private readonly buttonRef = FSComponent . createRef < HTMLButtonElement > ( )
15
+ private readonly loginButtonRef = FSComponent . createRef < HTMLButtonElement > ( )
16
16
private readonly qrCodeRef = FSComponent . createRef < HTMLImageElement > ( )
17
+ private readonly dropdownRef = FSComponent . createRef < Dropdown > ( )
18
+ private readonly downloadButtonRef = FSComponent . createRef < HTMLButtonElement > ( )
17
19
18
20
private cancelSource = CancelToken . source ( )
19
21
@@ -42,10 +44,20 @@ export class NavigraphLogin extends DisplayComponent<NavigraphLoginProps> {
42
44
public render ( ) : VNode {
43
45
return (
44
46
< div class = "auth-container" >
45
- < div ref = { this . textRef } />
46
- < div ref = { this . buttonRef } onClick = { this . handleClick . bind ( this ) } class = "login-button" />
47
- < div ref = { this . navdataTextRef } />
48
- < img ref = { this . qrCodeRef } class = "qr-code" />
47
+ < div class = "horizontal" >
48
+ < div class = "vertical" >
49
+ < div ref = { this . textRef } />
50
+ < div ref = { this . loginButtonRef } class = "button" />
51
+ < div ref = { this . navdataTextRef } />
52
+ < img ref = { this . qrCodeRef } class = "qr-code" />
53
+ </ div >
54
+ < div class = "vertical" >
55
+ < Dropdown ref = { this . dropdownRef } />
56
+ < div ref = { this . downloadButtonRef } class = "button" >
57
+ Download
58
+ </ div >
59
+ </ div >
60
+ </ div >
49
61
</ div >
50
62
)
51
63
}
@@ -57,18 +69,19 @@ export class NavigraphLogin extends DisplayComponent<NavigraphLoginProps> {
57
69
public onAfterRender ( node : VNode ) : void {
58
70
super . onAfterRender ( node )
59
71
60
- this . buttonRef . instance . addEventListener ( "click" , ( ) => this . handleClick ( ) . catch ( e => console . error ( e ) ) )
72
+ this . loginButtonRef . instance . addEventListener ( "click" , ( ) => this . handleClick ( ) . catch ( e => console . error ( e ) ) )
73
+ this . downloadButtonRef . instance . addEventListener ( "click" , ( ) => this . handleDownloadClick ( ) )
61
74
62
75
AuthService . user . sub ( user => {
63
76
if ( user ) {
64
77
this . qrCodeRef . instance . src = ""
65
78
this . qrCodeRef . instance . style . display = "none"
66
- this . buttonRef . instance . textContent = "Log out"
79
+ this . loginButtonRef . instance . textContent = "Log out"
67
80
this . textRef . instance . textContent = `Welcome, ${ user . preferred_username } `
68
81
69
- this . handleLogin ( ) . catch ( e => console . error ( e ) )
82
+ this . handleLogin ( )
70
83
} else {
71
- this . buttonRef . instance . textContent = "Sign in"
84
+ this . loginButtonRef . instance . textContent = "Sign in"
72
85
this . textRef . instance . textContent = "Not signed in"
73
86
}
74
87
} , true )
@@ -81,25 +94,42 @@ export class NavigraphLogin extends DisplayComponent<NavigraphLoginProps> {
81
94
this . cancelSource = CancelToken . source ( ) // Reset any previous cancellations
82
95
AuthService . signIn ( p => {
83
96
if ( p ) {
84
- this . qrCodeRef . instance . src = `https://api.qrserver.com/v1/create-qr-code/?size=500x500 &data=${ p . verification_uri_complete } `
97
+ this . qrCodeRef . instance . src = `https://api.qrserver.com/v1/create-qr-code/?size=200x200 &data=${ p . verification_uri_complete } `
85
98
this . qrCodeRef . instance . style . display = "block"
86
99
console . info ( p . verification_uri_complete )
87
100
}
88
101
} , this . cancelSource . token ) . catch ( e => console . error ( "Failed to sign in!" , e ) )
89
102
}
90
103
}
91
104
92
- private async handleLogin ( ) {
93
- console . log ( "successful login, downloading navdata" )
94
- this . navdataTextRef . instance . textContent = "Downloading navdata..."
95
- // leaving this here for now (messy) until the PR from the sdk is merged
96
- const result = await navigraphRequest
97
- . get ( `https://fmsdata.api.${ getDefaultAppDomain ( ) } /v3/packages?format=avionics_v1` )
105
+ private handleLogin ( ) {
106
+ // Let's display all of our packages
107
+ packages
108
+ . listPackages ( )
109
+ . then ( pkgs => {
110
+ for ( const pkg of pkgs ) {
111
+ this . dropdownRef . instance . addDropdownItem ( pkg . format , pkg . format )
112
+ }
113
+ } )
114
+ . catch ( e => console . error ( e ) )
115
+ }
116
+
117
+ private handleDownloadClick ( ) {
118
+ packages
119
+ . getPackage ( this . dropdownRef . instance . getNavdataFormat ( ) as string )
120
+ . then ( pkg => {
121
+ const url = pkg . file . url
122
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
123
+ this . commBusListener . call (
124
+ "COMM_BUS_WASM_CALLBACK" ,
125
+ "NAVIGRAPH_DownloadNavdata" ,
126
+ JSON . stringify ( {
127
+ url,
128
+ folder : pkg . format ,
129
+ } ) ,
130
+ )
131
+ this . navdataTextRef . instance . textContent = "Downloading navdata..."
132
+ } )
98
133
. catch ( e => console . error ( e ) )
99
- const signedUrl = result . data [ 0 ] . files [ 0 ] . signed_url
100
- console . log ( "signed url" , signedUrl )
101
- await this . commBusListener . call ( "COMM_BUS_WASM_CALLBACK" , "NAVIGRAPH_DownloadNavdata" , JSON . stringify ( {
102
- url : signedUrl
103
- } ) )
104
134
}
105
- }
135
+ }
0 commit comments