11import { useEffect , useState } from 'react'
22import Editor from '@monaco-editor/react'
33import { compile } from '@sayjava/phake-cli/lib/compile'
4+ import { templates } from '../templates'
45
5- function debounce ( func , delay ) {
6+ function debounce ( func , delay ) {
67 let timeoutId
78
89 return function ( ) {
@@ -17,56 +18,58 @@ function debounce (func, delay) {
1718}
1819
1920export const Compile = ( ) => {
20- const initialContent = `
21- {
22- "id": {{faker 'number.int' 10}},
23- "node_id": "{{faker 'string.alpha' 25}}",
24- "name": "{{faker 'word.noun'}}",
25- "language": "{{randomize 'javascript' 'ruby' 'golang' 'c++'}}",
26- "forks_count": {{faker 'number.int' max=10000}},
27- "size": {{faker 'number.float' max=10000 precision=0.2}},
28- "default_branch": "{{randomize 'main' 'master'}}",
29- "open_issues_count": {{faker 'number.int' max=50}},
30- "is_template": {{randomize false true}},
31- "description": "{{faker 'lorem.sentences' max=1}}",
32- "topics": [
33- {{#repeat 5}}
34- "{{faker 'string.alpha' 5}}"
35- {{/repeat}}
36- ]
37- }
38- `
3921
22+ const [ currTemplate , setCurrTemplate ] = useState ( templates [ 0 ] . name )
4023 const [ content , setContent ] = useState ( '' )
24+
4125 const doCompile = ( template ) => {
4226 try {
43- const newContent = JSON . parse ( compile ( { template } ) )
44- setContent ( JSON . stringify ( newContent , null , 2 ) )
27+ const newContent = compile ( { template } )
28+ setContent ( newContent , null , 2 )
4529 } catch ( error ) {
4630 setContent ( error . toString ( ) )
4731 }
4832 }
4933
50- useEffect ( ( ) => {
51- doCompile ( initialContent )
52- } , [ ] )
53-
5434 const handleChange = debounce ( doCompile , 500 )
35+ const changeTemplate = ( e ) => {
36+ setCurrTemplate ( e . target . value )
37+ doCompile ( templates . find ( ( template ) => template . name === e . target . value ) . template )
38+ }
39+
40+ useEffect ( ( ) => {
41+ doCompile (
42+ templates . find ( ( template ) => template . name === currTemplate ) . template
43+ )
44+ } , [ currTemplate ] )
5545
5646 return (
5747 < div className = 'w-full' >
48+ < div className = 'my-4 flex space-x-4' >
49+ < label className = 'inline-block' htmlFor = "examples" > Examples</ label >
50+ < select id = 'examples' className = 'inline-block'
51+ onChange = { ( e ) => { changeTemplate ( e ) } } >
52+ {
53+ templates . map ( ( template ) => (
54+ < option key = { template . name } value = { template . name } > { template . label } </ option >
55+ ) )
56+ }
57+ </ select >
58+ </ div >
59+
60+
5861 < div style = { { display : 'flex' } } >
5962 < Editor
6063 height = '70vh' theme = 'vs-dark'
6164 options = { { minimap : { enabled : false } , readOnly : false } }
62- language = 'json'
63- defaultValue = { initialContent }
65+ language = { currTemplate }
66+ value = { templates . find ( ( template ) => template . name === currTemplate ) . template }
6467 onChange = { handleChange }
6568 />
6669 < Editor
6770 height = '70vh' theme = 'vs-dark'
68- options = { { minimap : { enabled : false } , readOnly : true } }
69- language = 'json'
71+ options = { { minimap : { enabled : false } , readOnly : true , formatOnPaste : true , formatOnType : true , renderControlCharacters : false , renderWhitespace : 'none' , renderFinalNewline : true , renderIndentGuides : true , renderLineHighlight : 'none' , renderLineNumbers : 'off' , renderValidationDecorations : 'off' , scrollBeyondLastLine : false , wordWrap : 'on' } }
72+ language = { currTemplate }
7073 value = { content }
7174 />
7275 </ div >
0 commit comments