1
1
import { Component , DestroyRef , Inject , OnInit } from '@angular/core' ;
2
2
import { FormControl } from '@angular/forms' ;
3
3
import { MAT_DIALOG_DATA } from '@angular/material/dialog' ;
4
- import { BehaviorSubject , combineLatest } from 'rxjs' ;
4
+ import { BehaviorSubject } from 'rxjs' ;
5
5
import { debounceTime , map , skip , startWith } from 'rxjs/operators' ;
6
6
import { OnlineStatusService } from 'xforge-common/online-status.service' ;
7
7
import { quietTakeUntilDestroyed } from 'xforge-common/util/rxjs-util' ;
@@ -21,44 +21,46 @@ export interface TranslatorSettingsDialogData {
21
21
standalone : false
22
22
} )
23
23
export class TranslatorSettingsDialogComponent implements OnInit {
24
- suggestionsEnabledSwitch = new FormControl < boolean > ( { value : false , disabled : ! this . onlineStatusService . isOnline } ) ;
25
- lynxMasterSwitch = new FormControl < boolean > ( false ) ;
26
- lynxAssessmentsEnabled = new FormControl < boolean > ( false ) ;
27
- lynxAutoCorrectEnabled = new FormControl < boolean > ( false ) ;
24
+ readonly suggestionsEnabledSwitch = new FormControl < boolean > ( {
25
+ value : false ,
26
+ disabled : ! this . onlineStatusService . isOnline
27
+ } ) ;
28
+ readonly lynxMasterSwitch = new FormControl < boolean > ( false ) ;
29
+ readonly lynxAssessmentsEnabled = new FormControl < boolean > ( false ) ;
30
+ readonly lynxAutoCorrectEnabled = new FormControl < boolean > ( false ) ;
31
+
32
+ showSuggestionsSettings = false ;
33
+ showLynxSettings = false ;
34
+ translationSuggestionsDisabled = false ;
35
+ lynxAssessmentsProjectEnabled = false ;
36
+ lynxAutoCorrectProjectEnabled = false ;
37
+ numSuggestions = '1' ;
38
+
39
+ readonly confidenceThreshold$ = new BehaviorSubject < number > ( 20 ) ;
28
40
29
41
private readonly projectDoc : SFProjectProfileDoc = this . data . projectDoc ;
30
42
private readonly projectUserConfigDoc : SFProjectUserConfigDoc = this . data . projectUserConfigDoc ;
31
- private confidenceThreshold$ = new BehaviorSubject < number > ( 20 ) ;
32
43
33
44
constructor (
34
45
@Inject ( MAT_DIALOG_DATA ) private readonly data : TranslatorSettingsDialogData ,
35
46
readonly onlineStatusService : OnlineStatusService ,
36
- private destroyRef : DestroyRef
47
+ private readonly destroyRef : DestroyRef
37
48
) { }
38
49
39
50
ngOnInit ( ) : void {
40
- if ( this . projectUserConfigDoc . data != null ) {
41
- const percent = Math . round ( this . projectUserConfigDoc . data . confidenceThreshold * 100 ) ;
42
- this . confidenceThreshold$ . next ( percent ) ;
43
- }
51
+ this . updateComponentState ( ) ;
44
52
45
- this . suggestionsEnabledSwitch . setValue ( this . translationSuggestionsUserEnabled ) ;
46
- this . lynxAssessmentsEnabled . setValue ( this . lynxAssessmentsUserEnabled ) ;
47
- this . lynxAutoCorrectEnabled . setValue ( this . lynxAutoCorrectUserEnabled ) ;
48
- this . lynxMasterSwitch . setValue ( this . lynxMasterEnabled ) ;
53
+ this . onlineStatusService . onlineStatus$
54
+ . pipe ( quietTakeUntilDestroyed ( this . destroyRef ) )
55
+ . subscribe ( ( ) => this . onOnlineStatusChange ( ) ) ;
56
+
57
+ this . projectDoc . changes$
58
+ . pipe ( startWith ( null ) , quietTakeUntilDestroyed ( this . destroyRef ) )
59
+ . subscribe ( ( ) => this . updateComponentState ( ) ) ;
49
60
50
- combineLatest ( [ this . onlineStatusService . onlineStatus$ , this . projectDoc . changes$ . pipe ( startWith ( null ) ) ] )
61
+ this . projectUserConfigDoc . changes$
51
62
. pipe ( quietTakeUntilDestroyed ( this . destroyRef ) )
52
- . subscribe ( ( ) => {
53
- this . updateTranslationSuggestionsSwitch ( ) ;
54
- } ) ;
55
-
56
- this . projectUserConfigDoc . changes$ . pipe ( quietTakeUntilDestroyed ( this . destroyRef ) ) . subscribe ( ( ) => {
57
- this . updateTranslationSuggestionsSwitch ( ) ;
58
- this . lynxAssessmentsEnabled . setValue ( this . lynxAssessmentsUserEnabled , { emitEvent : false } ) ;
59
- this . lynxAutoCorrectEnabled . setValue ( this . lynxAutoCorrectUserEnabled , { emitEvent : false } ) ;
60
- this . lynxMasterSwitch . setValue ( this . lynxMasterEnabled , { emitEvent : false } ) ;
61
- } ) ;
63
+ . subscribe ( ( ) => this . updateComponentState ( ) ) ;
62
64
63
65
this . confidenceThreshold$
64
66
. pipe (
@@ -73,88 +75,82 @@ export class TranslatorSettingsDialogComponent implements OnInit {
73
75
) ;
74
76
}
75
77
76
- get translationSuggestionsDisabled ( ) : boolean {
77
- return ! this . translationSuggestionsUserEnabled || ! this . onlineStatusService . isOnline ;
78
+ setConfidenceThreshold ( value : number ) : void {
79
+ this . confidenceThreshold$ . next ( value ) ;
78
80
}
79
81
80
- get translationSuggestionsUserEnabled ( ) : boolean {
81
- return this . projectUserConfigDoc . data == null ? true : this . projectUserConfigDoc . data . translationSuggestionsEnabled ;
82
+ setNumSuggestions ( value : string ) : void {
83
+ this . numSuggestions = value ;
84
+ void this . projectUserConfigDoc . submitJson0Op ( op => op . set ( puc => puc . numSuggestions , parseInt ( value , 10 ) ) ) ;
82
85
}
83
86
84
- get numSuggestions ( ) : string {
85
- return this . projectUserConfigDoc . data == null ? '1' : this . projectUserConfigDoc . data . numSuggestions . toString ( ) ;
87
+ setTranslationSettingsEnabled ( value : boolean ) : void {
88
+ void this . projectUserConfigDoc . submitJson0Op ( op =>
89
+ op . set < boolean > ( puc => puc . translationSuggestionsEnabled , value )
90
+ ) ;
86
91
}
87
92
88
- set numSuggestions ( value : string ) {
89
- void this . projectUserConfigDoc . submitJson0Op ( op => op . set ( puc => puc . numSuggestions , parseInt ( value , 10 ) ) ) ;
93
+ setLynxAssessmentsEnabled ( value : boolean ) : void {
94
+ this . updateLynxInsightState ( { assessmentsEnabled : value } ) ;
90
95
}
91
96
92
- get confidenceThreshold ( ) : number {
93
- return this . confidenceThreshold$ . value ;
97
+ setLynxAutoCorrectEnabled ( value : boolean ) : void {
98
+ this . updateLynxInsightState ( { autoCorrectionsEnabled : value } ) ;
94
99
}
95
100
96
- set confidenceThreshold ( value : number ) {
97
- this . confidenceThreshold$ . next ( value ) ;
101
+ setLynxMasterEnabled ( value : boolean ) : void {
102
+ this . updateLynxInsightState ( {
103
+ assessmentsEnabled : value ,
104
+ autoCorrectionsEnabled : value
105
+ } ) ;
106
+ }
107
+ private get translationSuggestionsUserEnabled ( ) : boolean {
108
+ return this . projectUserConfigDoc . data ?. translationSuggestionsEnabled ?? true ;
98
109
}
99
110
100
- get lynxAssessmentsUserEnabled ( ) : boolean {
111
+ private get lynxAssessmentsUserEnabled ( ) : boolean {
101
112
return this . projectUserConfigDoc . data ?. lynxInsightState ?. assessmentsEnabled ?? true ;
102
113
}
103
114
104
- get lynxAutoCorrectUserEnabled ( ) : boolean {
115
+ private get lynxAutoCorrectUserEnabled ( ) : boolean {
105
116
return this . projectUserConfigDoc . data ?. lynxInsightState ?. autoCorrectionsEnabled ?? true ;
106
117
}
107
118
108
- get lynxAssessmentsProjectEnabled ( ) : boolean {
109
- return ! ! this . projectDoc . data ?. lynxConfig ?. assessmentsEnabled ;
110
- }
111
-
112
- get lynxAutoCorrectProjectEnabled ( ) : boolean {
113
- return ! ! this . projectDoc . data ?. lynxConfig ?. autoCorrectionsEnabled ;
114
- }
115
-
116
- get lynxMasterEnabled ( ) : boolean {
119
+ private get lynxMasterEnabled ( ) : boolean {
117
120
return (
118
121
( this . lynxAssessmentsProjectEnabled && this . lynxAssessmentsUserEnabled ) ||
119
122
( this . lynxAutoCorrectProjectEnabled && this . lynxAutoCorrectUserEnabled )
120
123
) ;
121
124
}
122
125
123
- get showSuggestionsSettings ( ) : boolean {
124
- return ! ! this . projectDoc . data ?. translateConfig . translationSuggestionsEnabled ;
125
- }
126
-
127
- get showLynxSettings ( ) : boolean {
128
- return this . lynxAssessmentsProjectEnabled || this . lynxAutoCorrectProjectEnabled ;
129
- }
130
-
131
- setTranslationSettingsEnabled ( value : boolean ) : void {
132
- void this . projectUserConfigDoc . submitJson0Op ( op =>
133
- op . set < boolean > ( puc => puc . translationSuggestionsEnabled , value )
134
- ) ;
135
- }
136
-
137
- updateTranslationSuggestionsSwitch ( ) : void {
126
+ private onOnlineStatusChange ( ) : void {
138
127
if ( this . onlineStatusService . isOnline ) {
139
128
this . suggestionsEnabledSwitch . enable ( ) ;
129
+ this . translationSuggestionsDisabled = ! this . translationSuggestionsUserEnabled ;
140
130
} else {
141
131
this . suggestionsEnabledSwitch . disable ( ) ;
132
+ this . translationSuggestionsDisabled = true ;
142
133
}
143
134
}
144
135
145
- setLynxAssessmentsEnabled ( value : boolean ) : void {
146
- this . updateLynxInsightState ( { assessmentsEnabled : value } ) ;
147
- }
136
+ private updateComponentState ( ) : void {
137
+ this . showSuggestionsSettings = ! ! this . projectDoc . data ?. translateConfig . translationSuggestionsEnabled ;
138
+ this . lynxAssessmentsProjectEnabled = ! ! this . projectDoc . data ?. lynxConfig ?. assessmentsEnabled ;
139
+ this . lynxAutoCorrectProjectEnabled = ! ! this . projectDoc . data ?. lynxConfig ?. autoCorrectionsEnabled ;
140
+ this . showLynxSettings = this . lynxAssessmentsProjectEnabled || this . lynxAutoCorrectProjectEnabled ;
141
+ this . translationSuggestionsDisabled = ! this . translationSuggestionsUserEnabled || ! this . onlineStatusService . isOnline ;
148
142
149
- setLynxAutoCorrectEnabled ( value : boolean ) : void {
150
- this . updateLynxInsightState ( { autoCorrectionsEnabled : value } ) ;
151
- }
143
+ if ( this . projectUserConfigDoc . data != null ) {
144
+ const percent = Math . round ( this . projectUserConfigDoc . data . confidenceThreshold * 100 ) ;
145
+ this . numSuggestions = this . projectUserConfigDoc . data . numSuggestions . toString ( ) ;
146
+ this . confidenceThreshold$ . next ( percent ) ;
147
+ }
152
148
153
- setLynxMasterEnabled ( value : boolean ) : void {
154
- this . updateLynxInsightState ( {
155
- assessmentsEnabled : value ,
156
- autoCorrectionsEnabled : value
157
- } ) ;
149
+ // Update form control state
150
+ this . suggestionsEnabledSwitch . setValue ( this . translationSuggestionsUserEnabled , { emitEvent : false } ) ;
151
+ this . lynxAssessmentsEnabled . setValue ( this . lynxAssessmentsUserEnabled , { emitEvent : false } ) ;
152
+ this . lynxAutoCorrectEnabled . setValue ( this . lynxAutoCorrectUserEnabled , { emitEvent : false } ) ;
153
+ this . lynxMasterSwitch . setValue ( this . lynxMasterEnabled , { emitEvent : false } ) ;
158
154
}
159
155
160
156
private updateLynxInsightState ( updates : { assessmentsEnabled ?: boolean ; autoCorrectionsEnabled ?: boolean } ) : void {
0 commit comments