1
1
import { onValue , ref , set } from "@firebase/database" ;
2
2
import { useEffect , useState } from "react" ;
3
3
import { realtimedb } from "../firebase" ;
4
+ import { useParams } from "react-router-dom" ;
4
5
5
6
type TextData = {
6
7
text : string ;
8
+ collaborators : [ ] ;
7
9
} ;
8
10
9
11
function LiveRoom ( ) {
12
+ const { id } = useParams ( ) ;
13
+
14
+ const [ text , setText ] = useState < string > ( "" ) ;
15
+ const [ roomIdCopied , setRoomIdCopied ] = useState < boolean > ( false ) ;
16
+
10
17
const updateFirebaseDatabase = async ( currentText : string ) => {
11
- set ( ref ( realtimedb , "users /" + "testing" ) , {
18
+ set ( ref ( realtimedb , "liveroom /" + ` ${ id } ` ) , {
12
19
text : currentText ,
13
20
} ) ;
14
21
} ;
15
22
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
+ } ;
17
32
18
33
useEffect ( ( ) => {
19
34
// 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
21
37
22
38
const onDataChange = ( snapshot : { val ( ) : TextData } ) => {
23
39
const data = snapshot . val ( ) ;
24
40
if ( data !== null ) {
25
41
setText ( data . text ) ;
26
42
} else {
27
43
// Handle the case where there is no data
28
- setText ( "No data available " ) ;
44
+ setText ( "" ) ;
29
45
}
30
46
} ;
31
47
@@ -42,33 +58,45 @@ function LiveRoom() {
42
58
43
59
return (
44
60
< >
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 >
62
78
</ 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
+
64
96
< div className = "details w-11/12 m-auto my-2" >
65
97
< p className = "text-white bg-gray-400 p-2 rounded-lg font-bold" >
66
98
Text here will get updated in realtime due to other collaborators
67
99
</ 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 >
72
100
</ div >
73
101
</ >
74
102
) ;
0 commit comments