added all new chanes
This commit is contained in:
@@ -42,15 +42,22 @@ export default function LoginPage() {
|
||||
});
|
||||
navigate('/account');
|
||||
} catch (error: any) {
|
||||
console.error('Login error:', error);
|
||||
let errorMessage = 'خطا در ورود';
|
||||
|
||||
if (error.message) {
|
||||
if (error.message.includes('Invalid login credentials')) {
|
||||
if (error) {
|
||||
const errorMsg = error.message || error.error?.message || String(error);
|
||||
|
||||
if (errorMsg.includes('Invalid login credentials') || errorMsg.includes('invalid_credentials')) {
|
||||
errorMessage = 'ایمیل یا رمز عبور اشتباه است. لطفا دوباره تلاش کنید یا ثبت نام کنید.';
|
||||
} else if (error.message.includes('Email not confirmed')) {
|
||||
} else if (errorMsg.includes('Email not confirmed') || errorMsg.includes('email_not_confirmed')) {
|
||||
errorMessage = 'لطفا ابتدا ایمیل خود را تأیید کنید.';
|
||||
} else if (errorMsg.includes('Supabase not configured')) {
|
||||
errorMessage = 'Supabase تنظیم نشده است. لطفا فایل .env را بررسی کنید.';
|
||||
} else if (errorMsg.includes('Network') || errorMsg.includes('fetch')) {
|
||||
errorMessage = 'خطا در اتصال به سرور. لطفا اتصال اینترنت خود را بررسی کنید.';
|
||||
} else {
|
||||
errorMessage = error.message;
|
||||
errorMessage = errorMsg || 'خطا در ورود. لطفا دوباره تلاش کنید.';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,9 +77,13 @@ export default function LoginPage() {
|
||||
await signInWithGoogle();
|
||||
// Redirect happens automatically
|
||||
} catch (error: any) {
|
||||
console.error('Google login error:', error);
|
||||
const errorMsg = error?.message || error?.error?.message || String(error) || 'خطا در ورود با گوگل';
|
||||
toast({
|
||||
title: 'خطا',
|
||||
description: error.message || 'خطا در ورود با گوگل',
|
||||
description: errorMsg.includes('Supabase not configured')
|
||||
? 'Supabase تنظیم نشده است. لطفا فایل .env را بررسی کنید.'
|
||||
: errorMsg,
|
||||
variant: 'destructive'
|
||||
});
|
||||
setIsLoading(false);
|
||||
@@ -99,9 +110,23 @@ export default function LoginPage() {
|
||||
description: 'کد تأیید ارسال شد'
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error('Phone OTP error:', error);
|
||||
const errorMsg = error?.message || error?.error?.message || String(error) || 'خطا در ارسال کد';
|
||||
let errorMessage = 'خطا در ارسال کد';
|
||||
|
||||
if (errorMsg.includes('Supabase not configured')) {
|
||||
errorMessage = 'Supabase تنظیم نشده است. لطفا فایل .env را بررسی کنید.';
|
||||
} else if (errorMsg.includes('Invalid phone number') || errorMsg.includes('invalid_phone')) {
|
||||
errorMessage = 'شماره تلفن نامعتبر است. لطفا شماره را با فرمت صحیح وارد کنید.';
|
||||
} else if (errorMsg.includes('Network') || errorMsg.includes('fetch')) {
|
||||
errorMessage = 'خطا در اتصال به سرور. لطفا اتصال اینترنت خود را بررسی کنید.';
|
||||
} else {
|
||||
errorMessage = errorMsg;
|
||||
}
|
||||
|
||||
toast({
|
||||
title: 'خطا',
|
||||
description: error.message || 'خطا در ارسال کد',
|
||||
description: errorMessage,
|
||||
variant: 'destructive'
|
||||
});
|
||||
} finally {
|
||||
@@ -129,9 +154,25 @@ export default function LoginPage() {
|
||||
});
|
||||
navigate('/account');
|
||||
} catch (error: any) {
|
||||
console.error('OTP verification error:', error);
|
||||
const errorMsg = error?.message || error?.error?.message || String(error) || 'کد تأیید نامعتبر است';
|
||||
let errorMessage = 'کد تأیید نامعتبر است';
|
||||
|
||||
if (errorMsg.includes('Supabase not configured')) {
|
||||
errorMessage = 'Supabase تنظیم نشده است. لطفا فایل .env را بررسی کنید.';
|
||||
} else if (errorMsg.includes('Invalid OTP') || errorMsg.includes('invalid_token')) {
|
||||
errorMessage = 'کد تأیید نامعتبر است. لطفا دوباره تلاش کنید.';
|
||||
} else if (errorMsg.includes('expired')) {
|
||||
errorMessage = 'کد تأیید منقضی شده است. لطفا کد جدید درخواست کنید.';
|
||||
} else if (errorMsg.includes('Network') || errorMsg.includes('fetch')) {
|
||||
errorMessage = 'خطا در اتصال به سرور. لطفا اتصال اینترنت خود را بررسی کنید.';
|
||||
} else {
|
||||
errorMessage = errorMsg;
|
||||
}
|
||||
|
||||
toast({
|
||||
title: 'خطا',
|
||||
description: error.message || 'کد تأیید نامعتبر است',
|
||||
description: errorMessage,
|
||||
variant: 'destructive'
|
||||
});
|
||||
} finally {
|
||||
|
||||
+159
-45
@@ -5,8 +5,9 @@ import { Input } from '@/components/ui/input';
|
||||
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
|
||||
import { useAuth } from '@/hooks/useAuth';
|
||||
import { useChat } from '@/hooks/useChat';
|
||||
import { useTypewriter } from '@/hooks/useTypewriter';
|
||||
import { useToast } from '@/components/ui/use-toast';
|
||||
import { Send, MessageSquare, Loader2 } from 'lucide-react';
|
||||
import { Send, MessageSquare, Loader2, Play } from 'lucide-react';
|
||||
|
||||
export default function ChatPage() {
|
||||
const { user } = useAuth();
|
||||
@@ -15,13 +16,14 @@ export default function ChatPage() {
|
||||
const [message, setMessage] = useState('');
|
||||
const [isSending, setIsSending] = useState(false);
|
||||
const messagesEndRef = useRef<HTMLDivElement>(null);
|
||||
const typedMessagesRef = useRef<Set<string>>(new Set()); // Track which messages have been typed
|
||||
|
||||
useEffect(() => {
|
||||
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
|
||||
}, [messages]);
|
||||
|
||||
const handleSend = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
const handleSend = async (e?: React.FormEvent, initialMessage?: string, skipUserMessage = false) => {
|
||||
e?.preventDefault();
|
||||
if (!user) {
|
||||
toast({
|
||||
title: 'خطا',
|
||||
@@ -31,18 +33,26 @@ export default function ChatPage() {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!message.trim()) {
|
||||
const messageToSend = initialMessage || message;
|
||||
if (!messageToSend.trim()) {
|
||||
return;
|
||||
}
|
||||
|
||||
setIsSending(true);
|
||||
try {
|
||||
await sendMessage(message, user.id);
|
||||
setMessage('');
|
||||
// @ts-ignore - skipUserMessage parameter exists
|
||||
await sendMessage(messageToSend, user.id, skipUserMessage);
|
||||
if (!initialMessage) {
|
||||
setMessage('');
|
||||
}
|
||||
} catch (error) {
|
||||
const description =
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: 'خطا در ارسال پیام';
|
||||
toast({
|
||||
title: 'خطا',
|
||||
description: 'خطا در ارسال پیام',
|
||||
description,
|
||||
variant: 'destructive'
|
||||
});
|
||||
} finally {
|
||||
@@ -50,7 +60,13 @@ export default function ChatPage() {
|
||||
}
|
||||
};
|
||||
|
||||
const handleStartChat = () => {
|
||||
// skipUserMessage = true یعنی پیام کاربر نمایش داده نمیشود
|
||||
handleSend(undefined, 'سلام', true);
|
||||
};
|
||||
|
||||
const getUserDisplayName = (msg: typeof messages[0]) => {
|
||||
if (msg?.role === 'assistant') return 'دستیار هوشمند';
|
||||
if (msg.user_id === user?.id) return 'شما';
|
||||
// For other users, show a simplified identifier
|
||||
// In production, you'd fetch from a profiles table
|
||||
@@ -61,12 +77,113 @@ export default function ChatPage() {
|
||||
};
|
||||
|
||||
const getUserInitials = (msg: typeof messages[0]) => {
|
||||
if (msg?.role === 'assistant') return 'هو';
|
||||
const name = getUserDisplayName(msg);
|
||||
if (name === 'شما') return 'ش';
|
||||
if (name.length >= 2) return name.substring(0, 2);
|
||||
return 'ک';
|
||||
};
|
||||
|
||||
// Component for message bubble with typewriter effect for AI messages
|
||||
const MessageBubble = ({
|
||||
message,
|
||||
isOwnMessage,
|
||||
isAssistant,
|
||||
getUserDisplayName,
|
||||
getUserInitials
|
||||
}: {
|
||||
message: typeof messages[0];
|
||||
isOwnMessage: boolean;
|
||||
isAssistant: boolean;
|
||||
getUserDisplayName: (msg: typeof messages[0]) => string;
|
||||
getUserInitials: (msg: typeof messages[0]) => string;
|
||||
}) => {
|
||||
// Check if this message has already been typed
|
||||
const hasBeenTyped = typedMessagesRef.current.has(message.id);
|
||||
|
||||
// If it's an assistant message and hasn't been typed yet, use typewriter
|
||||
const shouldAnimate = isAssistant && !hasBeenTyped && !!message.content;
|
||||
|
||||
// Only use typewriter for assistant messages that haven't been typed yet
|
||||
const typewriterResult = useTypewriter({
|
||||
text: shouldAnimate ? message.content : '',
|
||||
speed: 10, // سرعت تایپ سریعتر (میلیثانیه)
|
||||
onComplete: () => {
|
||||
// Mark this message as typed when animation completes
|
||||
if (isAssistant && message.id) {
|
||||
typedMessagesRef.current.add(message.id);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Determine final text to display
|
||||
// Priority: animated text (if available) > message.content
|
||||
let finalText: string;
|
||||
if (isAssistant && shouldAnimate) {
|
||||
// For assistant messages with animation, use displayedText if it exists, otherwise fallback to content
|
||||
finalText = typewriterResult.displayedText.length > 0
|
||||
? typewriterResult.displayedText
|
||||
: message.content || '';
|
||||
} else {
|
||||
// For user messages or already-typed assistant messages, show full content
|
||||
finalText = message.content || '';
|
||||
}
|
||||
|
||||
const showCursor = isAssistant && shouldAnimate && typewriterResult.isTyping;
|
||||
|
||||
return (
|
||||
<div
|
||||
className={`flex gap-3 ${isOwnMessage ? 'flex-row-reverse' : 'flex-row'}`}
|
||||
>
|
||||
<Avatar className="h-8 w-8 flex-shrink-0">
|
||||
<AvatarImage src={message.user?.avatar_url} />
|
||||
<AvatarFallback className="text-xs">
|
||||
{getUserInitials(message)}
|
||||
</AvatarFallback>
|
||||
</Avatar>
|
||||
<div className={`flex-1 ${isOwnMessage ? 'text-left' : 'text-right'}`}>
|
||||
<div className="flex items-center gap-2 mb-1 flex-wrap">
|
||||
<span className="text-xs font-medium">
|
||||
{getUserDisplayName(message)}
|
||||
</span>
|
||||
<span className="text-xs text-muted-foreground">
|
||||
{new Date(message.created_at).toLocaleTimeString('fa-IR', {
|
||||
hour: '2-digit',
|
||||
minute: '2-digit'
|
||||
})}
|
||||
</span>
|
||||
</div>
|
||||
<div
|
||||
className={`rounded-lg p-3 ${
|
||||
isOwnMessage
|
||||
? 'bg-primary text-primary-foreground ml-auto'
|
||||
: isAssistant
|
||||
? 'bg-secondary text-secondary-foreground'
|
||||
: 'bg-muted'
|
||||
}`}
|
||||
style={{
|
||||
maxWidth: '85%',
|
||||
marginRight: isOwnMessage ? '0' : 'auto',
|
||||
marginLeft: isOwnMessage ? 'auto' : '0'
|
||||
}}
|
||||
>
|
||||
<p className="text-xs whitespace-pre-wrap break-words">
|
||||
{finalText}
|
||||
{showCursor && (
|
||||
<span className="inline-block w-2 h-4 bg-current ml-1 animate-pulse">|</span>
|
||||
)}
|
||||
</p>
|
||||
{message.status === 'error' && (
|
||||
<span className="mt-2 block text-[10px] text-destructive">
|
||||
ارسال ناموفق بود. دوباره تلاش کنید.
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-gradient-to-br from-background to-muted p-4 pb-20">
|
||||
<div className="mx-auto max-w-sm">
|
||||
@@ -94,49 +211,46 @@ export default function ChatPage() {
|
||||
<>
|
||||
<div className="flex-1 overflow-y-auto space-y-4 mb-4 pr-2">
|
||||
{messages.length === 0 ? (
|
||||
<div className="text-center py-8 text-muted-foreground">
|
||||
هیچ پیامی وجود ندارد. اولین پیام را ارسال کنید!
|
||||
<div className="flex flex-col items-center justify-center h-full py-8">
|
||||
<div className="text-center mb-6">
|
||||
<MessageSquare className="h-16 w-16 text-muted-foreground mx-auto mb-4 opacity-50" />
|
||||
<p className="text-muted-foreground mb-2">به چت هوشمند خوش آمدید</p>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
برای شروع گفتگو، دکمه زیر را بزنید
|
||||
</p>
|
||||
</div>
|
||||
<Button
|
||||
onClick={handleStartChat}
|
||||
disabled={isSending || !user}
|
||||
size="lg"
|
||||
className="gap-2"
|
||||
>
|
||||
{isSending ? (
|
||||
<>
|
||||
<Loader2 className="h-5 w-5 animate-spin" />
|
||||
در حال اتصال...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Play className="h-5 w-5" />
|
||||
شروع گفتگو
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
) : (
|
||||
messages.map((msg) => {
|
||||
const isOwnMessage = msg.user_id === user?.id;
|
||||
const isOwnMessage = msg.user_id === user?.id && msg.role !== 'assistant';
|
||||
const isAssistant = msg.role === 'assistant';
|
||||
return (
|
||||
<div
|
||||
<MessageBubble
|
||||
key={msg.id}
|
||||
className={`flex gap-3 ${isOwnMessage ? 'flex-row-reverse' : 'flex-row'}`}
|
||||
>
|
||||
<Avatar className="h-8 w-8 flex-shrink-0">
|
||||
<AvatarImage src={msg.user?.avatar_url} />
|
||||
<AvatarFallback className="text-xs">
|
||||
{getUserInitials(msg)}
|
||||
</AvatarFallback>
|
||||
</Avatar>
|
||||
<div
|
||||
className={`flex-1 ${isOwnMessage ? 'text-left' : 'text-right'}`}
|
||||
>
|
||||
<div className="flex items-center gap-2 mb-1 flex-wrap">
|
||||
<span className="text-xs font-medium">
|
||||
{getUserDisplayName(msg)}
|
||||
</span>
|
||||
<span className="text-xs text-muted-foreground">
|
||||
{new Date(msg.created_at).toLocaleTimeString('fa-IR', {
|
||||
hour: '2-digit',
|
||||
minute: '2-digit'
|
||||
})}
|
||||
</span>
|
||||
</div>
|
||||
<div
|
||||
className={`rounded-lg p-3 ${
|
||||
isOwnMessage
|
||||
? 'bg-primary text-primary-foreground ml-auto'
|
||||
: 'bg-muted'
|
||||
}`}
|
||||
style={{ maxWidth: '85%', marginRight: isOwnMessage ? '0' : 'auto', marginLeft: isOwnMessage ? 'auto' : '0' }}
|
||||
>
|
||||
<p className="text-xs whitespace-pre-wrap break-words">{msg.content}</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
message={msg}
|
||||
isOwnMessage={isOwnMessage}
|
||||
isAssistant={isAssistant}
|
||||
getUserDisplayName={getUserDisplayName}
|
||||
getUserInitials={getUserInitials}
|
||||
/>
|
||||
);
|
||||
})
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user