1- import React , { Component } from 'react'
1+ import React , { useState , useEffect } from 'react'
22import PropTypes from 'prop-types'
3- import { KeyboardNav , KeyboardNavItem , Preload } from '@cerebroapp/cerebro-ui'
3+ import { KeyboardNav , KeyboardNavItem } from '@cerebroapp/cerebro-ui'
44import { client } from 'lib/plugins'
55import plugins from 'plugins'
66import ReactMarkdown from 'react-markdown'
@@ -11,147 +11,111 @@ import getReadme from '../getReadme'
1111import styles from './styles.module.css'
1212import * as format from '../format'
1313
14- const isRelative = ( src ) => ! src . match ( / ^ ( h t t p s ? : | d a t a : ) / )
15- const urlTransform = ( repo , src ) => {
16- if ( isRelative ( src ) ) {
17- return `http://raw.githubusercontent.com/${ repo } /master/${ src } `
14+ function Description ( { repoName } ) {
15+ const isRelative = ( src ) => ! src . match ( / ^ ( h t t p s ? : | d a t a : ) / )
16+
17+ const urlTransform = ( src ) => {
18+ if ( isRelative ( src ) ) return `http://raw.githubusercontent.com/${ repoName } /master/${ src } `
19+ return src
1820 }
19- return src
21+
22+ const [ readme , setReadme ] = useState ( null )
23+
24+ useEffect ( ( ) => { getReadme ( repoName ) . then ( setReadme ) } , [ ] )
25+
26+ if ( ! readme ) return null
27+
28+ return (
29+ < ReactMarkdown className = { styles . markdown } transformImageUri = { ( src ) => urlTransform ( src ) } >
30+ { readme }
31+ </ ReactMarkdown >
32+ )
2033}
2134
22- class Preview extends Component {
23- constructor ( props ) {
24- super ( props )
25- this . onComplete = this . onComplete . bind ( this )
26- this . state = {
27- showDescription : false ,
28- showSettings : false ,
29- }
30- }
35+ Description . propTypes = {
36+ repoName : PropTypes . string . isRequired
37+ }
3138
32- onComplete ( ) {
33- this . setState ( { runningAction : null } )
34- this . props . onComplete ( )
35- }
39+ function Preview ( { onComplete, plugin } ) {
40+ const [ runningAction , setRunningAction ] = useState ( null )
41+ const [ showDescription , setShowDescription ] = useState ( null )
42+ const [ showSettings , setShowSettings ] = useState ( null )
3643
37- pluginAction ( plugin , runningAction ) {
38- return ( ) => [
39- this . setState ( { runningAction } ) ,
40- client [ runningAction ] ( plugin )
41- ]
44+ const onCompleteAction = ( ) => {
45+ setRunningAction ( null )
46+ onComplete ( )
4247 }
4348
44- renderDescription ( repo ) {
45- return (
46- < Preload promise = { getReadme ( repo ) } >
47- {
48- ( content ) => (
49- < ReactMarkdown
50- className = { styles . markdown }
51- transformImageUri = { ( src ) => urlTransform ( repo , src ) }
52- >
53- { content }
54- </ ReactMarkdown >
55- )
56- }
57- </ Preload >
58- )
59- }
49+ const pluginAction = ( pluginName , runningActionName ) => ( ) => [
50+ setRunningAction ( runningActionName ) ,
51+ client [ runningAction ] ( pluginName )
52+ ]
6053
61- render ( ) {
62- const {
63- name,
64- version,
65- description,
66- repo,
67- isInstalled = false ,
68- isDebugging = false ,
69- installedVersion,
70- isUpdateAvailable = false
71- } = this . props
72- const githubRepo = repo && repo . match ( / ^ .+ g i t h u b .c o m \/ ( [ ^ \/ ] + \/ [ ^ \/ ] + ) .* ?/ )
73- const { runningAction, showSettings } = this . state
74- const settings = plugins [ name ] ? plugins [ name ] . settings : null
75- return (
76- < div className = { styles . preview } key = { name } >
77- < h2 > { `${ format . name ( name ) } (${ version } )` } </ h2 >
78-
79- < p > { format . description ( description ) } </ p >
80- < KeyboardNav >
81- < div className = { styles . header } >
82- {
83- settings
84- && (
85- < KeyboardNavItem
86- onSelect = { ( ) => this . setState ( { showSettings : ! this . state . showSettings } ) }
87- >
88- Settings
89- </ KeyboardNavItem >
90- )
91- }
92- { showSettings && < Settings name = { name } settings = { settings } /> }
93- {
94- ! isInstalled && ! isDebugging
95- && (
96- < ActionButton
97- action = { this . pluginAction ( name , 'install' ) }
98- text = { runningAction === 'install' ? 'Installing...' : 'Install' }
99- onComplete = { this . onComplete }
100- />
101- )
102- }
103- {
104- isInstalled
105- && (
106- < ActionButton
107- action = { this . pluginAction ( name , 'uninstall' ) }
108- text = { runningAction === 'uninstall' ? 'Uninstalling...' : 'Uninstall' }
109- onComplete = { this . onComplete }
110- />
111- )
112- }
113- {
114- isUpdateAvailable
115- && (
116- < ActionButton
117- action = { this . pluginAction ( name , 'update' ) }
118- text = {
119- runningAction === 'update'
120- ? 'Updating...'
121- : `Update (${ installedVersion } → ${ version } )`
122- }
123- onComplete = { this . onComplete }
124- />
125- )
126- }
127- {
128- githubRepo
129- && (
130- < KeyboardNavItem
131- onSelect = { ( ) => this . setState ( { showDescription : ! this . state . showDescription } ) }
132- >
133- Details
134- </ KeyboardNavItem >
135- )
136- }
137- </ div >
138- </ KeyboardNav >
139- { this . state . showDescription && this . renderDescription ( githubRepo [ 1 ] ) }
140- </ div >
141- )
142- }
54+ const {
55+ name, version, description, repo,
56+ isInstalled = false ,
57+ isDebugging = false ,
58+ installedVersion,
59+ isUpdateAvailable = false
60+ } = plugin
61+
62+ const githubRepo = repo && repo . match ( / ^ .+ g i t h u b .c o m \/ ( [ ^ \/ ] + \/ [ ^ \/ ] + ) .* ?/ )
63+ const settings = plugins [ name ] ? plugins [ name ] . settings : null
64+ return (
65+ < div className = { styles . preview } key = { name } >
66+ < h2 > { `${ format . name ( name ) } (${ version } )` } </ h2 >
67+
68+ < p > { format . description ( description ) } </ p >
69+ < KeyboardNav >
70+ < div className = { styles . header } >
71+
72+ { settings && (
73+ < KeyboardNavItem onSelect = { ( ) => setShowSettings ( ( prev ) => ! prev ) } >
74+ Settings
75+ </ KeyboardNavItem >
76+ ) }
77+
78+ { showSettings && < Settings name = { name } settings = { settings } /> }
79+
80+ { ! isInstalled && ! isDebugging && (
81+ < ActionButton
82+ action = { pluginAction ( name , 'install' ) }
83+ text = { runningAction === 'install' ? 'Installing...' : 'Install' }
84+ onComplete = { onCompleteAction }
85+ />
86+ ) }
87+
88+ { isInstalled && (
89+ < ActionButton
90+ action = { pluginAction ( name , 'uninstall' ) }
91+ text = { runningAction === 'uninstall' ? 'Uninstalling...' : 'Uninstall' }
92+ onComplete = { onCompleteAction }
93+ />
94+ ) }
95+
96+ { isUpdateAvailable && (
97+ < ActionButton
98+ action = { pluginAction ( name , 'update' ) }
99+ text = { runningAction === 'update' ? 'Updating...' : `Update (${ installedVersion } → ${ version } )` }
100+ onComplete = { onCompleteAction }
101+ />
102+ ) }
103+
104+ { githubRepo && (
105+ < KeyboardNavItem onSelect = { ( ) => setShowDescription ( ( prev ) => ! prev ) } >
106+ Details
107+ </ KeyboardNavItem >
108+ ) }
109+
110+ </ div >
111+ </ KeyboardNav >
112+ { showDescription && < Description repoName = { githubRepo [ 1 ] } /> }
113+ </ div >
114+ )
143115}
144116
145117Preview . propTypes = {
146- name : PropTypes . string . isRequired ,
147- settings : PropTypes . object ,
148- version : PropTypes . string . isRequired ,
149- description : PropTypes . string ,
150- repo : PropTypes . string ,
151- installedVersion : PropTypes . string ,
152- isInstalled : PropTypes . bool ,
153- isDebugging : PropTypes . bool ,
154- isUpdateAvailable : PropTypes . bool ,
118+ plugin : PropTypes . object . isRequired ,
155119 onComplete : PropTypes . func . isRequired ,
156120}
157121
0 commit comments