From 26c0a898023a047fcb0fb985d9b214c4738a604c Mon Sep 17 00:00:00 2001 From: Vishesh Pandey <74998585+Vishesh-Pandey@users.noreply.github.com> Date: Wed, 10 Jan 2024 23:57:44 +0530 Subject: [PATCH] add create room option --- src/App.tsx | 4 +- src/components/CreateRoom.tsx | 23 +++++++++ src/components/LiveRoom.tsx | 80 +++++++++++++++++++++----------- src/components/PublishedText.tsx | 2 +- 4 files changed, 81 insertions(+), 28 deletions(-) create mode 100644 src/components/CreateRoom.tsx diff --git a/src/App.tsx b/src/App.tsx index 022d029..6368332 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -5,6 +5,7 @@ import ShareText from "./components/ShareText"; import PublishedText from "./components/PublishedText"; import About from "./components/About"; import LiveRoom from "./components/LiveRoom"; +import CreateRoom from "./components/CreateRoom"; function App() { return ( @@ -14,7 +15,8 @@ function App() { } /> } /> } /> - } /> + } /> + } /> ); diff --git a/src/components/CreateRoom.tsx b/src/components/CreateRoom.tsx new file mode 100644 index 0000000..3943221 --- /dev/null +++ b/src/components/CreateRoom.tsx @@ -0,0 +1,23 @@ +import { useNavigate } from "react-router-dom"; + +function CreateRoom() { + const navigate = useNavigate(); + + const createLiveRoom = () => { + const roomId = Math.floor(Math.random() * 1000); + navigate(`${roomId}`); + }; + + return ( +
+ +
+ ); +} + +export default CreateRoom; diff --git a/src/components/LiveRoom.tsx b/src/components/LiveRoom.tsx index 235f83c..fb187d0 100644 --- a/src/components/LiveRoom.tsx +++ b/src/components/LiveRoom.tsx @@ -1,23 +1,39 @@ import { onValue, ref, set } from "@firebase/database"; import { useEffect, useState } from "react"; import { realtimedb } from "../firebase"; +import { useParams } from "react-router-dom"; type TextData = { text: string; + collaborators: []; }; function LiveRoom() { + const { id } = useParams(); + + const [text, setText] = useState(""); + const [roomIdCopied, setRoomIdCopied] = useState(false); + const updateFirebaseDatabase = async (currentText: string) => { - set(ref(realtimedb, "users/" + "testing"), { + set(ref(realtimedb, "liveroom/" + `${id}`), { text: currentText, }); }; - const [text, setText] = useState(""); + const copyRoomId = () => { + navigator.clipboard.writeText( + `https://share.visheshpandey.com/#liveroom/${id}` + ); + setRoomIdCopied(true); + setTimeout(() => { + setRoomIdCopied(false); + }, 3000); + }; useEffect(() => { // Set up Firebase event listener - const databaseRef = ref(realtimedb, "users/testing"); // Replace with your actual path + setText("Loading..."); + const databaseRef = ref(realtimedb, `liveroom/${id}`); // Replace with your actual path const onDataChange = (snapshot: { val(): TextData }) => { const data = snapshot.val(); @@ -25,7 +41,7 @@ function LiveRoom() { setText(data.text); } else { // Handle the case where there is no data - setText("No data available"); + setText(""); } }; @@ -42,33 +58,45 @@ function LiveRoom() { return ( <> - <> -
-
- -
+
+
+ +
- +
+ +
+
+

Text here will get updated in realtime due to other collaborators

-

- LiveRoom on v-share is still under development. You might get - unexpected errors while using it. -

); diff --git a/src/components/PublishedText.tsx b/src/components/PublishedText.tsx index aa0ab86..09a13df 100644 --- a/src/components/PublishedText.tsx +++ b/src/components/PublishedText.tsx @@ -35,7 +35,7 @@ function PublishedText() { const copyLink = () => { navigator.clipboard.writeText( - "https://vishesh-pandey.github.io/v-share/#" + location.pathname + "https://share.visheshpandey.com/#" + location.pathname ); setLinkCopied(true); };