@nu-art/push-pub-sub-frontend 0.400.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/module-pack.d.ts +2 -0
- package/core/module-pack.js +26 -0
- package/index.d.ts +2 -0
- package/index.js +19 -0
- package/modules/ModuleFE_PushPubSub.d.ts +46 -0
- package/modules/ModuleFE_PushPubSub.js +157 -0
- package/modules/ModuleFE_PushSubscription.d.ts +8 -0
- package/modules/ModuleFE_PushSubscription.js +10 -0
- package/package.json +61 -0
- package/ui/ATS_PushPubSub/ATS_PushPubSub.d.ts +31 -0
- package/ui/ATS_PushPubSub/ATS_PushPubSub.js +100 -0
- package/ui/ATS_PushPubSub/ATS_PushPubSub.scss +46 -0
- package/ui/ATS_PushPubSub/index.d.ts +1 -0
- package/ui/ATS_PushPubSub/index.js +1 -0
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export declare const ModulePackFE_PushPubSub: (import("@nu-art/firebase-frontend/ModuleFE_Firebase").ModuleFE_Firebase_Class | import("../modules/ModuleFE_PushPubSub.js").ModuleFE_PushPubSub_Class | import("../modules/ModuleFE_PushSubscription.js").ModuleFE_PushSubscription_Class)[];
|
|
2
|
+
export * from '../modules/ModuleFE_PushPubSub.js';
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* A generic push pub sub infra for webapps
|
|
3
|
+
*
|
|
4
|
+
* Copyright (C) 2020 Adam van der Kruk aka TacB0sS
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
import { ModuleFE_PushPubSub } from '../modules/ModuleFE_PushPubSub.js';
|
|
19
|
+
import { ModuleFE_Firebase } from '@nu-art/firebase-frontend/index';
|
|
20
|
+
import { ModuleFE_PushSubscription } from '../modules/ModuleFE_PushSubscription.js';
|
|
21
|
+
export const ModulePackFE_PushPubSub = [
|
|
22
|
+
ModuleFE_Firebase,
|
|
23
|
+
ModuleFE_PushPubSub,
|
|
24
|
+
ModuleFE_PushSubscription
|
|
25
|
+
];
|
|
26
|
+
export * from '../modules/ModuleFE_PushPubSub.js';
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* A generic push pub sub infra for webapps
|
|
3
|
+
*
|
|
4
|
+
* Copyright (C) 2020 Adam van der Kruk aka TacB0sS
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
export * from './modules/ModuleFE_PushPubSub.js';
|
|
19
|
+
export * from './core/module-pack.js';
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Module } from '@nu-art/ts-common';
|
|
2
|
+
import { ApiDefCaller } from '@nu-art/thunderstorm-shared';
|
|
3
|
+
import { ApiStruct_PushMessages } from '@nu-art/push-pub-sub-shared';
|
|
4
|
+
export declare const Command_SwToApp = "SwToApp";
|
|
5
|
+
type FirebaseConfig = {
|
|
6
|
+
apiKey: string;
|
|
7
|
+
authDomain: string;
|
|
8
|
+
projectId: string;
|
|
9
|
+
messagingSenderId: string;
|
|
10
|
+
databaseURL?: string;
|
|
11
|
+
};
|
|
12
|
+
export type PushPubSubConfig = {
|
|
13
|
+
config?: FirebaseConfig;
|
|
14
|
+
publicKeyBase64: string;
|
|
15
|
+
swFileName?: string;
|
|
16
|
+
registerOnInit?: boolean;
|
|
17
|
+
};
|
|
18
|
+
export declare const pushSessionIdKey = "x-push-session-id";
|
|
19
|
+
export declare class ModuleFE_PushPubSub_Class extends Module<PushPubSubConfig> implements ApiDefCaller<ApiStruct_PushMessages> {
|
|
20
|
+
private subscriptions;
|
|
21
|
+
private firebaseToken?;
|
|
22
|
+
private messaging;
|
|
23
|
+
private dispatch_pushMessage;
|
|
24
|
+
readonly v1: ApiDefCaller<ApiStruct_PushMessages>['v1'];
|
|
25
|
+
private readonly pushSessionId;
|
|
26
|
+
protected timeout: number;
|
|
27
|
+
constructor();
|
|
28
|
+
private composeRegisterRequest;
|
|
29
|
+
init(): void;
|
|
30
|
+
getPushSessionId(): string;
|
|
31
|
+
private registerServiceWorker;
|
|
32
|
+
private getControllingServiceWorker;
|
|
33
|
+
deleteToken(): Promise<void>;
|
|
34
|
+
isNotificationEnabled(): boolean;
|
|
35
|
+
requestPermissions: () => Promise<void>;
|
|
36
|
+
getToken: (options?: {
|
|
37
|
+
vapidKey?: string;
|
|
38
|
+
serviceWorkerRegistration?: ServiceWorkerRegistration;
|
|
39
|
+
}) => Promise<void>;
|
|
40
|
+
hasToken: () => boolean;
|
|
41
|
+
private processMessageFromSw;
|
|
42
|
+
private processMessage;
|
|
43
|
+
private subscribeImpl;
|
|
44
|
+
}
|
|
45
|
+
export declare const ModuleFE_PushPubSub: ModuleFE_PushPubSub_Class;
|
|
46
|
+
export {};
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* A generic push pub sub infra for webapps
|
|
3
|
+
*
|
|
4
|
+
* Copyright (C) 2020 Adam van der Kruk aka TacB0sS
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
import { addItemToArray, compare, generateHex, ImplementationMissingException, Module, removeFromArray, ThisShouldNotHappenException } from '@nu-art/ts-common';
|
|
19
|
+
import { apiWithBody, StorageKey, ThunderDispatcher } from '@nu-art/thunderstorm-frontend/index';
|
|
20
|
+
import { ApiDef_PushMessages } from '@nu-art/push-pub-sub-shared';
|
|
21
|
+
export const Command_SwToApp = 'SwToApp';
|
|
22
|
+
export const pushSessionIdKey = 'x-push-session-id';
|
|
23
|
+
const pushSessionId = new StorageKey(pushSessionIdKey, false);
|
|
24
|
+
export class ModuleFE_PushPubSub_Class extends Module {
|
|
25
|
+
subscriptions = [];
|
|
26
|
+
firebaseToken;
|
|
27
|
+
messaging;
|
|
28
|
+
dispatch_pushMessage = new ThunderDispatcher('__onMessageReceived');
|
|
29
|
+
v1;
|
|
30
|
+
pushSessionId;
|
|
31
|
+
timeout = 800;
|
|
32
|
+
constructor() {
|
|
33
|
+
super();
|
|
34
|
+
window.name = window.name || generateHex(32);
|
|
35
|
+
this.pushSessionId = pushSessionId.set(window.name);
|
|
36
|
+
const register = apiWithBody(ApiDef_PushMessages.v1.register);
|
|
37
|
+
this.v1 = {
|
|
38
|
+
test: apiWithBody(ApiDef_PushMessages.v1.test),
|
|
39
|
+
register: (subscription) => {
|
|
40
|
+
this.subscribeImpl(subscription);
|
|
41
|
+
return register(this.composeRegisterRequest());
|
|
42
|
+
},
|
|
43
|
+
unregister: (subscription) => {
|
|
44
|
+
removeFromArray(this.subscriptions, d => d.topic === subscription.topic && compare(subscription.filter, d.filter));
|
|
45
|
+
return register(this.composeRegisterRequest());
|
|
46
|
+
},
|
|
47
|
+
registerAll: (subscriptions) => {
|
|
48
|
+
subscriptions.forEach(subscription => this.subscribeImpl(subscription));
|
|
49
|
+
return register(this.composeRegisterRequest());
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
composeRegisterRequest() {
|
|
54
|
+
if (!this.firebaseToken)
|
|
55
|
+
throw new ThisShouldNotHappenException('Firebase token not found');
|
|
56
|
+
const body = {
|
|
57
|
+
firebaseToken: this.firebaseToken,
|
|
58
|
+
pushSessionId: this.getPushSessionId(),
|
|
59
|
+
subscriptions: this.subscriptions.map(({ topic, filter }) => ({ topic, filter }))
|
|
60
|
+
};
|
|
61
|
+
return body;
|
|
62
|
+
}
|
|
63
|
+
init() {
|
|
64
|
+
if (this.config?.registerOnInit === false)
|
|
65
|
+
return;
|
|
66
|
+
if (!this.config?.publicKeyBase64)
|
|
67
|
+
throw new ImplementationMissingException(`ModuleFE_PushPubSub config is missing the publicKeyBase64`);
|
|
68
|
+
// this.initApp();
|
|
69
|
+
}
|
|
70
|
+
getPushSessionId() {
|
|
71
|
+
return this.pushSessionId;
|
|
72
|
+
}
|
|
73
|
+
// @ts-ignore
|
|
74
|
+
registerServiceWorker = async () => {
|
|
75
|
+
if (!('serviceWorker' in navigator)) {
|
|
76
|
+
this.logWarning('serviceWorker property is missing in navigator');
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
// const registration = await navigator.serviceWorker.register(`/${this.config.swFileName || 'pubsub-sw.js'}`);
|
|
80
|
+
// await registration.update();
|
|
81
|
+
// navigator.serviceWorker.oncontrollerchange = () => {
|
|
82
|
+
// this.logDebug('This page is now controlled by:', this.getControllingServiceWorker());
|
|
83
|
+
// };
|
|
84
|
+
//
|
|
85
|
+
// navigator.serviceWorker.onmessage = (event: MessageEvent) => {
|
|
86
|
+
// this.processMessageFromSw(event.data);
|
|
87
|
+
// };
|
|
88
|
+
// return registration;
|
|
89
|
+
};
|
|
90
|
+
// initApp = () => {
|
|
91
|
+
// this.runAsync('Initializing Firebase SDK and registering SW', async () => {
|
|
92
|
+
// const registration = await this.registerServiceWorker();
|
|
93
|
+
// const session = await ModuleFE_Firebase.createSession();
|
|
94
|
+
//
|
|
95
|
+
// this.messaging = session.getMessaging();
|
|
96
|
+
// this.messaging.onMessage((payload) => {
|
|
97
|
+
// if (!payload.data)
|
|
98
|
+
// return this.logInfo('No data passed to the message handler, I got this', payload);
|
|
99
|
+
//
|
|
100
|
+
// this.processMessage(payload.data as PushMessage_PayloadWrapper);
|
|
101
|
+
// });
|
|
102
|
+
//
|
|
103
|
+
// this.logDebug('Getting new Token');
|
|
104
|
+
// await this.getToken({vapidKey: this.config?.publicKeyBase64, serviceWorkerRegistration: registration});
|
|
105
|
+
// this.logDebug('GOT new Token');
|
|
106
|
+
//
|
|
107
|
+
// if (this.getControllingServiceWorker()) {
|
|
108
|
+
// this.logDebug(`This page is currently controlled by: `, this.getControllingServiceWorker());
|
|
109
|
+
// }
|
|
110
|
+
// });
|
|
111
|
+
// };
|
|
112
|
+
// @ts-ignore
|
|
113
|
+
getControllingServiceWorker() {
|
|
114
|
+
return navigator.serviceWorker.controller;
|
|
115
|
+
}
|
|
116
|
+
deleteToken() {
|
|
117
|
+
return this.messaging.deleteToken();
|
|
118
|
+
}
|
|
119
|
+
isNotificationEnabled() {
|
|
120
|
+
return Notification.permission === 'granted';
|
|
121
|
+
}
|
|
122
|
+
requestPermissions = async () => {
|
|
123
|
+
if (this.isNotificationEnabled())
|
|
124
|
+
return this.logVerbose('Notification already allowed');
|
|
125
|
+
const permission = await Notification.requestPermission();
|
|
126
|
+
if (permission !== 'granted')
|
|
127
|
+
return this.logWarning('Notification was NOT granted');
|
|
128
|
+
return this.logVerbose('Notification WAS granted');
|
|
129
|
+
};
|
|
130
|
+
getToken = async (options) => {
|
|
131
|
+
if (!this.isNotificationEnabled())
|
|
132
|
+
return;
|
|
133
|
+
this.firebaseToken = await this.messaging.getToken(options);
|
|
134
|
+
this.logVerbose('new token received: ' + this.firebaseToken);
|
|
135
|
+
};
|
|
136
|
+
hasToken = () => !!this.firebaseToken;
|
|
137
|
+
// @ts-ignore
|
|
138
|
+
processMessageFromSw = (data) => {
|
|
139
|
+
this.logDebug('Got message from service worker: ', data);
|
|
140
|
+
if (!data.command || !data.message || data.command !== Command_SwToApp)
|
|
141
|
+
return;
|
|
142
|
+
this.processMessage(data.message);
|
|
143
|
+
};
|
|
144
|
+
processMessage = (data) => {
|
|
145
|
+
if (data.sessionId !== this.pushSessionId)
|
|
146
|
+
return;
|
|
147
|
+
this.logInfo('process message', data);
|
|
148
|
+
const payload = JSON.parse(data.payload);
|
|
149
|
+
this.dispatch_pushMessage.dispatchAll(payload);
|
|
150
|
+
};
|
|
151
|
+
subscribeImpl(subscription) {
|
|
152
|
+
if (this.subscriptions.find(d => d.topic === subscription.topic && compare(subscription.filter, d.filter)))
|
|
153
|
+
return;
|
|
154
|
+
addItemToArray(this.subscriptions, subscription);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
export const ModuleFE_PushPubSub = new ModuleFE_PushPubSub_Class();
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ModuleFE_BaseApi } from '@nu-art/thunderstorm-frontend/index';
|
|
2
|
+
import { DispatcherDef, ThunderDispatcherV3 } from '@nu-art/thunderstorm-frontend/core/db-api-gen/types';
|
|
3
|
+
import { DBProto_PushSubscription } from '@nu-art/push-pub-sub-shared/push-subscription/index';
|
|
4
|
+
export declare const dispatch_onSubscriptionChanged: ThunderDispatcherV3<DispatcherDef<DBProto_PushSubscription, "__onSubscriptionUpdated">>;
|
|
5
|
+
export declare class ModuleFE_PushSubscription_Class extends ModuleFE_BaseApi<DBProto_PushSubscription> {
|
|
6
|
+
constructor();
|
|
7
|
+
}
|
|
8
|
+
export declare const ModuleFE_PushSubscription: ModuleFE_PushSubscription_Class;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ModuleFE_BaseApi } from '@nu-art/thunderstorm-frontend/index';
|
|
2
|
+
import { ThunderDispatcherV3 } from '@nu-art/thunderstorm-frontend/core/db-api-gen/types';
|
|
3
|
+
import { DBDef_PushSubscription } from '@nu-art/push-pub-sub-shared/push-subscription/index';
|
|
4
|
+
export const dispatch_onSubscriptionChanged = new ThunderDispatcherV3('__onSubscriptionUpdated');
|
|
5
|
+
export class ModuleFE_PushSubscription_Class extends ModuleFE_BaseApi {
|
|
6
|
+
constructor() {
|
|
7
|
+
super(DBDef_PushSubscription, dispatch_onSubscriptionChanged);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export const ModuleFE_PushSubscription = new ModuleFE_PushSubscription_Class();
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nu-art/push-pub-sub-frontend",
|
|
3
|
+
"version": "0.400.7",
|
|
4
|
+
"description": "push-pub-sub Frontend",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"TacB0sS",
|
|
7
|
+
"express",
|
|
8
|
+
"infra",
|
|
9
|
+
"nu-art",
|
|
10
|
+
"push-pub-sub",
|
|
11
|
+
"thunderstorm",
|
|
12
|
+
"typescript"
|
|
13
|
+
],
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+ssh://git@github.com:nu-art-js/push-pub-sub.git"
|
|
17
|
+
},
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"directory": "dist",
|
|
20
|
+
"linkDirectory": true
|
|
21
|
+
},
|
|
22
|
+
"license": "Apache-2.0",
|
|
23
|
+
"author": "TacB0sS",
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsc -b -f"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@nu-art/push-pub-sub-shared": "0.400.7",
|
|
29
|
+
"@nu-art/firebase-frontend": "0.400.7",
|
|
30
|
+
"@nu-art/firebase-shared": "0.400.7",
|
|
31
|
+
"@nu-art/permissions-frontend": "0.400.7",
|
|
32
|
+
"@nu-art/permissions-shared": "0.400.7",
|
|
33
|
+
"@nu-art/thunderstorm-frontend": "0.400.7",
|
|
34
|
+
"@nu-art/thunderstorm-shared": "0.400.7",
|
|
35
|
+
"@nu-art/ts-common": "0.400.7",
|
|
36
|
+
"@nu-art/ts-styles": "0.400.7",
|
|
37
|
+
"@nu-art/user-account-frontend": "0.400.7",
|
|
38
|
+
"@nu-art/user-account-shared": "0.400.7",
|
|
39
|
+
"firebase": "^11.9.0",
|
|
40
|
+
"firebase-admin": "13.4.0",
|
|
41
|
+
"moment": "^2.29.4",
|
|
42
|
+
"react": "^18.0.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@types/react": "^18.0.0"
|
|
46
|
+
},
|
|
47
|
+
"unitConfig": {
|
|
48
|
+
"type": "typescript-lib"
|
|
49
|
+
},
|
|
50
|
+
"type": "module",
|
|
51
|
+
"exports": {
|
|
52
|
+
".": {
|
|
53
|
+
"types": "./index.d.ts",
|
|
54
|
+
"import": "./index.js"
|
|
55
|
+
},
|
|
56
|
+
"./*": {
|
|
57
|
+
"types": "./*.d.ts",
|
|
58
|
+
"import": "./*.js"
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { AppToolsScreen, ComponentSync } from '@nu-art/thunderstorm-frontend/index';
|
|
3
|
+
import { OnPushMessageReceived, PushMessage_Payload } from '@nu-art/push-pub-sub-shared';
|
|
4
|
+
import './ATS_PushPubSub.scss';
|
|
5
|
+
import { ApiCallerEventType } from '@nu-art/thunderstorm-frontend/core/db-api-gen/types';
|
|
6
|
+
import { DBProto_PushSubscription } from '@nu-art/push-pub-sub-shared/push-subscription/index';
|
|
7
|
+
type ObjProps = {
|
|
8
|
+
key: string;
|
|
9
|
+
value: string;
|
|
10
|
+
};
|
|
11
|
+
type State = {
|
|
12
|
+
registerKey: string;
|
|
13
|
+
registerFilter: ObjProps[];
|
|
14
|
+
triggerKey: string;
|
|
15
|
+
triggerFilter: ObjProps[];
|
|
16
|
+
receivedPushPayloads: PushMessage_Payload[];
|
|
17
|
+
};
|
|
18
|
+
type Props = {};
|
|
19
|
+
export declare class ATS_PushPubSub extends ComponentSync<Props, State> implements OnPushMessageReceived {
|
|
20
|
+
static screen: AppToolsScreen;
|
|
21
|
+
__onSubscriptionUpdated(...params: ApiCallerEventType<DBProto_PushSubscription>): void;
|
|
22
|
+
__onMessageReceived(payload: PushMessage_Payload): void;
|
|
23
|
+
protected deriveStateFromProps(nextProps: Props, _state: (Partial<State>) | undefined): State;
|
|
24
|
+
private composeFilter;
|
|
25
|
+
private trigger;
|
|
26
|
+
private subscribe;
|
|
27
|
+
render(): import("react/jsx-runtime").JSX.Element;
|
|
28
|
+
private renderPanel;
|
|
29
|
+
title(title: string): React.ReactNode;
|
|
30
|
+
}
|
|
31
|
+
export {};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { _className, ATS_Fullstack, Button, ComponentSync, LL_H_C, LL_H_T, LL_V_L, ModuleFE_Toaster, TS_Input, TS_PropRenderer, TS_Table } from '@nu-art/thunderstorm-frontend/index';
|
|
3
|
+
import { __stringify, DateTimeFormat_yyyyMMDDTHHmmss, groupArrayBy, removeFromArrayByIndex, } from '@nu-art/ts-common';
|
|
4
|
+
import { TS_Icons } from '@nu-art/ts-styles';
|
|
5
|
+
import { ModuleFE_PushPubSub } from '../../modules/ModuleFE_PushPubSub.js';
|
|
6
|
+
import './ATS_PushPubSub.scss';
|
|
7
|
+
import { TS_InputV2 } from '@nu-art/thunderstorm-frontend/components/TS_V2_Input/index';
|
|
8
|
+
import { ModuleFE_PushSubscription } from '../../modules/ModuleFE_PushSubscription.js';
|
|
9
|
+
function createNewInstance(key = '', value = '') {
|
|
10
|
+
return { key, value };
|
|
11
|
+
}
|
|
12
|
+
const ConfigPreset_1 = {
|
|
13
|
+
label: 'Matching simple',
|
|
14
|
+
config: {
|
|
15
|
+
registerKey: 'test-key',
|
|
16
|
+
registerFilter: [createNewInstance('prop-key', 'value1')],
|
|
17
|
+
triggerKey: 'test-key',
|
|
18
|
+
triggerFilter: [createNewInstance('prop-key', 'value1')],
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
export class ATS_PushPubSub extends ComponentSync {
|
|
22
|
+
// ######################## Static ########################
|
|
23
|
+
static screen = {
|
|
24
|
+
name: `Push Messages`,
|
|
25
|
+
renderer: this,
|
|
26
|
+
group: ATS_Fullstack,
|
|
27
|
+
modulesToAwait: [ModuleFE_PushSubscription]
|
|
28
|
+
};
|
|
29
|
+
// ######################## Lifecycle ########################
|
|
30
|
+
__onSubscriptionUpdated(...params) {
|
|
31
|
+
this.forceUpdate();
|
|
32
|
+
}
|
|
33
|
+
__onMessageReceived(payload) {
|
|
34
|
+
console.log('GOT PUSH:', payload.message.topic, payload.message.props, payload.message.data);
|
|
35
|
+
this.setState({ receivedPushPayloads: [...this.state.receivedPushPayloads, payload] });
|
|
36
|
+
}
|
|
37
|
+
deriveStateFromProps(nextProps, _state) {
|
|
38
|
+
const state = _state ?? {};
|
|
39
|
+
return { ...state, ...ConfigPreset_1.config, receivedPushPayloads: [] };
|
|
40
|
+
}
|
|
41
|
+
// ######################## Logic ########################
|
|
42
|
+
composeFilter(objProps) {
|
|
43
|
+
return objProps.reduce((toRet, item) => {
|
|
44
|
+
if (!!item.key && item.key !== '')
|
|
45
|
+
toRet[item.key] = item.value;
|
|
46
|
+
return toRet;
|
|
47
|
+
}, {});
|
|
48
|
+
}
|
|
49
|
+
trigger = async () => {
|
|
50
|
+
if (!ModuleFE_PushPubSub.hasToken())
|
|
51
|
+
return ModuleFE_Toaster.toastError('No push token generated');
|
|
52
|
+
const filter = this.composeFilter(this.state.triggerFilter);
|
|
53
|
+
const data = { 'a': 'aaa' };
|
|
54
|
+
const message = { topic: this.state.triggerKey, filter, data };
|
|
55
|
+
this.logInfo(`triggering push: ${__stringify(message, true)}`);
|
|
56
|
+
await ModuleFE_PushPubSub.v1.test({ message }).executeSync();
|
|
57
|
+
};
|
|
58
|
+
subscribe = async () => {
|
|
59
|
+
if (!ModuleFE_PushPubSub.hasToken())
|
|
60
|
+
return ModuleFE_Toaster.toastError('No push token generated');
|
|
61
|
+
await ModuleFE_PushPubSub.v1.register({
|
|
62
|
+
topic: this.state.registerKey,
|
|
63
|
+
filter: this.composeFilter(this.state.registerFilter)
|
|
64
|
+
}).executeSync();
|
|
65
|
+
};
|
|
66
|
+
// ######################## Render ########################
|
|
67
|
+
render() {
|
|
68
|
+
const className = _className('notification-icon', ModuleFE_PushPubSub.isNotificationEnabled() ? 'notification-enabled' : 'notification-disabled');
|
|
69
|
+
const mySubscription = ModuleFE_PushSubscription.cache.all()
|
|
70
|
+
.filter(subscription => subscription.pushSessionId === ModuleFE_PushPubSub.getPushSessionId());
|
|
71
|
+
return _jsxs(LL_V_L, { className: "ats-PushPubSub", children: [_jsxs(LL_H_C, { children: ["Push Session Id: ", ModuleFE_PushPubSub.getPushSessionId()] }), _jsxs(LL_H_C, { className: "header match_width", children: [_jsx("div", { children: TS_Icons.bell.component({
|
|
72
|
+
className: className,
|
|
73
|
+
onClick: async (e) => {
|
|
74
|
+
await ModuleFE_PushPubSub.requestPermissions();
|
|
75
|
+
}
|
|
76
|
+
}) }), _jsx(Button, { onClick: ModuleFE_PushPubSub.deleteToken, children: "Delete Token" }), _jsx(Button, { onClick: async () => await ModuleFE_PushPubSub.getToken(), children: "Generate Token" })] }), _jsxs(LL_H_T, { className: "panels-container h-gap__n match_width", children: [this.renderPanel('Register', this.state.registerFilter, 'registerKey', this.subscribe), this.renderPanel('Trigger', this.state.triggerFilter, 'triggerKey', this.trigger)] }), _jsxs(LL_H_T, { className: "panels-container h-gap__n match_width", children: [_jsx(LL_V_L, { className: "panel v-gap__l", children: groupArrayBy(mySubscription, (subscription) => subscription.topic)
|
|
77
|
+
.map((subscription, index) => _jsxs(LL_H_C, { children: [_jsx(LL_H_C, { style: { width: 100 }, children: subscription.key }), _jsx(LL_V_L, { children: subscription.values.map((value, index) => _jsxs(LL_H_C, { children: [_jsx(TS_Icons.bin.component, { onClick: () => ModuleFE_PushSubscription.v1.delete({ _id: value._id }).executeSync() }), JSON.stringify(value.filter)] }, index)) })] }, index)) }), _jsx(LL_V_L, { className: "panel", children: this.state.receivedPushPayloads.map(payload => _jsxs(LL_H_C, { children: [_jsx("span", { style: { width: 200 }, children: DateTimeFormat_yyyyMMDDTHHmmss.format(payload.timestamp) }), JSON.stringify(payload.message)] })) })] })] });
|
|
78
|
+
}
|
|
79
|
+
renderPanel(title, rows, key, action) {
|
|
80
|
+
const cellRenderer = (prop, item, index) => {
|
|
81
|
+
if (prop === 'delete')
|
|
82
|
+
return _jsx(TS_Icons.bin.component, { onClick: () => {
|
|
83
|
+
removeFromArrayByIndex(rows, index);
|
|
84
|
+
this.forceUpdate();
|
|
85
|
+
}, children: "Delete Row" });
|
|
86
|
+
return _jsx(TS_Input, { onChange: (value) => {
|
|
87
|
+
item[prop] = value;
|
|
88
|
+
this.forceUpdate();
|
|
89
|
+
}, className: 'match_width', type: "text", placeholder: `Enter ${prop}`, id: `${key}-${prop}-${index}`, value: item[prop].toString() });
|
|
90
|
+
};
|
|
91
|
+
const addNewRow = () => {
|
|
92
|
+
rows[rows.length] = createNewInstance();
|
|
93
|
+
this.forceUpdate();
|
|
94
|
+
};
|
|
95
|
+
return _jsxs(LL_V_L, { className: "panel", children: [_jsxs(LL_H_C, { className: "panel-header match_width flex__justify-center", children: [title, _jsx(Button, { variant: 'secondary', onClick: addNewRow, children: "Add" }), _jsx(Button, { variant: 'primary', onClick: action, children: "Send" })] }), _jsxs(LL_V_L, { className: "panel-content v-gap__n", children: [_jsx(TS_PropRenderer.Vertical, { label: 'Key', children: _jsx(TS_InputV2, { onChange: (value) => this.setState({ [key]: value }), type: "text", id: key, value: this.state[key] }) }), _jsx(TS_Table, { id: key, table: { className: 'match_width' }, header: ['key', 'value', { header: 'delete', widthPx: 30 }], rows: rows, headerRenderer: columnKey => _jsx("div", { children: columnKey === 'delete' ? '' : columnKey }), cellRenderer: cellRenderer, tr: { style: { padding: '5px' } } })] })] });
|
|
96
|
+
}
|
|
97
|
+
title(title) {
|
|
98
|
+
return _jsx("div", { children: title });
|
|
99
|
+
}
|
|
100
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
@use '@nu-art/ts-styles' as styles;
|
|
2
|
+
|
|
3
|
+
.ats-PushPubSub {
|
|
4
|
+
.header {
|
|
5
|
+
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
.panels-container {
|
|
9
|
+
flex-grow: 1;
|
|
10
|
+
|
|
11
|
+
.panel {
|
|
12
|
+
width: 50%;
|
|
13
|
+
margin-inline: 4px;
|
|
14
|
+
border: 2px solid black;
|
|
15
|
+
border-radius: 6px;
|
|
16
|
+
|
|
17
|
+
.panel-header {
|
|
18
|
+
height: 40px;
|
|
19
|
+
background-color: #8A959B;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
.panel-content {
|
|
23
|
+
padding: 8px;
|
|
24
|
+
|
|
25
|
+
.ts-table {
|
|
26
|
+
width: 100%
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
.notification-icon {
|
|
33
|
+
width: 40px;
|
|
34
|
+
height: 40px;
|
|
35
|
+
@include styles.color-svg(white);
|
|
36
|
+
|
|
37
|
+
&.notification-enabled {
|
|
38
|
+
@include styles.color-svg(green);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
&.notification-disabled {
|
|
42
|
+
@include styles.color-svg(red);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './ATS_PushPubSub.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './ATS_PushPubSub.js';
|