-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathNotificationTranslationTopic.ts
More file actions
41 lines (38 loc) · 1.79 KB
/
NotificationTranslationTopic.ts
File metadata and controls
41 lines (38 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import {
attachmentUploadBlockedNotificationTranslator,
attachmentUploadFailedNotificationTranslator,
attachmentUploadNotTerminatedTranslator,
} from './attachmentUpload';
import { TranslationTopic } from '../../TranslationBuilder';
import type { Notification } from 'stream-chat';
import type { NotificationTranslatorOptions } from './types';
import type { TranslationTopicOptions, Translator } from '../../index';
import { pollCreationFailedNotificationTranslator } from './pollComposition';
import { pollVoteCountTrespass } from './pollVoteCountTrespass';
export const defaultNotificationTranslators: Record<
string,
Translator<NotificationTranslatorOptions>
> = {
'api:attachment:upload:failed': attachmentUploadFailedNotificationTranslator,
'api:poll:create:failed': pollCreationFailedNotificationTranslator,
'validation:attachment:upload:blocked': attachmentUploadBlockedNotificationTranslator,
'validation:attachment:upload:in-progress': attachmentUploadNotTerminatedTranslator,
'validation:poll:castVote:limit': pollVoteCountTrespass,
};
export class NotificationTranslationTopic extends TranslationTopic<NotificationTranslatorOptions> {
constructor({ i18next, translators }: TranslationTopicOptions) {
super({ i18next, translators: defaultNotificationTranslators });
if (translators) {
Object.entries(translators).forEach(([name, translator]) => {
this.setTranslator(name, translator);
});
}
}
translate = (value: string, key: string, options: { notification?: Notification }) => {
const { notification } = options;
if (!notification) return value;
const translator = notification.type && this.translators.get(notification.type);
if (!translator) return value;
return translator({ key, options, t: this.i18next.t, value }) || value;
};
}