@@ -5,143 +5,104 @@ import Link from 'next/link';
5
5
import { useSearchParams } from 'next/navigation' ;
6
6
import { io } from 'socket.io-client' ;
7
7
import debounce from 'lodash.debounce' ;
8
+ import LZString from 'lz-string' ;
8
9
9
10
const socket = io ( 'https://codenow-server.onrender.com' ) ;
10
- // const socket = io(process.env.SOCKET_URL);
11
11
12
12
const ShareCodePage : React . FC = ( ) => {
13
13
const searchParams = useSearchParams ( ) ;
14
- const initialCode = searchParams . get ( 'code' ) || '' ;
15
- const [ sharedCode , setSharedCode ] = useState < string > ( initialCode ) ;
14
+ const initialCode = LZString . decompressFromEncodedURIComponent ( searchParams . get ( 'code' ) || ' ') || '' ;
15
+ const [ sharedCode , setSharedCode ] = useState ( initialCode ) ;
16
16
const [ toastMessage , setToastMessage ] = useState < string | null > ( null ) ;
17
17
const textareaRef = useRef < HTMLTextAreaElement > ( null ) ;
18
18
19
19
useEffect ( ( ) => {
20
- if ( initialCode ) {
21
- setSharedCode ( initialCode ) ;
22
- }
20
+ if ( initialCode ) setSharedCode ( initialCode ) ;
23
21
24
- socket . on ( 'codeUpdate' , ( newCode : string ) => {
25
- console . log ( 'Received code update:' , newCode ) ;
26
- setSharedCode ( newCode ) ;
27
- } ) ;
22
+ const handleCodeUpdate = ( newCode : string ) => setSharedCode ( newCode ) ;
23
+ const handleMessage = ( message : string ) => console . log ( message ) ;
28
24
29
- socket . on ( 'message' , ( message : string ) => {
30
- console . log ( message ) ;
31
- } ) ;
25
+ socket . on ( 'codeUpdate' , handleCodeUpdate ) ;
26
+ socket . on ( 'message' , handleMessage ) ;
32
27
33
28
return ( ) => {
34
- socket . off ( 'codeUpdate' ) ;
35
- socket . off ( 'message' ) ;
29
+ socket . off ( 'codeUpdate' , handleCodeUpdate ) ;
30
+ socket . off ( 'message' , handleMessage ) ;
36
31
} ;
37
32
} , [ initialCode ] ) ;
38
33
34
+ const showToast = ( message : string ) => {
35
+ setToastMessage ( message ) ;
36
+ setTimeout ( ( ) => setToastMessage ( null ) , 3000 ) ;
37
+ } ;
38
+
39
39
const handleCopy = ( ) => {
40
40
navigator . clipboard . writeText ( sharedCode ) ;
41
- setToastMessage ( "The code has been copied to your clipboard." ) ;
42
- setTimeout ( ( ) => setToastMessage ( null ) , 3000 ) ;
41
+ showToast ( "The code has been copied to your clipboard." ) ;
43
42
} ;
44
43
45
44
const handleShare = ( ) => {
46
- const url = `${ window . location . origin } ${ window . location . pathname } ?code=${ encodeURIComponent ( sharedCode ) } ` ;
47
- navigator . clipboard . writeText ( url ) ;
48
- setToastMessage ( "The shareable link has been copied to your clipboard." ) ;
49
- setTimeout ( ( ) => setToastMessage ( null ) , 3000 ) ;
45
+ const compressedCode = LZString . compressToEncodedURIComponent ( sharedCode ) ;
46
+ navigator . clipboard . writeText ( `${ window . location . origin } ${ window . location . pathname } ?code=${ compressedCode } ` ) ;
47
+ showToast ( "The shareable link has been copied to your clipboard." ) ;
50
48
} ;
51
49
52
- // Debounced function to emit codeChange with URL
53
- const debouncedEmitCodeChange = useRef (
54
- debounce ( ( newCode ) => {
55
- const url = window . location . href ; // Capture the current URL
56
- console . log ( 'Emitting Code Change:' , { newCode, url } ) ; // Debugging line
57
- socket . emit ( 'codeChange' , { newCode, url } ) ; // Emit both the new code and the URL
58
- localStorage . setItem ( 'sharedCode' , newCode ) ;
59
- } , 500 )
60
- ) . current ;
50
+ const debouncedEmitCodeChange = useRef ( debounce ( ( newCode : string ) => {
51
+ socket . emit ( 'codeChange' , { newCode, url : window . location . href } ) ;
52
+ localStorage . setItem ( 'sharedCode' , newCode ) ;
53
+ } , 500 ) ) . current ;
61
54
62
55
const handleTextareaChange = ( e : React . ChangeEvent < HTMLTextAreaElement > ) => {
63
56
const newCode = e . target . value ;
64
- console . log ( 'New Code:' , newCode ) ; // Debugging line
65
57
setSharedCode ( newCode ) ;
66
58
debouncedEmitCodeChange ( newCode ) ;
67
59
} ;
68
60
69
- const calculateLineNumbers = ( ) => {
70
- if ( textareaRef . current ) {
71
- const lineCount = textareaRef . current . value . split ( '\n' ) . length ;
72
- return Array . from ( { length : lineCount } , ( _ , i ) => i + 1 ) ;
73
- }
74
- return [ ] ;
75
- } ;
76
-
77
- const lines = calculateLineNumbers ( ) ;
61
+ const lines = textareaRef . current ? Array . from ( { length : textareaRef . current . value . split ( '\n' ) . length } , ( _ , i ) => i + 1 ) : [ ] ;
78
62
79
63
return (
80
64
< div className = "flex flex-col min-h-screen font-sans bg-gray-950 text-gray-100" >
81
- < header className = "px-4 lg:px-6 h-14 flex items-center justify-between" >
82
- < Link className = "flex items-center justify-center" href = "/" >
83
- < Code className = "h-6 w-6 mr-2" />
84
- < span className = "font-bold text-xl" > CodeNow</ span >
85
- </ Link >
86
- < a
87
- href = "https://github.com/krishvsoni/CodeNow"
88
- target = "_blank"
89
- rel = "noopener noreferrer"
90
- className = "flex items-center text-sm text-blue-500 hover:underline"
91
- >
92
- < svg
93
- className = "h-5 w-5 mr-1"
94
- fill = "currentColor"
95
- viewBox = "0 0 24 24"
96
- xmlns = "http://www.w3.org/2000/svg"
97
- >
98
- < path
99
- fillRule = "evenodd"
100
- d = "M12 0C5.37 0 0 5.37 0 12c0 5.3 3.438 9.8 8.205 11.385.6.11.82-.26.82-.577v-2.165c-3.338.726-4.042-1.61-4.042-1.61-.546-1.387-1.333-1.757-1.333-1.757-1.09-.745.083-.73.083-.73 1.205.084 1.84 1.237 1.84 1.237 1.07 1.835 2.807 1.305 3.492.997.108-.775.42-1.305.763-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.467-2.38 1.235-3.22-.123-.303-.535-1.523.117-3.176 0 0 1.008-.322 3.3 1.23.957-.266 1.98-.398 3-.403 1.02.005 2.043.137 3 .403 2.29-1.552 3.297-1.23 3.297-1.23.653 1.653.24 2.873.118 3.176.77.84 1.233 1.91 1.233 3.22 0 4.61-2.803 5.625-5.475 5.92.43.37.823 1.102.823 2.222v3.293c0 .32.218.694.825.576C20.565 21.795 24 17.295 24 12c0-6.63-5.37-12-12-12z"
101
- clipRule = "evenodd"
102
- />
103
- </ svg >
104
- CodeNow
105
- </ a >
106
- </ header >
107
-
65
+ < header className = "px-4 lg:px-6 h-14 flex items-center justify-between" >
66
+ < Link className = "flex items-center justify-center" href = "/" >
67
+ < Code className = "h-6 w-6 mr-2" />
68
+ < span className = "font-bold text-xl" > CodeNow</ span >
69
+ </ Link >
70
+ < a
71
+ href = "https://github.com/krishvsoni/CodeNow"
72
+ target = "_blank"
73
+ rel = "noopener noreferrer"
74
+ className = "flex items-center text-sm text-blue-500 hover:underline"
75
+ >
76
+ < svg className = "h-5 w-5 mr-1" fill = "currentColor" viewBox = "0 0 24 24" >
77
+ < path fillRule = "evenodd" d = "M12 0C5.37 0 0 5.37 0 12c0 5.3 3.438 9.8 8.205 11.385.6.11.82-.26.82-.577v-2.165c-3.338.726-4.042-1.61-4.042-1.61-.546-1.387-1.333-1.757-1.333-1.757-1.09-.745.083-.73.083-.73 1.205.084 1.84 1.237 1.84 1.237 1.07 1.835 2.807 1.305 3.492.997.108-.775.42-1.305.763-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.467-2.38 1.235-3.22-.123-.303-.535-1.523.117-3.176 0 0 1.008-.322 3.3 1.23.957-.266 1.98-.398 3-.403 1.02.005 2.043.137 3 .403 2.29-1.552 3.297-1.23 3.297-1.23.653 1.653.24 2.873.118 3.176.77.84 1.233 1.91 1.233 3.22 0 4.61-2.803 5.625-5.475 5.92.43.37.823 1.102.823 2.222v3.293c0 .32.218.694.825.576C20.565 21.795 24 17.295 24 12c0-6.63-5.37-12-12-12z" clipRule = "evenodd" />
78
+ </ svg >
79
+ CodeNow
80
+ </ a >
81
+ </ header >
108
82
109
83
< main className = "flex-1 p-4 md:p-6" >
110
- < div className = "max-w-4xl mx-auto space-y-4" >
111
- < div className = "flex items-center justify-between" >
112
- < h1 className = "text-2xl font-bold" > Shared Code</ h1 >
113
- </ div >
84
+ < div className = "w-full max-w-4xl lg:max-w-full mx-auto space-y-4" >
85
+ < h1 className = "text-2xl font-bold" > Shared Code</ h1 >
114
86
< div className = "border border-gray-800 rounded-lg overflow-hidden" >
115
87
< div className = "bg-gray-900 p-2 flex justify-between items-center" >
116
- < span className = "text-sm font-medium" > </ span >
117
88
< div className = "flex space-x-2" >
118
- < button
119
- className = "p-2 bg-gray-800 hover:bg-gray-700 rounded text-gray-300"
120
- onClick = { handleCopy }
121
- >
122
- < Copy className = "h-4 w-4 inline" />
123
- < span className = "sr-only" > Copy code</ span >
89
+ < button className = "p-2 bg-gray-800 hover:bg-gray-700 rounded text-gray-300" onClick = { handleCopy } >
90
+ < Copy className = "h-4 w-4 inline" /> < span className = "sr-only" > Copy code</ span >
124
91
</ button >
125
- < button
126
- className = "p-2 bg-gray-800 hover:bg-gray-700 rounded text-gray-300"
127
- onClick = { handleShare }
128
- >
129
- < Share2 className = "h-4 w-4 inline" />
130
- < span className = "sr-only" > Share code</ span >
92
+ < button className = "p-2 bg-gray-800 hover:bg-gray-700 rounded text-gray-300" onClick = { handleShare } >
93
+ < Share2 className = "h-4 w-4 inline" /> < span className = "sr-only" > Share code</ span >
131
94
</ button >
132
95
</ div >
133
96
</ div >
134
97
< div className = "relative" >
135
- < div className = "absolute top-0 left-0 h-full w-10 bg-gray-900 border-r border-gray-800 flex flex-col items-center pt-4 text-gray-500 text-sm select-none" >
98
+ < div className = "absolute top-0 left-0 h-[600px] bg-gray-900 border-r border-gray-800 flex flex-col items-center pt-4 text-gray-500 text-sm select-none" >
136
99
{ lines . map ( ( lineNumber ) => (
137
- < div key = { lineNumber } className = "h-6 w-full text-right pr-2" >
138
- { lineNumber }
139
- </ div >
100
+ < div key = { lineNumber } className = "h-6 w-full text-right pr-2" > { lineNumber } </ div >
140
101
) ) }
141
102
</ div >
142
103
< textarea
143
104
ref = { textareaRef }
144
- className = "w-full h-[400px ] bg-gray-950 text-gray-100 p-4 pl-12 font-mono text-sm resize-none focus:outline-none"
105
+ className = "w-full h-[600px ] bg-gray-950 text-gray-100 p-4 pl-12 font-mono text-sm resize-none focus:outline-none"
145
106
value = { sharedCode }
146
107
onChange = { handleTextareaChange }
147
108
spellCheck = "false"
@@ -160,9 +121,7 @@ const ShareCodePage: React.FC = () => {
160
121
161
122
< footer className = "flex flex-col gap-2 sm:flex-row py-6 w-full shrink-0 items-center justify-center px-4 md:px-6 border-t border-gray-800" >
162
123
< p className = "text-sm text-gray-400" > © 2024 CodeNow. All rights reserved.</ p >
163
- < p className = "text-slate-600 font-mono hover:text-white transition-colors duration-300" >
164
- built by Krish Soni 🚀
165
- </ p >
124
+ < p className = "text-slate-600 font-mono hover:text-white transition-colors duration-300" > built by Krish Soni</ p >
166
125
</ footer >
167
126
</ div >
168
127
) ;
0 commit comments