@@ -7,14 +7,29 @@ import FormatterWorker from 'solid-repl/repl/formatter?worker';
77import LinterWorker from 'solid-repl/repl/linter?worker' ;
88import onigasm from 'onigasm/lib/onigasm.wasm?url' ;
99import { 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' ;
1111import { API , useAppContext } from '../context' ;
1212import { debounce } from '@solid-primitives/scheduled' ;
1313import { defaultTabs } from 'solid-repl/src' ;
1414import type { Tab } from 'solid-repl' ;
15- import type { APIRepl } from './home' ;
1615import { Header } from '../components/header' ;
1716
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+
1833const Repl = lazy ( ( ) => import ( '../components/setupSolid' ) ) ;
1934
2035window . MonacoEnvironment = {
@@ -40,7 +55,6 @@ interface InternalTab extends Tab {
4055}
4156export const Edit = ( ) => {
4257 const [ searchParams ] = useSearchParams ( ) ;
43- const scratchpad = useMatch ( ( ) => '/scratchpad' ) ;
4458 const compiler = new CompilerWorker ( ) ;
4559 const formatter = new FormatterWorker ( ) ;
4660 const linter = new LinterWorker ( ) ;
@@ -51,8 +65,6 @@ export const Edit = () => {
5165
5266 let disableFetch : true | undefined ;
5367
54- let readonly = ( ) => ! scratchpad ( ) && context . user ( ) ?. display != params . user && ! localStorage . getItem ( params . repl ) ;
55-
5668 const mapTabs = ( toMap : ( Tab | InternalTab ) [ ] ) : InternalTab [ ] =>
5769 toMap . map ( ( tab ) => {
5870 if ( '_source' in tab ) return tab ;
@@ -82,16 +94,19 @@ export const Edit = () => {
8294 onCleanup ( ( ) => context . setTabs ( undefined ) ) ;
8395
8496 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 > => {
88100 if ( disableFetch ) {
89101 disableFetch = undefined ;
90102 if ( resource . latest ) return resource . latest ;
91103 }
92104
93105 let output : APIRepl ;
94- if ( scratchpad ) {
106+
107+ if ( repl ) {
108+ output = await fetch ( `${ API } /repl/${ repl } ` ) . then ( ( r ) => r . json ( ) ) ;
109+ } else {
95110 const myScratchpad = localStorage . getItem ( 'scratchpad' ) ;
96111 if ( ! myScratchpad ) {
97112 output = {
@@ -104,10 +119,6 @@ export const Edit = () => {
104119 } else {
105120 output = JSON . parse ( myScratchpad ) ;
106121 }
107- } else {
108- output = await fetch ( `${ API } /repl/${ repl } ` , {
109- headers : { authorization : context . token ? `Bearer ${ context . token } ` : '' } ,
110- } ) . then ( ( r ) => r . json ( ) ) ;
111122 }
112123
113124 batch ( ( ) => {
@@ -129,109 +140,51 @@ export const Edit = () => {
129140 setCurrent ( defaultTabs [ 0 ] . name ) ;
130141 } ) ;
131142 } ;
143+ const updateRepl = debounce ( ( ) => {
144+ const files = tabs ( ) . map ( ( x ) => ( { name : x . name , content : x . source } ) ) ;
132145
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 ) ;
170148 return (
171149 < >
172150 < Header
173151 compiler = { compiler }
174152 fork = { ( ) => { } }
175153 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 ) ;
221170 }
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 } ` ;
222186 } }
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+ />
235188 < Suspense
236189 fallback = {
237190 < svg
0 commit comments