import React, { useState } from 'react' import { Users, Trash2, UserPlus, Shield } from 'lucide-react' import { useStoryStore } from '../../store/storyStore' import { useAuthStore } from '../../store/authStore' import { useNotificationStore } from '../../store/notificationStore' import { useUIStore } from '../../store/uiStore' import { Collaboration, CollaboratorRole, PermissionLevel } from '../../types' import { Button } from '../ui/Button' import { Avatar } from '../ui/Avatar' import { Modal } from '../ui/Modal' interface CollaboratorManagerProps { storyId: number storyTitle: string ownerId: number } const roles: CollaboratorRole[] = ['co-author', 'editor', 'proofreader', 'beta-reader'] const permissionLabels: Record = { 1: 'View Only', 2: 'Comment', 3: 'Edit Chapters', 4: 'Manage Story', 5: 'Full Access', } export const CollaboratorManager: React.FC = ({ storyId, storyTitle, ownerId, }) => { const { collaborations, addCollaboration, removeCollaboration, updateCollaborationPermission } = useStoryStore() const { allUsers } = useAuthStore() const { addNotification } = useNotificationStore() const { addToast } = useUIStore() const [inviteOpen, setInviteOpen] = useState(false) const [selectedUser, setSelectedUser] = useState('') const [selectedRole, setSelectedRole] = useState('editor') const [selectedLevel, setSelectedLevel] = useState(3) const storyCollabs = collaborations.filter(c => c.story_id === storyId) const collabUserIds = new Set(storyCollabs.map(c => c.user_id)) const availableUsers = allUsers.filter(u => u.user_id !== ownerId && !collabUserIds.has(u.user_id)) const handleInvite = () => { const user = availableUsers.find(u => u.username === selectedUser) if (!user) { addToast('User not found', 'error'); return } const newCollab: Collaboration = { collab_id: Date.now(), story_id: storyId, story_title: storyTitle, user_id: user.user_id, username: user.username, name: user.name, role: selectedRole, permission_level: selectedLevel, joined_at: new Date().toISOString(), } addCollaboration(newCollab) addNotification({ user_id: user.user_id, type: 'collaboration', title: 'Collaboration Invite', message: `You've been invited to collaborate on "${storyTitle}" as ${selectedRole}.`, link: `/story/${storyId}`, }) addToast(`${user.username} added as collaborator!`) setInviteOpen(false) setSelectedUser('') } const handleRemove = (collab: Collaboration) => { removeCollaboration(collab.user_id, storyId) addToast(`${collab.username} removed from collaborators`, 'info') } return (

Collaborators

({storyCollabs.length})
{storyCollabs.length === 0 ? (
No collaborators yet
) : (
{storyCollabs.map(collab => (

@{collab.username}

{collab.role} · Level {collab.permission_level}: {permissionLabels[collab.permission_level]}
))}
)} {/* Invite Modal */} setInviteOpen(false)} title="Invite Collaborator">
) }