@credithub/harlan-components 1.62.8 → 1.62.9

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.
@@ -3,10 +3,12 @@ import React, { createContext, useEffect, useState } from 'react';
3
3
  import { useQuery } from './webservice';
4
4
  export var ConsultaRfbContext = createContext(null);
5
5
  export var ConsultaRfbProvider = function (_a) {
6
- var _b, _c, _d, _e;
6
+ var _b, _c;
7
7
  var children = _a.children, documento = _a.documento;
8
- var _f = useState(false), enableFallback = _f[0], setEnableFallback = _f[1];
9
- var query = useQuery("SELECT FROM 'RFB'.'CERTIDAO'", { documento: documento });
8
+ var _d = useState(false), enableFallback = _d[0], setEnableFallback = _d[1];
9
+ var query = useQuery("SELECT FROM 'RFB'.'CERTIDAO'", {
10
+ documento: documento
11
+ });
10
12
  var queryFallback = useQuery("SELECT FROM 'RECEITAWS'.'CNPJ'", { documento: documento }, enableFallback);
11
13
  var executeFallback = function () { return setEnableFallback(true); };
12
14
  useEffect(function () {
@@ -15,7 +17,9 @@ export var ConsultaRfbProvider = function (_a) {
15
17
  }
16
18
  }, [enableFallback]);
17
19
  return (React.createElement(ConsultaRfbContext.Provider, { value: {
18
- rfbResponse: (_e = (_c = (_b = queryFallback === null || queryFallback === void 0 ? void 0 : queryFallback.response) === null || _b === void 0 ? void 0 : _b.document) !== null && _c !== void 0 ? _c : (_d = query === null || query === void 0 ? void 0 : query.response) === null || _d === void 0 ? void 0 : _d.document) !== null && _e !== void 0 ? _e : undefined,
20
+ rfbResponse: enableFallback
21
+ ? (_b = queryFallback === null || queryFallback === void 0 ? void 0 : queryFallback.response) === null || _b === void 0 ? void 0 : _b.document
22
+ : (_c = query === null || query === void 0 ? void 0 : query.response) === null || _c === void 0 ? void 0 : _c.document,
19
23
  executeFallback: executeFallback,
20
24
  refetch: query.refetch,
21
25
  error: query.error || queryFallback.error,
@@ -17,17 +17,17 @@ import { useGlobalData } from '../../contexts/globalDataContext';
17
17
  import { useConsultaRfb } from '../../hooks/useConsultaRfb';
18
18
  import { formatCnpj, formatCpf } from '../../utils/string';
19
19
  import XPathUtils from '../../utils/xpath';
20
- import React, { useContext, useEffect, useState, useCallback, useMemo } from 'react';
20
+ import React, { useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
21
21
  import styled from 'styled-components';
22
22
  import { ReclameAquiCarousel } from '../reclameAqui/reclameAquiCarousel';
23
23
  import Section from '../section';
24
24
  import { Queries, RequestStatus, useQuery } from '../webservice';
25
25
  import { createDossieData } from './dossieData';
26
+ import GenerativeAI from './generativeAi/generativeAi';
26
27
  import { Summary } from './summary/summary';
27
28
  import { TransactionsTable } from './transactionTable/transactionTable';
28
- import GenerativeAI from './generativeAi/generativeAi';
29
- var DossieContent = styled.div(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n display: grid;\n grid-template-columns: 1fr 2fr;\n justify-items: center;\n align-items: flex-start;\n margin-bottom: 20px;\n gap: 20px;\n\n @media screen and (max-width: 992px) {\n grid-template-columns: 1fr;\n }\n"], ["\n display: grid;\n grid-template-columns: 1fr 2fr;\n justify-items: center;\n align-items: flex-start;\n margin-bottom: 20px;\n gap: 20px;\n\n @media screen and (max-width: 992px) {\n grid-template-columns: 1fr;\n }\n"])));
30
- var DossieContainer = styled.div(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n padding: 0px 70px;\n\n @media (max-width: 1000px) {\n padding: 0px 30px;\n }\n"], ["\n padding: 0px 70px;\n\n @media (max-width: 1000px) {\n padding: 0px 30px;\n }\n"])));
29
+ var DossieContent = styled.div(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n display: grid;\n grid-template-columns: 1fr 2fr;\n justify-items: center;\n align-items: flex-start;\n margin-bottom: 20px;\n gap: 20px;\n @media screen and (max-width: 992px) {\n grid-template-columns: 1fr;\n }\n"], ["\n display: grid;\n grid-template-columns: 1fr 2fr;\n justify-items: center;\n align-items: flex-start;\n margin-bottom: 20px;\n gap: 20px;\n @media screen and (max-width: 992px) {\n grid-template-columns: 1fr;\n }\n"])));
30
+ var DossieContainer = styled.div(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n padding: 0px 70px;\n @media (max-width: 1000px) {\n padding: 0px 30px;\n }\n"], ["\n padding: 0px 70px;\n @media (max-width: 1000px) {\n padding: 0px 30px;\n }\n"])));
31
31
  var GenerativeAIContainer = styled.div(templateObject_3 || (templateObject_3 = __makeTemplateObject(["\n padding: 32px;\n background-color: ", ";\n"], ["\n padding: 32px;\n background-color: ", ";\n"])), function (_a) {
32
32
  var theme = _a.theme;
33
33
  return theme.colors.white;
@@ -36,21 +36,30 @@ var Dossie = function (_a) {
36
36
  var documento = _a.documento, printMode = _a.printMode;
37
37
  var ctx = useContext(Queries.Finder);
38
38
  var rfbResponse = useConsultaRfb().rfbResponse;
39
- var setData = useGlobalData().setData;
40
- var _b = useState(false), dataUpdated = _b[0], setDataUpdated = _b[1];
41
- // Fetch ReclameAqui data here instead of in the carousel component
42
- var _c = useQuery("SELECT FROM 'RECLAMEAQUI'.'CONSULTA'", { documento: documento }), reclameAquiResponse = _c.response, reclameAquiLoading = _c.isLoading, reclameAquiError = _c.error;
43
- // Parse ReclameAqui data
39
+ var _b = useGlobalData(), globalData = _b.data, setData = _b.setData;
40
+ var lastDossieRef = useRef();
41
+ // Ref para guardar o status de receita anterior
42
+ var prevReceitaStatusRef = useRef(undefined);
43
+ // Estado para sinalizar que houve mudança relevante no rfbResponse
44
+ var _c = useState(false), refreshQuery = _c[0], setRefreshQuery = _c[1];
45
+ // Consulta ReclameAqui
46
+ var _d = useQuery("SELECT FROM 'RECLAMEAQUI'.'CONSULTA'", {
47
+ documento: documento
48
+ }), reclameAquiResponse = _d.response, reclameAquiLoading = _d.isLoading, reclameAquiError = _d.error;
49
+ // Processa os dados do ReclameAqui
44
50
  var reclameAquiCompanies = useMemo(function () {
45
51
  var _a;
46
- if (!reclameAquiLoading && !reclameAquiError && ((_a = reclameAquiResponse === null || reclameAquiResponse === void 0 ? void 0 : reclameAquiResponse.document) === null || _a === void 0 ? void 0 : _a.length)) {
52
+ if (!reclameAquiLoading &&
53
+ !reclameAquiError &&
54
+ ((_a = reclameAquiResponse === null || reclameAquiResponse === void 0 ? void 0 : reclameAquiResponse.document) === null || _a === void 0 ? void 0 : _a.length)) {
47
55
  var isPF = (documento === null || documento === void 0 ? void 0 : documento.length) === 11;
48
56
  if (isPF)
49
57
  return undefined;
50
- // Get the name from the Finder document
51
- var name_1 = ctx.document && XPathUtils.select('string(//cadastro/razaoSocial)', ctx.document) || '';
58
+ var name_1 = (ctx.document &&
59
+ XPathUtils.select('string(//cadastro/razaoSocial)', ctx.document)) ||
60
+ '';
52
61
  return reclameAquiResponse.document.map(function (item) { return ({
53
- nome: name_1, // Use the name from Finder instead of from ReclameAqui response
62
+ nome: name_1,
54
63
  cnpj: documento,
55
64
  score: item.infos.finalScore,
56
65
  status: item.infos.status,
@@ -61,10 +70,19 @@ var Dossie = function (_a) {
61
70
  }); });
62
71
  }
63
72
  return undefined;
64
- }, [reclameAquiResponse, reclameAquiLoading, reclameAquiError, documento, ctx.document]);
65
- // Move the data processing to a useCallback to avoid recreating it on every render
73
+ }, [
74
+ reclameAquiResponse,
75
+ reclameAquiLoading,
76
+ reclameAquiError,
77
+ documento,
78
+ ctx.document
79
+ ]);
80
+ // Reseta o cache do dossiê sempre que o rfbResponse mudar
81
+ useEffect(function () {
82
+ lastDossieRef.current = undefined;
83
+ }, [rfbResponse]);
66
84
  var processDossieData = useCallback(function () {
67
- if (ctx.type === RequestStatus.Success && !dataUpdated && ctx.document) {
85
+ if (ctx.type === RequestStatus.Success && ctx.document) {
68
86
  var data = ctx.document;
69
87
  var isPF = (documento === null || documento === void 0 ? void 0 : documento.length) === 11;
70
88
  var name_2 = (isPF
@@ -72,13 +90,24 @@ var Dossie = function (_a) {
72
90
  : XPathUtils.select('string(//cadastro/razaoSocial)', data) ||
73
91
  (rfbResponse &&
74
92
  XPathUtils.select('string(//RFB/nome)', rfbResponse)));
75
- var document_1 = isPF ? formatCpf(documento) : formatCnpj(documento);
76
- var dossieData_1 = createDossieData(data, rfbResponse, name_2, document_1);
77
- setData(function (prevState) { return (__assign(__assign({}, prevState), { dossie: dossieData_1 })); });
78
- setDataUpdated(true);
93
+ var formattedDocument = isPF
94
+ ? formatCpf(documento)
95
+ : formatCnpj(documento);
96
+ var newDossie_1 = createDossieData(data, rfbResponse, name_2, formattedDocument);
97
+ var newReceitaStatus = newDossie_1.summary.receitaStatus;
98
+ var prevReceitaStatus = prevReceitaStatusRef.current;
99
+ if (JSON.stringify(lastDossieRef.current) !== JSON.stringify(newDossie_1)) {
100
+ lastDossieRef.current = newDossie_1;
101
+ setData(function (prevState) { return (__assign(__assign({}, prevState), { dossie: newDossie_1 })); });
102
+ if (prevReceitaStatus !== undefined &&
103
+ newReceitaStatus !== prevReceitaStatus) {
104
+ setRefreshQuery(true);
105
+ }
106
+ prevReceitaStatusRef.current = newReceitaStatus;
107
+ }
108
+ console.log(globalData);
79
109
  }
80
- }, [ctx.type, ctx.document, dataUpdated, documento, rfbResponse, setData]);
81
- // Only update global state once when data is available
110
+ }, [ctx.type, ctx.document, documento, rfbResponse, setData]);
82
111
  useEffect(function () {
83
112
  processDossieData();
84
113
  }, [processDossieData]);
@@ -89,7 +118,8 @@ var Dossie = function (_a) {
89
118
  var name = (isPF
90
119
  ? XPathUtils.select('string(//cadastro/nome)', data)
91
120
  : XPathUtils.select('string(//cadastro/razaoSocial)', data) ||
92
- (rfbResponse && XPathUtils.select('string(//RFB/nome)', rfbResponse)));
121
+ (rfbResponse &&
122
+ XPathUtils.select('string(//RFB/nome)', rfbResponse)));
93
123
  var document = isPF ? formatCpf(documento) : formatCnpj(documento);
94
124
  return (React.createElement(React.Fragment, null,
95
125
  React.createElement(DossieContainer, null,
@@ -98,9 +128,9 @@ var Dossie = function (_a) {
98
128
  React.createElement(Summary, { finderResponse: data, document: document })),
99
129
  React.createElement(TransactionsTable, { events: XPathUtils.select('//historico/consulta', data) || [] })),
100
130
  !printMode && (React.createElement(GenerativeAIContainer, null,
101
- React.createElement(GenerativeAI, { documento: document })))));
131
+ React.createElement(GenerativeAI, { documento: document, refreshQuery: refreshQuery })))));
102
132
  };
103
- }, [documento, rfbResponse, printMode, reclameAquiCompanies]);
133
+ }, [documento, rfbResponse, printMode, reclameAquiCompanies, refreshQuery]);
104
134
  return (React.createElement(Section, { ctx: ctx, hideHeader: true, minimized: false, isError: function (e) { return response(e); }, onSuccess: response }));
105
135
  };
106
136
  export default React.memo(Dossie);
@@ -33,10 +33,13 @@ export function createDossieData(data, rfbResponse, name, document) {
33
33
  ppe: isPF ? XPathUtils.select('string(//cadastro/ppe)', data) : undefined,
34
34
  nomeFantasia: XPathUtils.select('string(//cadastro/nomeFantasia)', data) ||
35
35
  XPathUtils.select('string(//RFB/nome/@fantasia)', rfbResponse),
36
- receitaStatus: XPathUtils.select('string(//cadastro/receitaStatus)', data) ||
37
- XPathUtils.select('string(//RFB/situacao)', rfbResponse),
38
- dataReceitaStatus: XPathUtils.select('string(//cadastro/dataReceitaStatus)', data) ||
39
- XPathUtils.select('string(//RFB/dataSituacao)', rfbResponse),
36
+ // Se houver rfbResponse, use os valores do fallback
37
+ receitaStatus: rfbResponse
38
+ ? XPathUtils.select('string(//body/RFB/situacao)', rfbResponse)
39
+ : XPathUtils.select('string(//cadastro/receitaStatus)', data),
40
+ dataReceitaStatus: rfbResponse
41
+ ? XPathUtils.select('string(//body/RFB/data-situacao)', rfbResponse)
42
+ : XPathUtils.select('string(//cadastro/dataReceitaStatus)', data),
40
43
  tipoEmpresa: XPathUtils.select('string(//cadastro/tipoEmpresa)', data),
41
44
  cnaeDescricao: XPathUtils.select('string(//cadastro/cnaeDescricao)', data) ||
42
45
  XPathUtils.select('string(//RFB/atividade-economica)', rfbResponse),
@@ -1,6 +1,7 @@
1
1
  import React from 'react';
2
2
  interface GenerativeAIProps {
3
3
  documento: string;
4
+ refreshQuery?: boolean;
4
5
  }
5
6
  declare const GenerativeAI: React.FC<GenerativeAIProps>;
6
7
  export default GenerativeAI;
@@ -1,3 +1,14 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
1
12
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
13
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
14
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -63,30 +74,33 @@ import { createQueryAndMessages } from './queryUtils';
63
74
  import { BetaTag, ChatInput, ChatInputContainer, Container, ContainerInput, GenerativeAi, Header, HeaderContent, InitialMessageBox, InitialMessageContent, LoadingDots, Message, MessageHistory, SendButton, Title } from './styles';
64
75
  var GenerativeAI = function (_a) {
65
76
  var _b, _c, _d;
66
- var documento = _a.documento;
77
+ var documento = _a.documento, refreshQuery = _a.refreshQuery;
67
78
  var globalData = useGlobalData().data;
68
79
  var _e = useState('{}'), serializedData = _e[0], setSerializedData = _e[1];
69
80
  var _f = useState(''), userInput = _f[0], setUserInput = _f[1];
70
81
  var _g = useState([]), messageHistory = _g[0], setMessageHistory = _g[1];
71
82
  var _h = useToggle(), isOpen = _h[0], toggleOpen = _h[1];
72
83
  var _j = useState('summary'), streamMode = _j[0], setStreamMode = _j[1];
73
- var _k = useState(null), summaryQueryData = _k[0], setSummaryQueryData = _k[1];
74
- var _l = useState(null), chatQueryData = _l[0], setChatQueryData = _l[1];
75
- var _m = useState(false), pendingRefetch = _m[0], setPendingRefetch = _m[1];
84
+ // Estado para a query de chat (inicia como undefined)
85
+ var _k = useState(undefined), chatQueryData = _k[0], setChatQueryData = _k[1];
86
+ // Estado para forçar o refresh da query summary
87
+ var _l = useState(0), refreshKey = _l[0], setRefreshKey = _l[1];
88
+ // Refs para guardar a última mensagem (evita refetch repetido)
76
89
  var lastQueryRef = useRef(null);
77
90
  var lastUserMessageRef = useRef(null);
78
91
  var messageHistoryRef = useRef(null);
92
+ // Verifica se os dados globais estão carregados
79
93
  var isGlobalDataLoaded = useMemo(function () {
80
94
  return (globalData.documentHistory !== undefined &&
81
95
  globalData.documentHistory !== null);
82
96
  }, [globalData]);
97
+ console.log('refreshQuery', refreshQuery);
98
+ // Atualiza o payload serializado sempre que os dados globais mudarem
83
99
  useEffect(function () {
84
- // Only update serializedData if globalData has actually changed and is loaded
85
100
  if (globalData && documento && isGlobalDataLoaded) {
86
101
  try {
87
102
  var relevantData = selectRelevantData(globalData);
88
103
  var serialized = flattenData(relevantData);
89
- // Only update state if the data has actually changed
90
104
  if (serialized !== serializedData) {
91
105
  setSerializedData(serialized);
92
106
  }
@@ -99,26 +113,45 @@ var GenerativeAI = function (_a) {
99
113
  }
100
114
  }
101
115
  }, [documento, globalData, isGlobalDataLoaded, serializedData]);
102
- // Only set summary query data once when serializedData is available
103
- useEffect(function () {
104
- if (serializedData &&
105
- serializedData !== '{}' &&
106
- streamMode === 'summary' &&
107
- !summaryQueryData) {
116
+ // Deriva o payload para a consulta summary a partir do serializedData.
117
+ // Incluímos o refreshKey para forçar a atualização da query.
118
+ var summaryQueryData = useMemo(function () {
119
+ if (serializedData && serializedData !== '{}' && streamMode === 'summary') {
108
120
  var queryData = createQueryAndMessages(serializedData, []);
109
- setSummaryQueryData(queryData);
121
+ return queryData
122
+ ? __assign(__assign({}, queryData), { refreshKey: refreshKey.toString() }) : undefined;
123
+ }
124
+ return undefined;
125
+ }, [serializedData, streamMode, refreshKey]);
126
+ // Configura a query summary com o hook useStreamQuery
127
+ var _m = useStreamQuery("SELECT FROM 'LlamaApi'.'Consulta'", summaryQueryData, streamMode === 'summary' && !!summaryQueryData), summaryIterator = _m.responseIterator, summaryError = _m.error, summaryLoading = _m.isLoading, refetchSummary = _m.refetch, abortSummary = _m.abort;
128
+ // Configura a query chat
129
+ var _o = useStreamQuery("SELECT FROM 'LlamaApi'.'Consulta'", chatQueryData, streamMode === 'chat' && !!chatQueryData), chatIterator = _o.responseIterator, chatError = _o.error, chatLoading = _o.isLoading, refetchChat = _o.refetch, abortChat = _o.abort;
130
+ // Dispara o refetch inicial quando summaryQueryData estiver definido
131
+ useEffect(function () {
132
+ if (summaryQueryData) {
133
+ refetchSummary();
134
+ }
135
+ }, [summaryQueryData, refetchSummary]);
136
+ // Detecta a transição de refreshQuery de false para true para reiniciar a consulta
137
+ var prevRefreshQueryRef = useRef(refreshQuery);
138
+ useEffect(function () {
139
+ if (refreshQuery && !prevRefreshQueryRef.current) {
140
+ console.log('executou refreshQuery');
141
+ // Aborta o stream summary atual, se houver
142
+ abortSummary();
143
+ // Permite que o stream summary seja processado novamente
144
+ processedSummaryRef.current = false;
145
+ // Garante que o modo esteja definido como summary
146
+ if (streamMode !== 'summary') {
147
+ setStreamMode('summary');
148
+ }
149
+ // Incrementa o refreshKey para forçar a atualização da query
150
+ setRefreshKey(function (prev) { return prev + 1; });
110
151
  }
111
- }, [serializedData, streamMode, summaryQueryData]);
112
- // Memoize the useStreamQuery parameters to prevent unnecessary API calls
113
- var summaryQueryEnabled = useMemo(function () {
114
- return streamMode === 'summary' && !!summaryQueryData;
115
- }, [streamMode, summaryQueryData]);
116
- var chatQueryEnabled = useMemo(function () {
117
- return streamMode === 'chat' && !!chatQueryData;
118
- }, [streamMode, chatQueryData]);
119
- var _o = useStreamQuery("SELECT FROM 'LlamaApi'.'Consulta'", summaryQueryData, summaryQueryEnabled), summaryIterator = _o.responseIterator, summaryError = _o.error, summaryLoading = _o.isLoading, refetchSummary = _o.refetch, abortSummary = _o.abort;
120
- var _p = useStreamQuery("SELECT FROM 'LlamaApi'.'Consulta'", chatQueryData, chatQueryEnabled), chatIterator = _p.responseIterator, chatError = _p.error, chatLoading = _p.isLoading, refetchChat = _p.refetch, abortChat = _p.abort;
121
- // Use a ref to track if we've already processed this iterator
152
+ prevRefreshQueryRef.current = refreshQuery;
153
+ }, [refreshQuery, abortSummary, streamMode]);
154
+ // Processa o stream da consulta summary (apenas uma vez)
122
155
  var processedSummaryRef = useRef(false);
123
156
  useEffect(function () {
124
157
  if (summaryIterator && !processedSummaryRef.current) {
@@ -140,7 +173,7 @@ var GenerativeAI = function (_a) {
140
173
  }); })();
141
174
  }
142
175
  }, [summaryIterator]);
143
- // Use a ref to track each chat iterator
176
+ // Processa o stream da consulta chat
144
177
  var processedChatRef = useRef(new Map());
145
178
  useEffect(function () {
146
179
  if (chatIterator && !processedChatRef.current.get(chatIterator)) {
@@ -165,19 +198,17 @@ var GenerativeAI = function (_a) {
165
198
  lastQueryRef.current = newChatQueryData.messages;
166
199
  abortChat();
167
200
  setChatQueryData(newChatQueryData);
168
- setPendingRefetch(true);
169
201
  }
170
202
  }
171
203
  }, [messageHistory, serializedData, streamMode, chatLoading, abortChat]);
172
204
  useEffect(function () {
173
- if (pendingRefetch &&
174
- chatQueryData &&
205
+ if (chatQueryData &&
175
206
  streamMode === 'chat' &&
176
- !chatLoading) {
207
+ !chatLoading &&
208
+ messageHistory.length > 0) {
177
209
  refetchChat();
178
- setPendingRefetch(false);
179
210
  }
180
- }, [pendingRefetch, chatQueryData, streamMode, chatLoading, refetchChat]);
211
+ }, [chatQueryData, streamMode, chatLoading, refetchChat, messageHistory]);
181
212
  var handleStopStream = function () {
182
213
  abortChat();
183
214
  };
@@ -18,7 +18,6 @@ import { Queries, RequestStatus } from '../webservice';
18
18
  import ProcessosJuridicosList from './processosJuridicosList';
19
19
  var ProcessosJuridicos = function (_a) {
20
20
  var onClickConsultarProcessoJuridico = _a.onClickConsultarProcessoJuridico;
21
- console.log('Processos Juridicos Renderizou');
22
21
  var ctx = useContext(Queries.ProcessosJuridicos);
23
22
  var setData = useGlobalData().setData;
24
23
  var processedRef = useRef(false);