@@ -25,7 +25,7 @@ import { DirectoryItemIcon } from '../../utils/DirectoryItemIcon';
2525const FileExplorer : React . FC = ( ) => {
2626 const [ draggedItem , setDraggedItem ] = useState < DirectoryItem | null > ( ) ;
2727 const [ dropTarget , setDropTarget ] = useState < string > ( "" ) ;
28- const [ sortConfig , setSortConfig ] = useState < { key : string , direction : 'asc' | 'desc' } > ( { key : 'name ' , direction : 'asc ' } ) ;
28+ const [ sortConfig , setSortConfig ] = useState < { key : string , direction : 'asc' | 'desc' } > ( { key : 'date ' , direction : 'desc ' } ) ;
2929 const childElements = useRef < Array < HTMLDivElement > > ( [ ] ) ;
3030 const previousDirRef = useRef < HTMLButtonElement | null > ( null ) ;
3131 const [ selectedChildEl , setSelectedChildEl ] = useState < number | null > ( null ) ;
@@ -34,12 +34,7 @@ const FileExplorer: React.FC = () => {
3434 path, directory, setDirectory, directoryInfo, moveItem, itemInfo, setItemInfo, readDir, getParent, setShowCreateItemMenu, downloading, permissions, unzipping, waitingResponse, contextMenu, deleteItem, setContextMenu, scrollIndex, isDirLoading : isLoading
3535 } = useContext < ExplorerContextType > ( ExplorerContext )
3636
37- const handleSort = ( key : string ) => {
38- let direction : 'asc' | 'desc' = 'asc' ;
39- if ( sortConfig . key === key && sortConfig . direction === 'asc' ) {
40- direction = 'desc' ;
41- }
42-
37+ const updateSort = useCallback ( ( key : string , direction : string ) => {
4338 let sortedItems = [ ...directory ?? [ ] ] ;
4439
4540 switch ( key ) {
@@ -66,16 +61,26 @@ const FileExplorer: React.FC = () => {
6661 break ;
6762 }
6863
69- setSortConfig ( { key, direction } ) ;
7064 setDirectory ( sortedItems . map ( ( file , index ) => ( { ...file , id : index } ) ) ) ;
71- } ;
65+ } , [ directory ] )
7266
73- const getSortIcon = ( key : string ) => {
67+ const handleSort = useCallback ( ( key : string ) => {
68+ let direction : 'asc' | 'desc' = 'asc' ;
69+ if ( sortConfig . key === key && sortConfig . direction === 'asc' ) {
70+ direction = 'desc' ;
71+ }
72+
73+ updateSort ( key , direction )
74+
75+ setSortConfig ( { key, direction } ) ;
76+ } , [ sortConfig , updateSort ] ) ;
77+
78+ const getSortIcon = useCallback ( ( key : string ) => {
7479 if ( sortConfig . key !== key ) return < FaSort className = "text-gray-400" /> ;
7580 return sortConfig . direction === 'asc'
7681 ? < FaSortUp className = "text-blue-400" />
7782 : < FaSortDown className = "text-blue-400" /> ;
78- } ;
83+ } , [ sortConfig ] ) ;
7984
8085 function handleContextMenu ( event : React . MouseEvent < HTMLDivElement , MouseEvent > , element : DirectoryItem | null ) {
8186 event . preventDefault ( )
@@ -98,6 +103,12 @@ const FileExplorer: React.FC = () => {
98103 }
99104 } , [ directory ] )
100105
106+ useLayoutEffect ( ( ) => {
107+ if ( ! isLoading ) {
108+ updateSort ( sortConfig . key , sortConfig . direction )
109+ }
110+ } , [ isLoading ] )
111+
101112 const handleDeleteKeyDown = useCallback ( ( event : KeyboardEvent ) => {
102113 if ( event . key !== 'Delete' || itemInfo ?. id == - 1 || ! permissions ?. delete ) {
103114 return
0 commit comments