@controleonline/ui-crm 1.0.63 → 1.0.67

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.
@@ -0,0 +1,81 @@
1
+ /*
2
+ * @agents Pure helpers for CRM / Support funnels and ticket screens.
3
+ * Covered by src/tests/react/pages/tickets/ticketUtils.test.js.
4
+ */
5
+ const {idOf, isCustomerConversation} = require('../mcp/mcpChatUtils');
6
+
7
+ const TRIAGE_COLUMN = 'triage';
8
+
9
+ const TICKET_TYPES = {
10
+ relationship: {title: 'Funil comercial', label: 'Comercial', empty: 'Nenhuma oportunidade.'},
11
+ support: {title: 'Suporte', label: 'Suporte', empty: 'Nenhum chamado.'},
12
+ };
13
+
14
+ const statusOrder = status => {
15
+ const real = String(status?.realStatus || '').toLowerCase();
16
+ const rank = {open: 0, pending: 1, closed: 2, canceled: 3};
17
+ return [rank[real] ?? 1, idOf(status) || 0];
18
+ };
19
+
20
+ /**
21
+ * Columns: "Triagem IA" (customer conversations still unclassified) + one per status of the context,
22
+ * open stages first and closed/canceled last.
23
+ */
24
+ const buildColumns = (statuses, tasks, triage = []) => {
25
+ const sorted = [...(statuses || [])].sort((a, b) => {
26
+ const [ra, ia] = statusOrder(a);
27
+ const [rb, ib] = statusOrder(b);
28
+ return ra - rb || ia - ib;
29
+ });
30
+ const columns = [
31
+ {key: TRIAGE_COLUMN, title: 'Triagem IA', color: '#7c3aed', status: null, tasks: (triage || []).filter(isCustomerConversation)},
32
+ ];
33
+ sorted.forEach(status => {
34
+ columns.push({
35
+ key: String(idOf(status)),
36
+ title: status.status || status.name || `#${idOf(status)}`,
37
+ color: status.color || '#1d9bf0',
38
+ status,
39
+ tasks: [],
40
+ });
41
+ });
42
+ const byStatus = new Map(columns.map(c => [c.key, c]));
43
+ (tasks || []).forEach(task => {
44
+ const column = byStatus.get(String(idOf(task.taskStatus)));
45
+ if (column && column.key !== TRIAGE_COLUMN) column.tasks.push(task);
46
+ });
47
+ return columns;
48
+ };
49
+
50
+ /** Neighbour status column for the arrow buttons (never into/out of triage). */
51
+ const neighbourStatus = (columns, task, step) => {
52
+ const stages = columns.filter(c => c.status);
53
+ const index = stages.findIndex(c => c.key === String(idOf(task?.taskStatus)));
54
+ if (index < 0) return null;
55
+ return stages[index + step]?.status || null;
56
+ };
57
+
58
+ const personName = people => {
59
+ if (!people) return '';
60
+ if (typeof people === 'string') return '';
61
+ return people.name || people.alias || '';
62
+ };
63
+
64
+ const timeAgo = (value, now = Date.now()) => {
65
+ const t = new Date(value || 0).getTime();
66
+ if (!t) return '';
67
+ const minutes = Math.max(0, Math.round((now - t) / 60000));
68
+ if (minutes < 60) return `${minutes} min`;
69
+ const hours = Math.round(minutes / 60);
70
+ if (hours < 48) return `${hours} h`;
71
+ return `${Math.round(hours / 24)} d`;
72
+ };
73
+
74
+ module.exports = {
75
+ TRIAGE_COLUMN,
76
+ TICKET_TYPES,
77
+ buildColumns,
78
+ neighbourStatus,
79
+ personName,
80
+ timeAgo,
81
+ };
@@ -0,0 +1,61 @@
1
+ import {StyleSheet} from 'react-native';
2
+
3
+ // Same dark palette as /ai.
4
+ export default StyleSheet.create({
5
+ root: {flex: 1, backgroundColor: '#0f1419'},
6
+ header: {flexDirection: 'row', alignItems: 'center', gap: 8, padding: 12, borderBottomWidth: 1, borderBottomColor: '#2a3441'},
7
+ headerText: {flex: 1, minWidth: 0},
8
+ title: {color: '#e7e9ea', fontSize: 18, fontWeight: '700'},
9
+ subtitle: {color: '#8b98a5', fontSize: 12, marginTop: 2},
10
+ search: {minWidth: 180, maxWidth: 280, flex: 1, borderWidth: 1, borderColor: '#38444d', borderRadius: 10, paddingHorizontal: 10, paddingVertical: 6, color: '#e7e9ea'},
11
+ board: {flex: 1},
12
+ boardContent: {padding: 12, gap: 12},
13
+ column: {width: 290, backgroundColor: '#15202b', borderRadius: 12, borderTopWidth: 3, maxHeight: '100%'},
14
+ columnHeader: {flexDirection: 'row', justifyContent: 'space-between', padding: 10},
15
+ columnTitle: {color: '#e7e9ea', fontWeight: '700', fontSize: 13, textTransform: 'capitalize'},
16
+ columnCount: {color: '#8b98a5', fontSize: 12},
17
+ columnBody: {paddingHorizontal: 8, paddingBottom: 8},
18
+ card: {backgroundColor: '#1e2732', borderRadius: 10, padding: 10, marginBottom: 8},
19
+ cardTitle: {color: '#e7e9ea', fontSize: 13, fontWeight: '600'},
20
+ cardMeta: {color: '#8b98a5', fontSize: 11, marginTop: 4},
21
+ badges: {flexDirection: 'row', flexWrap: 'wrap', gap: 4, marginTop: 6},
22
+ badge: {borderRadius: 6, paddingHorizontal: 6, paddingVertical: 2, backgroundColor: '#2a3441'},
23
+ badgeAi: {backgroundColor: '#4c1d95'},
24
+ badgeText: {color: '#e7e9ea', fontSize: 10, fontWeight: '600'},
25
+ cardActions: {flexDirection: 'row', gap: 6, marginTop: 8},
26
+ iconBtn: {borderWidth: 1, borderColor: '#38444d', borderRadius: 8, paddingHorizontal: 8, paddingVertical: 4},
27
+ iconBtnText: {color: '#e7e9ea', fontSize: 12},
28
+ emptyText: {color: '#8b98a5', fontSize: 12, padding: 8},
29
+ spinner: {marginTop: 24},
30
+ // ticket screen
31
+ messages: {padding: 12, paddingBottom: 24},
32
+ bubble: {maxWidth: '80%', borderRadius: 12, paddingHorizontal: 12, paddingVertical: 8, marginVertical: 4},
33
+ bubbleLeft: {alignSelf: 'flex-start', backgroundColor: '#1f3a2b'},
34
+ bubbleAi: {alignSelf: 'flex-start', backgroundColor: '#212a33'},
35
+ bubbleRight: {alignSelf: 'flex-end', backgroundColor: '#1d9bf0'},
36
+ bubbleNote: {alignSelf: 'center', backgroundColor: '#3a2f12', borderWidth: 1, borderColor: '#8a6d1d'},
37
+ bubbleAuthor: {color: '#c9d1d9', fontSize: 11, fontWeight: '700', marginBottom: 2},
38
+ bubbleText: {color: '#e7e9ea', fontSize: 14, lineHeight: 20},
39
+ bubbleMeta: {color: '#c9d1d9', fontSize: 10, marginTop: 4, opacity: 0.8},
40
+ composer: {padding: 10, borderTopWidth: 1, borderTopColor: '#2a3441', gap: 8},
41
+ modeRow: {flexDirection: 'row', gap: 6},
42
+ modeChip: {borderWidth: 1, borderColor: '#38444d', borderRadius: 14, paddingHorizontal: 10, paddingVertical: 4},
43
+ modeChipActive: {backgroundColor: '#1d9bf0', borderColor: '#1d9bf0'},
44
+ modeChipNote: {backgroundColor: '#8a6d1d', borderColor: '#8a6d1d'},
45
+ modeChipText: {color: '#e7e9ea', fontSize: 12},
46
+ composerRow: {flexDirection: 'row', alignItems: 'flex-end', gap: 8},
47
+ input: {flex: 1, minHeight: 40, maxHeight: 140, borderWidth: 1, borderColor: '#38444d', borderRadius: 12, paddingHorizontal: 12, paddingVertical: 8, color: '#e7e9ea', backgroundColor: '#15202b'},
48
+ primaryBtn: {backgroundColor: '#1d9bf0', borderRadius: 10, paddingHorizontal: 14, paddingVertical: 10, alignItems: 'center', justifyContent: 'center'},
49
+ primaryBtnText: {color: '#fff', fontWeight: '700'},
50
+ ghostBtn: {borderWidth: 1, borderColor: '#1d9bf0', borderRadius: 10, paddingHorizontal: 12, paddingVertical: 8},
51
+ ghostBtnText: {color: '#1d9bf0', fontWeight: '600', fontSize: 12},
52
+ aiBar: {flexDirection: 'row', alignItems: 'center', gap: 8, paddingHorizontal: 12, paddingVertical: 8, backgroundColor: '#1b1530', borderBottomWidth: 1, borderBottomColor: '#2a3441'},
53
+ aiBarText: {flex: 1, color: '#d8ccff', fontSize: 12},
54
+ // modal
55
+ modalBackdrop: {flex: 1, backgroundColor: 'rgba(0,0,0,0.6)', justifyContent: 'center', alignItems: 'center', padding: 16},
56
+ modalCard: {width: '100%', maxWidth: 480, backgroundColor: '#15202b', borderRadius: 14, padding: 16, gap: 10},
57
+ modalTitle: {color: '#e7e9ea', fontSize: 16, fontWeight: '700'},
58
+ modalText: {color: '#8b98a5', fontSize: 13, lineHeight: 18},
59
+ modalInput: {minHeight: 90, flex: 0},
60
+ modalActions: {flexDirection: 'row', justifyContent: 'flex-end', gap: 8},
61
+ });
@@ -0,0 +1,28 @@
1
+ /*
2
+ * @agents Calls `reload(events)` (debounced) whenever new conversation events arrive on the
3
+ * websocket store. Used by funnels and ticket screens; /ai has its own finer-grained handling.
4
+ */
5
+ import {useEffect, useRef} from 'react';
6
+ import {useStore} from '@store';
7
+
8
+ const {extractChatEvents} = require('../mcp/mcpChatUtils');
9
+
10
+ export default function useRealtimeReload(reload, {filter = () => true, delay = 500} = {}) {
11
+ const websocketStore = useStore('websocket');
12
+ const messages = websocketStore?.getters?.messages;
13
+ const cursor = useRef(Array.isArray(messages) ? messages.length : 0);
14
+ const timer = useRef(null);
15
+ const reloadRef = useRef(reload);
16
+ reloadRef.current = reload;
17
+
18
+ useEffect(() => {
19
+ const {events, nextIndex} = extractChatEvents(messages, cursor.current);
20
+ cursor.current = nextIndex;
21
+ const relevant = events.filter(filter);
22
+ if (!relevant.length) return;
23
+ clearTimeout(timer.current);
24
+ timer.current = setTimeout(() => reloadRef.current(relevant), delay);
25
+ }, [messages, filter, delay]);
26
+
27
+ useEffect(() => () => clearTimeout(timer.current), []);
28
+ }
@@ -8,6 +8,8 @@ import CrmConversation from '../pages/crm/conversation';
8
8
  import GeneralSettings from '../pages/settings/GeneralSettings';
