@@ -2,36 +2,18 @@ import {
22 Card ,
33 CardActions ,
44 CardContent ,
5- Drawer ,
65 Tab ,
76 Tabs ,
87 Typography ,
9- WithStyles ,
10- withStyles ,
118} from '@material-ui/core' ;
12- import React , {
13- ComponentType ,
14- FormEvent ,
15- PureComponent ,
16- ReactNode ,
17- } from 'react' ;
18- import Inspector , {
19- NodeRendererProps ,
20- ObjectLabel ,
21- ObjectName ,
22- ObjectRootLabel ,
23- } from 'react-inspector' ;
9+ import React , { ComponentType , FormEvent , PureComponent } from 'react' ;
2410import { Observable as obs } from 'rxjs' ;
25- import uriTemplate from 'uri-template' ;
26- import { StreamBrowser } from '../../../components' ;
2711import { Notes , Settings } from '../../../components/Icons' ;
2812import { Table , TableBody } from '../../../components/StripeyTable' ;
2913import { connect , createState } from '../../../reactive' ;
30- import { HalResource , NavigatableProps } from '../../../types' ;
31- import { hal , http } from '../../../utils' ;
32- import rels from '../../rels' ;
14+ import { HalResource } from '../../../types' ;
3315import store from '../../store' ;
34- import { StreamHeader , StreamMessageDetails } from './components' ;
16+ import { JsonViewer , StreamHeader , StreamMessageDetails } from './components' ;
3517import { HalViewerProps } from './types' ;
3618
3719const tryParseJson = ( payload : string ) : object => {
@@ -79,147 +61,6 @@ const state$ = createState<StreamMessageState>(
7961 } ) ,
8062) ;
8163
82- const isPotentialStreamId = ( data : any ) =>
83- typeof data === 'number' || typeof data === 'string' ;
84-
85- const getStreamLinks = ( { _embedded } : HalResource ) : HalResource [ ] =>
86- _embedded [ rels . feed ] || [ ] ;
87-
88- interface StreamMessageJsonState {
89- streams : HalResource [ ] ;
90- loading : boolean ;
91- open : boolean ;
92- }
93-
94- const style = {
95- drawerPaper : {
96- width : 480 ,
97- } ,
98- streamId : {
99- '&:hover' : {
100- textDecoration : 'underline' ,
101- } ,
102- } ,
103- } ;
104-
105- interface StreamMessageJsonProps {
106- json : object ;
107- }
108-
109- const StreamMessageJson = withStyles ( style ) ( class extends PureComponent <
110- StreamMessageJsonProps &
111- NavigatableProps &
112- HalResource &
113- WithStyles < typeof style > ,
114- StreamMessageJsonState
115- > {
116- state : StreamMessageJsonState = {
117- loading : false ,
118- open : false ,
119- streams : [ ] ,
120- } ;
121-
122- _handlePotentialStreamIdClick : (
123- e : FormEvent ,
124- pattern : string ,
125- ) => Promise < void > = async ( { currentTarget : anchorElement } , pattern ) => {
126- const { authorization, _links } = this . props ;
127-
128- this . setState ( {
129- loading : true ,
130- open : true ,
131- } ) ;
132-
133- if ( ! _links || ! _links [ rels . browse ] ) {
134- return ;
135- }
136-
137- const template = uriTemplate . parse (
138- decodeURI ( _links [ rels . browse ] [ 0 ] . href ) ,
139- ) ;
140-
141- const responses = await Promise . all (
142- [ ...new Set ( [ pattern , String ( pattern ) . replace ( '-' , '' ) ] ) ] . map ( p =>
143- http . get ( {
144- headers : { authorization } ,
145- link : {
146- href : template . expand ( { p, t : 'e' } ) ,
147- } ,
148- } ) ,
149- ) ,
150- ) ;
151-
152- this . setState ( {
153- loading : false ,
154- streams : Object . values (
155- responses . flatMap ( ( { body } ) =>
156- getStreamLinks ( hal . normalizeResource ( body as HalResource ) ) ,
157- ) ,
158- ) ,
159- } ) ;
160- } ;
161-
162- _handlePotentialStreamIdClose = ( ) =>
163- this . setState ( {
164- open : false ,
165- } ) ;
166-
167- _renderNode = ( {
168- depth,
169- name,
170- data,
171- isNonenumerable,
172- ...props
173- } : NodeRendererProps & { children ?: ReactNode } ) =>
174- depth === 0 ? (
175- < ObjectRootLabel name = { name } data = { { } } { ...props } />
176- ) : isPotentialStreamId ( data ) ? (
177- < span >
178- < ObjectName name = { name } dimmed = { isNonenumerable } />
179- < span > : </ span >
180- < a
181- className = { this . props . classes . streamId }
182- onClick = { e => this . _handlePotentialStreamIdClick ( e , data ) }
183- >
184- { data }
185- </ a >
186- </ span >
187- ) : (
188- < ObjectLabel
189- name = { name }
190- data = { props . children ? { } : data }
191- isNonenumerable = { isNonenumerable }
192- { ...props }
193- />
194- ) ;
195-
196- render ( ) {
197- const { json, classes } = this . props ;
198- const { streams, loading, open } = this . state ;
199-
200- return (
201- < div className = { classes . drawerPaper } >
202- < Inspector
203- data = { json }
204- expandLevel = { 32 }
205- nodeRenderer = { this . _renderNode }
206- />
207- < Drawer
208- variant = { 'temporary' }
209- open = { open }
210- onClose = { this . _handlePotentialStreamIdClose }
211- anchor = { 'right' }
212- classes = { {
213- paper : classes . drawerPaper ,
214- } }
215- >
216- < StreamBrowser streams = { streams } loading = { loading } />
217- </ Drawer >
218- </ div >
219- ) ;
220- }
221- } as ComponentType < StreamMessageJsonProps & HalResource > ) ;
222-
22364interface StreamMessageTabsState {
22465 value : number ;
22566}
@@ -231,7 +72,6 @@ interface StreamMessageTabsProps {
23172 } ;
23273}
23374
234- // tslint:disable-next-line:max-classes-per-file
23575class StreamMessageTabs extends PureComponent <
23676 StreamMessageTabsProps & HalResource ,
23777 StreamMessageTabsState
@@ -284,12 +124,8 @@ class StreamMessageTabs extends PureComponent<
284124 </ Tabs >
285125 </ CardActions >
286126 < CardContent >
287- { value === 0 && (
288- < StreamMessageJson json = { payload } { ...props } />
289- ) }
290- { value === 1 && (
291- < StreamMessageJson json = { metadata } { ...props } />
292- ) }
127+ { value === 0 && < JsonViewer json = { payload } { ...props } /> }
128+ { value === 1 && < JsonViewer json = { metadata } { ...props } /> }
293129 </ CardContent >
294130 </ Card >
295131 ) ;
0 commit comments