@@ -10,12 +10,13 @@ Component that allows a user to select a directory in a project.
10
10
*/
11
11
12
12
import { join } from "path" ;
13
- import { Button , Card , Checkbox , Input , Tooltip } from "antd" ;
13
+ import { Button , Card , Checkbox , Input , InputRef , Modal , Tooltip } from "antd" ;
14
14
import {
15
15
CSSProperties ,
16
16
ReactNode ,
17
17
useCallback ,
18
18
useEffect ,
19
+ useRef ,
19
20
useState ,
20
21
} from "react" ;
21
22
import { Icon , Loading } from "@cocalc/frontend/components" ;
@@ -26,6 +27,7 @@ import { delay } from "awaiting";
26
27
import { redux , useTypedRedux } from "@cocalc/frontend/app-framework" ;
27
28
import useIsMountedRef from "@cocalc/frontend/app-framework/is-mounted-hook" ;
28
29
import { useFrameContext } from "@cocalc/frontend/frame-editors/frame-tree/frame-context" ;
30
+ import ShowError from "@cocalc/frontend/components/error" ;
29
31
30
32
const NEW_FOLDER = "New Folder" ;
31
33
@@ -419,6 +421,7 @@ function Subdirs(props) {
419
421
project_id,
420
422
showHidden,
421
423
style,
424
+ toggleSelection,
422
425
} = props ;
423
426
const x = directoryListings ?. get ( path ) ;
424
427
const v = x ?. toJS ?.( ) ;
@@ -457,6 +460,7 @@ function Subdirs(props) {
457
460
path = { path }
458
461
computeServerId = { computeServerId }
459
462
directoryListings = { directoryListings }
463
+ toggleSelection = { toggleSelection }
460
464
/> ,
461
465
) ;
462
466
return (
@@ -467,62 +471,113 @@ function Subdirs(props) {
467
471
}
468
472
}
469
473
474
+ async function getValidPath (
475
+ project_id ,
476
+ target ,
477
+ directoryListings ,
478
+ computeServerId ,
479
+ ) {
480
+ if (
481
+ await pathExists ( project_id , target , directoryListings , computeServerId )
482
+ ) {
483
+ let i : number = 1 ;
484
+ while (
485
+ await pathExists (
486
+ project_id ,
487
+ target + ` (${ i } )` ,
488
+ directoryListings ,
489
+ computeServerId ,
490
+ )
491
+ ) {
492
+ i += 1 ;
493
+ }
494
+ target += ` (${ i } )` ;
495
+ }
496
+ return target ;
497
+ }
498
+
470
499
function CreateDirectory ( {
471
500
computeServerId,
472
501
project_id,
473
502
path,
474
503
directoryListings,
504
+ toggleSelection,
475
505
} ) {
476
- return (
477
- < div
478
- style = { { cursor : "pointer" , color : "#666" } }
479
- key = { "...-create-dir" }
480
- onClick = { async ( ) => {
481
- let target = path + ( path != "" ? "/" : "" ) + NEW_FOLDER ;
482
- if (
483
- await pathExists (
506
+ const [ error , setError ] = useState < string > ( "" ) ;
507
+ const [ open , setOpen ] = useState < boolean > ( false ) ;
508
+ const [ value , setValue ] = useState < string > ( NEW_FOLDER ) ;
509
+ const input_ref = useRef < InputRef > ( null ) ;
510
+
511
+ useEffect ( ( ) => {
512
+ if ( ! open ) {
513
+ return ;
514
+ }
515
+ const target = path + ( path != "" ? "/" : "" ) + value ;
516
+ ( async ( ) => {
517
+ try {
518
+ setValue (
519
+ await getValidPath (
484
520
project_id ,
485
521
target ,
486
522
directoryListings ,
487
523
computeServerId ,
488
- )
489
- ) {
490
- let i : number = 1 ;
491
- while (
492
- await pathExists (
493
- project_id ,
494
- target + ` (${ i } )` ,
495
- directoryListings ,
496
- computeServerId ,
497
- )
498
- ) {
499
- i += 1 ;
500
- }
501
- target += ` (${ i } )` ;
502
- }
503
- try {
504
- await exec ( {
505
- command : "mkdir" ,
506
- args : [ "-p" , target ] ,
507
- project_id,
508
- compute_server_id : computeServerId ,
509
- filesystem : true ,
510
- } ) ;
511
- } catch ( err ) {
512
- alert_message ( { type : "error" , message : err . toString ( ) } ) ;
513
- }
514
- } }
515
- >
524
+ ) ,
525
+ ) ;
526
+ input_ref . current ?. select ( ) ;
527
+ } catch ( err ) {
528
+ setError ( `${ err } ` ) ;
529
+ }
530
+ } ) ( ) ;
531
+ } , [ open ] ) ;
532
+
533
+ const createFolder = async ( ) => {
534
+ setOpen ( false ) ;
535
+ try {
536
+ await exec ( {
537
+ command : "mkdir" ,
538
+ args : [ "-p" , value ] ,
539
+ project_id,
540
+ compute_server_id : computeServerId ,
541
+ filesystem : true ,
542
+ } ) ;
543
+ toggleSelection ( value ) ;
544
+ } catch ( err ) {
545
+ setError ( `${ err } ` ) ;
546
+ }
547
+ } ;
548
+
549
+ return (
550
+ < div style = { { color : "#666" } } key = { "...-create-dir" } >
516
551
< Tooltip
517
552
title = "Create a new directory (double click to rename)"
518
553
placement = "left"
519
554
mouseEnterDelay = { 0.9 }
520
555
>
521
- < Button size = "small" type = "text" style = { { color : "#666" } } >
556
+ < Modal open = { open } onOk = { createFolder } onCancel = { ( ) => setOpen ( false ) } >
557
+ < Input
558
+ ref = { input_ref }
559
+ title = "Create Folder"
560
+ style = { { marginTop : "30px" } }
561
+ value = { value }
562
+ onChange = { ( e ) => setValue ( e . target . value ) }
563
+ onPressEnter = { createFolder }
564
+ autoFocus
565
+ />
566
+ </ Modal >
567
+ < Button
568
+ size = "small"
569
+ type = "text"
570
+ style = { { color : "#666" } }
571
+ disabled = { open }
572
+ onClick = { ( ) => {
573
+ setOpen ( true ) ;
574
+ } }
575
+ >
522
576
< Icon name = "plus" style = { { marginRight : "5px" } } /> Create{ " " }
523
- { NEW_FOLDER }
577
+ { NEW_FOLDER } ...
524
578
</ Button >
525
579
</ Tooltip >
580
+ < ShowError error = { error } setError = { setError } />
526
581
</ div >
527
582
) ;
528
583
}
0 commit comments