9
9
  import McpPage from '../pages/mcp/McpPage';
10
10
  import ProposalDetails from '../pages/proposals/ProposalDetails';
11
+ import {CrmFunnelPage, SupportFunnelPage} from '../pages/tickets/TicketFunnelPage';
12
+ import TicketConversationPage from '../pages/tickets/TicketConversationPage';
11
13
 
12
14
  const crmRoutes = [
13
15
  {
@@ -144,6 +146,48 @@ const crmRoutes = [
144
146
  title: 'Conversas',
145
147
  },
146
148
  },
149
+ {
150
+ // Kanban of CRM opportunities (+ AI triage column).
151
+ name: 'CrmFunnel',
152
+ component: CrmFunnelPage,
153
+ path: 'crm/funil',
154
+ options: {
155
+ showCompanyFilter: true,
156
+ showBottomToolBar: true,
157
+ headerShown: true,
158
+ headerBackVisible: true,
159
+ companyFilterMode: 'icon',
160
+ title: 'Funil comercial',
161
+ },
162
+ },
163
+ {
164
+ // Support tickets funnel (clone of the CRM funnel for type=support).
165
+ name: 'SupportFunnel',
166
+ component: SupportFunnelPage,
167
+ path: 'suporte',
168
+ options: {
169
+ showCompanyFilter: true,
170
+ showBottomToolBar: true,
171
+ headerShown: true,
172
+ headerBackVisible: true,
173
+ companyFilterMode: 'icon',
174
+ title: 'Suporte',
175
+ },
176
+ },
177
+ {
178
+ // One customer ticket (ia/relationship/support): conversation, notes, AI.
179
+ name: 'TicketConversation',
180
+ component: TicketConversationPage,
181
+ path: 'ticket/:taskId',
182
+ options: {
183
+ showCompanyFilter: true,
184
+ showBottomToolBar: true,
185
+ headerShown: true,
186
+ headerBackVisible: true,
187
+ companyFilterMode: 'icon',
188
+ title: 'Atendimento',
189
+ },
190
+ },
147
191
  ];
