@controleonline/ui-common 1.2.140 → 1.2.141

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.
@@ -1,15 +1,14 @@
1
1
  {
2
- "version": "1.2.140",
2
+ "version": "1.2.141",
3
3
  "rc": 1,
4
- "branch": "rc/1.2.140-rc.1",
5
- "baseMaster": "208f0d67142877d715cc1f2d7640cfafc8326451",
6
- "baseTag": "v1.2.137",
4
+ "branch": "rc/1.2.141-rc.1",
5
+ "baseMaster": "37059521852cd73265e08e87f335a1473c6b3531",
6
+ "baseTag": "v1.2.140",
7
7
  "tasks": [
8
- 171,
9
- 180
8
+ 1766
10
9
  ],
11
10
  "frozen": true,
12
11
  "repositories": {
13
- "ControleOnline/ui-common": "65e6c3f3245bc019e1ee8c97a1819bb3f6a311c8"
12
+ "ControleOnline/ui-common": "ae28b00d8f8b2737c7aa676dc8cce6efb34e1407"
14
13
  }
15
14
  }
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@controleonline/ui-common",
3
- "version": "1.2.140",
3
+ "version": "1.2.141",
4
4
  "author": "ControleOnline",
5
5
  "description": "ControleOnline Common Quasar UI Component",
6
6
  "license": "MIT",
7
7
  "main": "src/react/index.js",
