@@ -22,6 +22,7 @@ import React, {
2222 useCallback ,
2323 useContext ,
2424 useEffect ,
25+ useMemo ,
2526 useState ,
2627} from 'react'
2728import { TableCellProps } from 'react-virtualized'
@@ -33,7 +34,7 @@ import { ContentContextMenu } from '../context-menu/content-context-menu'
3334import { useDialog } from '../dialogs'
3435import { DropFileArea } from '../DropFileArea'
3536import { SelectionControl } from '../SelectionControl'
36- import { SETTINGS_FOLDER_FILTER } from '../tree/tree-with-data '
37+ import { isFolderLikeTreeItem , SETTINGS_FOLDER_FILTER } from '../tree/tree-helpers '
3738import { ContextMenuWrapper } from './context-menu-wrapper'
3839import {
3940 ActionsField ,
@@ -94,6 +95,8 @@ export const isReferenceField = (fieldName: string, repo: Repository, schema = '
9495
9596const rowHeightConst = 67
9697const headerHeightConst = 48
98+ const displayNameInArray = [ 'DisplayName' ]
99+ const sortableColumns = [ 'DisplayName' , 'Path' , 'Type' , 'Name' , 'Version' , 'CreationDate' , 'ModificationDate' ]
97100
98101/**
99102 * Compare passed minutes with
@@ -121,7 +124,7 @@ const ColumnSettingsContainer: ColumnSettingsContainerType = {}
121124export const ContentList = < T extends GenericContent = GenericContent > ( props : ContentListProps < T > ) => {
122125 const selectionService = useSelectionService ( )
123126 const parentContent = useContext ( CurrentContentContext )
124- const children = useContext ( CurrentChildrenContext ) as T [ ]
127+ const currentChildren = useContext ( CurrentChildrenContext ) as T [ ]
125128 const ancestors = useContext ( CurrentAncestorsContext ) as T [ ]
126129 const device = useContext ( ResponsiveContext )
127130 const personalSettings = useContext ( ResponsivePersonalSettings )
@@ -133,8 +136,6 @@ export const ContentList = <T extends GenericContent = GenericContent>(props: Co
133136 const { openDialog, closeLastDialog } = useDialog ( )
134137 const logger = useLogger ( 'ContentList' )
135138 const localization = useLocalization ( )
136- const [ selected , setSelected ] = useState < T [ ] > ( [ ] )
137- const [ activeContent , setActiveContent ] = useState < T > ( children [ 0 ] )
138139 const [ isFocused , setIsFocused ] = useState ( true )
139140 const [ isContextMenuOpened , setIsContextMenuOpened ] = useState ( false )
140141 const [ schema , setSchema ] = useState ( repo . schemas . getSchemaByName ( props . schema || 'GenericContent' ) )
@@ -150,6 +151,35 @@ export const ContentList = <T extends GenericContent = GenericContent>(props: Co
150151 const [ currentDirection , setCurrentDirection ] = useState < 'asc' | 'desc' > (
151152 ( loadChildrenSettingsOrderBy ?. [ 0 ] [ 1 ] as 'asc' | 'desc' ) || 'asc' ,
152153 )
154+ const children = useMemo ( ( ) => {
155+ return [ ...currentChildren ] . sort ( ( a , b ) => {
156+ if ( userPersonalSettings . sortFoldersFirst ) {
157+ const folderOrder = Number ( ! isFolderLikeTreeItem ( a ) ) - Number ( ! isFolderLikeTreeItem ( b ) )
158+
159+ if ( folderOrder ) {
160+ return folderOrder
161+ }
162+ }
163+
164+ if ( sortableColumns . includes ( String ( currentOrder ) ) ) {
165+ const nameA = String ( a [ currentOrder ] ?? '' )
166+ const nameB = String ( b [ currentOrder ] ?? '' )
167+
168+ return currentDirection === 'asc' ? nameA . localeCompare ( nameB ) : nameB . localeCompare ( nameA )
169+ }
170+
171+ if ( currentOrder === 'CreatedBy' || currentOrder === 'ModifiedBy' ) {
172+ const nameA = String ( ( a [ currentOrder ] as GenericContent ) ?. DisplayName ?? '' )
173+ const nameB = String ( ( b [ currentOrder ] as GenericContent ) ?. DisplayName ?? '' )
174+
175+ return currentDirection === 'asc' ? nameA . localeCompare ( nameB ) : nameB . localeCompare ( nameA )
176+ }
177+
178+ return 0
179+ } )
180+ } , [ currentChildren , currentDirection , currentOrder , userPersonalSettings . sortFoldersFirst ] )
181+ const [ selected , setSelected ] = useState < T [ ] > ( [ ] )
182+ const [ activeContent , setActiveContent ] = useState < T > ( children [ 0 ] )
153183
154184 const [ columnSettings , setColumnSettings ] = useState < Array < ColumnSetting < GenericContent > > > (
155185 personalSettings . content . fields ,
@@ -673,9 +703,6 @@ export const ContentList = <T extends GenericContent = GenericContent>(props: Co
673703 } ,
674704 }
675705
676- const displayNameInArray = [ 'DisplayName' ]
677- const sortableColumns = [ 'DisplayName' , 'Path' , 'Type' , 'Name' , 'Version' , 'CreationDate' , 'ModificationDate' ]
678-
679706 return (
680707 < div style = { { ...props . style , ...{ height : '100%' } } } { ...props . containerProps } >
681708 { props . enableBreadcrumbs ? (
@@ -719,34 +746,7 @@ export const ContentList = <T extends GenericContent = GenericContent>(props: Co
719746 displayNameInArray ) as any
720747 }
721748 getSelectionControl = { getSelectionControl }
722- /* If the Order by Column Is The Display. The client will sort it. Due to some locale and indexing issues */
723- items = {
724- sortableColumns . includes ( String ( currentOrder ) )
725- ? children ?. sort ( ( a , b ) => {
726- // If no display Name
727- const nameA = String ( a [ currentOrder ] ) ?? '' // Provide a default value if displayName is undefined
728- const nameB = String ( b [ currentOrder ] ) ?? '' // Provide a default value if displayName is undefined
729-
730- if ( currentDirection === 'asc' ) {
731- return nameA . localeCompare ( nameB )
732- }
733- return nameB . localeCompare ( nameA )
734- } )
735- : currentOrder === 'CreatedBy' || currentOrder === 'ModifiedBy'
736- ? children ?. sort ( ( a , b ) => {
737- const aTmp = a [ currentOrder ] as GenericContent
738- const bTmp = b [ currentOrder ] as GenericContent
739-
740- const nameA = String ( aTmp ?. DisplayName ) ?? ''
741- const nameB = String ( bTmp ?. DisplayName ) ?? ''
742-
743- if ( currentDirection === 'asc' ) {
744- return nameA . localeCompare ( nameB )
745- }
746- return nameB . localeCompare ( nameA )
747- } )
748- : children
749- }
749+ items = { children }
750750 onRequestOrderChange = { onRequestOrderChangeFunc }
751751 onRequestSelectionChange = { setSelected }
752752 orderBy = { currentOrder }
0 commit comments