Sengi-Windows-MacOS-Linux/src/app/components/stream/status/status-translate/status-translate.component.ts

91 lines
3.2 KiB
TypeScript
Raw Normal View History

2023-08-05 05:36:21 +02:00
import { Component, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core';
2023-08-05 04:57:06 +02:00
import { Subscription } from 'rxjs';
import { StatusWrapper } from '../../../../models/common.model';
import { ILanguage } from '../../../../states/settings.state';
import { LanguageService } from '../../../../services/language.service';
import { InstancesInfoService } from '../../../../services/instances-info.service';
2023-08-05 05:36:21 +02:00
import { MastodonWrapperService } from '../../../../services/mastodon-wrapper.service';
import { Translation } from '../../../../services/models/mastodon.interfaces';
2023-08-05 04:57:06 +02:00
@Component({
selector: 'app-status-translate',
templateUrl: './status-translate.component.html',
styleUrls: ['./status-translate.component.scss']
})
export class StatusTranslateComponent implements OnInit, OnDestroy {
private languageSub: Subscription;
private languagesSub: Subscription;
selectedLanguage: ILanguage;
configuredLanguages: ILanguage[] = [];
isTranslationAvailable: boolean;
2023-08-05 05:36:21 +02:00
translatedBy: string;
2023-08-05 04:57:06 +02:00
@Input() status: StatusWrapper;
2023-08-05 05:36:21 +02:00
@Output() translation = new EventEmitter<Translation>();
2023-08-05 04:57:06 +02:00
constructor(
2023-08-05 05:36:21 +02:00
private readonly mastodonWrapperService: MastodonWrapperService,
2023-08-05 04:57:06 +02:00
private readonly languageService: LanguageService,
private readonly instancesInfoService: InstancesInfoService,
) { }
ngOnInit() {
this.languageSub = this.languageService.selectedLanguageChanged.subscribe(l => {
if (l) {
this.selectedLanguage = l;
this.analyseAvailability();
}
});
this.languagesSub = this.languageService.configuredLanguagesChanged.subscribe(l => {
if (l) {
this.configuredLanguages = l;
this.analyseAvailability();
}
});
2023-08-05 05:36:21 +02:00
}
ngOnDestroy(): void {
if (this.languageSub) this.languageSub.unsubscribe();
if (this.languagesSub) this.languagesSub.unsubscribe();
2023-08-05 04:57:06 +02:00
}
private analyseAvailability() {
this.instancesInfoService.getTranslationAvailability(this.status.provider)
.then(canTranslate => {
if (canTranslate
2023-08-05 05:36:21 +02:00
&& !this.status.isRemote
2023-08-05 04:57:06 +02:00
&& this.configuredLanguages.length > 0
&& this.configuredLanguages.findIndex(x => x.iso639 === this.status.status.language) === -1) {
console.warn('can translate');
this.isTranslationAvailable = true;
}
else {
this.isTranslationAvailable = false;
}
})
.catch(err => {
console.error(err);
this.isTranslationAvailable = false;
});
}
2023-08-05 05:36:21 +02:00
translate(): boolean {
this.mastodonWrapperService.translate(this.status.provider, this.status.status.id, this.selectedLanguage.iso639)
.then(x => {
this.translation.next(x);
this.translatedBy = x.provider;
this.isTranslationAvailable = false;
})
.catch(err => {
console.error(err);
});
return false;
2023-08-05 04:57:06 +02:00
}
}