8
8
  "scripts": {
9
- "test": "npm run test:receita-federal && npm run test:imports-status && npm run test:runtime-language && npm run test:runtime-footer && npm run test:runtime-footer-marquee && npm run test:runtime-footer-platform && npm run test:invoice-presentation && npm run test:system-error-message && npm run test:bottom-navigation-toolbar && npm run test:copy-device-configs && npm run test:print-routing && npm run test:device-detail-dependencies && npm run test:runtime-zoom && npm run test:cash-register-summary && npm run test:linked-order-payload && npm run test:document-file-content && npm run test:order-products-summary && npm run test:client-portfolio",
9
+ "test": "npm run test:receita-federal && npm run test:imports-status && npm run test:runtime-language && npm run test:runtime-footer && npm run test:runtime-footer-marquee && npm run test:runtime-footer-platform && npm run test:invoice-presentation && npm run test:system-error-message && npm run test:bottom-navigation-toolbar && npm run test:copy-device-configs && npm run test:print-routing && npm run test:device-detail-dependencies && npm run test:runtime-zoom && npm run test:cash-register-summary && npm run test:linked-order-payload && npm run test:document-file-content && npm run test:order-products-summary && npm run test:client-portfolio && npm run test:conversation-alert",
10
+ "test:conversation-alert": "node --test src/tests/react/utils/conversationAlert.test.js",
10
11
  "test:receita-federal": "node --test src/tests/react/pages/receitaFederalIntegrationContract.test.mjs",
11
12
  "test:imports-status": "mkdir -p node_modules/@controleonline && rm -rf node_modules/@controleonline/ui-common && ln -s ../.. node_modules/@controleonline/ui-common && node --test src/tests/react/utils/importStatus.test.js",
12
13
  "test:cash-register-summary": "node --test src/tests/react/utils/cashRegisterSummary.test.js",
@@ -0,0 +1,3 @@
1
+ const ConversationAlertService = () => null;
2
+
3
+ export default ConversationAlertService;
@@ -0,0 +1,87 @@
1
+ /*
2
+ * Alerta de novas mensagens dos chamados no navegador (app-community#1766).
3
+ * - Fonte: evento `conversation_message` (status `received`) do websocket, o mesmo que atualiza as telas.
4
+ * - Só no MANAGER e no CRM, para a empresa atual, quando a aba não está em foco.
5
+ * - Notificação do navegador; a permissão é pedida no primeiro clique do usuário (o navegador exige um gesto).
6
+ * - Com o navegador fechado, o aviso vem do push (Firebase), não daqui.
7
+ */
8
+ import {useEffect, useRef} from 'react';
9
+ import {useStores} from '@store';
10
+ import {app_type} from '@appType';
11
+ import {
12
+ conversationAlertContent,
13
+ conversationAlertKey,
14
+ idOf,
15
+ isConversationAlert,
16
+ } from '@controleonline/ui-common/src/react/utils/conversationAlert';
17
+
18
+ const ALERT_APPS = ['MANAGER', 'CRM'];
19
+ const MAX_SEEN = 300;
20
+
21
+ const normalizeText = value => String(value ?? '').trim();
22
+
23
+ const webNotification = () =>
24
+ typeof globalThis !== 'undefined' ? globalThis.Notification : undefined;
25
+
26
+ const isTabFocused = () => {
27
+ if (typeof document === 'undefined') return false;
28
+ if (document.visibilityState === 'hidden') return false;
29
+ return typeof document.hasFocus === 'function' ? document.hasFocus() : true;
30
+ };
31
+
32
+ const ConversationAlertService = () => {
33
+ const websocketMessages = useStores(state => state?.websocket?.getters?.messages);
34
+ const currentCompany = useStores(state => state?.people?.getters?.currentCompany);
35
+ const seenRef = useRef(new Set());
36
+ const enabled = ALERT_APPS.includes(normalizeText(app_type).toUpperCase());
37
+ const companyId = idOf(currentCompany?.id);
38
+
39
+ useEffect(() => {
40
+ const Notification = webNotification();
41
+ if (!enabled || !Notification || Notification.permission !== 'default' || typeof document === 'undefined') {
42
+ return undefined;
43
+ }
44
+ const ask = () => {
45
+ document.removeEventListener('click', ask, true);
46
+ try {
47
+ Notification.requestPermission();
48
+ } catch {
49
+ // Navegadores antigos: segue sem alerta.
50
+ }
51
+ };
52
+ document.addEventListener('click', ask, true);
53
+ return () => document.removeEventListener('click', ask, true);
54
+ }, [enabled]);
55
+
56
+ useEffect(() => {
57
+ if (!enabled || !Array.isArray(websocketMessages)) return;
58
+ const Notification = webNotification();
59
+ const seen = seenRef.current;
60
+ websocketMessages.forEach(message => {
61
+ if (!isConversationAlert(message, companyId)) return;
62
+ const key = conversationAlertKey(message);
63
+ if (seen.has(key)) return;
64
+ seen.add(key);
65
+ if (seen.size > MAX_SEEN) seen.delete(seen.values().next().value);
66
+ if (!Notification || Notification.permission !== 'granted' || isTabFocused()) return;
67
+ try {
68
+ const {title, body, tag} = conversationAlertContent(message);
69
+ const notification = new Notification(title, {body, tag, renotify: true});
70
+ notification.onclick = () => {
71
+ try {
72
+ globalThis.focus?.();
73
+ notification.close();
74
+ } catch {
75
+ // ignora
76
+ }
77
+ };
78
+ } catch {
79
+ // Sem permissão ou bloqueado pelo navegador.
80
+ }
81
+ });
82
+ }, [companyId, enabled, websocketMessages]);
83
+
84
+ return null;
85
+ };
86
+
87
+ export default ConversationAlertService;
@@ -14,6 +14,7 @@ import { View } from 'react-native';
14
14
  import Translate from '@controleonline/ui-common/src/utils/translate';
15
15
  import { WebsocketListener } from '@controleonline/ui-common/src/react/components/WebsocketListener';
16
16
  import DeviceAlertSoundService from '@controleonline/ui-common/src/react/components/DeviceAlertSoundService';
17
+ import ConversationAlertService from '@controleonline/ui-common/src/react/components/ConversationAlertService';
17
18
  import PrintService from '@controleonline/ui-common/src/react/components/PrintService';
18
19
  import RemoteCheckoutService from '@controleonline/ui-common/src/react/components/RemoteCheckoutService';
19
20
  import ProductCatalogCacheService from '@controleonline/ui-common/src/react/components/ProductCatalogCacheService';
@@ -425,6 +426,7 @@ export const DefaultProvider = ({
425
426
  <>
426
427
  <WebsocketListener />
427
428
  <DeviceAlertSoundService />
429
+ <ConversationAlertService />
428
430
  <ProductCatalogCacheService />
429
431
  <RemoteCheckoutService />
430
432
  <PrintService />
@@ -0,0 +1,28 @@
1
+ // Pure helpers of the browser alert for new request messages (app-community#1766).
2
+ const CHANNELS = {whatsapp: 'WhatsApp', email: 'E-mail'};
3
+
4
+ const normalizeText = value => String(value ?? '').trim();
5
+ const idOf = value => normalizeText(value && typeof value === 'object' ? value.id : value).replace(/\D/g, '');
6
+
7
+ // Only customer messages ("received") of the current company raise an alert.
8
+ const isConversationAlert = (message, companyId) => {
9
+ if (normalizeText(message?.event) !== 'conversation_message') return false;
10
+ if (normalizeText(message?.status).toLowerCase() !== 'received') return false;
11
+ const messageCompany = idOf(message?.companyId ?? message?.company);
12
+ return !companyId || !messageCompany || messageCompany === String(companyId);
13
+ };
14
+
15
+ const conversationAlertKey = message =>
16
+ `${idOf(message?.task_id)}:${normalizeText(message?.interaction_id)}:${normalizeText(message?.sentAt)}`;
17
+
18
+ const conversationAlertContent = message => {
19
+ const taskId = idOf(message?.task_id);
20
+ const channel = CHANNELS[normalizeText(message?.channel).toLowerCase()];
21
+ return {
22
+ title: 'Nova mensagem',
23
+ body: [taskId ? `Chamado #${taskId}` : 'Chamado', channel].filter(Boolean).join(' · '),
24
+ tag: `conversation-${taskId || 'new'}`,
25
+ };
26
+ };
27
+
28
+ module.exports = {conversationAlertContent, conversationAlertKey, idOf, isConversationAlert};
@@ -0,0 +1,19 @@
1
+ const test = require('node:test');
2
+ const assert = require('node:assert/strict');
3
+ const {conversationAlertContent, conversationAlertKey, isConversationAlert} = require('../../../react/utils/conversationAlert');
4
+
5
+ const message = {event: 'conversation_message', status: 'received', companyId: 7, task_id: 54, interaction_id: 258, channel: 'whatsapp', sentAt: 'x'};
6
+
7
+ test('alerts customer messages of the current company only', () => {
8
+ assert.equal(isConversationAlert(message, '7'), true);
9
+ assert.equal(isConversationAlert(message, ''), true);
10
+ assert.equal(isConversationAlert({...message, companyId: 8}, '7'), false);
11
+ assert.equal(isConversationAlert({...message, status: 'updated'}, '7'), false);
12
+ assert.equal(isConversationAlert({...message, event: 'mcp_chat_reply'}, '7'), false);
13
+ });
14
+
15
+ test('builds the alert text and a stable key', () => {
16
+ assert.deepEqual(conversationAlertContent(message), {title: 'Nova mensagem', body: 'Chamado #54 · WhatsApp', tag: 'conversation-54'});
17
+ assert.equal(conversationAlertContent({task_id: '/tasks/9'}).body, 'Chamado #9');
18
+ assert.equal(conversationAlertKey(message), '54:258:x');
19
+ });