first commit

This commit is contained in:
ahmadrezabd
2025-11-18 20:22:25 +03:30
commit 948238b6d9
55 changed files with 13772 additions and 0 deletions
+125
View File
@@ -0,0 +1,125 @@
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
import { useAuth } from '@/hooks/useAuth';
import { useNavigate } from 'react-router-dom';
import { LogOut, User as UserIcon, Mail, Phone } from 'lucide-react';
import { useToast } from '@/components/ui/use-toast';
export default function AccountPage() {
const { user, signOut } = useAuth();
const navigate = useNavigate();
const { toast } = useToast();
const handleSignOut = async () => {
try {
await signOut();
toast({
title: 'خروج موفق',
description: 'با موفقیت خارج شدید'
});
navigate('/');
} catch (error) {
toast({
title: 'خطا',
description: 'خطا در خروج از حساب کاربری',
variant: 'destructive'
});
}
};
const getUserInitials = () => {
if (user?.email) {
return user.email.substring(0, 2).toUpperCase();
}
if (user?.phone) {
return user.phone.slice(-2);
}
return 'U';
};
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">
<div className="mb-4">
<h1 className="text-2xl font-bold mb-2">حساب کاربری</h1>
<p className="text-sm text-muted-foreground">مدیریت اطلاعات و تنظیمات حساب کاربری</p>
</div>
<Card>
<CardHeader>
<CardTitle>اطلاعات کاربری</CardTitle>
<CardDescription>جزئیات حساب کاربری شما</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
<div className="flex items-center gap-4">
<Avatar className="h-16 w-16">
<AvatarImage src={user?.user_metadata?.avatar_url} alt={user?.email || 'User'} />
<AvatarFallback className="text-lg">{getUserInitials()}</AvatarFallback>
</Avatar>
<div className="flex-1 min-w-0">
<h3 className="text-lg font-semibold mb-2 truncate">
{user?.user_metadata?.full_name || user?.email || user?.phone || 'کاربر'}
</h3>
<div className="space-y-2 text-sm text-muted-foreground">
{user?.email && (
<div className="flex items-center gap-2">
<Mail className="h-4 w-4 flex-shrink-0" />
<span className="truncate">{user.email}</span>
</div>
)}
{user?.phone && (
<div className="flex items-center gap-2">
<Phone className="h-4 w-4 flex-shrink-0" />
<span className="truncate">{user.phone}</span>
</div>
)}
</div>
</div>
</div>
<div className="border-t pt-6">
<div className="space-y-4">
<div className="flex items-center justify-between">
<div>
<div className="font-medium">شناسه کاربری</div>
<div className="text-sm text-muted-foreground font-mono">
{user?.id?.substring(0, 8)}...
</div>
</div>
<UserIcon className="h-5 w-5 text-muted-foreground" />
</div>
{user?.created_at && (
<div className="flex items-center justify-between">
<div>
<div className="font-medium">تاریخ عضویت</div>
<div className="text-sm text-muted-foreground">
{new Date(user.created_at).toLocaleDateString('fa-IR')}
</div>
</div>
</div>
)}
</div>
</div>
<div className="border-t pt-6">
<Button
variant="destructive"
className="w-full"
onClick={handleSignOut}
>
<LogOut className="ml-2 h-4 w-4" />
خروج از حساب کاربری
</Button>
</div>
</CardContent>
</Card>
</div>
</div>
);
}
+349
View File
@@ -0,0 +1,349 @@
import { useState } from 'react';
import { useNavigate, Link } from 'react-router-dom';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { useAuth } from '@/hooks/useAuth';
import { useTheme } from '@/hooks/useTheme';
import { useToast } from '@/components/ui/use-toast';
import { Loader2, Mail, Phone, LogIn, Moon, Sun } from 'lucide-react';
export default function LoginPage() {
const navigate = useNavigate();
const { signIn, signInWithGoogle, signInWithPhone, verifyOTP } = useAuth();
const { toggleTheme, isDark } = useTheme();
const { toast } = useToast();
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [phone, setPhone] = useState('');
const [otp, setOtp] = useState('');
const [isLoading, setIsLoading] = useState(false);
const [otpSent, setOtpSent] = useState(false);
const [authMethod, setAuthMethod] = useState<'email' | 'phone' | 'google'>('email');
const handleEmailLogin = async (e: React.FormEvent) => {
e.preventDefault();
if (!email || !password) {
toast({
title: 'خطا',
description: 'لطفا ایمیل و رمز عبور را وارد کنید',
variant: 'destructive'
});
return;
}
setIsLoading(true);
try {
await signIn(email, password);
toast({
title: 'موفق',
description: 'با موفقیت وارد شدید'
});
navigate('/account');
} catch (error: any) {
let errorMessage = 'خطا در ورود';
if (error.message) {
if (error.message.includes('Invalid login credentials')) {
errorMessage = 'ایمیل یا رمز عبور اشتباه است. لطفا دوباره تلاش کنید یا ثبت نام کنید.';
} else if (error.message.includes('Email not confirmed')) {
errorMessage = 'لطفا ابتدا ایمیل خود را تأیید کنید.';
} else {
errorMessage = error.message;
}
}
toast({
title: 'خطا',
description: errorMessage,
variant: 'destructive'
});
} finally {
setIsLoading(false);
}
};
const handleGoogleLogin = async () => {
setIsLoading(true);
try {
await signInWithGoogle();
// Redirect happens automatically
} catch (error: any) {
toast({
title: 'خطا',
description: error.message || 'خطا در ورود با گوگل',
variant: 'destructive'
});
setIsLoading(false);
}
};
const handleSendOTP = async (e: React.FormEvent) => {
e.preventDefault();
if (!phone) {
toast({
title: 'خطا',
description: 'لطفا شماره تلفن را وارد کنید',
variant: 'destructive'
});
return;
}
setIsLoading(true);
try {
await signInWithPhone(phone);
setOtpSent(true);
toast({
title: 'موفق',
description: 'کد تأیید ارسال شد'
});
} catch (error: any) {
toast({
title: 'خطا',
description: error.message || 'خطا در ارسال کد',
variant: 'destructive'
});
} finally {
setIsLoading(false);
}
};
const handleVerifyOTP = async (e: React.FormEvent) => {
e.preventDefault();
if (!otp) {
toast({
title: 'خطا',
description: 'لطفا کد تأیید را وارد کنید',
variant: 'destructive'
});
return;
}
setIsLoading(true);
try {
await verifyOTP(phone, otp);
toast({
title: 'موفق',
description: 'با موفقیت وارد شدید'
});
navigate('/account');
} catch (error: any) {
toast({
title: 'خطا',
description: error.message || 'کد تأیید نامعتبر است',
variant: 'destructive'
});
} finally {
setIsLoading(false);
}
};
return (
<div className="min-h-screen flex items-center justify-center bg-gradient-to-br from-background to-muted p-4 relative">
{/* Dark Mode Toggle Button */}
<button
onClick={toggleTheme}
className="absolute top-4 left-4 p-2 rounded-lg bg-background/80 backdrop-blur-sm border border-input hover:bg-accent transition-colors"
aria-label={isDark ? 'تغییر به حالت روشن' : 'تغییر به حالت تاریک'}
>
{isDark ? (
<Sun className="h-5 w-5" />
) : (
<Moon className="h-5 w-5" />
)}
</button>
<Card className="w-full max-w-sm">
<CardHeader className="space-y-1">
<div className="flex justify-center mb-4">
<LogIn className="h-12 w-12 text-primary" />
</div>
<CardTitle className="text-2xl text-center">ورود</CardTitle>
<CardDescription className="text-center">
به حساب کاربری خود وارد شوید
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
{/* Auth Method Selector */}
<div className="flex gap-2">
<Button
type="button"
variant={authMethod === 'email' ? 'default' : 'outline'}
className="flex-1"
onClick={() => {
setAuthMethod('email');
setOtpSent(false);
}}
>
<Mail className="ml-2 h-4 w-4" />
ایمیل
</Button>
<Button
type="button"
variant={authMethod === 'phone' ? 'default' : 'outline'}
className="flex-1"
onClick={() => {
setAuthMethod('phone');
setOtpSent(false);
}}
>
<Phone className="ml-2 h-4 w-4" />
تلفن
</Button>
</div>
{/* Email Login */}
{authMethod === 'email' && (
<form onSubmit={handleEmailLogin} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="email">ایمیل</Label>
<Input
id="email"
type="email"
placeholder="example@email.com"
value={email}
onChange={(e) => setEmail(e.target.value)}
disabled={isLoading}
/>
</div>
<div className="space-y-2">
<Label htmlFor="password">رمز عبور</Label>
<Input
id="password"
type="password"
placeholder="••••••••"
value={password}
onChange={(e) => setPassword(e.target.value)}
disabled={isLoading}
/>
</div>
<Button type="submit" className="w-full" disabled={isLoading}>
{isLoading ? (
<>
<Loader2 className="ml-2 h-4 w-4 animate-spin" />
در حال ورود...
</>
) : (
'ورود'
)}
</Button>
</form>
)}
{/* Phone OTP Login */}
{authMethod === 'phone' && (
<>
{!otpSent ? (
<form onSubmit={handleSendOTP} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="phone">شماره تلفن</Label>
<Input
id="phone"
type="tel"
placeholder="09123456789"
value={phone}
onChange={(e) => setPhone(e.target.value)}
disabled={isLoading}
/>
</div>
<Button type="submit" className="w-full" disabled={isLoading}>
{isLoading ? (
<>
<Loader2 className="ml-2 h-4 w-4 animate-spin" />
در حال ارسال...
</>
) : (
'ارسال کد تأیید'
)}
</Button>
</form>
) : (
<form onSubmit={handleVerifyOTP} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="otp">کد تأیید</Label>
<Input
id="otp"
type="text"
placeholder="123456"
value={otp}
onChange={(e) => setOtp(e.target.value)}
disabled={isLoading}
/>
</div>
<Button type="submit" className="w-full" disabled={isLoading}>
{isLoading ? (
<>
<Loader2 className="ml-2 h-4 w-4 animate-spin" />
در حال تأیید...
</>
) : (
'تأیید کد'
)}
</Button>
<Button
type="button"
variant="ghost"
className="w-full"
onClick={() => setOtpSent(false)}
disabled={isLoading}
>
تغییر شماره
</Button>
</form>
)}
</>
)}
{/* Google Login */}
<div className="relative">
<div className="absolute inset-0 flex items-center">
<span className="w-full border-t" />
</div>
<div className="relative flex justify-center text-xs uppercase">
<span className="bg-card px-2 text-muted-foreground">یا</span>
</div>
</div>
<Button
type="button"
variant="outline"
className="w-full"
onClick={handleGoogleLogin}
disabled={isLoading}
>
<svg className="ml-2 h-4 w-4" viewBox="0 0 24 24">
<path
fill="currentColor"
d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z"
/>
<path
fill="currentColor"
d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"
/>
<path
fill="currentColor"
d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z"
/>
<path
fill="currentColor"
d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"
/>
</svg>
ورود با گوگل
</Button>
<div className="text-center text-sm">
حساب کاربری ندارید؟{' '}
<Link to="/signup" className="text-primary hover:underline">
ثبت نام
</Link>
</div>
</CardContent>
</Card>
</div>
);
}
+245
View File
@@ -0,0 +1,245 @@
import { useState } from 'react';
import { useNavigate, Link } from 'react-router-dom';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { useAuth } from '@/hooks/useAuth';
import { useTheme } from '@/hooks/useTheme';
import { useToast } from '@/components/ui/use-toast';
import { Loader2, UserPlus, Moon, Sun } from 'lucide-react';
export default function SignupPage() {
const navigate = useNavigate();
const { signUp, signInWithGoogle } = useAuth();
const { toggleTheme, isDark } = useTheme();
const { toast } = useToast();
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [confirmPassword, setConfirmPassword] = useState('');
const [isLoading, setIsLoading] = useState(false);
const handleSignup = async (e: React.FormEvent) => {
e.preventDefault();
if (!email || !password || !confirmPassword) {
toast({
title: 'خطا',
description: 'لطفا تمام فیلدها را پر کنید',
variant: 'destructive'
});
return;
}
if (password !== confirmPassword) {
toast({
title: 'خطا',
description: 'رمز عبور و تکرار آن مطابقت ندارند',
variant: 'destructive'
});
return;
}
if (password.length < 6) {
toast({
title: 'خطا',
description: 'رمز عبور باید حداقل 6 کاراکتر باشد',
variant: 'destructive'
});
return;
}
setIsLoading(true);
try {
const result = await signUp(email, password);
if (result.error) {
let errorMessage = 'خطا در ثبت نام';
if (result.error.message.includes('User already registered')) {
errorMessage = 'این ایمیل قبلاً ثبت شده است. لطفا وارد شوید.';
} else if (result.error.message.includes('Password')) {
errorMessage = 'رمز عبور باید حداقل 6 کاراکتر باشد.';
} else {
errorMessage = result.error.message;
}
toast({
title: 'خطا',
description: errorMessage,
variant: 'destructive'
});
} else if (result.data) {
// Check if email confirmation is required
if (result.data.user && !result.data.session) {
toast({
title: 'موفق',
description: 'حساب کاربری ایجاد شد. لطفا ایمیل خود را بررسی کنید و لینک تأیید را کلیک کنید.',
duration: 5000
});
navigate('/login');
} else if (result.data.session) {
toast({
title: 'موفق',
description: 'حساب کاربری ایجاد شد. در حال ورود...'
});
navigate('/account');
}
}
} catch (error: any) {
let errorMessage = 'خطا در ثبت نام';
if (error.message) {
if (error.message.includes('User already registered')) {
errorMessage = 'این ایمیل قبلاً ثبت شده است. لطفا وارد شوید.';
} else if (error.message.includes('Password')) {
errorMessage = 'رمز عبور باید حداقل 6 کاراکتر باشد.';
} else {
errorMessage = error.message;
}
}
toast({
title: 'خطا',
description: errorMessage,
variant: 'destructive'
});
} finally {
setIsLoading(false);
}
};
const handleGoogleSignup = async () => {
setIsLoading(true);
try {
await signInWithGoogle();
// Redirect happens automatically
} catch (error: any) {
toast({
title: 'خطا',
description: error.message || 'خطا در ثبت نام با گوگل',
variant: 'destructive'
});
setIsLoading(false);
}
};
return (
<div className="min-h-screen flex items-center justify-center bg-gradient-to-br from-background to-muted p-4 relative">
{/* Dark Mode Toggle Button */}
<button
onClick={toggleTheme}
className="absolute top-4 left-4 p-2 rounded-lg bg-background/80 backdrop-blur-sm border border-input hover:bg-accent transition-colors"
aria-label={isDark ? 'تغییر به حالت روشن' : 'تغییر به حالت تاریک'}
>
{isDark ? (
<Sun className="h-5 w-5" />
) : (
<Moon className="h-5 w-5" />
)}
</button>
<Card className="w-full max-w-sm">
<CardHeader className="space-y-1">
<div className="flex justify-center mb-4">
<UserPlus className="h-12 w-12 text-primary" />
</div>
<CardTitle className="text-2xl text-center">ثبت نام</CardTitle>
<CardDescription className="text-center">
حساب کاربری جدید ایجاد کنید
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
<form onSubmit={handleSignup} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="email">ایمیل</Label>
<Input
id="email"
type="email"
placeholder="example@email.com"
value={email}
onChange={(e) => setEmail(e.target.value)}
disabled={isLoading}
/>
</div>
<div className="space-y-2">
<Label htmlFor="password">رمز عبور</Label>
<Input
id="password"
type="password"
placeholder="••••••••"
value={password}
onChange={(e) => setPassword(e.target.value)}
disabled={isLoading}
/>
</div>
<div className="space-y-2">
<Label htmlFor="confirmPassword">تکرار رمز عبور</Label>
<Input
id="confirmPassword"
type="password"
placeholder="••••••••"
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
disabled={isLoading}
/>
</div>
<Button type="submit" className="w-full" disabled={isLoading}>
{isLoading ? (
<>
<Loader2 className="ml-2 h-4 w-4 animate-spin" />
در حال ثبت نام...
</>
) : (
'ثبت نام'
)}
</Button>
</form>
<div className="relative">
<div className="absolute inset-0 flex items-center">
<span className="w-full border-t" />
</div>
<div className="relative flex justify-center text-xs uppercase">
<span className="bg-card px-2 text-muted-foreground">یا</span>
</div>
</div>
<Button
type="button"
variant="outline"
className="w-full"
onClick={handleGoogleSignup}
disabled={isLoading}
>
<svg className="ml-2 h-4 w-4" viewBox="0 0 24 24">
<path
fill="currentColor"
d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z"
/>
<path
fill="currentColor"
d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"
/>
<path
fill="currentColor"
d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z"
/>
<path
fill="currentColor"
d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"
/>
</svg>
ثبت نام با گوگل
</Button>
<div className="text-center text-sm">
قبلا ثبت نام کرده‌اید؟{' '}
<Link to="/login" className="text-primary hover:underline">
ورود
</Link>
</div>
</CardContent>
</Card>
</div>
);
}
+167
View File
@@ -0,0 +1,167 @@
import { useState, useRef, useEffect } from 'react';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
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 { useToast } from '@/components/ui/use-toast';
import { Send, MessageSquare, Loader2 } from 'lucide-react';
export default function ChatPage() {
const { user } = useAuth();
const { messages, loading, sendMessage } = useChat();
const { toast } = useToast();
const [message, setMessage] = useState('');
const [isSending, setIsSending] = useState(false);
const messagesEndRef = useRef<HTMLDivElement>(null);
useEffect(() => {
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
}, [messages]);
const handleSend = async (e: React.FormEvent) => {
e.preventDefault();
if (!user) {
toast({
title: 'خطا',
description: 'لطفا ابتدا وارد حساب کاربری شوید',
variant: 'destructive'
});
return;
}
if (!message.trim()) {
return;
}
setIsSending(true);
try {
await sendMessage(message, user.id);
setMessage('');
} catch (error) {
toast({
title: 'خطا',
description: 'خطا در ارسال پیام',
variant: 'destructive'
});
} finally {
setIsSending(false);
}
};
const getUserDisplayName = (msg: typeof messages[0]) => {
if (msg.user_id === user?.id) return 'شما';
// For other users, show a simplified identifier
// In production, you'd fetch from a profiles table
if (msg.user?.full_name) return msg.user.full_name;
if (msg.user?.email) return msg.user.email;
if (msg.user?.phone) return msg.user.phone;
return `کاربر ${msg.user_id.substring(0, 8)}`;
};
const getUserInitials = (msg: typeof messages[0]) => {
const name = getUserDisplayName(msg);
if (name === 'شما') return 'ش';
if (name.length >= 2) return name.substring(0, 2);
return 'ک';
};
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">
<div className="mb-4">
<h1 className="text-2xl font-bold mb-2">چت</h1>
<p className="text-sm text-muted-foreground">ارسال پیام و ارتباط با دیگران</p>
</div>
<Card className="flex flex-col h-[calc(100vh-14rem)]">
<CardHeader>
<div className="flex items-center gap-2">
<MessageSquare className="h-6 w-6 text-primary" />
<CardTitle className="text-lg">پیام‌ها</CardTitle>
</div>
<CardDescription className="text-sm">
{loading ? 'در حال بارگذاری...' : `${messages.length} پیام`}
</CardDescription>
</CardHeader>
<CardContent className="flex-1 flex flex-col overflow-hidden">
{loading ? (
<div className="flex items-center justify-center flex-1">
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
</div>
) : (
<>
<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>
) : (
messages.map((msg) => {
const isOwnMessage = msg.user_id === user?.id;
return (
<div
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>
);
})
)}
<div ref={messagesEndRef} />
</div>
<form onSubmit={handleSend} className="flex gap-2">
<Input
placeholder="پیام خود را بنویسید..."
value={message}
onChange={(e) => setMessage(e.target.value)}
disabled={isSending || !user}
/>
<Button type="submit" disabled={isSending || !user || !message.trim()}>
{isSending ? (
<Loader2 className="h-4 w-4 animate-spin" />
) : (
<Send className="h-4 w-4" />
)}
</Button>
</form>
</>
)}
</CardContent>
</Card>
</div>
</div>
);
}
+96
View File
@@ -0,0 +1,96 @@
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { useAuth } from '@/hooks/useAuth';
import { useNavigate } from 'react-router-dom';
import { Home, User, MessageSquare, FileText } from 'lucide-react';
export default function MainPage() {
const { user, isAuthenticated } = useAuth();
const navigate = useNavigate();
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">
<div className="mb-6 text-center">
<h1 className="text-2xl font-bold mb-2">خوش آمدید</h1>
<p className="text-sm text-muted-foreground">
{isAuthenticated && user
? `سلام ${user.email || user.phone || 'کاربر'}`
: 'به اپلیکیشن پیشرفته PWA خوش آمدید'}
</p>
</div>
<div className="grid gap-4 grid-cols-1">
<Card className="cursor-pointer transition-all hover:shadow-lg" onClick={() => navigate('/account')}>
<CardHeader>
<User className="mb-2 h-7 w-7 text-primary" />
<CardTitle className="text-lg">حساب کاربری</CardTitle>
<CardDescription className="text-sm">مدیریت پروفایل و تنظیمات</CardDescription>
</CardHeader>
<CardContent>
<Button variant="outline" className="w-full">
مشاهده
</Button>
</CardContent>
</Card>
<Card className="cursor-pointer transition-all hover:shadow-lg" onClick={() => navigate('/request')}>
<CardHeader>
<FileText className="mb-2 h-7 w-7 text-primary" />
<CardTitle className="text-lg">درخواست‌ها</CardTitle>
<CardDescription className="text-sm">ایجاد و مدیریت درخواست‌ها</CardDescription>
</CardHeader>
<CardContent>
<Button variant="outline" className="w-full">
مشاهده
</Button>
</CardContent>
</Card>
<Card className="cursor-pointer transition-all hover:shadow-lg" onClick={() => navigate('/chat')}>
<CardHeader>
<MessageSquare className="mb-2 h-7 w-7 text-primary" />
<CardTitle className="text-lg">چت</CardTitle>
<CardDescription className="text-sm">ارسال پیام و ارتباط</CardDescription>
</CardHeader>
<CardContent>
<Button variant="outline" className="w-full">
مشاهده
</Button>
</CardContent>
</Card>
</div>
<Card className="mt-6">
<CardHeader>
<Home className="mb-2 h-7 w-7 text-primary" />
<CardTitle className="text-lg">داشبورد اصلی</CardTitle>
<CardDescription className="text-sm">نمای کلی از عملکرد سیستم</CardDescription>
</CardHeader>
<CardContent>
<div className="grid gap-3 grid-cols-1">
<div className="rounded-lg border p-4">
<div className="text-xl font-bold">0</div>
<div className="text-xs text-muted-foreground">درخواست‌های فعال</div>
</div>
<div className="rounded-lg border p-4">
<div className="text-xl font-bold">0</div>
<div className="text-xs text-muted-foreground">پیام‌های جدید</div>
</div>
<div className="rounded-lg border p-4">
<div className="text-xl font-bold">
{isAuthenticated ? '✓' : '✗'}
</div>
<div className="text-xs text-muted-foreground">وضعیت ورود</div>
</div>
</div>
</CardContent>
</Card>
</div>
</div>
);
}
+186
View File
@@ -0,0 +1,186 @@
import { useState } from 'react';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { useAuth } from '@/hooks/useAuth';
import { useRequests } from '@/hooks/useRequests';
import { useToast } from '@/components/ui/use-toast';
import { Plus, FileText, Loader2 } from 'lucide-react';
export default function RequestPage() {
const { user } = useAuth();
const { requests, loading, createRequest } = useRequests();
const { toast } = useToast();
const [title, setTitle] = useState('');
const [description, setDescription] = useState('');
const [isSubmitting, setIsSubmitting] = useState(false);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (!user) {
toast({
title: 'خطا',
description: 'لطفا ابتدا وارد حساب کاربری شوید',
variant: 'destructive'
});
return;
}
if (!title.trim() || !description.trim()) {
toast({
title: 'خطا',
description: 'لطفا تمام فیلدها را پر کنید',
variant: 'destructive'
});
return;
}
setIsSubmitting(true);
try {
await createRequest(title, description, user.id);
toast({
title: 'موفق',
description: 'درخواست با موفقیت ایجاد شد'
});
setTitle('');
setDescription('');
} catch (error) {
toast({
title: 'خطا',
description: 'خطا در ایجاد درخواست',
variant: 'destructive'
});
} finally {
setIsSubmitting(false);
}
};
const getStatusBadge = (status: string) => {
const variants = {
pending: 'bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-200',
in_progress: 'bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-200',
completed: 'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200'
};
const labels = {
pending: 'در انتظار',
in_progress: 'در حال انجام',
completed: 'تکمیل شده'
};
return (
<span className={`px-2 py-1 rounded-full text-xs font-medium ${variants[status as keyof typeof variants] || ''}`}>
{labels[status as keyof typeof labels] || status}
</span>
);
};
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">
<div className="mb-4">
<h1 className="text-2xl font-bold mb-2">درخواست‌ها</h1>
<p className="text-sm text-muted-foreground">ایجاد و مدیریت درخواست‌های خود</p>
</div>
<div className="grid gap-4 grid-cols-1">
<Card>
<CardHeader>
<FileText className="mb-2 h-7 w-7 text-primary" />
<CardTitle className="text-lg">ایجاد درخواست جدید</CardTitle>
<CardDescription className="text-sm">فرم ایجاد درخواست جدید</CardDescription>
</CardHeader>
<CardContent>
<form onSubmit={handleSubmit} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="title">عنوان</Label>
<Input
id="title"
placeholder="عنوان درخواست"
value={title}
onChange={(e) => setTitle(e.target.value)}
disabled={isSubmitting}
/>
</div>
<div className="space-y-2">
<Label htmlFor="description">توضیحات</Label>
<textarea
id="description"
className="flex min-h-[120px] w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50"
placeholder="توضیحات درخواست"
value={description}
onChange={(e) => setDescription(e.target.value)}
disabled={isSubmitting}
/>
</div>
<Button type="submit" className="w-full" disabled={isSubmitting}>
{isSubmitting ? (
<>
<Loader2 className="ml-2 h-4 w-4 animate-spin" />
در حال ایجاد...
</>
) : (
<>
<Plus className="ml-2 h-4 w-4" />
ایجاد درخواست
</>
)}
</Button>
</form>
</CardContent>
</Card>
<Card>
<CardHeader>
<CardTitle className="text-lg">فهرست درخواست‌ها</CardTitle>
<CardDescription className="text-sm">
{loading ? 'در حال بارگذاری...' : `${requests.length} درخواست موجود است`}
</CardDescription>
</CardHeader>
<CardContent>
{loading ? (
<div className="flex items-center justify-center py-8">
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
</div>
) : requests.length === 0 ? (
<div className="text-center py-8 text-muted-foreground">
درخواستی وجود ندارد
</div>
) : (
<div className="space-y-3">
{requests.map((request) => (
<Card key={request.id} className="border">
<CardHeader>
<div className="flex items-start justify-between gap-2">
<CardTitle className="text-base flex-1">{request.title}</CardTitle>
{getStatusBadge(request.status)}
</div>
<CardDescription>
{new Date(request.created_at).toLocaleDateString('fa-IR', {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: '2-digit',
minute: '2-digit'
})}
</CardDescription>
</CardHeader>
<CardContent>
<p className="text-sm text-muted-foreground whitespace-pre-wrap">
{request.description}
</p>
</CardContent>
</Card>
))}
</div>
)}
</CardContent>
</Card>
</div>
</div>
</div>
);
}