148
192
 
149
193
  export default crmRoutes;
@@ -0,0 +1,73 @@
1
+ const {test} = require('node:test');
2
+ const assert = require('node:assert/strict');
3
+
4
+ const {
5
+ idOf,
6
+ isCustomerConversation,
7
+ customerPhone,
8
+ extractChatEvents,
9
+ mergeConversations,
10
+ messageRole,
11
+ stageLabel,
12
+ conversationChannel,
13
+ } = require('../../../../react/pages/mcp/mcpChatUtils');
14
+
15
+ const webTask = {id: 1, type: 'ia', announce: '{"channel":"mcp","ai_provider":"claude"}', registeredBy: '/people/10', alterDate: '2026-11-14T10:00:00'};
16
+ const otherUserTask = {id: 2, type: 'ia', announce: '{"channel":"mcp"}', registeredBy: '/people/11', alterDate: '2026-11-14T12:00:00'};
17
+ const whatsTask = {id: 3, type: 'relationship', announce: '["5511999998888","conn:4"]', client: '/people/50', registeredBy: '/people/50', alterDate: '2026-11-14T11:00:00'};
18
+
19
+ test('idOf reads numbers, IRIs and entities', () => {
20
+ assert.equal(idOf('/people/42'), 42);
21
+ assert.equal(idOf({'@id': '/tasks/7'}), 7);
22
+ assert.equal(idOf({id: '9'}), 9);
23
+ assert.equal(idOf(null), null);
24
+ });
25
+
26
+ test('internal chats vs customer conversations and their channel', () => {
27
+ assert.equal(conversationChannel(webTask), 'ai');
28
+ assert.equal(conversationChannel(whatsTask), 'whatsapp');
29
+ assert.equal(conversationChannel({type: 'support', announce: '["ana@x.com","conn:9"]'}), 'email');
30
+ assert.equal(isCustomerConversation(webTask), false);
31
+ assert.equal(isCustomerConversation(whatsTask), true);
32
+ assert.equal(customerPhone(whatsTask), '5511999998888');
33
+ assert.equal(stageLabel(whatsTask), 'Comercial');
34
+ });
35
+
36
+ test('extractChatEvents only returns new chat events and flattens batches', () => {
37
+ const store = [
38
+ {event: 'order_update'},
39
+ [{event: 'mcp_chat_reply', task_id: 1, status: 'processing'}, {event: 'mcp_chat_reply', task_id: 1, status: 'done'}],
40
+ ];
41
+ const first = extractChatEvents(store, 0);
42
+ assert.deepEqual(first.events.map(e => e.status), ['processing', 'done']);
43
+ assert.equal(first.nextIndex, 2);
44
+
45
+ store.push({event: 'conversation_message', task_id: 3});
46
+ const second = extractChatEvents(store, first.nextIndex);
47
+ assert.deepEqual(second.events.map(e => e.task_id), [3]);
48
+
49
+ // store reset (shorter than our cursor) is re-read from the start
50
+ assert.equal(extractChatEvents([{event: 'mcp_chat_reply'}], 5).events.length, 1);
51
+ });
52
+
53
+ test('mergeConversations hides other people internal chats, keeps customer ones, sorts by activity', () => {
54
+ const merged = mergeConversations([[webTask, otherUserTask], [whatsTask, webTask]], 10);
55
+ assert.deepEqual(merged.map(t => t.id), [3, 1]);
56
+ });
57
+
58
+ test('messageRole distinguishes ai, notes, customer and staff', () => {
59
+ const ctx = {companyId: 99, task: whatsTask, currentPeopleId: 10};
60
+ assert.equal(messageRole({registeredBy: '/people/99', visibility: 'public'}, ctx), 'ai');
61
+ assert.equal(messageRole({registeredBy: '/people/99', visibility: 'private'}, ctx), 'note');
62
+ assert.equal(messageRole({registeredBy: '/people/50'}, ctx), 'customer');
63
+ assert.equal(messageRole({registeredBy: '/people/10'}, ctx), 'me', 'my own reply');
64
+ assert.equal(messageRole({registeredBy: '/people/11'}, ctx), 'staff', 'a colleague');
65
+
66
+ const myTicket = {id: 9, type: 'support', announce: '["via:ai"]', client: '/people/10'};
67
+ assert.equal(messageRole({registeredBy: '/people/10'}, {companyId: 99, task: myTicket, currentPeopleId: 10}), 'me');
68
+
69
+ const webCtx = {companyId: 99, task: webTask, currentPeopleId: 10};
70
+ assert.equal(messageRole({registeredBy: '/people/99', visibility: 'private'}, webCtx), 'ai');
71
+ assert.equal(messageRole({registeredBy: '/people/10'}, webCtx), 'me');
72
+ assert.equal(messageRole({id: 'local-1'}, webCtx), 'me');
73
+ });
@@ -0,0 +1,43 @@
1
+ const {test} = require('node:test');
2
+ const assert = require('node:assert/strict');
3
+
4
+ const {buildColumns, neighbourStatus, personName, timeAgo} = require('../../../../react/pages/tickets/ticketUtils');
5
+
6
+ const statuses = [
7
+ {'@id': '/statuses/30', status: 'ganho', realStatus: 'closed'},
8
+ {'@id': '/statuses/10', status: 'novo', realStatus: 'open'},
9
+ {'@id': '/statuses/20', status: 'proposta', realStatus: 'pending'},
10
+ ];
11
+ const tasks = [
12
+ {id: 1, taskStatus: '/statuses/10'},
13
+ {id: 2, taskStatus: {'@id': '/statuses/20'}},
14
+ {id: 3, taskStatus: '/statuses/99'},
15
+ ];
16
+ const triage = [
17
+ {id: 7, type: 'ia', announce: '["5511999998888","conn:1"]'},
18
+ {id: 8, type: 'ia', announce: '{"channel":"mcp"}'},
19
+ ];
20
+
21
+ test('columns: triage first, open stages, closed last; tasks placed by status', () => {
22
+ const columns = buildColumns(statuses, tasks, triage);
23
+ assert.deepEqual(columns.map(c => c.title), ['Triagem IA', 'novo', 'proposta', 'ganho']);
24
+ assert.deepEqual(columns[0].tasks.map(t => t.id), [7], 'internal /ai chats never show in triage');
25
+ assert.deepEqual(columns[1].tasks.map(t => t.id), [1]);
26
+ assert.deepEqual(columns[2].tasks.map(t => t.id), [2]);
27
+ });
28
+
29
+ test('neighbourStatus moves between stages only', () => {
30
+ const columns = buildColumns(statuses, tasks, triage);
31
+ assert.equal(neighbourStatus(columns, tasks[0], 1)['@id'], '/statuses/20');
32
+ assert.equal(neighbourStatus(columns, tasks[0], -1), null);
33
+ assert.equal(neighbourStatus(columns, triage[0], 1), null);
34
+ });
35
+
36
+ test('personName and timeAgo', () => {
37
+ assert.equal(personName({name: 'Maria'}), 'Maria');
38
+ assert.equal(personName('/people/1'), '');
39
+ const now = new Date('2026-11-14T12:00:00Z').getTime();
40
+ assert.equal(timeAgo('2026-11-14T11:30:00Z', now), '30 min');
41
+ assert.equal(timeAgo('2026-11-14T02:00:00Z', now), '10 h');
42
+ assert.equal(timeAgo('2026-11-10T12:00:00Z', now), '4 d');
43
+ });