Spaces:
Running
Running
File size: 9,507 Bytes
542c765 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | "use client"
import { useState, useRef, useEffect } from "react";
import { AnimatePresence, motion } from "framer-motion";
import { useGUCStore } from "@/lib/store";
interface Message {
id: string;
text: string;
sender: "user" | "doctor";
}
interface DoctorChatProps {
onSend: (message: string) => Promise<string>;
}
const suggestions = [
"क्या मैं चावल खा सकता हूँ?",
"मुझे क्या खाना चाहिए?",
"यह कितना गंभीर है?",
];
const BouncingDots = () => (
<div className="flex items-start">
<div className="rounded-2xl px-4 py-3 bg-[#1e293b] flex gap-1.5 items-center">
{[0, 1, 2].map((i) => (
<motion.span
key={i}
className="block w-2 h-2 rounded-full bg-[#94a3b8]"
animate={{ y: [0, -6, 0] }}
transition={{ duration: 0.5, repeat: Infinity, delay: i * 0.15 }}
/>
))}
</div>
</div>
);
const DoctorChat: React.FC<DoctorChatProps> = ({ onSend }) => {
const { profile } = useGUCStore()
const language = profile.language === "HI" ? "hindi" : "english"
const [open, setOpen] = useState(false);
const [messages, setMessages] = useState<Message[]>([]);
const [input, setInput] = useState("");
const [loading, setLoading] = useState(false);
const bottomRef = useRef<HTMLDivElement>(null);
useEffect(() => {
bottomRef.current?.scrollIntoView({ behavior: "smooth" });
}, [messages, loading]);
const send = async (text: string) => {
if (!text.trim() || loading) return;
const userMsg: Message = { id: Date.now().toString(), text, sender: "user" };
setMessages((prev) => [...prev, userMsg]);
setInput("");
setLoading(true);
try {
const reply = await onSend(text);
setMessages((prev) => [
...prev,
{ id: (Date.now() + 1).toString(), text: reply, sender: "doctor" },
]);
} catch {
setMessages((prev) => [
...prev,
{
id: (Date.now() + 1).toString(),
text: "कुछ गड़बड़ हो गई। कृपया दोबारा कोशिश करें।",
sender: "doctor",
},
]);
} finally {
setLoading(false);
}
};
return (
<>
<button
onClick={() => setOpen((o) => !o)}
className="fixed bottom-5 left-64 z-50 rounded-full px-5 py-2.5 font-bold text-sm shadow-lg transition-all hover:scale-105 active:scale-95 flex items-center gap-2"
style={{
backgroundColor: open ? "rgba(255,153,51,0.9)" : "#FF9933",
color: "#0d0d1a",
boxShadow: "0 4px 20px rgba(255,153,51,0.35)",
backdropFilter: "blur(8px)",
}}
>
{open ? "✕ Close" : "💬 Dr. Raahat"}
</button>
<AnimatePresence>
{open && (
<>
<motion.div
className="fixed inset-0 z-40 bg-black/40 sm:hidden"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
onClick={() => setOpen(false)}
/>
<motion.div
initial={{ x: "100%" }}
animate={{ x: 0 }}
exit={{ x: "100%" }}
transition={{ type: "spring", damping: 26, stiffness: 200 }}
className="fixed top-0 right-0 z-50 h-full w-full sm:w-96 flex flex-col"
style={{
background: "rgba(10,10,20,0.96)",
borderLeft: "1px solid rgba(255,153,51,0.1)",
backdropFilter: "blur(24px)",
boxShadow: "-8px 0 40px rgba(0,0,0,0.6)",
}}
>
<div
className="flex items-center justify-between px-4 py-4 border-b"
style={{ borderColor: "rgba(255,255,255,0.07)" }}
>
<div>
<div className="flex items-center gap-2.5">
<div
className="w-8 h-8 rounded-xl flex items-center justify-center"
style={{ background: "rgba(255,153,51,0.15)", border: "1px solid rgba(255,153,51,0.25)" }}
>
🩺
</div>
<div>
<span className="font-bold text-base" style={{ color: "#FF9933" }}>
Dr. Raahat
</span>
<p className="text-[10px]" style={{ color: "rgba(255,255,255,0.3)" }}>
Aapka AI Doctor • Online
</p>
</div>
</div>
</div>
<button
onClick={() => setOpen(false)}
className="w-8 h-8 flex items-center justify-center rounded-xl text-sm transition-all hover:bg-white/5"
style={{ color: "rgba(255,255,255,0.4)" }}
>
✕
</button>
</div>
<div className="flex-1 overflow-y-auto px-4 py-4 space-y-3">
{messages.length === 0 && !loading && (
<div className="flex flex-col gap-2 mt-8">
<p
className="text-center text-xs mb-3 font-medium"
style={{ color: "rgba(255,255,255,0.35)" }}
>
{language === "hindi" ? "कुछ पूछें:" : "Ask something:"}
</p>
{suggestions.map((s) => (
<button
key={s}
onClick={() => send(s)}
className="rounded-xl px-4 py-3 text-sm text-left transition-all hover:opacity-80"
style={{
background: "rgba(255,255,255,0.04)",
color: "rgba(255,255,255,0.8)",
border: "1px solid rgba(255,255,255,0.08)",
}}
>
{s}
</button>
))}
</div>
)}
{messages.map((msg) => (
<div
key={msg.id}
className={`flex ${msg.sender === "user" ? "justify-end" : "justify-start"}`}
>
<div
className="max-w-[80%] rounded-2xl px-4 py-2.5 text-sm"
style={
msg.sender === "user"
? {
background: "rgba(255,153,51,0.12)",
borderLeft: "3px solid #FF9933",
color: "rgba(255,255,255,0.9)",
}
: {
background: "rgba(255,255,255,0.05)",
color: "rgba(255,255,255,0.85)",
border: "1px solid rgba(255,255,255,0.07)",
}
}
>
{msg.text}
</div>
</div>
))}
{loading && <BouncingDots />}
<div ref={bottomRef} />
</div>
<div
className="px-3 py-3 flex items-center gap-2 border-t"
style={{ borderColor: "rgba(255,255,255,0.07)" }}
>
<button
className="w-10 h-10 flex items-center justify-center rounded-full text-lg shrink-0 hover:opacity-80 transition-opacity"
style={{ background: "rgba(255,255,255,0.05)", color: "rgba(255,255,255,0.4)" }}
>
🎤
</button>
<input
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyDown={(e) => e.key === "Enter" && send(input)}
placeholder={
language === "hindi"
? "अपना सवाल लिखें..."
: "Type your question..."
}
className="flex-1 rounded-xl px-4 py-2.5 text-sm outline-none transition-all"
style={{
background: "rgba(255,255,255,0.05)",
color: "rgba(255,255,255,0.9)",
border: "2px solid transparent",
}}
onFocus={(e) => (e.currentTarget.style.borderColor = "rgba(255,153,51,0.5)")}
onBlur={(e) => (e.currentTarget.style.borderColor = "transparent")}
/>
<button
onClick={() => send(input)}
disabled={loading || !input.trim()}
className="w-10 h-10 flex items-center justify-center rounded-full text-lg shrink-0 font-bold disabled:opacity-40 hover:opacity-90 transition-opacity"
style={{
background: "linear-gradient(135deg, #FF9933 0%, #FFAA55 100%)",
color: "#0d0d1a",
boxShadow: "0 2px 12px rgba(255,153,51,0.3)",
}}
>
→
</button>
</div>
</motion.div>
</>
)}
</AnimatePresence>
</>
);
};
export default DoctorChat; |