restored everything after nuking the repo
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
const AuthImagePattern = ({ title, subtitle }) => {
|
||||
return (
|
||||
<div className="hidden lg:flex items-center justify-center bg-base-200 p-12">
|
||||
<div className="max-w-md text-center">
|
||||
<div className="grid grid-cols-3 gap-3 mb-8">
|
||||
{[...Array(9)].map((_, i) => (
|
||||
<div
|
||||
key={i}
|
||||
className={`aspect-square rounded-2xl bg-primary/10 ${
|
||||
i % 2 === 0 ? "animate-pulse" : ""
|
||||
}`}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
<h2 className="text-2xl font-bold mb-4">{title}</h2>
|
||||
<p className="text-base-content/60">{subtitle}</p>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default AuthImagePattern;
|
||||
@@ -0,0 +1,92 @@
|
||||
import { useChatStore } from "../store/useChatStore";
|
||||
import { useEffect, useRef } from "react";
|
||||
|
||||
import ChatHeader from "./ChatHeader";
|
||||
import MessageInput from "./MessageInput";
|
||||
import MessageSkeleton from "./skeletons/MessageSkeleton";
|
||||
import { useAuthStore } from "../store/useAuthStore";
|
||||
import { formatMessageTime } from "../lib/utils";
|
||||
|
||||
const ChatContainer = () => {
|
||||
const {
|
||||
messages,
|
||||
getMessages,
|
||||
isMessagesLoading,
|
||||
selectedUser,
|
||||
subscribeToMessages,
|
||||
unsubscribeFromMessages,
|
||||
} = useChatStore();
|
||||
const { authUser } = useAuthStore();
|
||||
const messageEndRef = useRef(null);
|
||||
|
||||
useEffect(() => {
|
||||
getMessages(selectedUser._id);
|
||||
|
||||
subscribeToMessages();
|
||||
|
||||
return () => unsubscribeFromMessages();
|
||||
}, [selectedUser._id, getMessages, subscribeToMessages, unsubscribeFromMessages]);
|
||||
|
||||
useEffect(() => {
|
||||
if (messageEndRef.current && messages) {
|
||||
messageEndRef.current.scrollIntoView({ behavior: "smooth" });
|
||||
}
|
||||
}, [messages]);
|
||||
|
||||
if (isMessagesLoading) {
|
||||
return (
|
||||
<div className="flex-1 flex flex-col overflow-auto">
|
||||
<ChatHeader />
|
||||
<MessageSkeleton />
|
||||
<MessageInput />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex-1 flex flex-col overflow-auto">
|
||||
<ChatHeader />
|
||||
|
||||
<div className="flex-1 overflow-y-auto p-4 space-y-4">
|
||||
{messages.map((message) => (
|
||||
<div
|
||||
key={message._id}
|
||||
className={`chat ${message.senderId === authUser._id ? "chat-end" : "chat-start"}`}
|
||||
ref={messageEndRef}
|
||||
>
|
||||
<div className=" chat-image avatar">
|
||||
<div className="size-10 rounded-full border">
|
||||
<img
|
||||
src={
|
||||
message.senderId === authUser._id
|
||||
? authUser.profilePic || "/avatar.png"
|
||||
: selectedUser.profilePic || "/avatar.png"
|
||||
}
|
||||
alt="profile pic"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className="chat-header mb-1">
|
||||
<time className="text-xs opacity-50 ml-1">
|
||||
{formatMessageTime(message.createdAt)}
|
||||
</time>
|
||||
</div>
|
||||
<div className="chat-bubble flex flex-col">
|
||||
{message.image && (
|
||||
<img
|
||||
src={message.image}
|
||||
alt="Attachment"
|
||||
className="sm:max-w-[200px] rounded-md mb-2"
|
||||
/>
|
||||
)}
|
||||
{message.text && <p>{message.text}</p>}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<MessageInput />
|
||||
</div>
|
||||
);
|
||||
};
|
||||
export default ChatContainer;
|
||||
@@ -0,0 +1,37 @@
|
||||
import { X } from "lucide-react";
|
||||
import { useAuthStore } from "../store/useAuthStore";
|
||||
import { useChatStore } from "../store/useChatStore";
|
||||
|
||||
const ChatHeader = () => {
|
||||
const { selectedUser, setSelectedUser } = useChatStore();
|
||||
const { onlineUsers } = useAuthStore();
|
||||
|
||||
return (
|
||||
<div className="p-2.5 border-b border-base-300">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center gap-3">
|
||||
{/* Avatar */}
|
||||
<div className="avatar">
|
||||
<div className="size-10 rounded-full relative">
|
||||
<img src={selectedUser.profilePic || "/avatar.png"} alt={selectedUser.fullName} />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* User info */}
|
||||
<div>
|
||||
<h3 className="font-medium">{selectedUser.fullName}</h3>
|
||||
<p className="text-sm text-base-content/70">
|
||||
{onlineUsers.includes(selectedUser._id) ? "Online" : "Offline"}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Close button */}
|
||||
<button onClick={() => setSelectedUser(null)}>
|
||||
<X />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
export default ChatHeader;
|
||||
@@ -0,0 +1,109 @@
|
||||
import { useRef, useState } from "react";
|
||||
import { useChatStore } from "../store/useChatStore";
|
||||
import { Image, Send, X } from "lucide-react";
|
||||
import toast from "react-hot-toast";
|
||||
|
||||
const MessageInput = () => {
|
||||
const [text, setText] = useState("");
|
||||
const [imagePreview, setImagePreview] = useState(null);
|
||||
const fileInputRef = useRef(null);
|
||||
const { sendMessage } = useChatStore();
|
||||
|
||||
const handleImageChange = (e) => {
|
||||
const file = e.target.files[0];
|
||||
if (!file.type.startsWith("image/")) {
|
||||
toast.error("Please select an image file");
|
||||
return;
|
||||
}
|
||||
|
||||
const reader = new FileReader();
|
||||
reader.onloadend = () => {
|
||||
setImagePreview(reader.result);
|
||||
};
|
||||
reader.readAsDataURL(file);
|
||||
};
|
||||
|
||||
const removeImage = () => {
|
||||
setImagePreview(null);
|
||||
if (fileInputRef.current) fileInputRef.current.value = "";
|
||||
};
|
||||
|
||||
const handleSendMessage = async (e) => {
|
||||
e.preventDefault();
|
||||
if (!text.trim() && !imagePreview) return;
|
||||
|
||||
try {
|
||||
await sendMessage({
|
||||
text: text.trim(),
|
||||
image: imagePreview,
|
||||
});
|
||||
|
||||
// Clear form
|
||||
setText("");
|
||||
setImagePreview(null);
|
||||
if (fileInputRef.current) fileInputRef.current.value = "";
|
||||
} catch (error) {
|
||||
console.error("Failed to send message:", error);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="p-4 w-full">
|
||||
{imagePreview && (
|
||||
<div className="mb-3 flex items-center gap-2">
|
||||
<div className="relative">
|
||||
<img
|
||||
src={imagePreview}
|
||||
alt="Preview"
|
||||
className="w-20 h-20 object-cover rounded-lg border border-zinc-700"
|
||||
/>
|
||||
<button
|
||||
onClick={removeImage}
|
||||
className="absolute -top-1.5 -right-1.5 w-5 h-5 rounded-full bg-base-300
|
||||
flex items-center justify-center"
|
||||
type="button"
|
||||
>
|
||||
<X className="size-3" />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<form onSubmit={handleSendMessage} className="flex items-center gap-2">
|
||||
<div className="flex-1 flex gap-2">
|
||||
<input
|
||||
type="text"
|
||||
className="w-full input input-bordered rounded-lg input-sm sm:input-md"
|
||||
placeholder="Type a message..."
|
||||
value={text}
|
||||
onChange={(e) => setText(e.target.value)}
|
||||
/>
|
||||
<input
|
||||
type="file"
|
||||
accept="image/*"
|
||||
className="hidden"
|
||||
ref={fileInputRef}
|
||||
onChange={handleImageChange}
|
||||
/>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
className={`hidden sm:flex btn btn-circle
|
||||
${imagePreview ? "text-emerald-500" : "text-zinc-400"}`}
|
||||
onClick={() => fileInputRef.current?.click()}
|
||||
>
|
||||
<Image size={20} />
|
||||
</button>
|
||||
</div>
|
||||
<button
|
||||
type="submit"
|
||||
className="btn btn-sm btn-circle"
|
||||
disabled={!text.trim() && !imagePreview}
|
||||
>
|
||||
<Send size={22} />
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
export default MessageInput;
|
||||
@@ -0,0 +1,55 @@
|
||||
import { Link } from "react-router-dom";
|
||||
import { useAuthStore } from "../store/useAuthStore";
|
||||
import { LogOut, MessageSquare, Settings, User } from "lucide-react";
|
||||
|
||||
const Navbar = () => {
|
||||
const { logout, authUser } = useAuthStore();
|
||||
|
||||
return (
|
||||
<header
|
||||
className="bg-base-100 border-b border-base-300 fixed w-full top-0 z-40
|
||||
backdrop-blur-lg bg-base-100/80"
|
||||
>
|
||||
<div className="container mx-auto px-4 h-16">
|
||||
<div className="flex items-center justify-between h-full">
|
||||
<div className="flex items-center gap-8">
|
||||
<Link to="/" className="flex items-center gap-2.5 hover:opacity-80 transition-all">
|
||||
<div className="size-9 rounded-lg bg-primary/10 flex items-center justify-center">
|
||||
<MessageSquare className="w-5 h-5 text-primary" />
|
||||
</div>
|
||||
<h1 className="text-lg font-bold">Chatty</h1>
|
||||
</Link>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-2">
|
||||
<Link
|
||||
to={"/settings"}
|
||||
className={`
|
||||
btn btn-sm gap-2 transition-colors
|
||||
|
||||
`}
|
||||
>
|
||||
<Settings className="w-4 h-4" />
|
||||
<span className="hidden sm:inline">Settings</span>
|
||||
</Link>
|
||||
|
||||
{authUser && (
|
||||
<>
|
||||
<Link to={"/profile"} className={`btn btn-sm gap-2`}>
|
||||
<User className="size-5" />
|
||||
<span className="hidden sm:inline">Profile</span>
|
||||
</Link>
|
||||
|
||||
<button className="flex gap-2 items-center" onClick={logout}>
|
||||
<LogOut className="size-5" />
|
||||
<span className="hidden sm:inline">Logout</span>
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
);
|
||||
};
|
||||
export default Navbar;
|
||||
@@ -0,0 +1,29 @@
|
||||
import { MessageSquare } from "lucide-react";
|
||||
|
||||
const NoChatSelected = () => {
|
||||
return (
|
||||
<div className="w-full flex flex-1 flex-col items-center justify-center p-16 bg-base-100/50">
|
||||
<div className="max-w-md text-center space-y-6">
|
||||
{/* Icon Display */}
|
||||
<div className="flex justify-center gap-4 mb-4">
|
||||
<div className="relative">
|
||||
<div
|
||||
className="w-16 h-16 rounded-2xl bg-primary/10 flex items-center
|
||||
justify-center animate-bounce"
|
||||
>
|
||||
<MessageSquare className="w-8 h-8 text-primary " />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Welcome Text */}
|
||||
<h2 className="text-2xl font-bold">Welcome to Chatty!</h2>
|
||||
<p className="text-base-content/60">
|
||||
Select a conversation from the sidebar to start chatting
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default NoChatSelected;
|
||||
@@ -0,0 +1,87 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { useChatStore } from "../store/useChatStore";
|
||||
import { useAuthStore } from "../store/useAuthStore";
|
||||
import SidebarSkeleton from "./skeletons/SidebarSkeleton";
|
||||
import { Users } from "lucide-react";
|
||||
|
||||
const Sidebar = () => {
|
||||
const { getUsers, users, selectedUser, setSelectedUser, isUsersLoading } = useChatStore();
|
||||
|
||||
const { onlineUsers } = useAuthStore();
|
||||
const [showOnlineOnly, setShowOnlineOnly] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
getUsers();
|
||||
}, [getUsers]);
|
||||
|
||||
const filteredUsers = showOnlineOnly
|
||||
? users.filter((user) => onlineUsers.includes(user._id))
|
||||
: users;
|
||||
|
||||
if (isUsersLoading) return <SidebarSkeleton />;
|
||||
|
||||
return (
|
||||
<aside className="h-full w-20 lg:w-72 border-r border-base-300 flex flex-col transition-all duration-200">
|
||||
<div className="border-b border-base-300 w-full p-5">
|
||||
<div className="flex items-center gap-2">
|
||||
<Users className="size-6" />
|
||||
<span className="font-medium hidden lg:block">Contacts</span>
|
||||
</div>
|
||||
{/* TODO: Online filter toggle */}
|
||||
<div className="mt-3 hidden lg:flex items-center gap-2">
|
||||
<label className="cursor-pointer flex items-center gap-2">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={showOnlineOnly}
|
||||
onChange={(e) => setShowOnlineOnly(e.target.checked)}
|
||||
className="checkbox checkbox-sm"
|
||||
/>
|
||||
<span className="text-sm">Show online only</span>
|
||||
</label>
|
||||
<span className="text-xs text-zinc-500">({onlineUsers.length - 1} online)</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="overflow-y-auto w-full py-3">
|
||||
{filteredUsers.map((user) => (
|
||||
<button
|
||||
key={user._id}
|
||||
onClick={() => setSelectedUser(user)}
|
||||
className={`
|
||||
w-full p-3 flex items-center gap-3
|
||||
hover:bg-base-300 transition-colors
|
||||
${selectedUser?._id === user._id ? "bg-base-300 ring-1 ring-base-300" : ""}
|
||||
`}
|
||||
>
|
||||
<div className="relative mx-auto lg:mx-0">
|
||||
<img
|
||||
src={user.profilePic || "/avatar.png"}
|
||||
alt={user.name}
|
||||
className="size-12 object-cover rounded-full"
|
||||
/>
|
||||
{onlineUsers.includes(user._id) && (
|
||||
<span
|
||||
className="absolute bottom-0 right-0 size-3 bg-green-500
|
||||
rounded-full ring-2 ring-zinc-900"
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* User info - only visible on larger screens */}
|
||||
<div className="hidden lg:block text-left min-w-0">
|
||||
<div className="font-medium truncate">{user.fullName}</div>
|
||||
<div className="text-sm text-zinc-400">
|
||||
{onlineUsers.includes(user._id) ? "Online" : "Offline"}
|
||||
</div>
|
||||
</div>
|
||||
</button>
|
||||
))}
|
||||
|
||||
{filteredUsers.length === 0 && (
|
||||
<div className="text-center text-zinc-500 py-4">No online users</div>
|
||||
)}
|
||||
</div>
|
||||
</aside>
|
||||
);
|
||||
};
|
||||
export default Sidebar;
|
||||
@@ -0,0 +1,28 @@
|
||||
const MessageSkeleton = () => {
|
||||
// Create an array of 6 items for skeleton messages
|
||||
const skeletonMessages = Array(6).fill(null);
|
||||
|
||||
return (
|
||||
<div className="flex-1 overflow-y-auto p-4 space-y-4">
|
||||
{skeletonMessages.map((_, idx) => (
|
||||
<div key={idx} className={`chat ${idx % 2 === 0 ? "chat-start" : "chat-end"}`}>
|
||||
<div className="chat-image avatar">
|
||||
<div className="size-10 rounded-full">
|
||||
<div className="skeleton w-full h-full rounded-full" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="chat-header mb-1">
|
||||
<div className="skeleton h-4 w-16" />
|
||||
</div>
|
||||
|
||||
<div className="chat-bubble bg-transparent p-0">
|
||||
<div className="skeleton h-16 w-[200px]" />
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default MessageSkeleton;
|
||||
@@ -0,0 +1,41 @@
|
||||
import { Users } from "lucide-react";
|
||||
|
||||
const SidebarSkeleton = () => {
|
||||
// Create 8 skeleton items
|
||||
const skeletonContacts = Array(8).fill(null);
|
||||
|
||||
return (
|
||||
<aside
|
||||
className="h-full w-20 lg:w-72 border-r border-base-300
|
||||
flex flex-col transition-all duration-200"
|
||||
>
|
||||
{/* Header */}
|
||||
<div className="border-b border-base-300 w-full p-5">
|
||||
<div className="flex items-center gap-2">
|
||||
<Users className="w-6 h-6" />
|
||||
<span className="font-medium hidden lg:block">Contacts</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Skeleton Contacts */}
|
||||
<div className="overflow-y-auto w-full py-3">
|
||||
{skeletonContacts.map((_, idx) => (
|
||||
<div key={idx} className="w-full p-3 flex items-center gap-3">
|
||||
{/* Avatar skeleton */}
|
||||
<div className="relative mx-auto lg:mx-0">
|
||||
<div className="skeleton size-12 rounded-full" />
|
||||
</div>
|
||||
|
||||
{/* User info skeleton - only visible on larger screens */}
|
||||
<div className="hidden lg:block text-left min-w-0 flex-1">
|
||||
<div className="skeleton h-4 w-32 mb-2" />
|
||||
<div className="skeleton h-3 w-16" />
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</aside>
|
||||
);
|
||||
};
|
||||
|
||||
export default SidebarSkeleton;
|
||||
Reference in New Issue
Block a user