@@ -2,12 +2,18 @@ import Tooltip from '@codesandbox/common/lib/components/Tooltip';
22import track from '@codesandbox/common/lib/utils/analytics' ;
33import { getSandboxName } from '@codesandbox/common/lib/utils/get-sandbox-name' ;
44import { ESC } from '@codesandbox/common/lib/utils/keycodes' ;
5- import { inject , hooksObserver } from 'app/componentConnectors' ;
65import { basename } from 'path' ;
7- import React , { useState } from 'react' ;
6+ import React , {
7+ ChangeEvent ,
8+ FunctionComponent ,
9+ KeyboardEvent ,
10+ useState ,
11+ } from 'react' ;
812import { Link } from 'react-router-dom' ;
913import { useSpring , animated } from 'react-spring' ;
1014
15+ import { useOvermind } from 'app/overmind' ;
16+
1117import {
1218 Container ,
1319 Folder ,
@@ -19,136 +25,133 @@ import {
1925 Main ,
2026} from './elements' ;
2127
22- export const SandboxName = inject ( 'store' , 'signals' ) (
23- hooksObserver (
24- ( {
25- store : {
26- isLoggedIn,
27- editor : { currentSandbox } ,
28- } ,
29- signals : {
30- modalOpened,
31- workspace : { sandboxInfoUpdated, valueChanged } ,
32- } ,
33- } ) => {
34- const [ updatingName , setUpdatingName ] = useState ( false ) ;
35- const [ name , setName ] = useState ( '' ) ;
36-
37- const sandboxName = getSandboxName ( currentSandbox ) || 'Untitled' ;
38-
39- const updateSandboxInfo = ( ) => {
40- sandboxInfoUpdated ( ) ;
41- setUpdatingName ( false ) ;
42- } ;
43-
44- const submitNameChange = ( e : React . ChangeEvent < HTMLFormElement > ) => {
45- e . preventDefault ( ) ;
46- updateSandboxInfo ( ) ;
47- track ( 'Change Sandbox Name From Header' ) ;
48- } ;
49-
50- const handleNameClick = ( ) => {
51- setUpdatingName ( true ) ;
52- setName ( sandboxName ) ;
53- } ;
54-
55- const handleKeyUp = ( e : React . KeyboardEvent < HTMLInputElement > ) => {
56- if ( e . keyCode === ESC ) {
57- updateSandboxInfo ( ) ;
58- }
59- } ;
60-
61- const handleBlur = ( ) => {
62- updateSandboxInfo ( ) ;
63- } ;
64-
65- const handleInputUpdate = ( e : React . ChangeEvent < HTMLInputElement > ) => {
66- valueChanged ( {
67- field : 'title' ,
68- value : e . target . value ,
69- } ) ;
70-
71- setName ( e . target . value ) ;
72- } ;
73-
74- const value = name !== 'Untitled' && updatingName ? name : '' ;
75-
76- const folderName = currentSandbox . collection
77- ? basename ( currentSandbox . collection . path ) ||
78- ( currentSandbox . team ? currentSandbox . team . name : 'My Sandboxes' )
79- : 'My Sandboxes' ;
80-
81- const template = currentSandbox . customTemplate && ! updatingName ;
82- const spring = useSpring ( {
83- opacity : updatingName ? 0 : 1 ,
84- pointerEvents : updatingName ? 'none' : 'initial' ,
85- } ) ;
86- const { customTemplate, owned } = currentSandbox ;
87-
88- return (
89- < Main >
90- < Container >
91- { ! customTemplate && owned && (
92- < animated . div style = { spring } >
93- < Folder >
94- { isLoggedIn ? (
95- < FolderName
96- onClick = { ( ) => modalOpened ( { modal : 'moveSandbox' } ) }
97- >
98- { folderName }
99- </ FolderName >
100- ) : (
101- 'Anonymous '
102- ) }
103- /{ ' ' }
104- </ Folder >
105- </ animated . div >
106- ) }
107- { updatingName ? (
108- < Form onSubmit = { submitNameChange } >
109- < NameInput
110- autoFocus
111- innerRef = { ( el : HTMLInputElement ) => {
112- if ( el ) {
113- el . focus ( ) ;
114- }
115- } }
116- onKeyUp = { handleKeyUp }
117- onBlur = { handleBlur }
118- onChange = { handleInputUpdate }
119- placeholder = { name }
120- value = { value }
121- />
122- </ Form >
123- ) : (
124- < Name owned = { owned } onClick = { owned ? handleNameClick : null } >
125- { sandboxName }
126- </ Name >
127- ) }
128- { template ? (
129- < Tooltip
130- interactive
131- delay = { 0 }
132- placement = "bottom"
133- content = {
134- < >
135- This sandbox is a template, you can learn about templates in
136- the{ ' ' }
137- < Link target = "_blank" to = "/docs/templates" >
138- docs
139- </ Link >
140- .
141- </ >
142- }
143- >
144- < TemplateBadge color = { customTemplate . color } >
145- Template
146- </ TemplateBadge >
147- </ Tooltip >
148- ) : null }
149- </ Container >
150- </ Main >
151- ) ;
28+ const noop = ( ) => undefined ;
29+ export const SandboxName : FunctionComponent = ( ) => {
30+ const {
31+ actions : {
32+ modalOpened,
33+ workspace : { sandboxInfoUpdated, valueChanged } ,
34+ } ,
35+ state : {
36+ editor : { currentSandbox } ,
37+ isLoggedIn,
38+ } ,
39+ } = useOvermind ( ) ;
40+ const [ updatingName , setUpdatingName ] = useState ( false ) ;
41+ const [ name , setName ] = useState ( '' ) ;
42+
43+ const sandboxName = getSandboxName ( currentSandbox ) || 'Untitled' ;
44+
45+ const updateSandboxInfo = ( ) => {
46+ sandboxInfoUpdated ( ) ;
47+ setUpdatingName ( false ) ;
48+ } ;
49+
50+ const submitNameChange = ( e : ChangeEvent < HTMLFormElement > ) => {
51+ e . preventDefault ( ) ;
52+ updateSandboxInfo ( ) ;
53+ track ( 'Change Sandbox Name From Header' ) ;
54+ } ;
55+
56+ const handleNameClick = ( ) => {
57+ setUpdatingName ( true ) ;
58+ setName ( sandboxName ) ;
59+ } ;
60+
61+ const handleKeyUp = ( e : KeyboardEvent < HTMLInputElement > ) => {
62+ if ( e . keyCode === ESC ) {
63+ updateSandboxInfo ( ) ;
15264 }
153- )
154- ) ;
65+ } ;
66+
67+ const handleBlur = ( ) => {
68+ updateSandboxInfo ( ) ;
69+ } ;
70+
71+ const handleInputUpdate = ( e : ChangeEvent < HTMLInputElement > ) => {
72+ valueChanged ( {
73+ field : 'title' ,
74+ value : e . target . value ,
75+ } ) ;
76+
77+ setName ( e . target . value ) ;
78+ } ;
79+
80+ const value = name !== 'Untitled' && updatingName ? name : '' ;
81+
82+ const folderName = currentSandbox . collection
83+ ? basename ( currentSandbox . collection . path ) ||
84+ ( currentSandbox . team ? currentSandbox . team . name : 'My Sandboxes' )
85+ : 'My Sandboxes' ;
86+
87+ const template = currentSandbox . customTemplate && ! updatingName ;
88+ const spring = useSpring ( {
89+ opacity : updatingName ? 0 : 1 ,
90+ pointerEvents : updatingName ? 'none' : 'initial' ,
91+ } ) ;
92+ const { customTemplate, owned } = currentSandbox ;
93+
94+ return (
95+ < Main >
96+ < Container >
97+ { ! customTemplate && owned && (
98+ < animated . div style = { spring } >
99+ < Folder >
100+ { isLoggedIn ? (
101+ < FolderName
102+ onClick = { ( ) => modalOpened ( { modal : 'moveSandbox' } ) }
103+ >
104+ { folderName }
105+ </ FolderName >
106+ ) : (
107+ 'Anonymous '
108+ ) }
109+ /{ ' ' }
110+ </ Folder >
111+ </ animated . div >
112+ ) }
113+
114+ { updatingName ? (
115+ < Form onSubmit = { submitNameChange } >
116+ < NameInput
117+ autoFocus
118+ innerRef = { ( el : HTMLInputElement ) => {
119+ if ( el ) {
120+ el . focus ( ) ;
121+ }
122+ } }
123+ onBlur = { handleBlur }
124+ onChange = { handleInputUpdate }
125+ onKeyUp = { handleKeyUp }
126+ placeholder = { name }
127+ value = { value }
128+ />
129+ </ Form >
130+ ) : (
131+ < Name onClick = { owned ? handleNameClick : noop } owned = { owned } >
132+ { sandboxName }
133+ </ Name >
134+ ) }
135+
136+ { template ? (
137+ < Tooltip
138+ content = {
139+ < >
140+ This sandbox is a template, you can learn about templates in the{ ' ' }
141+ < Link target = "_blank" to = "/docs/templates" >
142+ docs
143+ </ Link >
144+ .
145+ </ >
146+ }
147+ delay = { 0 }
148+ interactive
149+ placement = "bottom"
150+ >
151+ < TemplateBadge color = { customTemplate . color } > Template</ TemplateBadge >
152+ </ Tooltip >
153+ ) : null }
154+ </ Container >
155+ </ Main >
156+ ) ;
157+ } ;
0 commit comments