feat: create sharing UI components (task 13)
- Add PermissionBadge component to display owner/editor/viewer roles - Add InvitationCard component for accepting/declining invitations - Add CollaboratorList component to manage list collaborators - Add ShareListModal component for inviting users and viewing access - Add invitations page to view and manage pending invitations Requirements: 5.1, 5.4, 8.1, 8.2
This commit is contained in:
155
components/CollaboratorList.tsx
Normal file
155
components/CollaboratorList.tsx
Normal file
@@ -0,0 +1,155 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import { UserMinus, ChevronDown } from 'lucide-react'
|
||||
import PermissionBadge from './PermissionBadge'
|
||||
import { updateCollaboratorPermission, removeCollaborator } from '@/lib/sharing-actions'
|
||||
import { useRouter } from 'next/navigation'
|
||||
|
||||
interface Collaborator {
|
||||
id: string
|
||||
permission: 'editor' | 'viewer'
|
||||
user: {
|
||||
id: string
|
||||
name: string | null
|
||||
email: string
|
||||
}
|
||||
}
|
||||
|
||||
interface CollaboratorListProps {
|
||||
listId: string
|
||||
collaborators: Collaborator[]
|
||||
isOwner: boolean
|
||||
ownerId: string
|
||||
ownerName: string | null
|
||||
ownerEmail: string
|
||||
}
|
||||
|
||||
export default function CollaboratorList({
|
||||
listId,
|
||||
collaborators,
|
||||
isOwner,
|
||||
ownerId,
|
||||
ownerName,
|
||||
ownerEmail,
|
||||
}: CollaboratorListProps) {
|
||||
const [loadingStates, setLoadingStates] = useState<Record<string, boolean>>({})
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const router = useRouter()
|
||||
|
||||
const handlePermissionChange = async (collaboratorId: string, newPermission: 'editor' | 'viewer') => {
|
||||
setLoadingStates(prev => ({ ...prev, [collaboratorId]: true }))
|
||||
setError(null)
|
||||
|
||||
try {
|
||||
await updateCollaboratorPermission(listId, collaboratorId, newPermission)
|
||||
router.refresh()
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Failed to update permission')
|
||||
} finally {
|
||||
setLoadingStates(prev => ({ ...prev, [collaboratorId]: false }))
|
||||
}
|
||||
}
|
||||
|
||||
const handleRemove = async (collaboratorId: string) => {
|
||||
if (!confirm('Are you sure you want to remove this collaborator?')) {
|
||||
return
|
||||
}
|
||||
|
||||
setLoadingStates(prev => ({ ...prev, [collaboratorId]: true }))
|
||||
setError(null)
|
||||
|
||||
try {
|
||||
await removeCollaborator(listId, collaboratorId)
|
||||
router.refresh()
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Failed to remove collaborator')
|
||||
setLoadingStates(prev => ({ ...prev, [collaboratorId]: false }))
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
{error && (
|
||||
<div className="p-3 bg-red-50 border border-red-200 rounded-lg text-sm text-red-700">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Owner */}
|
||||
<div className="flex items-center justify-between p-3 bg-purple-50 border border-purple-100 rounded-lg">
|
||||
<div className="flex-1 min-w-0">
|
||||
<p className="font-medium text-gray-900 truncate">
|
||||
{ownerName || ownerEmail}
|
||||
</p>
|
||||
{ownerName && (
|
||||
<p className="text-sm text-gray-600 truncate">{ownerEmail}</p>
|
||||
)}
|
||||
</div>
|
||||
<PermissionBadge permission="owner" size="sm" />
|
||||
</div>
|
||||
|
||||
{/* Collaborators */}
|
||||
{collaborators.length === 0 ? (
|
||||
<p className="text-sm text-gray-500 text-center py-4">
|
||||
No collaborators yet. Share this list to collaborate!
|
||||
</p>
|
||||
) : (
|
||||
collaborators.map((collaborator) => (
|
||||
<div
|
||||
key={collaborator.id}
|
||||
className="flex items-center justify-between gap-3 p-3 bg-white border border-gray-200 rounded-lg"
|
||||
>
|
||||
<div className="flex-1 min-w-0">
|
||||
<p className="font-medium text-gray-900 truncate">
|
||||
{collaborator.user.name || collaborator.user.email}
|
||||
</p>
|
||||
{collaborator.user.name && (
|
||||
<p className="text-sm text-gray-600 truncate">
|
||||
{collaborator.user.email}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-2">
|
||||
{isOwner ? (
|
||||
<>
|
||||
<div className="relative">
|
||||
<select
|
||||
value={collaborator.permission}
|
||||
onChange={(e) =>
|
||||
handlePermissionChange(
|
||||
collaborator.id,
|
||||
e.target.value as 'editor' | 'viewer'
|
||||
)
|
||||
}
|
||||
disabled={loadingStates[collaborator.id]}
|
||||
className="appearance-none bg-white border border-gray-300 rounded-lg pl-3 pr-8 py-1.5 text-sm font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-blue-500 disabled:bg-gray-100 disabled:cursor-not-allowed cursor-pointer"
|
||||
>
|
||||
<option value="editor">Editor</option>
|
||||
<option value="viewer">Viewer</option>
|
||||
</select>
|
||||
<ChevronDown
|
||||
size={14}
|
||||
className="absolute right-2 top-1/2 -translate-y-1/2 text-gray-500 pointer-events-none"
|
||||
/>
|
||||
</div>
|
||||
<button
|
||||
onClick={() => handleRemove(collaborator.id)}
|
||||
disabled={loadingStates[collaborator.id]}
|
||||
className="p-2 text-red-600 hover:bg-red-50 active:bg-red-100 rounded-lg transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
aria-label="Remove collaborator"
|
||||
>
|
||||
<UserMinus size={18} />
|
||||
</button>
|
||||
</>
|
||||
) : (
|
||||
<PermissionBadge permission={collaborator.permission} size="sm" />
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
))
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
124
components/InvitationCard.tsx
Normal file
124
components/InvitationCard.tsx
Normal file
@@ -0,0 +1,124 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import { Check, X, Clock } from 'lucide-react'
|
||||
import PermissionBadge from './PermissionBadge'
|
||||
import { acceptInvitation, declineInvitation } from '@/lib/sharing-actions'
|
||||
import { useRouter } from 'next/navigation'
|
||||
|
||||
interface InvitationCardProps {
|
||||
invitation: {
|
||||
id: string
|
||||
permission: 'editor' | 'viewer'
|
||||
createdAt: Date
|
||||
list: {
|
||||
id: string
|
||||
title: string
|
||||
color: string
|
||||
}
|
||||
inviter: {
|
||||
name: string | null
|
||||
email: string
|
||||
}
|
||||
}
|
||||
onUpdate?: () => void
|
||||
}
|
||||
|
||||
export default function InvitationCard({ invitation, onUpdate }: InvitationCardProps) {
|
||||
const [isAccepting, setIsAccepting] = useState(false)
|
||||
const [isDeclining, setIsDeclining] = useState(false)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const router = useRouter()
|
||||
|
||||
const handleAccept = async () => {
|
||||
setIsAccepting(true)
|
||||
setError(null)
|
||||
|
||||
try {
|
||||
await acceptInvitation(invitation.id)
|
||||
onUpdate?.()
|
||||
router.refresh()
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Failed to accept invitation')
|
||||
setIsAccepting(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleDecline = async () => {
|
||||
setIsDeclining(true)
|
||||
setError(null)
|
||||
|
||||
try {
|
||||
await declineInvitation(invitation.id)
|
||||
onUpdate?.()
|
||||
router.refresh()
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Failed to decline invitation')
|
||||
setIsDeclining(false)
|
||||
}
|
||||
}
|
||||
|
||||
const formatDate = (date: Date) => {
|
||||
const now = new Date()
|
||||
const diffMs = now.getTime() - new Date(date).getTime()
|
||||
const diffMins = Math.floor(diffMs / 60000)
|
||||
const diffHours = Math.floor(diffMs / 3600000)
|
||||
const diffDays = Math.floor(diffMs / 86400000)
|
||||
|
||||
if (diffMins < 1) return 'Just now'
|
||||
if (diffMins < 60) return `${diffMins}m ago`
|
||||
if (diffHours < 24) return `${diffHours}h ago`
|
||||
if (diffDays < 7) return `${diffDays}d ago`
|
||||
return new Date(date).toLocaleDateString()
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="bg-white rounded-lg border border-gray-200 p-4 sm:p-5 shadow-sm hover:shadow-md transition-shadow">
|
||||
<div className="flex items-start gap-3 mb-3">
|
||||
<div
|
||||
className="w-3 h-3 rounded-full flex-shrink-0 mt-1"
|
||||
style={{ backgroundColor: invitation.list.color }}
|
||||
/>
|
||||
<div className="flex-1 min-w-0">
|
||||
<h3 className="font-semibold text-gray-900 text-base sm:text-lg truncate">
|
||||
{invitation.list.title}
|
||||
</h3>
|
||||
<p className="text-sm text-gray-600 mt-1">
|
||||
Invited by {invitation.inviter.name || invitation.inviter.email}
|
||||
</p>
|
||||
</div>
|
||||
<PermissionBadge permission={invitation.permission} size="sm" />
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-2 text-xs text-gray-500 mb-4">
|
||||
<Clock size={12} />
|
||||
<span>{formatDate(invitation.createdAt)}</span>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div className="mb-3 p-2 bg-red-50 border border-red-200 rounded text-sm text-red-700">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="flex gap-2">
|
||||
<button
|
||||
onClick={handleAccept}
|
||||
disabled={isAccepting || isDeclining}
|
||||
className="flex-1 flex items-center justify-center gap-2 px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 active:bg-blue-800 disabled:bg-gray-300 disabled:cursor-not-allowed transition-colors text-sm font-medium"
|
||||
>
|
||||
<Check size={16} />
|
||||
{isAccepting ? 'Accepting...' : 'Accept'}
|
||||
</button>
|
||||
<button
|
||||
onClick={handleDecline}
|
||||
disabled={isAccepting || isDeclining}
|
||||
className="flex-1 flex items-center justify-center gap-2 px-4 py-2 bg-white text-gray-700 border border-gray-300 rounded-lg hover:bg-gray-50 active:bg-gray-100 disabled:bg-gray-100 disabled:cursor-not-allowed transition-colors text-sm font-medium"
|
||||
>
|
||||
<X size={16} />
|
||||
{isDeclining ? 'Declining...' : 'Decline'}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
43
components/PermissionBadge.tsx
Normal file
43
components/PermissionBadge.tsx
Normal file
@@ -0,0 +1,43 @@
|
||||
'use client'
|
||||
|
||||
import { Shield, Eye, Edit3 } from 'lucide-react'
|
||||
|
||||
type Permission = 'owner' | 'editor' | 'viewer'
|
||||
|
||||
interface PermissionBadgeProps {
|
||||
permission: Permission
|
||||
size?: 'sm' | 'md'
|
||||
}
|
||||
|
||||
export default function PermissionBadge({ permission, size = 'md' }: PermissionBadgeProps) {
|
||||
const config = {
|
||||
owner: {
|
||||
icon: Shield,
|
||||
label: 'Owner',
|
||||
className: 'bg-purple-100 text-purple-700 border-purple-200',
|
||||
},
|
||||
editor: {
|
||||
icon: Edit3,
|
||||
label: 'Editor',
|
||||
className: 'bg-blue-100 text-blue-700 border-blue-200',
|
||||
},
|
||||
viewer: {
|
||||
icon: Eye,
|
||||
label: 'Viewer',
|
||||
className: 'bg-gray-100 text-gray-700 border-gray-200',
|
||||
},
|
||||
}
|
||||
|
||||
const { icon: Icon, label, className } = config[permission]
|
||||
const sizeClasses = size === 'sm' ? 'text-xs px-2 py-1' : 'text-sm px-3 py-1.5'
|
||||
const iconSize = size === 'sm' ? 12 : 14
|
||||
|
||||
return (
|
||||
<span
|
||||
className={`inline-flex items-center gap-1.5 rounded-full border font-medium ${className} ${sizeClasses}`}
|
||||
>
|
||||
<Icon size={iconSize} />
|
||||
{label}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
171
components/ShareListModal.tsx
Normal file
171
components/ShareListModal.tsx
Normal file
@@ -0,0 +1,171 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import { Users, Mail, Send } from 'lucide-react'
|
||||
import Modal from './Modal'
|
||||
import CollaboratorList from './CollaboratorList'
|
||||
import { inviteUserToList, getCollaborators } from '@/lib/sharing-actions'
|
||||
import { useRouter } from 'next/navigation'
|
||||
|
||||
interface Collaborator {
|
||||
id: string
|
||||
permission: 'editor' | 'viewer'
|
||||
user: {
|
||||
id: string
|
||||
name: string | null
|
||||
email: string
|
||||
}
|
||||
}
|
||||
|
||||
interface ShareListModalProps {
|
||||
isOpen: boolean
|
||||
onClose: () => void
|
||||
listId: string
|
||||
listTitle: string
|
||||
isOwner: boolean
|
||||
ownerId: string
|
||||
ownerName: string | null
|
||||
ownerEmail: string
|
||||
initialCollaborators?: Collaborator[]
|
||||
}
|
||||
|
||||
export default function ShareListModal({
|
||||
isOpen,
|
||||
onClose,
|
||||
listId,
|
||||
listTitle,
|
||||
isOwner,
|
||||
ownerId,
|
||||
ownerName,
|
||||
ownerEmail,
|
||||
initialCollaborators = [],
|
||||
}: ShareListModalProps) {
|
||||
const [email, setEmail] = useState('')
|
||||
const [permission, setPermission] = useState<'editor' | 'viewer'>('editor')
|
||||
const [isInviting, setIsInviting] = useState(false)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [success, setSuccess] = useState<string | null>(null)
|
||||
const [collaborators, setCollaborators] = useState<Collaborator[]>(initialCollaborators)
|
||||
const router = useRouter()
|
||||
|
||||
const handleInvite = async (e: React.FormEvent) => {
|
||||
e.preventDefault()
|
||||
setIsInviting(true)
|
||||
setError(null)
|
||||
setSuccess(null)
|
||||
|
||||
try {
|
||||
await inviteUserToList(listId, email, permission)
|
||||
setSuccess(`Invitation sent to ${email}`)
|
||||
setEmail('')
|
||||
setPermission('editor')
|
||||
|
||||
// Refresh collaborators list
|
||||
const updatedCollaborators = await getCollaborators(listId) as Collaborator[]
|
||||
setCollaborators(updatedCollaborators)
|
||||
router.refresh()
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Failed to send invitation')
|
||||
} finally {
|
||||
setIsInviting(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleClose = () => {
|
||||
setEmail('')
|
||||
setPermission('editor')
|
||||
setError(null)
|
||||
setSuccess(null)
|
||||
onClose()
|
||||
}
|
||||
|
||||
return (
|
||||
<Modal isOpen={isOpen} onClose={handleClose} title={`Share "${listTitle}"`}>
|
||||
<div className="space-y-6">
|
||||
{/* Invite Form - Only show if user is owner */}
|
||||
{isOwner && (
|
||||
<div>
|
||||
<div className="flex items-center gap-2 mb-3">
|
||||
<Mail size={18} className="text-gray-600" />
|
||||
<h3 className="font-medium text-gray-900">Invite Collaborator</h3>
|
||||
</div>
|
||||
|
||||
<form onSubmit={handleInvite} className="space-y-3">
|
||||
<div>
|
||||
<label htmlFor="email" className="block text-sm font-medium text-gray-700 mb-1">
|
||||
Email Address
|
||||
</label>
|
||||
<input
|
||||
type="email"
|
||||
id="email"
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
placeholder="colleague@example.com"
|
||||
required
|
||||
disabled={isInviting}
|
||||
className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 disabled:bg-gray-100 disabled:cursor-not-allowed"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label htmlFor="permission" className="block text-sm font-medium text-gray-700 mb-1">
|
||||
Permission Level
|
||||
</label>
|
||||
<select
|
||||
id="permission"
|
||||
value={permission}
|
||||
onChange={(e) => setPermission(e.target.value as 'editor' | 'viewer')}
|
||||
disabled={isInviting}
|
||||
className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 disabled:bg-gray-100 disabled:cursor-not-allowed"
|
||||
>
|
||||
<option value="editor">Editor - Can add, edit, and delete items</option>
|
||||
<option value="viewer">Viewer - Can only view items</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div className="p-3 bg-red-50 border border-red-200 rounded-lg text-sm text-red-700">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{success && (
|
||||
<div className="p-3 bg-green-50 border border-green-200 rounded-lg text-sm text-green-700">
|
||||
{success}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={isInviting || !email}
|
||||
className="w-full flex items-center justify-center gap-2 px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 active:bg-blue-800 disabled:bg-gray-300 disabled:cursor-not-allowed transition-colors font-medium"
|
||||
>
|
||||
<Send size={18} />
|
||||
{isInviting ? 'Sending...' : 'Send Invitation'}
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Collaborators List */}
|
||||
<div>
|
||||
<div className="flex items-center gap-2 mb-3">
|
||||
<Users size={18} className="text-gray-600" />
|
||||
<h3 className="font-medium text-gray-900">
|
||||
{isOwner ? 'People with Access' : 'Collaborators'}
|
||||
</h3>
|
||||
</div>
|
||||
|
||||
<CollaboratorList
|
||||
listId={listId}
|
||||
collaborators={collaborators}
|
||||
isOwner={isOwner}
|
||||
ownerId={ownerId}
|
||||
ownerName={ownerName}
|
||||
ownerEmail={ownerEmail}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</Modal>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user