diff --git a/client/src/app/[id]/page.tsx b/client/src/app/[id]/page.tsx deleted file mode 100644 index a2f3d21..0000000 --- a/client/src/app/[id]/page.tsx +++ /dev/null @@ -1,167 +0,0 @@ -"use client"; -import { useEffect, useState } from "react"; -import toast from "react-hot-toast"; -import useDebounce from "@/hook/useDebounce"; -import API from "@/lib/api"; -import socket from "@/lib/socket"; -import { - saveDocumentLocally, - getLocalDocument, - deleteLocalDocument, -} from "@/lib/db"; - -export default function Page({ params }: { params: { id: string } }) { - const { id } = params; - const [participants, setParticipants] = useState([]); - const [message, setMessage] = useState(""); - const [messages, setMessages] = useState([]); - const [content, setContent] = useState(""); - const [isOffline, setIsOffline] = useState(false); - const debouncedContent = useDebounce(content, 500); - - const handleOnline = () => setIsOffline(false); - const handleOffline = () => setIsOffline(true); - - const fetchDocument = async () => { - try { - const localData = await getLocalDocument(id); - if (localData) { - setContent(localData.content); - setIsOffline(true); - } else { - const { data } = await API.get(`/api/document/${id}`); - setContent(data.content); - } - } catch (error) { - console.error("Failed to fetch document:", error); - } - }; - - const handleChangeContent = (value: string) => { - setContent(value); - socket.emit("edit-document", id, value); - }; - - const sendMessage = () => { - socket.emit("send-message", message); - setMessages((prevMessages) => [...prevMessages, message]); - setMessage(""); - }; - - const syncData = async () => { - if (!isOffline) { - const localData = await getLocalDocument(id); - if (localData) { - try { - await API.put(`/api/document/${id}`, { content: localData.content }); - await deleteLocalDocument(id); - toast.success("Changes synced with server"); - } catch (error) { - console.error("Failed to sync with server: ", error); - } - } - } - }; - - useEffect(() => { - fetchDocument(); - - window.addEventListener("online", handleOnline); - window.addEventListener("offline", handleOffline); - - return () => { - window.removeEventListener("online", handleOnline); - window.removeEventListener("offline", handleOffline); - }; - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [id]); - - useEffect(() => { - if (debouncedContent) { - if (isOffline) { - saveDocumentLocally(id, debouncedContent); - } else { - API.put(`/api/document/${id}`, { content: debouncedContent }) - .then(() => { - deleteLocalDocument(id); - }) - .catch((error) => { - console.error("Failed to save document: ", error); - saveDocumentLocally(id, debouncedContent); - }); - } - } - }, [debouncedContent, isOffline, id]); - - useEffect(() => { - socket.on("update-participants", (participants: string[]) => { - setParticipants(participants); - }); - - return () => { - socket.off("update-participants"); - }; - }, []); - - useEffect(() => { - socket.on("receive-message", (message: string) => { - toast("New message received"); - setMessages((prevMessages) => [...prevMessages, message]); - }); - - return () => { - socket.off("receive-message"); - }; - }, []); - - useEffect(() => { - window.addEventListener("online", syncData); - - return () => { - window.removeEventListener("online", syncData); - }; - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [id, isOffline]); - - return ( -
-

Editing Document

- {isOffline && ( -
- You are offline. Changes will be synced when you are back online. -
- )} -