1- import React , { useState , useEffect } from 'react' ;
2- import CrossIcon from 'react-icons/lib/md/clear' ;
3- import RefreshIcon from 'react-icons/lib/md/refresh' ;
4- import ArrowDropDown from 'react-icons/lib/md/keyboard-arrow-down' ;
5- import ArrowDropUp from 'react-icons/lib/md/keyboard-arrow-up' ;
6- import algoliasearch from 'algoliasearch/lite' ;
7- import compareVersions from 'compare-versions' ;
81import Tooltip , {
92 SingletonTooltip ,
103} from '@codesandbox/common/lib/components/Tooltip' ;
114import { formatVersion } from '@codesandbox/common/lib/utils/ci' ;
12-
13- import css from '@styled-system/css' ;
145import {
6+ Button ,
7+ Link ,
158 ListAction ,
16- Stack ,
17- SidebarRow ,
189 Select ,
10+ SidebarRow ,
11+ Stack ,
1912 Text ,
20- Link ,
21- Button ,
2213} from '@codesandbox/components' ;
14+ import css from '@styled-system/css' ;
15+ import algoliasearch from 'algoliasearch/lite' ;
16+ import compareVersions from 'compare-versions' ;
17+ import React , { FunctionComponent , useEffect , useState } from 'react' ;
18+ import CrossIcon from 'react-icons/lib/md/clear' ;
19+ import RefreshIcon from 'react-icons/lib/md/refresh' ;
20+ import ArrowDropDown from 'react-icons/lib/md/keyboard-arrow-down' ;
21+ import ArrowDropUp from 'react-icons/lib/md/keyboard-arrow-up' ;
2322
24- import { BundleSizes } from './BundleSizes ' ;
23+ import { useOvermind } from 'app/overmind ' ;
2524
26- interface Props {
27- dependencies : { [ dep : string ] : string } ;
28- dependency : string ;
29- onRemove : ( dep : string ) => void ;
30- onRefresh : ( dep : string , version ?: string ) => void ;
31- }
25+ import { BundleSizes } from './BundleSizes' ;
3226
3327const client = algoliasearch ( 'OFCNCOG2CU' , '00383ecd8441ead30b1b0ff981c426f5' ) ;
3428const index = client . initIndex ( 'npm-search' ) ;
3529
36- export const Dependency = ( {
30+ type Props = {
31+ dependencies : { [ dependency : string ] : string } ;
32+ dependency : string ;
33+ } ;
34+ export const Dependency : FunctionComponent < Props > = ( {
3735 dependencies,
3836 dependency,
39- onRemove,
40- onRefresh,
41- } : Props ) => {
42- const [ version , setVersion ] = useState ( null ) ;
37+ } ) => {
38+ const {
39+ actions : {
40+ editor : { addNpmDependency, npmDependencyRemoved } ,
41+ } ,
42+ } = useOvermind ( ) ;
4343 const [ open , setOpen ] = useState ( false ) ;
44+ const [ version , setVersion ] = useState ( null ) ;
4445 const [ versions , setVersions ] = useState ( [ ] ) ;
4546
4647 const setVersionsForLatestPkg = ( pkg : string ) => {
4748 fetch ( `/api/v1/dependencies/${ pkg } ` )
4849 . then ( response => response . json ( ) )
49- . then ( data => setVersion ( data . data . version ) )
50+ . then ( ( { data } ) => setVersion ( data . version ) )
5051 . catch ( err => {
5152 if ( process . env . NODE_ENV === 'development' ) {
5253 console . error ( err ) ; // eslint-disable-line no-console
5354 }
5455 } ) ;
5556 } ;
56-
5757 useEffect ( ( ) => {
5858 // @ts -ignore
5959 index . getObject ( dependency , [ 'versions' ] ) . then ( ( { versions : results } ) => {
6060 const orderedVersions = Object . keys ( results ) . sort ( ( a , b ) => {
6161 try {
6262 return compareVersions ( b , a ) ;
63- } catch ( e ) {
63+ } catch {
6464 return 0 ;
6565 }
6666 } ) ;
@@ -84,15 +84,16 @@ export const Dependency = ({
8484 e . preventDefault ( ) ;
8585 e . stopPropagation ( ) ;
8686 }
87- onRemove ( dependency ) ;
88- } ;
8987
88+ npmDependencyRemoved ( dependency ) ;
89+ } ;
9090 const handleRefresh = e => {
9191 if ( e ) {
9292 e . preventDefault ( ) ;
9393 e . stopPropagation ( ) ;
9494 }
95- onRefresh ( dependency ) ;
95+
96+ addNpmDependency ( { name : dependency } ) ;
9697 } ;
9798
9899 if ( typeof dependencies [ dependency ] !== 'string' ) {
@@ -102,67 +103,67 @@ export const Dependency = ({
102103 return (
103104 < >
104105 < ListAction
105- justify = "space-between"
106106 align = "center"
107107 css = { css ( {
108108 position : 'relative' ,
109+
109110 '.actions' : {
110111 backgroundColor : 'sideBar.background' ,
111112 display : 'none' ,
112113 } ,
114+
113115 ':hover .actions' : {
114116 backgroundColor : 'list.hoverBackground' ,
115117 display : 'flex' ,
116118 } ,
117119 } ) }
120+ justify = "space-between"
118121 >
119122 < Link
123+ css = { { maxWidth : '60%' , position : 'absolute' , zIndex : 2 } }
120124 href = { `/examples/package/${ dependency } ` }
125+ maxWidth = "60%"
121126 target = "_blank"
122127 title = { dependency }
123- maxWidth = "60%"
124- css = { {
125- position : 'absolute' ,
126- zIndex : 2 ,
127- maxWidth : '60%' ,
128- } }
129128 >
130129 { dependency }
131130 </ Link >
132131
133132 < Stack
134133 align = "center"
135- justify = "flex-end"
136134 css = { css ( {
137135 position : 'absolute' ,
138136 right : 2 ,
139137 flexGrow : 0 ,
140138 flexShrink : 1 ,
141139 width : '100%' ,
142140 } ) }
141+ justify = "flex-end"
143142 >
144- < Text variant = "muted" maxWidth = "30% ">
143+ < Text maxWidth = "30%" variant = "muted ">
145144 { formatVersion ( dependencies [ dependency ] ) } { ' ' }
146145 { version && < span > ({ formatVersion ( version ) } )</ span > }
147146 </ Text >
148147 </ Stack >
149148
150149 < Stack
151- className = "actions"
152150 align = "center"
153- justify = "flex-end"
154151 css = { css ( {
155152 position : 'absolute' ,
156153 right : 0 ,
157154 width : 'auto' ,
158155 zIndex : 2 , // overlay on dependency name
159156 paddingLeft : 1 ,
160157 } ) }
158+ className = "actions"
159+ justify = "flex-end"
161160 >
162161 < Select
163162 css = { { width : '80px' } }
163+ onChange = { ( { target : { value } } ) =>
164+ addNpmDependency ( { name : dependency , version : value } )
165+ }
164166 value = { versions . find ( v => v === dependencies [ dependency ] ) }
165- onChange = { e => onRefresh ( dependency , e . target . value ) }
166167 >
167168 { versions . map ( a => (
168169 < option key = { a } > { a } </ option >
@@ -174,39 +175,41 @@ export const Dependency = ({
174175 < >
175176 < Tooltip
176177 content = { open ? 'Hide sizes' : 'Show sizes' }
177- style = { { outline : 'none' } }
178178 singleton = { singleton }
179+ style = { { outline : 'none' } }
179180 >
180181 < Button
181- variant = "link"
182- onClick = { ( ) => setOpen ( ! open ) }
183182 css = { css ( { minWidth : 5 } ) }
183+ onClick = { ( ) => setOpen ( show => ! show ) }
184+ variant = "link"
184185 >
185186 { open ? < ArrowDropUp /> : < ArrowDropDown /> }
186187 </ Button >
187188 </ Tooltip >
189+
188190 < Tooltip
189191 content = "Update to latest"
190- style = { { outline : 'none' } }
191192 singleton = { singleton }
193+ style = { { outline : 'none' } }
192194 >
193195 < Button
194- variant = "link"
195- onClick = { handleRefresh }
196196 css = { css ( { minWidth : 5 } ) }
197+ onClick = { handleRefresh }
198+ variant = "link"
197199 >
198200 < RefreshIcon />
199201 </ Button >
200202 </ Tooltip >
203+
201204 < Tooltip
202205 content = "Remove"
203206 style = { { outline : 'none' } }
204207 singleton = { singleton }
205208 >
206209 < Button
207- variant = "link"
208- onClick = { handleRemove }
209210 css = { css ( { minWidth : 5 } ) }
211+ onClick = { handleRemove }
212+ variant = "link"
210213 >
211214 < CrossIcon />
212215 </ Button >
@@ -216,6 +219,7 @@ export const Dependency = ({
216219 </ SingletonTooltip >
217220 </ Stack >
218221 </ ListAction >
222+
219223 { open ? (
220224 < SidebarRow marginX = { 2 } >
221225 < BundleSizes
0 commit comments