import React, { useState, useEffect, useCallback } from "react";
import dynamic from "next/dynamic";
import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
import {
ReadClipboardButton,
WriteClipboardButton,
} from "@/components/common/clipboard_btn";
import { FileUploadHandler } from "@/components/ClipboardApp/FileUploadHandler";
import FileListDisplay from "@/components/ClipboardApp/FileListDisplay";
import AnimatedButton from "@/components/ui/AnimatedButton";
import type { Messages } from "@/types/messages";
import type { CustomFile, FileMeta } from "@/types/webrtc";
import { useFileTransferStore } from "@/stores/fileTransferStore";
// Dynamically import the RichTextEditor
const RichTextEditor = dynamic(
() => import("@/components/Editor/RichTextEditor"),
{
ssr: false, // This component is client-side only
loading: () => (
Loading Editor...
),
}
);
interface SendTabPanelProps {
messages: Messages;
updateShareContent: (content: string) => void;
addFilesToSend: (files: CustomFile[]) => void;
removeFileToSend: (meta: FileMeta) => void;
richTextToPlainText: (html: string) => string;
processRoomIdInput: (roomId: string) => void; // Passed from useRoomManager
joinRoom: (isSender: boolean, roomId: string) => void;
generateShareLinkAndBroadcast: () => void;
shareMessage: string;
currentValidatedShareRoomId: string;
handleLeaveSenderRoom: () => void; // New prop for leaving room
}
export function SendTabPanel({
messages,
updateShareContent,
addFilesToSend,
removeFileToSend,
richTextToPlainText,
processRoomIdInput,
joinRoom,
generateShareLinkAndBroadcast,
shareMessage,
currentValidatedShareRoomId,
handleLeaveSenderRoom,
}: SendTabPanelProps) {
// Get the status from the store
const {
shareContent,
sendFiles,
shareRoomStatusText,
sendProgress,
isAnyFileTransferring,
isSenderInRoom,
} = useFileTransferStore();
// Local state for immediate response in the input field
const [inputFieldValue, setInputFieldValue] = useState(
currentValidatedShareRoomId
);
// State to track ID generation mode (false = will show simple next, true = will show random next)
const [isSimpleIdMode, setIsSimpleIdMode] = useState(true);
// When the validatedShareRoomId from the parent component changes (e.g., after initial fetch), synchronize the local input field's value
useEffect(() => {
setInputFieldValue(currentValidatedShareRoomId);
}, [currentValidatedShareRoomId]);
const handleInputChange = useCallback(
(e: React.ChangeEvent) => {
const newValue = e.target.value;
setInputFieldValue(newValue); // Immediately update the input field display
processRoomIdInput(newValue); // Call the handler function from the hook, which will perform debounced validation
},
[processRoomIdInput]
);
const handlePaste = useCallback(
(e: React.ClipboardEvent) => {
const pastedText = e.clipboardData.getData("text").trim();
setInputFieldValue(pastedText);
processRoomIdInput(pastedText);
},
[processRoomIdInput]
);
// Handle ID generation toggle
const handleIdGeneration = useCallback(async () => {
if (isSimpleIdMode) {
// Generate random UUID
const randomId = crypto.randomUUID();
processRoomIdInput(randomId);
} else {
// Generate simple 4-digit ID by calling backend API
try {
const { fetchRoom } = await import("@/app/config/api");
const simpleRoomId = await fetchRoom();
if (simpleRoomId) {
// fetchRoom() already created the room, so set it as initial room ID
// This prevents joinRoom() from trying to create it again
setInputFieldValue(simpleRoomId);
const { useFileTransferStore } = await import(
"@/stores/fileTransferStore"
);
const store = useFileTransferStore.getState();
store.setShareRoomId(simpleRoomId);
// IMPORTANT: Set as initial room ID to prevent duplicate creation
store.setInitShareRoomId(simpleRoomId);
} else {
processRoomIdInput(crypto.randomUUID());
}
} catch (error) {
processRoomIdInput(crypto.randomUUID());
}
}
// Toggle mode for next click
setIsSimpleIdMode(!isSimpleIdMode);
}, [isSimpleIdMode, processRoomIdInput, setInputFieldValue]);
return (
{shareRoomStatusText ||
(isSenderInRoom
? messages.text.ClipboardApp.roomStatus.onlyOneMsg
: messages.text.ClipboardApp.roomStatus.senderEmptyMsg)}
{/* Room ID input section */}
{/* Action buttons */}
{messages.text.ClipboardApp.html.SyncSending_dis}
{shareMessage && (
{shareMessage}
)}
);
}