tahamajs/IE / Projects /front /src /components /collaboration /ShareToMessagesModal.tsx
tahamajs's picture
download
raw
10.8 kB
// src/components/collaboration/ShareToMessagesModal.tsx
import { useState, useEffect, useMemo } from "react";
import { Search, Users, User, Send, X, MessageSquare, Loader2 } from "lucide-react";
import { motion, AnimatePresence } from "framer-motion";
import api from "@/lib/api";
import { toast } from "react-toastify";
interface ShareToMessagesModalProps {
isOpen: boolean;
onClose: () => void;
docId: string;
docTitle: string;
}
interface ChatTarget {
id: string | number;
name: string;
type: "private" | "group";
subtitle?: string;
}
export function ShareToMessagesModal({ isOpen, onClose, docId, docTitle }: ShareToMessagesModalProps) {
const [searchQuery, setSearchQuery] = useState("");
const [chats, setChats] = useState<ChatTarget[]>([]);
const [selectedTarget, setSelectedTarget] = useState<ChatTarget | null>(null);
const [accompanyingMessage, setAccompanyingMessage] = useState("Hey! Check out this paper I'm editing. You can join the collaborative workspace.");
const [loading, setLoading] = useState(false);
const [fetching, setFetching] = useState(false);
useEffect(() => {
if (!isOpen) {
setSelectedTarget(null);
setSearchQuery("");
return;
}
const fetchChats = async () => {
setFetching(true);
try {
const [convRes, groupsRes] = await Promise.all([
api.get("/messages/conversations").catch(() => ({ data: [] })),
api.get("/groups").catch(() => ({ data: [] })),
]);
const conversations = (convRes.data?.conversations || convRes.data?.result || convRes.data || []) as any[];
const groups = (groupsRes.data?.groups || groupsRes.data?.result || groupsRes.data || []) as any[];
const formattedConvs: ChatTarget[] = conversations.map((c: any) => ({
id: c.with,
name: c.with,
type: "private",
subtitle: "Direct Message",
}));
const formattedGroups: ChatTarget[] = groups.map((g: any) => ({
id: g.id,
name: g.name,
type: "group",
subtitle: `${g.memberCount || 0} members`,
}));
setChats([...formattedConvs, ...formattedGroups]);
} catch (err) {
console.error("Failed to fetch chats for sharing", err);
toast.error("Failed to load your chats");
} finally {
setFetching(false);
}
};
fetchChats();
}, [isOpen]);
const filteredChats = useMemo(() => {
if (!searchQuery.trim()) return chats;
const lower = searchQuery.toLowerCase();
return chats.filter(c => c.name.toLowerCase().includes(lower));
}, [chats, searchQuery]);
const handleShare = async () => {
if (!selectedTarget) return;
setLoading(true);
const link = `${window.location.origin}/editor/${docId}`;
const messageText = accompanyingMessage.trim()
? `${accompanyingMessage.trim()}\n\n๐Ÿ“„ **Paper**: [${docTitle || "Untitled Document"}](${link})`
: `๐Ÿ“„ **Paper Workspace**: [${docTitle || "Untitled Document"}](${link})`;
try {
if (selectedTarget.type === "private") {
await api.post(`/messages/conversations/${selectedTarget.id}`, {
body: messageText,
messageType: "text"
});
} else {
await api.post(`/groups/${selectedTarget.id}/messages`, {
body: messageText,
messageType: "text"
});
}
toast.success("Document shared successfully!");
onClose();
} catch (err) {
console.error("Failed to share document", err);
toast.error("Failed to send message. Please try again.");
} finally {
setLoading(false);
}
};
if (!isOpen) return null;
return (
<AnimatePresence>
<div className="fixed inset-0 z-50 flex items-center justify-center p-4">
{/* Backdrop */}
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
className="fixed inset-0 bg-black/60 backdrop-blur-sm"
onClick={onClose}
/>
{/* Modal Content */}
<motion.div
initial={{ opacity: 0, scale: 0.95, y: 20 }}
animate={{ opacity: 1, scale: 1, y: 0 }}
exit={{ opacity: 0, scale: 0.95, y: 20 }}
transition={{ type: "spring", duration: 0.4 }}
className="relative w-full max-w-lg overflow-hidden rounded-2xl bg-white dark:bg-gray-900 border border-gray-250 dark:border-gray-800 shadow-2xl p-6 flex flex-col max-h-[85vh] z-10"
>
{/* Header */}
<div className="flex items-center justify-between pb-4 border-b border-gray-150 dark:border-gray-800">
<h3 className="text-lg font-bold text-gray-900 dark:text-white flex items-center gap-2">
<MessageSquare className="w-5 h-5 text-indigo-500" /> Share in Messages
</h3>
<button
onClick={onClose}
className="p-1.5 rounded-full hover:bg-gray-100 dark:hover:bg-gray-800 text-gray-500 dark:text-gray-400 transition-colors"
>
<X className="w-5 h-5" />
</button>
</div>
{/* Search Box */}
<div className="relative my-4">
<Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-gray-400" />
<input
type="text"
placeholder="Search chats and groups..."
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
className="w-full pl-9 pr-4 py-2 text-sm rounded-xl border border-gray-200 dark:border-gray-800 bg-gray-50 dark:bg-gray-950 text-gray-950 dark:text-white placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-indigo-500 transition-all"
/>
</div>
{/* Chat List */}
<div className="flex-1 overflow-y-auto mb-4 min-h-[160px] max-h-[240px] pr-1 space-y-1.5 scrollbar-thin scrollbar-thumb-gray-200 dark:scrollbar-thumb-gray-800">
{fetching ? (
<div className="flex flex-col items-center justify-center py-8 text-gray-400 text-sm">
<Loader2 className="w-8 h-8 animate-spin mb-2 text-indigo-500" />
Loading your chats...
</div>
) : filteredChats.length === 0 ? (
<div className="text-center py-8 text-gray-500 dark:text-gray-400 text-sm">
No conversations or groups found.
</div>
) : (
filteredChats.map((chat) => {
const isSelected = selectedTarget?.id === chat.id && selectedTarget?.type === chat.type;
return (
<button
key={`${chat.type}-${chat.id}`}
onClick={() => setSelectedTarget(chat)}
className={`w-full flex items-center justify-between p-3 rounded-xl transition-all border text-left ${
isSelected
? "bg-indigo-500/10 border-indigo-500 text-indigo-600 dark:text-indigo-400"
: "bg-gray-50/50 hover:bg-gray-150/50 dark:bg-gray-950/20 dark:hover:bg-gray-950/60 border-transparent text-gray-800 dark:text-gray-200"
}`}
>
<div className="flex items-center gap-3">
<div className={`w-9 h-9 rounded-full flex items-center justify-center text-sm font-bold shadow-inner ${
chat.type === "group"
? "bg-indigo-100 text-indigo-600 dark:bg-indigo-950/50 dark:text-indigo-400"
: "bg-emerald-100 text-emerald-600 dark:bg-emerald-950/50 dark:text-emerald-400"
}`}>
{chat.type === "group" ? <Users className="w-4 h-4" /> : <User className="w-4 h-4" />}
</div>
<div>
<p className="text-sm font-semibold truncate max-w-[200px]">{chat.name}</p>
<p className="text-xs text-gray-500 dark:text-gray-400">{chat.subtitle}</p>
</div>
</div>
{isSelected && (
<div className="w-2 h-2 rounded-full bg-indigo-500 shadow-lg shadow-indigo-500/50" />
)}
</button>
);
})
)}
</div>
{/* Accompanying Message */}
<div className="mb-6 space-y-1.5">
<label className="text-xs font-bold text-gray-500 dark:text-gray-400 uppercase tracking-wide">
Accompanying Message
</label>
<textarea
value={accompanyingMessage}
onChange={(e) => setAccompanyingMessage(e.target.value)}
placeholder="Write a message to send with the paper link..."
rows={3}
className="w-full p-3 text-sm rounded-xl border border-gray-200 dark:border-gray-800 bg-gray-50 dark:bg-gray-950 text-gray-950 dark:text-white placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-indigo-500 resize-none transition-all"
/>
<div className="p-3 bg-gray-50 dark:bg-gray-950 rounded-xl border border-dashed border-gray-200 dark:border-gray-800 text-xs text-gray-500 dark:text-gray-400">
<span className="font-bold text-gray-700 dark:text-gray-300">Message Preview:</span>
<p className="mt-1 line-clamp-2 break-all italic">
{accompanyingMessage || "(No accompanying message)"}
<br />
๐Ÿ“„ **Paper**: [{docTitle || "Untitled Document"}]({window.location.origin}/editor/{docId})
</p>
</div>
</div>
{/* Action Buttons */}
<div className="flex gap-3 justify-end">
<button
onClick={onClose}
className="px-4 py-2 border border-gray-300 dark:border-gray-700 rounded-xl hover:bg-gray-50 dark:hover:bg-gray-800 text-sm font-semibold transition"
>
Cancel
</button>
<button
onClick={handleShare}
disabled={loading || !selectedTarget}
className="px-5 py-2 bg-indigo-600 hover:bg-indigo-500 disabled:opacity-50 text-white rounded-xl text-sm font-semibold flex items-center gap-2 transition"
>
{loading ? (
<>
<Loader2 className="w-4 h-4 animate-spin" /> Sharing...
</>
) : (
<>
<Send className="w-4 h-4" /> Send message
</>
)}
</button>
</div>
</motion.div>
</div>
</AnimatePresence>
);
}

Xet Storage Details

Size:
10.8 kB
ยท
Xet hash:
5f1208353a0a1f5ab9897fdc1b372f79962f693e75395287ac05a24feaee3b10

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.