| // src/components/collaboration/ShareDocumentModal.tsx | |
| import { useState, useEffect, useCallback } from "react"; | |
| import api from "@/lib/api"; | |
| import { toast } from "react-toastify"; | |
| import { | |
| X, Link, Copy, Users, Trash2, UserPlus, Globe, Lock, | |
| } from "lucide-react"; | |
| interface Props { | |
| isOpen: boolean; | |
| onClose: () => void; | |
| articleId: number; | |
| currentUserName: string; | |
| } | |
| export function ShareDocumentModal({ | |
| isOpen, | |
| onClose, | |
| articleId, | |
| currentUserName, | |
| }: Props) { | |
| const [inviteLink, setInviteLink] = useState(""); | |
| const [collaborators, setCollaborators] = useState<any[]>([]); | |
| const [searchQuery, setSearchQuery] = useState(""); | |
| const [searchResults, setSearchResults] = useState<any[]>([]); | |
| const [isPublic, setIsPublic] = useState(false); | |
| const [loading, setLoading] = useState(true); | |
| const fetchCollaborators = useCallback(async () => { | |
| try { | |
| const res = await api.get(`/articles/${articleId}/collaborators`); | |
| setCollaborators(res.data ?? []); | |
| } catch {} | |
| }, [articleId]); | |
| useEffect(() => { | |
| if (isOpen) { | |
| setLoading(true); | |
| Promise.all([ | |
| fetchCollaborators(), | |
| api.get(`/articles/${articleId}/share-link`).then(r => setInviteLink(r.data.link ?? "")), | |
| api.get(`/articles/${articleId}`).then(r => setIsPublic(r.data.isPublic ?? false)), | |
| ]).finally(() => setLoading(false)); | |
| } | |
| }, [isOpen, articleId, fetchCollaborators]); | |
| const handleSearch = async () => { | |
| if (!searchQuery.trim()) return; | |
| try { | |
| const res = await api.get(`/users/search?q=${encodeURIComponent(searchQuery)}`); | |
| const data = Array.isArray(res.data) ? res.data : res.data?.users || res.data?.result || []; | |
| setSearchResults(data); | |
| } catch { | |
| toast.error("Search failed"); | |
| } | |
| }; | |
| const handleAddCollaborator = async (username: string) => { | |
| try { | |
| await api.post(`/articles/${articleId}/collaborators`, { username }); | |
| toast.success(`${username} added as collaborator`); | |
| fetchCollaborators(); | |
| setSearchResults([]); | |
| setSearchQuery(""); | |
| } catch { | |
| toast.error("Failed to add collaborator"); | |
| } | |
| }; | |
| const handleRemoveCollaborator = async (username: string) => { | |
| try { | |
| await api.delete(`/articles/${articleId}/collaborators/${username}`); | |
| toast.success(`${username} removed`); | |
| fetchCollaborators(); | |
| } catch { | |
| toast.error("Failed to remove collaborator"); | |
| } | |
| }; | |
| const handleCopyLink = () => { | |
| navigator.clipboard.writeText(inviteLink); | |
| toast.success("Invite link copied!"); | |
| }; | |
| const handleGenerateLink = async () => { | |
| try { | |
| const res = await api.post(`/articles/${articleId}/share-link`); | |
| setInviteLink(res.data.link); | |
| toast.success("New invite link generated"); | |
| } catch { | |
| toast.error("Failed to generate link"); | |
| } | |
| }; | |
| const togglePublic = async () => { | |
| try { | |
| await api.put(`/articles/${articleId}`, { isPublic: !isPublic }); | |
| setIsPublic(!isPublic); | |
| toast.success(isPublic ? "Document is now private" : "Document is now public"); | |
| } catch { | |
| toast.error("Failed to update visibility"); | |
| } | |
| }; | |
| if (!isOpen) return null; | |
| return ( | |
| <div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4" onClick={onClose}> | |
| <div | |
| className="bg-white dark:bg-gray-800 rounded-2xl max-w-lg w-full p-6 shadow-xl max-h-[85vh] overflow-y-auto" | |
| onClick={(e) => e.stopPropagation()} | |
| > | |
| <div className="flex items-center justify-between mb-4"> | |
| <h2 className="text-xl font-bold flex items-center gap-2"> | |
| <Users className="w-5 h-5 text-primary" /> | |
| Share Document | |
| </h2> | |
| <button onClick={onClose} className="text-gray-500 hover:text-gray-700"> | |
| <X className="w-5 h-5" /> | |
| </button> | |
| </div> | |
| {/* Visibility toggle */} | |
| <div className="mb-4 p-3 bg-gray-50 dark:bg-gray-700/50 rounded-xl flex items-center justify-between"> | |
| <div className="flex items-center gap-2"> | |
| {isPublic ? <Globe className="w-4 h-4 text-green-500" /> : <Lock className="w-4 h-4 text-gray-400" />} | |
| <span className="text-sm font-medium">{isPublic ? "Public" : "Private"}</span> | |
| </div> | |
| <button | |
| onClick={togglePublic} | |
| className={`relative inline-flex h-6 w-11 items-center rounded-full transition ${ | |
| isPublic ? "bg-green-500" : "bg-gray-300 dark:bg-gray-600" | |
| }`} | |
| > | |
| <span className={`inline-block h-4 w-4 rounded-full bg-white transition-transform ${ | |
| isPublic ? "translate-x-6" : "translate-x-1" | |
| }`} /> | |
| </button> | |
| </div> | |
| {/* Invite link */} | |
| <div className="mb-4"> | |
| <h4 className="text-sm font-semibold mb-2 flex items-center gap-1"> | |
| <a className="w-4 h-4" /> Invite Link | |
| </h4> | |
| <div className="flex gap-2"> | |
| <input | |
| readOnly | |
| value={inviteLink} | |
| className="flex-1 text-sm p-2 border rounded-lg dark:bg-gray-700 bg-gray-50" | |
| /> | |
| <button onClick={handleCopyLink} className="btn btn-secondary btn-sm gap-1"> | |
| <Copy className="w-4 h-4" /> Copy | |
| </button> | |
| <button onClick={handleGenerateLink} className="btn btn-ghost btn-sm"> | |
| ↻ | |
| </button> | |
| </div> | |
| </div> | |
| {/* Current collaborators */} | |
| <div className="mb-4"> | |
| <h4 className="text-sm font-semibold mb-2"> | |
| Collaborators ({collaborators.length}) | |
| </h4> | |
| {collaborators.length === 0 ? ( | |
| <p className="text-sm text-gray-400">No collaborators yet</p> | |
| ) : ( | |
| <div className="space-y-2"> | |
| {collaborators.map((c) => ( | |
| <div key={c.username} className="flex items-center justify-between p-2 bg-gray-50 dark:bg-gray-700/50 rounded-lg"> | |
| <div className="flex items-center gap-2"> | |
| <div className="w-8 h-8 rounded-full bg-primary/20 flex items-center justify-center text-primary font-bold text-xs"> | |
| {c.username[0]?.toUpperCase()} | |
| </div> | |
| <span className="text-sm font-medium">{c.username}</span> | |
| </div> | |
| <button onClick={() => handleRemoveCollaborator(c.username)} className="text-red-500 hover:text-red-700 p-1"> | |
| <Trash2 className="w-4 h-4" /> | |
| </button> | |
| </div> | |
| ))} | |
| </div> | |
| )} | |
| </div> | |
| {/* Add collaborator */} | |
| <div> | |
| <h4 className="text-sm font-semibold mb-2 flex items-center gap-1"> | |
| <UserPlus className="w-4 h-4" /> Add Collaborator | |
| </h4> | |
| <div className="flex gap-2 mb-2"> | |
| <input | |
| type="text" | |
| placeholder="Search by username..." | |
| value={searchQuery} | |
| onChange={(e) => setSearchQuery(e.target.value)} | |
| onKeyDown={(e) => e.key === "Enter" && handleSearch()} | |
| className="flex-1 text-sm p-2 border rounded-lg dark:bg-gray-700" | |
| /> | |
| <button onClick={handleSearch} className="btn btn-secondary btn-sm"> | |
| Search | |
| </button> | |
| </div> | |
| {searchResults.length > 0 && ( | |
| <div className="space-y-1 max-h-32 overflow-y-auto"> | |
| {searchResults.map((user: any) => ( | |
| <div | |
| key={user.id} | |
| className="flex items-center justify-between p-2 hover:bg-gray-50 dark:hover:bg-gray-700 rounded-lg cursor-pointer" | |
| onClick={() => handleAddCollaborator(user.username)} | |
| > | |
| <div className="flex items-center gap-2"> | |
| <div className="w-7 h-7 rounded-full bg-primary/20 flex items-center justify-center text-primary font-bold text-xs"> | |
| {(user.username[0] ?? "").toUpperCase()} | |
| </div> | |
| <span className="text-sm">{user.username}</span> | |
| </div> | |
| <span className="text-xs text-primary">+ Add</span> | |
| </div> | |
| ))} | |
| </div> | |
| )} | |
| </div> | |
| </div> | |
| </div> | |
| ); | |
| } |
Xet Storage Details
- Size:
- 8.47 kB
- Xet hash:
- 97a7a093b7f00289904ef603316ef01a3dfba5ad7117e67946b7de02cd8c581c
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.