File size: 8,258 Bytes
d988ae4
 
 
 
 
3bbb98d
d988ae4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3bbb98d
d988ae4
3bbb98d
d988ae4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3bbb98d
 
 
 
 
 
 
 
d988ae4
 
3bbb98d
 
d988ae4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import { useState, useEffect, useRef } from 'react';
import io, { Socket } from 'socket.io-client';
import { ClipboardEntryType } from '@/app/[roomCode]/components/ClipboardEntry';
import { FileEntryType } from '@/app/[roomCode]/components/FileEntry';
import { apiUrl, socketUrl } from '../constants';
import { appendTokenToUrl, getAdminToken, withAdminTokenHeader } from '../adminAuth';

interface UseSocketManagerOptions {
  roomCode: string;
  clientId: string;
}

interface UseSocketManagerResult {
  entries: ClipboardEntryType[];
  files: FileEntryType[];
  connectedUsers: number;
  expiresIn: string | null;
  isLoading: boolean;
  error: string | null;
  socketRef: React.MutableRefObject<Socket | null>;
  addEntry: (content: string) => void;
  deleteEntry: (entryId: string) => void;
  clearClipboard: () => void;
  deleteFile: (fileId: string) => void;
}

export function useSocketManager({ roomCode, clientId }: UseSocketManagerOptions): UseSocketManagerResult {
  const [entries, setEntries] = useState<ClipboardEntryType[]>([]);
  const [files, setFiles] = useState<FileEntryType[]>([]);
  const [connectedUsers, setConnectedUsers] = useState(1);
  const [expiresIn, setExpiresIn] = useState<string | null>(null);
  const [error, setError] = useState<string | null>(null);
  const [isLoading, setIsLoading] = useState(true);
  const socketRef = useRef<Socket | null>(null);
  const dataLoadTimeoutRef = useRef<NodeJS.Timeout | null>(null);

  // Socket event handlers
  const addEntry = (content: string) => {
    if (socketRef.current) {
      socketRef.current.emit('addEntry', {
        roomCode,
        content,
        clientId,
      });
    }
  };

  const deleteEntry = (entryId: string) => {
    if (socketRef.current) {
      socketRef.current.emit('deleteEntry', {
        roomCode,
        entryId,
        clientId,
      });
    }
  };

  const clearClipboard = () => {
    if (socketRef.current) {
      socketRef.current.emit('clearClipboard', {
        roomCode,
        clientId,
      });
    }
  };

  const deleteFile = (fileId: string) => {
    if (socketRef.current) {
      socketRef.current.emit('deleteFile', {
        roomCode,
        fileId,
        clientId,
      });

      fetch(appendTokenToUrl(`${apiUrl}/clipboard/${roomCode}/files/${fileId}`), {
        method: 'DELETE',
        headers: withAdminTokenHeader(),
      }).catch(err => {
        console.error('Error deleting file:', err);
      });
    }
  };

  // Validate room code format (6 alphanumeric characters, uppercase)
  const isValidRoomCode = (code: string): boolean => {
    const roomCodeRegex = /^[A-Z0-9]{4}$/;
    return roomCodeRegex.test(code);
  };

  useEffect(() => {
    // Validate room code format before connecting
    if (!isValidRoomCode(roomCode)) {
      setError(`Invalid clipboard code format: ${roomCode}. Please use a 4-character uppercase alphanumeric code.`);
      setIsLoading(false);
      return;
    }

    const adminToken = getAdminToken();

    if (!adminToken) {
      setError('Admin token missing. Please refresh and enter your token.');
      setIsLoading(false);
      return;
    }

    // Connect directly to the Socket.IO server using our dedicated WebSocket port
    socketRef.current = io(socketUrl, {
      query: { roomCode, token: adminToken },
      extraHeaders: { 'x-admin-token': adminToken },
      transports: ['websocket', 'polling'],
      reconnectionAttempts: 5,
      reconnectionDelay: 1000,
      forceNew: true,
    });

    // Handle connection events
    socketRef.current.on('connect', () => {
      console.log('Connected to WebSocket server');
      // Join the room after successful connection
      if (socketRef.current) {
        socketRef.current.emit('joinRoom', { roomCode, clientId });
      }

      // Set a timeout for receiving initial clipboard data
      if (dataLoadTimeoutRef.current) clearTimeout(dataLoadTimeoutRef.current);
      dataLoadTimeoutRef.current = setTimeout(() => {
        if (isLoading) { // Only if still loading
          console.warn(`Timeout: Did not receive clipboardData for room ${roomCode} in time.`);
          setError('Failed to load clipboard data in time. Please check your connection or try refreshing.');
          setIsLoading(false);
        }
      }, 10000); // 10 seconds timeout
    });

    socketRef.current.on('connect_error', (error) => {
      console.error('Connection error:', error);
      setError('Failed to connect to the server. Please try again later.');
      setIsLoading(false);
    });

    socketRef.current.on('disconnect', (reason) => {
      console.log('Disconnected:', reason);
      if (reason === 'io server disconnect') {
        setError('Disconnected from server. The clipboard may not exist.');
        setIsLoading(false);
      }
    });

    // Handle clipboard not found error
    socketRef.current.on('error', (data: { message: string }) => {
      console.error('Socket error:', data.message);
      setError(data.message);
      setIsLoading(false);

      // If clipboard not found, redirect back to home after a delay
      // The backend now handles non-existent rooms by disconnecting, 
      // but this client-side redirect can be a fallback or for specific error messages.
      if (data.message.toLowerCase().includes('not found') || data.message.toLowerCase().includes('does not exist')) {
        setTimeout(() => {
          window.location.href = '/';
        }, 3000);
      }
    });

    // Listen for initial clipboard data
    socketRef.current.on('clipboardData', (data: { entries: ClipboardEntryType[], files?: FileEntryType[], connectedUsers: number, expiresIn?: string }) => {
      if (dataLoadTimeoutRef.current) clearTimeout(dataLoadTimeoutRef.current); // Clear timeout
      setEntries(data.entries);
      if (data.files) {
        setFiles(data.files);
      }
      setConnectedUsers(data.connectedUsers);
      if (data.expiresIn) {
        setExpiresIn(data.expiresIn);
      }
      setIsLoading(false);
    });

    // Listen for new entries
    socketRef.current.on('newEntry', (entry: ClipboardEntryType) => {
      setEntries((prev: ClipboardEntryType[]) => [entry, ...prev]);
    });

    // Listen for deleted entries
    socketRef.current.on('deleteEntry', (entryId: string) => {
      setEntries((prev: ClipboardEntryType[]) => prev.filter(entry => entry.id !== entryId));
    });

    // Listen for user count updates
    socketRef.current.on('userCount', (count: number) => {
      setConnectedUsers(count);
    });

    // Listen for expiration updates
    socketRef.current.on('expirationUpdate', (time: string) => {
      setExpiresIn(time);
    });

    // Listen for file upload
    socketRef.current.on('fileUploaded', (fileEntry: FileEntryType) => {
      setFiles((prev: FileEntryType[]) => [fileEntry, ...prev]);
    });

    // Listen for file deletion
    socketRef.current.on('fileDeleted', (fileId: string) => {
      setFiles((prev: FileEntryType[]) => prev.filter(file => file.id !== fileId));
    });

    // Cleanup on unmount
    return () => {
      console.log(`Cleaning up SocketManager for ${roomCode}`);
      if (dataLoadTimeoutRef.current) {
        clearTimeout(dataLoadTimeoutRef.current);
      }
      if (socketRef.current) {
        // Turn off all specific listeners
        socketRef.current.off('connect');
        socketRef.current.off('connect_error');
        socketRef.current.off('disconnect');
        socketRef.current.off('error');
        socketRef.current.off('clipboardData');
        socketRef.current.off('newEntry');
        socketRef.current.off('deleteEntry');
        socketRef.current.off('userCount');
        socketRef.current.off('expirationUpdate');
        socketRef.current.off('fileUploaded');
        socketRef.current.off('fileDeleted');

        // Then disconnect
        if (socketRef.current.connected) {
          socketRef.current.disconnect();
        }
        socketRef.current = null; // Help with garbage collection and prevent reuse
      }
    };
  }, [roomCode, clientId, isLoading]);

  return {
    entries,
    files,
    connectedUsers,
    expiresIn,
    isLoading,
    error,
    socketRef,
    addEntry,
    deleteEntry,
    clearClipboard,
    deleteFile
  };
}