|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +import React, { createContext, useContext, useState, useEffect, useCallback, useMemo } from 'react'; |
| 10 | +import type { CollaborationConfig, CollaborationPresence, CollaborationOperation } from '@object-ui/types'; |
| 11 | + |
| 12 | +export interface CollaborationContextValue { |
| 13 | + /** Active users in the session */ |
| 14 | + users: CollaborationPresence[]; |
| 15 | + /** Whether collaboration is connected */ |
| 16 | + isConnected: boolean; |
| 17 | + /** Send an operation to collaborators */ |
| 18 | + sendOperation: (operation: Omit<CollaborationOperation, 'id' | 'timestamp' | 'version'>) => void; |
| 19 | + /** Current user ID */ |
| 20 | + currentUserId?: string; |
| 21 | +} |
| 22 | + |
| 23 | +const CollabCtx = createContext<CollaborationContextValue | null>(null); |
| 24 | +CollabCtx.displayName = 'CollaborationContext'; |
| 25 | + |
| 26 | +export interface CollaborationProviderProps { |
| 27 | + /** Collaboration configuration */ |
| 28 | + config: CollaborationConfig; |
| 29 | + /** Current user info */ |
| 30 | + user?: { |
| 31 | + id: string; |
| 32 | + name: string; |
| 33 | + avatar?: string; |
| 34 | + }; |
| 35 | + /** Callback when an operation is received from another user */ |
| 36 | + onOperation?: (operation: CollaborationOperation) => void; |
| 37 | + /** Children */ |
| 38 | + children: React.ReactNode; |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Provider for multi-user collaborative editing. |
| 43 | + * Manages WebSocket connection, presence, and operation broadcasting. |
| 44 | + */ |
| 45 | +export function CollaborationProvider({ |
| 46 | + config, |
| 47 | + user, |
| 48 | + onOperation, |
| 49 | + children, |
| 50 | +}: CollaborationProviderProps) { |
| 51 | + const [users, setUsers] = useState<CollaborationPresence[]>([]); |
| 52 | + const [isConnected, setIsConnected] = useState(false); |
| 53 | + |
| 54 | + // Add current user to presence list |
| 55 | + useEffect(() => { |
| 56 | + if (!config.enabled || !user) return; |
| 57 | + |
| 58 | + const currentUser: CollaborationPresence = { |
| 59 | + userId: user.id, |
| 60 | + userName: user.name, |
| 61 | + avatar: user.avatar, |
| 62 | + color: generateColor(user.id), |
| 63 | + status: 'active', |
| 64 | + lastActivity: new Date().toISOString(), |
| 65 | + }; |
| 66 | + |
| 67 | + setUsers([currentUser]); |
| 68 | + setIsConnected(true); |
| 69 | + |
| 70 | + return () => { |
| 71 | + setIsConnected(false); |
| 72 | + setUsers([]); |
| 73 | + }; |
| 74 | + }, [config.enabled, user]); |
| 75 | + |
| 76 | + const sendOperation = useCallback( |
| 77 | + (operation: Omit<CollaborationOperation, 'id' | 'timestamp' | 'version'>) => { |
| 78 | + if (!isConnected || !user) return; |
| 79 | + |
| 80 | + const fullOp: CollaborationOperation = { |
| 81 | + ...operation, |
| 82 | + id: `op-${Date.now()}`, |
| 83 | + userId: user.id, |
| 84 | + timestamp: new Date().toISOString(), |
| 85 | + version: Date.now(), |
| 86 | + }; |
| 87 | + |
| 88 | + // In a real implementation, this would send via WebSocket |
| 89 | + onOperation?.(fullOp); |
| 90 | + }, |
| 91 | + [isConnected, user, onOperation], |
| 92 | + ); |
| 93 | + |
| 94 | + const value = useMemo<CollaborationContextValue>( |
| 95 | + () => ({ |
| 96 | + users, |
| 97 | + isConnected, |
| 98 | + sendOperation, |
| 99 | + currentUserId: user?.id, |
| 100 | + }), |
| 101 | + [users, isConnected, sendOperation, user?.id], |
| 102 | + ); |
| 103 | + |
| 104 | + return <CollabCtx.Provider value={value}>{children}</CollabCtx.Provider>; |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Hook to access the collaboration context. |
| 109 | + */ |
| 110 | +export function useCollaboration(): CollaborationContextValue | null { |
| 111 | + return useContext(CollabCtx); |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Generate a consistent color for a user ID. |
| 116 | + */ |
| 117 | +function generateColor(userId: string): string { |
| 118 | + const colors = [ |
| 119 | + '#3b82f6', '#ef4444', '#22c55e', '#f59e0b', |
| 120 | + '#8b5cf6', '#ec4899', '#06b6d4', '#f97316', |
| 121 | + ]; |
| 122 | + let hash = 0; |
| 123 | + for (let i = 0; i < userId.length; i++) { |
| 124 | + hash = ((hash << 5) - hash + userId.charCodeAt(i)) | 0; |
| 125 | + } |
| 126 | + return colors[Math.abs(hash) % colors.length]; |
| 127 | +} |
0 commit comments