@@ -7,14 +7,29 @@ import FormatterWorker from 'solid-repl/repl/formatter?worker';
7
7
import LinterWorker from 'solid-repl/repl/linter?worker' ;
8
8
import onigasm from 'onigasm/lib/onigasm.wasm?url' ;
9
9
import { batch , createResource , createSignal , lazy , onCleanup , Show , Suspense } from 'solid-js' ;
10
- import { useMatch , useNavigate , useParams , useSearchParams } from '@solidjs/router' ;
10
+ import { useNavigate , useParams , useSearchParams } from '@solidjs/router' ;
11
11
import { API , useAppContext } from '../context' ;
12
12
import { debounce } from '@solid-primitives/scheduled' ;
13
13
import { defaultTabs } from 'solid-repl/src' ;
14
14
import type { Tab } from 'solid-repl' ;
15
- import type { APIRepl } from './home' ;
16
15
import { Header } from '../components/header' ;
17
16
17
+ export interface ReplFile {
18
+ name : string ;
19
+ content : string ;
20
+ }
21
+ export interface APIRepl {
22
+ id : string ;
23
+ title : string ;
24
+ labels : string [ ] ;
25
+ files : ReplFile [ ] ;
26
+ version : string ;
27
+ public : boolean ;
28
+ size : number ;
29
+ created_at : string ;
30
+ updated_at ?: string ;
31
+ }
32
+
18
33
const Repl = lazy ( ( ) => import ( '../components/setupSolid' ) ) ;
19
34
20
35
window . MonacoEnvironment = {
@@ -40,7 +55,6 @@ interface InternalTab extends Tab {
40
55
}
41
56
export const Edit = ( ) => {
42
57
const [ searchParams ] = useSearchParams ( ) ;
43
- const scratchpad = useMatch ( ( ) => '/scratchpad' ) ;
44
58
const compiler = new CompilerWorker ( ) ;
45
59
const formatter = new FormatterWorker ( ) ;
46
60
const linter = new LinterWorker ( ) ;
@@ -51,8 +65,6 @@ export const Edit = () => {
51
65
52
66
let disableFetch : true | undefined ;
53
67
54
- let readonly = ( ) => ! scratchpad ( ) && context . user ( ) ?. display != params . user && ! localStorage . getItem ( params . repl ) ;
55
-
56
68
const mapTabs = ( toMap : ( Tab | InternalTab ) [ ] ) : InternalTab [ ] =>
57
69
toMap . map ( ( tab ) => {
58
70
if ( '_source' in tab ) return tab ;
@@ -82,16 +94,19 @@ export const Edit = () => {
82
94
onCleanup ( ( ) => context . setTabs ( undefined ) ) ;
83
95
84
96
const [ current , setCurrent ] = createSignal < string | undefined > ( undefined , { equals : false } ) ;
85
- const [ resource , { mutate } ] = createResource < APIRepl , { repl : string ; scratchpad : boolean } > (
86
- ( ) => ( { repl : params . repl , scratchpad : ! ! scratchpad ( ) } ) ,
87
- async ( { repl, scratchpad } ) : Promise < APIRepl > => {
97
+ const [ resource , { mutate } ] = createResource < APIRepl , { repl : string } > (
98
+ ( ) => ( { repl : params . repl } ) ,
99
+ async ( { repl } ) : Promise < APIRepl > => {
88
100
if ( disableFetch ) {
89
101
disableFetch = undefined ;
90
102
if ( resource . latest ) return resource . latest ;
91
103
}
92
104
93
105
let output : APIRepl ;
94
- if ( scratchpad ) {
106
+
107
+ if ( repl ) {
108
+ output = await fetch ( `${ API } /repl/${ repl } ` ) . then ( ( r ) => r . json ( ) ) ;
109
+ } else {
95
110
const myScratchpad = localStorage . getItem ( 'scratchpad' ) ;
96
111
if ( ! myScratchpad ) {
97
112
output = {
@@ -104,10 +119,6 @@ export const Edit = () => {
104
119
} else {
105
120
output = JSON . parse ( myScratchpad ) ;
106
121
}
107
- } else {
108
- output = await fetch ( `${ API } /repl/${ repl } ` , {
109
- headers : { authorization : context . token ? `Bearer ${ context . token } ` : '' } ,
110
- } ) . then ( ( r ) => r . json ( ) ) ;
111
122
}
112
123
113
124
batch ( ( ) => {
@@ -129,109 +140,51 @@ export const Edit = () => {
129
140
setCurrent ( defaultTabs [ 0 ] . name ) ;
130
141
} ) ;
131
142
} ;
143
+ const updateRepl = debounce ( ( ) => {
144
+ const files = tabs ( ) . map ( ( x ) => ( { name : x . name , content : x . source } ) ) ;
132
145
133
- const updateRepl = debounce (
134
- ( ) => {
135
- const files = tabs ( ) . map ( ( x ) => ( { name : x . name , content : x . source } ) ) ;
136
-
137
- if ( readonly ( ) ) {
138
- localStorage . setItem ( 'scratchpad' , JSON . stringify ( { files } ) ) ;
139
- disableFetch = true ;
140
- navigate ( '/scratchpad' ) ;
141
- return ;
142
- } else if ( scratchpad ( ) ) {
143
- localStorage . setItem ( 'scratchpad' , JSON . stringify ( { files } ) ) ;
144
- }
145
-
146
- const repl = resource . latest ;
147
- if ( ! repl ) return ;
148
-
149
- if ( ( context . token && context . user ( ) ?. display == params . user ) || localStorage . getItem ( params . repl ) ) {
150
- fetch ( `${ API } /repl/${ params . repl } ` , {
151
- method : 'PUT' ,
152
- headers : {
153
- 'authorization' : context . token ? `Bearer ${ context . token } ` : '' ,
154
- 'Content-Type' : 'application/json' ,
155
- } ,
156
- body : JSON . stringify ( {
157
- ...( localStorage . getItem ( params . repl ) ? { write_token : localStorage . getItem ( params . repl ) } : { } ) ,
158
- title : repl . title ,
159
- version : repl . version ,
160
- public : repl . public ,
161
- labels : repl . labels ,
162
- files,
163
- } ) ,
164
- } ) ;
165
- }
166
- } ,
167
- ! ! scratchpad ( ) ? 10 : 1000 ,
168
- ) ;
169
-
146
+ localStorage . setItem ( 'scratchpad' , JSON . stringify ( { files } ) ) ;
147
+ } , 10 ) ;
170
148
return (
171
149
< >
172
150
< Header
173
151
compiler = { compiler }
174
152
fork = { ( ) => { } }
175
153
share = { async ( ) => {
176
- if ( scratchpad ( ) ) {
177
- const newRepl = {
178
- title : context . user ( ) ?. display ? `${ context . user ( ) ! . display } 's Scratchpad` : 'Anonymous Scratchpad' ,
179
- public : true ,
180
- labels : [ ] ,
181
- version : '1.0' ,
182
- files : tabs ( ) . map ( ( x ) => ( { name : x . name , content : x . source } ) ) ,
183
- } ;
184
- const response = await fetch ( `${ API } /repl` , {
185
- method : 'POST' ,
186
- headers : {
187
- 'authorization' : context . token ? `Bearer ${ context . token } ` : '' ,
188
- 'Content-Type' : 'application/json' ,
189
- } ,
190
- body : JSON . stringify ( newRepl ) ,
191
- } ) ;
192
- if ( response . status >= 400 ) {
193
- throw new Error ( response . statusText ) ;
194
- }
195
- const { id, write_token } = await response . json ( ) ;
196
- if ( write_token ) {
197
- localStorage . setItem ( id , write_token ) ;
198
- const repls = localStorage . getItem ( 'repls' ) ;
199
- if ( repls ) {
200
- localStorage . setItem ( 'repls' , JSON . stringify ( [ ...JSON . parse ( repls ) , id ] ) ) ;
201
- } else {
202
- localStorage . setItem ( 'repls' , JSON . stringify ( [ id ] ) ) ;
203
- }
204
- }
205
- mutate ( ( ) => ( {
206
- id,
207
- title : newRepl . title ,
208
- labels : newRepl . labels ,
209
- files : newRepl . files ,
210
- version : newRepl . version ,
211
- public : newRepl . public ,
212
- size : 0 ,
213
- created_at : '' ,
214
- } ) ) ;
215
- const url = `/${ context . user ( ) ?. display || 'anonymous' } /${ id } ` ;
216
- disableFetch = true ;
217
- navigate ( url ) ;
218
- return `${ window . location . origin } ${ url } ` ;
219
- } else {
220
- return location . href ;
154
+ const newRepl = {
155
+ title : 'Anonymous Scratchpad' ,
156
+ public : true ,
157
+ labels : [ ] ,
158
+ version : '1.0' ,
159
+ files : tabs ( ) . map ( ( x ) => ( { name : x . name , content : x . source } ) ) ,
160
+ } ;
161
+ const response = await fetch ( `${ API } /repl` , {
162
+ method : 'POST' ,
163
+ headers : {
164
+ 'Content-Type' : 'application/json' ,
165
+ } ,
166
+ body : JSON . stringify ( newRepl ) ,
167
+ } ) ;
168
+ if ( response . status >= 400 ) {
169
+ throw new Error ( response . statusText ) ;
221
170
}
171
+ const { id } = await response . json ( ) ;
172
+ mutate ( ( ) => ( {
173
+ id,
174
+ title : newRepl . title ,
175
+ labels : newRepl . labels ,
176
+ files : newRepl . files ,
177
+ version : newRepl . version ,
178
+ public : newRepl . public ,
179
+ size : 0 ,
180
+ created_at : '' ,
181
+ } ) ) ;
182
+ const url = `/anonymous/${ id } ` ;
183
+ disableFetch = true ;
184
+ navigate ( url ) ;
185
+ return `${ window . location . origin } ${ url } ` ;
222
186
} }
223
- >
224
- { resource ( ) ?. title && (
225
- < input
226
- class = "w-96 shrink rounded border border-solid border-transparent bg-transparent px-3 py-1.5 transition focus:border-blue-600 focus:outline-none"
227
- value = { resource ( ) ?. title }
228
- onChange = { ( e ) => {
229
- mutate ( ( x ) => x && { ...x , title : e . currentTarget . value } ) ;
230
- updateRepl ( ) ;
231
- } }
232
- />
233
- ) }
234
- </ Header >
187
+ />
235
188
< Suspense
236
189
fallback = {
237
190
< svg
0 commit comments