1
1
import * as React from 'react' ;
2
- import { getStateFromPath } from '@react-navigation/core' ;
2
+ import { getActionFromState , getStateFromPath } from '@react-navigation/core' ;
3
3
import escape from 'escape-html' ;
4
4
import Editor from 'react-simple-code-editor' ;
5
5
import Highlight , { defaultProps } from 'prism-react-renderer' ;
@@ -10,6 +10,24 @@ import RouteMap from './RouteMap';
10
10
11
11
const parse = ( value ) => eval ( `(function() { return ${ value } ; }())` ) ;
12
12
13
+ function Code ( { code, theme, language } ) {
14
+ return (
15
+ < Highlight { ...defaultProps } code = { code } theme = { theme } language = { language } >
16
+ { ( { className, style, tokens, getLineProps, getTokenProps } ) => (
17
+ < pre className = { className } style = { { ...style , ...styles . json } } >
18
+ { tokens . map ( ( line , i ) => (
19
+ < div { ...getLineProps ( { line, key : i } ) } >
20
+ { line . map ( ( token , key ) => (
21
+ < span { ...getTokenProps ( { token, key } ) } />
22
+ ) ) }
23
+ </ div >
24
+ ) ) }
25
+ </ pre >
26
+ ) }
27
+ </ Highlight >
28
+ ) ;
29
+ }
30
+
13
31
export default function LinkingTester ( ) {
14
32
const { isDarkTheme } = React . useContext ( ThemeContext ) ;
15
33
const theme = isDarkTheme ? dracula : github ;
@@ -38,12 +56,13 @@ export default function LinkingTester() {
38
56
39
57
const [ path , setPath ] = React . useState ( '/user/@vergil/edit' ) ;
40
58
const [ config , setConfig ] = React . useState ( ( ) => parse ( rawConfig ) ) ;
41
- const [ showJSON , setShowJSON ] = React . useState ( false ) ;
59
+ const [ pane , setPane ] = React . useState ( 'chart' ) ;
42
60
43
- let state ;
61
+ let state , action ;
44
62
45
63
try {
46
64
state = getStateFromPath ( path . replace ( / ( ^ \w + : | ^ ) \/ \/ / , '' ) , config ) ;
65
+ action = getActionFromState ( state , config ) ;
47
66
} catch ( e ) {
48
67
// Ignore
49
68
}
@@ -88,35 +107,51 @@ export default function LinkingTester() {
88
107
style = { { ...styles . code , ...styles . editor } }
89
108
/>
90
109
< div style = { styles . preview } >
91
- < button
92
- type = "button"
93
- style = { styles . button }
94
- onClick = { ( ) => setShowJSON ( ( show ) => ! show ) }
95
- >
96
- { showJSON ? 'Chart' : 'JSON' }
97
- </ button >
98
- { showJSON ? (
99
- < Highlight
100
- { ...defaultProps }
110
+ < div style = { styles . toggles } >
111
+ < button
112
+ type = "button"
113
+ style = { styles . button }
114
+ onClick = { ( ) => setPane ( 'chart' ) }
115
+ >
116
+ Chart
117
+ </ button >
118
+ < button
119
+ type = "button"
120
+ style = { styles . button }
121
+ onClick = { ( ) => setPane ( 'state' ) }
122
+ >
123
+ State
124
+ </ button >
125
+ < button
126
+ type = "button"
127
+ style = { styles . button }
128
+ onClick = { ( ) => setPane ( 'action' ) }
129
+ >
130
+ Action
131
+ </ button >
132
+ </ div >
133
+ { pane === 'state' ? (
134
+ < Code
135
+ theme = { theme }
101
136
code = { JSON . stringify ( state , null , 2 ) || '' }
137
+ language = "json"
138
+ />
139
+ ) : pane === 'action' ? (
140
+ < Code
102
141
theme = { theme }
142
+ code = { JSON . stringify ( action , null , 2 ) || '' }
103
143
language = "json"
104
- >
105
- { ( { className, style, tokens, getLineProps, getTokenProps } ) => (
106
- < pre className = { className } style = { { ...style , ...styles . json } } >
107
- { tokens . map ( ( line , i ) => (
108
- < div { ...getLineProps ( { line, key : i } ) } >
109
- { line . map ( ( token , key ) => (
110
- < span { ...getTokenProps ( { token, key } ) } />
111
- ) ) }
112
- </ div >
113
- ) ) }
114
- </ pre >
115
- ) }
116
- </ Highlight >
117
- ) : state ? (
118
- < RouteMap routes = { state . routes } />
119
- ) : < p style = { styles . error } > Failed to parse the path. Make sure that the path matches the patterns specified in the config.</ p > }
144
+ />
145
+ ) : pane === 'chart' ? (
146
+ state ? (
147
+ < RouteMap routes = { state . routes } />
148
+ ) : (
149
+ < p style = { styles . error } >
150
+ Failed to parse the path. Make sure that the path matches the
151
+ patterns specified in the config.
152
+ </ p >
153
+ )
154
+ ) : null }
120
155
</ div >
121
156
</ >
122
157
) ;
@@ -145,26 +180,31 @@ const styles = {
145
180
position : 'relative' ,
146
181
border : '1px solid var(--ifm-contents-border-color)' ,
147
182
borderRadius : 'var(--ifm-pre-border-radius)' ,
148
- minHeight : 70
183
+ minHeight : 70 ,
149
184
} ,
150
185
json : {
151
186
margin : 0 ,
152
187
fontFamily : 'var(--ifm-font-family-monospace)' ,
153
188
fontSize : 'var(--ifm-code-font-size)' ,
154
189
borderRadius : 'var(--ifm-pre-border-radius)' ,
155
190
padding : 'var(--ifm-pre-padding)' ,
156
- minHeight : 70
191
+ minHeight : 70 ,
157
192
} ,
158
- button : {
193
+ toggles : {
159
194
position : 'absolute' ,
195
+ flexDirection : 'row' ,
160
196
right : 0 ,
161
- border : '1px solid var(--ifm-contents-border-color)' ,
162
- borderRadius : '0 var(--ifm-pre-border-radius)' ,
163
- borderRight : 0 ,
164
- borderTop : 0 ,
197
+ top : 0 ,
198
+ borderBottom : '1px solid var(--ifm-contents-border-color)' ,
199
+ } ,
200
+ button : {
201
+ border : 0 ,
202
+ borderLeft : '1px solid var(--ifm-contents-border-color)' ,
203
+ borderRadius : 0 ,
165
204
cursor : 'pointer' ,
166
- display : 'block ' ,
205
+ display : 'inline-flex ' ,
167
206
fontSize : 12 ,
207
+ margin : 0 ,
168
208
padding : '4px 8px' ,
169
209
color : 'inherit' ,
170
210
background : 'none' ,
@@ -173,6 +213,6 @@ const styles = {
173
213
} ,
174
214
error : {
175
215
margin : 24 ,
176
- color : '#A12027'
177
- }
216
+ color : '#A12027' ,
217
+ } ,
178
218
} ;
0 commit comments