|
| 1 | +import { MessageService } from '@theia/core'; |
| 2 | +import { FrontendApplicationContribution } from '@theia/core/lib/browser'; |
| 3 | +import { inject, injectable } from '@theia/core/shared/inversify'; |
| 4 | +import { shell } from '@theia/electron/shared/electron'; |
| 5 | +import { LocalStorageService } from '@theia/core/lib/browser'; |
| 6 | +import { SurveyRetriever } from '../survey/survey-retriever'; |
| 7 | +import { nls } from '@theia/core/lib/common'; |
| 8 | + |
| 9 | +const SURVEY_MESSAGE = nls.localize( |
| 10 | + 'arduino/survey/surveyMessage', |
| 11 | + 'Please help us improve by answering this super short survey. We value our community and would like to get to know our supporters a little better.' |
| 12 | +); |
| 13 | +const DO_NOT_SHOW_AGAIN = nls.localize( |
| 14 | + 'arduino/survey/dismissSurvey', |
| 15 | + 'DON’T SHOW ANYMORE' |
| 16 | +); |
| 17 | +const GO_TO_SURVEY = nls.localize( |
| 18 | + 'arduino/survey/answerSurvey', |
| 19 | + 'ANSWER SURVEY' |
| 20 | +); |
| 21 | + |
| 22 | +@injectable() |
| 23 | +export class SurveyNotification implements FrontendApplicationContribution { |
| 24 | + @inject(MessageService) |
| 25 | + protected readonly messageService: MessageService; |
| 26 | + |
| 27 | + @inject(LocalStorageService) |
| 28 | + protected readonly localStorageService: LocalStorageService; |
| 29 | + |
| 30 | + @inject(SurveyRetriever) |
| 31 | + protected readonly surveyRetriever: SurveyRetriever; |
| 32 | + |
| 33 | + async onStart(): Promise<void> { |
| 34 | + const survey = await this.surveyRetriever.getSurvey(); |
| 35 | + const surveyAnswered = await this.localStorageService.getData( |
| 36 | + this.surveyKey(survey.id), |
| 37 | + 'notAnswered' |
| 38 | + ); |
| 39 | + |
| 40 | + if (surveyAnswered !== 'notAnswered') { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + this.messageService |
| 45 | + .info(SURVEY_MESSAGE, DO_NOT_SHOW_AGAIN, GO_TO_SURVEY) |
| 46 | + .then(async (answer) => { |
| 47 | + switch (answer) { |
| 48 | + case GO_TO_SURVEY: |
| 49 | + shell.openExternal(survey.url.href); |
| 50 | + this.localStorageService.setData(this.surveyKey(survey.id), true); |
| 51 | + break; |
| 52 | + case DO_NOT_SHOW_AGAIN: |
| 53 | + this.localStorageService.setData(this.surveyKey(survey.id), false); |
| 54 | + break; |
| 55 | + } |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + private surveyKey(id: string): string { |
| 60 | + return `answered_survey:${id}`; |
| 61 | + } |
| 62 | +} |
0 commit comments