Skip to content

Commit 26c0a89

Browse files
add create room option
1 parent 18740ef commit 26c0a89

File tree

4 files changed

+81
-28
lines changed

4 files changed

+81
-28
lines changed

src/App.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import ShareText from "./components/ShareText";
55
import PublishedText from "./components/PublishedText";
66
import About from "./components/About";
77
import LiveRoom from "./components/LiveRoom";
8+
import CreateRoom from "./components/CreateRoom";
89

910
function App() {
1011
return (
@@ -14,7 +15,8 @@ function App() {
1415
<Route path="/" element={<ShareText />} />
1516
<Route path="about" element={<About />} />
1617
<Route path="published/:id" element={<PublishedText />} />
17-
<Route path="liveroom" element={<LiveRoom />} />
18+
<Route path="liveroom" element={<CreateRoom />} />
19+
<Route path="liveroom/:id" element={<LiveRoom />} />
1820
</Routes>
1921
</>
2022
);

src/components/CreateRoom.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { useNavigate } from "react-router-dom";
2+
3+
function CreateRoom() {
4+
const navigate = useNavigate();
5+
6+
const createLiveRoom = () => {
7+
const roomId = Math.floor(Math.random() * 1000);
8+
navigate(`${roomId}`);
9+
};
10+
11+
return (
12+
<div className="text-center py-5">
13+
<button
14+
onClick={createLiveRoom}
15+
className="bg-green-300 p-4 rounded-md hover:bg-green-400 duration-500"
16+
>
17+
Create Room
18+
</button>
19+
</div>
20+
);
21+
}
22+
23+
export default CreateRoom;

src/components/LiveRoom.tsx

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,47 @@
11
import { onValue, ref, set } from "@firebase/database";
22
import { useEffect, useState } from "react";
33
import { realtimedb } from "../firebase";
4+
import { useParams } from "react-router-dom";
45

56
type TextData = {
67
text: string;
8+
collaborators: [];
79
};
810

911
function LiveRoom() {
12+
const { id } = useParams();
13+
14+
const [text, setText] = useState<string>("");
15+
const [roomIdCopied, setRoomIdCopied] = useState<boolean>(false);
16+
1017
const updateFirebaseDatabase = async (currentText: string) => {
11-
set(ref(realtimedb, "users/" + "testing"), {
18+
set(ref(realtimedb, "liveroom/" + `${id}`), {
1219
text: currentText,
1320
});
1421
};
1522

16-
const [text, setText] = useState<string>("");
23+
const copyRoomId = () => {
24+
navigator.clipboard.writeText(
25+
`https://share.visheshpandey.com/#liveroom/${id}`
26+
);
27+
setRoomIdCopied(true);
28+
setTimeout(() => {
29+
setRoomIdCopied(false);
30+
}, 3000);
31+
};
1732

1833
useEffect(() => {
1934
// Set up Firebase event listener
20-
const databaseRef = ref(realtimedb, "users/testing"); // Replace with your actual path
35+
setText("Loading...");
36+
const databaseRef = ref(realtimedb, `liveroom/${id}`); // Replace with your actual path
2137

2238
const onDataChange = (snapshot: { val(): TextData }) => {
2339
const data = snapshot.val();
2440
if (data !== null) {
2541
setText(data.text);
2642
} else {
2743
// Handle the case where there is no data
28-
setText("No data available");
44+
setText("");
2945
}
3046
};
3147

@@ -42,33 +58,45 @@ function LiveRoom() {
4258

4359
return (
4460
<>
45-
<>
46-
<div className="bg-gray-200 w-11/12 m-auto my-2 rounded-md p-2">
47-
<div className="textarea text-center">
48-
<textarea
49-
className="w-full border-black border-0 p-2 rounded-md outline-none resize-none"
50-
value={text}
51-
onChange={(e) => {
52-
setText(e.target.value);
53-
updateFirebaseDatabase(e.target.value);
54-
}}
55-
name=""
56-
id=""
57-
cols={30}
58-
rows={20}
59-
placeholder="start typing here... or paste your text using ctrl + v"
60-
></textarea>
61-
</div>
61+
<div className="bg-gray-200 w-11/12 m-auto my-2 rounded-md p-2">
62+
<div className="buttons py-1 flex justify-between ">
63+
<span className="text-red-600 font-bold animate-pulse"></span>
64+
<button
65+
onClick={copyRoomId}
66+
className="bg-gray-300 p-4 rounded-md hover:bg-black duration-500 hover:text-white lg:w-1/12 md:w-2/12"
67+
>
68+
{roomIdCopied ? (
69+
<span>
70+
Copied <i className="bi bi-clipboard2-check"></i>
71+
</span>
72+
) : (
73+
<span>
74+
Invite <i className="bi bi-clipboard-plus"></i>
75+
</span>
76+
)}
77+
</button>
6278
</div>
63-
</>
79+
<div className="textarea text-center">
80+
<textarea
81+
className="w-full border-black border-0 p-2 rounded-md outline-none resize-none"
82+
value={text}
83+
onChange={(e) => {
84+
setText(e.target.value);
85+
updateFirebaseDatabase(e.target.value);
86+
}}
87+
name=""
88+
id=""
89+
cols={30}
90+
rows={20}
91+
placeholder="start typing here... or paste your text using ctrl + v"
92+
></textarea>
93+
</div>
94+
</div>
95+
6496
<div className="details w-11/12 m-auto my-2">
6597
<p className="text-white bg-gray-400 p-2 rounded-lg font-bold">
6698
Text here will get updated in realtime due to other collaborators
6799
</p>
68-
<p className="text-red-200">
69-
LiveRoom on v-share is still under development. You might get
70-
unexpected errors while using it.
71-
</p>
72100
</div>
73101
</>
74102
);

src/components/PublishedText.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function PublishedText() {
3535

3636
const copyLink = () => {
3737
navigator.clipboard.writeText(
38-
"https://vishesh-pandey.github.io/v-share/#" + location.pathname
38+
"https://share.visheshpandey.com/#" + location.pathname
3939
);
4040
setLinkCopied(true);
4141
};

0 commit comments

Comments
 (0)