@@ -498,95 +498,121 @@ export default function FileExplorer({ onFileSelect }: FileExplorerProps) {
498
498
} ;
499
499
500
500
const deleteItem = async (
501
- nodeId : string ,
502
- nodeName : string ,
503
- nodeType : "file" | "folder"
504
- ) => {
505
- const confirmMessage = `Are you sure you want to delete this ${ nodeType } ${
506
- nodeType === "folder" ? " and all its contents" : ""
507
- } ?`;
508
- if ( ! confirm ( confirmMessage ) ) {
509
- return ;
510
- }
511
-
512
- const fullPath = getNodeFullPath ( fileTree , nodeId ) ;
513
- if ( ! fullPath ) {
514
- console . error ( "Could not find full path for node" ) ;
515
- return ;
516
- }
517
-
518
- try {
519
- // Delete the file/folder
520
- const response = await fetch ( `${ API_URL } /api/files` , {
521
- method : "DELETE" ,
522
- headers : {
523
- "Content-Type" : "application/json" ,
524
- } ,
525
- body : JSON . stringify ( {
526
- path : fullPath ,
527
- type : nodeType ,
528
- } ) ,
529
- } ) ;
501
+ nodeId : string ,
502
+ nodeName : string ,
503
+ nodeType : 'file' | 'folder'
504
+ ) => {
505
+ const confirmMessage = `Are you sure you want to delete this ${ nodeType } ${
506
+ nodeType === 'folder' ? ' and all its contents' : ''
507
+ } ?`;
508
+ if ( ! confirm ( confirmMessage ) ) {
509
+ return ;
510
+ }
530
511
531
- if ( ! response . ok ) {
532
- throw new Error ( "Failed to delete item" ) ;
512
+ const fullPath = getNodeFullPath ( fileTree , nodeId ) ;
513
+ if ( ! fullPath ) {
514
+ console . error ( 'Could not find full path for node' ) ;
515
+ return ;
533
516
}
534
517
535
- // Update metadata
536
- const pathParts = fullPath . split ( '/' ) ;
537
- const language = pathParts [ 0 ] ;
538
- const section = pathParts [ 1 ] ;
539
- const fileId = nodeName . replace ( '.json' , '' ) ;
518
+ try {
519
+ console . log ( 'Deleting item from tree:' , { nodeName, fullPath } ) ;
520
+ const response = await fetch ( `${ API_URL } /api/files` , {
521
+ method : 'DELETE' ,
522
+ headers : {
523
+ 'Content-Type' : 'application/json' ,
524
+ } ,
525
+ body : JSON . stringify ( {
526
+ path : fullPath ,
527
+ type : nodeType ,
528
+ } ) ,
529
+ } ) ;
530
+
531
+ if ( ! response . ok ) {
532
+ throw new Error ( 'Failed to delete item' ) ;
533
+ }
540
534
541
- // Get current metadata
542
- const rootMetaPath = `${ language } /${ section } /_meta.json` ;
543
- const metaResponse = await fetch ( `${ API_URL } /api/files?path=${ encodeURIComponent ( rootMetaPath ) } ` ) ;
544
-
545
- if ( metaResponse . ok ) {
546
- const rootMeta = await metaResponse . json ( ) as RootMeta ;
547
-
548
- // Navigate through the path to find the right section
549
- let currentSection = rootMeta ;
550
- for ( let i = 2 ; i < pathParts . length - 1 ; i ++ ) {
551
- const part = pathParts [ i ] ;
552
-
553
- // Convert path to camelCase for section key
554
- const sectionKey = part . replace ( / - / g, ' ' )
555
- . split ( ' ' )
556
- . map ( ( word , index ) => {
557
- const capitalized = word . charAt ( 0 ) . toUpperCase ( ) + word . slice ( 1 ) ;
558
- return index === 0 ? capitalized . toLowerCase ( ) : capitalized ;
559
- } )
560
- . join ( '' ) ;
561
-
562
- if ( currentSection [ sectionKey ] && ( currentSection [ sectionKey ] as MetaItem ) . items ) {
563
- currentSection = ( currentSection [ sectionKey ] as MetaItem ) . items ! ;
535
+ // Update metadata
536
+ const pathParts = fullPath . split ( '/' ) ;
537
+ const language = pathParts [ 0 ] ;
538
+ const section = pathParts [ 1 ] ;
539
+ const fileId = nodeName . replace ( '.json' , '' ) ;
540
+
541
+ // Get current metadata
542
+ const rootMetaPath = `${ language } /${ section } /_meta.json` ;
543
+ const metaResponse = await fetch (
544
+ `${ API_URL } /api/files?path=${ encodeURIComponent ( rootMetaPath ) } `
545
+ ) ;
546
+
547
+ if ( metaResponse . ok ) {
548
+ const rootMeta = ( await metaResponse . json ( ) ) as RootMeta ;
549
+
550
+ // Navigate through the path to find the right section
551
+ let currentSection = rootMeta ;
552
+ for ( let i = 2 ; i < pathParts . length - 1 ; i ++ ) {
553
+ const part = pathParts [ i ] ;
554
+
555
+ // Convert path to camelCase for section key
556
+ const sectionKey = part
557
+ . replace ( / - / g, ' ' )
558
+ . split ( ' ' )
559
+ . map ( ( word , index ) => {
560
+ const capitalized =
561
+ word . charAt ( 0 ) . toUpperCase ( ) + word . slice ( 1 ) ;
562
+ return index === 0 ? capitalized . toLowerCase ( ) : capitalized ;
563
+ } )
564
+ . join ( '' ) ;
565
+
566
+ // For deletion, we only need to delete the key itself,
567
+ // whether it's a file or folder
568
+ if ( i === pathParts . length - 2 ) {
569
+ // We've reached the parent level where we need to delete
570
+ if ( currentSection [ sectionKey ] ) {
571
+ delete currentSection [ sectionKey ] ;
572
+ }
573
+ break ;
574
+ } else if (
575
+ currentSection [ sectionKey ] &&
576
+ ( currentSection [ sectionKey ] as MetaItem ) . items
577
+ ) {
578
+ currentSection = ( currentSection [ sectionKey ] as MetaItem ) . items ! ;
579
+ }
564
580
}
565
- }
566
581
567
- // Remove the entry
568
- if ( nodeType === 'file' && currentSection [ fileId ] ) {
569
- delete currentSection [ fileId ] ;
582
+ // If it's a top-level item
583
+ if ( pathParts . length <= 3 ) {
584
+ const key = fileId
585
+ . replace ( / - / g, ' ' )
586
+ . split ( ' ' )
587
+ . map ( ( word , index ) => {
588
+ const capitalized =
589
+ word . charAt ( 0 ) . toUpperCase ( ) + word . slice ( 1 ) ;
590
+ return index === 0 ? capitalized . toLowerCase ( ) : capitalized ;
591
+ } )
592
+ . join ( '' ) ;
593
+
594
+ if ( currentSection [ key ] ) {
595
+ delete currentSection [ key ] ;
596
+ }
597
+ }
570
598
571
599
// Save updated metadata
572
600
await fetch ( `${ API_URL } /api/files` , {
573
601
method : 'POST' ,
574
602
headers : { 'Content-Type' : 'application/json' } ,
575
603
body : JSON . stringify ( {
576
604
path : rootMetaPath ,
577
- content : rootMeta
578
- } )
605
+ content : rootMeta ,
606
+ } ) ,
579
607
} ) ;
580
608
}
581
- }
582
609
583
- // Update the file tree state
584
- const updatedTree = deleteItemFromTree ( fileTree , nodeId ) ;
585
- setFileTree ( updatedTree ) ;
586
- } catch ( error ) {
587
- console . error ( "Error deleting item:" , error ) ;
588
- }
589
- } ;
610
+ const updatedTree = deleteItemFromTree ( fileTree , nodeId ) ;
611
+ setFileTree ( updatedTree ) ;
612
+ } catch ( error ) {
613
+ console . error ( 'Error deleting item:' , error ) ;
614
+ }
615
+ } ;
590
616
591
617
const deleteItemFromTree = ( tree : FileNode [ ] , nodeId : string ) : FileNode [ ] => {
592
618
return tree . filter ( ( node ) => {
0 commit comments