|
@@ -1,7 +1,7 @@
|
|
|
import { useState, useEffect, useCallback } from "react";
|
|
import { useState, useEffect, useCallback } from "react";
|
|
|
import { notifications } from "@/lib/notifications";
|
|
import { notifications } from "@/lib/notifications";
|
|
|
import { generateReportFromMessages } from "@/utils/reports";
|
|
import { generateReportFromMessages } from "@/utils/reports";
|
|
|
-import { Message, ChatState, ChatResponse, SuggestedPrompt } from "@/components/chatbot/types";
|
|
|
|
|
|
|
+import { Message, ChatState, ChatResponse, SuggestedPrompt, MedicalAlert } from "@/components/chatbot/types";
|
|
|
|
|
|
|
|
const MAX_MESSAGES = 3;
|
|
const MAX_MESSAGES = 3;
|
|
|
|
|
|
|
@@ -19,6 +19,9 @@ export const useChat = () => {
|
|
|
const [currentSuggestions, setCurrentSuggestions] = useState<SuggestedPrompt[]>([]);
|
|
const [currentSuggestions, setCurrentSuggestions] = useState<SuggestedPrompt[]>([]);
|
|
|
const [showDynamicSuggestions, setShowDynamicSuggestions] = useState(false);
|
|
const [showDynamicSuggestions, setShowDynamicSuggestions] = useState(false);
|
|
|
const [inputDisabledForSuggestions, setInputDisabledForSuggestions] = useState(false);
|
|
const [inputDisabledForSuggestions, setInputDisabledForSuggestions] = useState(false);
|
|
|
|
|
+ const [medicalAlertDetected, setMedicalAlertDetected] = useState<MedicalAlert | null>(null);
|
|
|
|
|
+ const [isSchedulingFromAlert, setIsSchedulingFromAlert] = useState(false);
|
|
|
|
|
+ const [showMedicalAlertBanner, setShowMedicalAlertBanner] = useState(false);
|
|
|
|
|
|
|
|
const remainingMessages = Math.max(0, MAX_MESSAGES - messageCount);
|
|
const remainingMessages = Math.max(0, MAX_MESSAGES - messageCount);
|
|
|
const isLastMessage = remainingMessages === 1;
|
|
const isLastMessage = remainingMessages === 1;
|
|
@@ -192,6 +195,13 @@ export const useChat = () => {
|
|
|
suggestionsCount: data.suggestions?.length || 0
|
|
suggestionsCount: data.suggestions?.length || 0
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+ // Detectar alerta médica
|
|
|
|
|
+ if (data.medicalAlert && data.medicalAlert !== "NO_AGENDAR" && !medicalAlertDetected) {
|
|
|
|
|
+ console.log("🚨 [CHAT] Alerta médica detectada:", data.medicalAlert);
|
|
|
|
|
+ setMedicalAlertDetected(data.medicalAlert);
|
|
|
|
|
+ setShowMedicalAlertBanner(true);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// Crear mensaje del asistente con streaming
|
|
// Crear mensaje del asistente con streaming
|
|
|
const assistantMessage: Message = {
|
|
const assistantMessage: Message = {
|
|
|
role: "assistant",
|
|
role: "assistant",
|
|
@@ -351,6 +361,9 @@ export const useChat = () => {
|
|
|
setCurrentSuggestions([]);
|
|
setCurrentSuggestions([]);
|
|
|
setShowDynamicSuggestions(false);
|
|
setShowDynamicSuggestions(false);
|
|
|
setInputDisabledForSuggestions(false);
|
|
setInputDisabledForSuggestions(false);
|
|
|
|
|
+ setMedicalAlertDetected(null);
|
|
|
|
|
+ setShowMedicalAlertBanner(false);
|
|
|
|
|
+ setIsSchedulingFromAlert(false);
|
|
|
// Limpiar localStorage
|
|
// Limpiar localStorage
|
|
|
localStorage.removeItem("chatState");
|
|
localStorage.removeItem("chatState");
|
|
|
notifications.chat.newConsultation();
|
|
notifications.chat.newConsultation();
|
|
@@ -394,6 +407,48 @@ export const useChat = () => {
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
+ const handleScheduleFromAlert = async (onSuccess: (reportId: string) => void) => {
|
|
|
|
|
+ setIsSchedulingFromAlert(true);
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ // Generar reporte con la conversación actual
|
|
|
|
|
+ const currentReport = generateReportFromMessages(messages);
|
|
|
|
|
+
|
|
|
|
|
+ // Guardar el reporte en la base de datos
|
|
|
|
|
+ const response = await fetch("/api/chat/report", {
|
|
|
|
|
+ method: "POST",
|
|
|
|
|
+ headers: {
|
|
|
|
|
+ "Content-Type": "application/json",
|
|
|
|
|
+ },
|
|
|
|
|
+ body: JSON.stringify({
|
|
|
|
|
+ content: currentReport,
|
|
|
|
|
+ messages: messages,
|
|
|
|
|
+ }),
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ if (response.ok) {
|
|
|
|
|
+ const data = await response.json();
|
|
|
|
|
+ const reportId = data.id;
|
|
|
|
|
+
|
|
|
|
|
+ // Callback con el reportId para abrir el modal
|
|
|
|
|
+ onSuccess(reportId);
|
|
|
|
|
+
|
|
|
|
|
+ notifications.records.saved();
|
|
|
|
|
+ } else {
|
|
|
|
|
+ notifications.records.saveError();
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ console.error("Error al generar reporte para cita:", error);
|
|
|
|
|
+ notifications.records.generateError();
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ setIsSchedulingFromAlert(false);
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const dismissMedicalAlertBanner = () => {
|
|
|
|
|
+ setShowMedicalAlertBanner(false);
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
return {
|
|
return {
|
|
|
messages,
|
|
messages,
|
|
|
messageCount,
|
|
messageCount,
|
|
@@ -418,6 +473,11 @@ export const useChat = () => {
|
|
|
setShowDynamicSuggestions,
|
|
setShowDynamicSuggestions,
|
|
|
inputDisabledForSuggestions,
|
|
inputDisabledForSuggestions,
|
|
|
setInputDisabledForSuggestions,
|
|
setInputDisabledForSuggestions,
|
|
|
|
|
+ medicalAlertDetected,
|
|
|
|
|
+ showMedicalAlertBanner,
|
|
|
|
|
+ isSchedulingFromAlert,
|
|
|
|
|
+ handleScheduleFromAlert,
|
|
|
|
|
+ dismissMedicalAlertBanner,
|
|
|
};
|
|
};
|
|
|
};
|
|
};
|
|
|
|
|
|