2024-05-23 11:47:02 -04:00
|
|
|
import { CustomIconName } from 'components/CustomIcon'
|
2024-08-24 17:47:09 -07:00
|
|
|
import { Project } from 'lib/project'
|
2023-08-28 20:31:49 -04:00
|
|
|
|
|
|
|
const DESC = ':desc'
|
|
|
|
|
2024-05-23 11:47:02 -04:00
|
|
|
export function getSortIcon(
|
|
|
|
currentSort: string,
|
|
|
|
newSort: string
|
|
|
|
): CustomIconName {
|
2023-08-28 20:31:49 -04:00
|
|
|
if (currentSort === newSort) {
|
2024-05-23 11:47:02 -04:00
|
|
|
return 'arrowUp'
|
2023-08-28 20:31:49 -04:00
|
|
|
} else if (currentSort === newSort + DESC) {
|
2024-05-23 11:47:02 -04:00
|
|
|
return 'arrowDown'
|
2023-08-28 20:31:49 -04:00
|
|
|
}
|
2024-05-23 11:47:02 -04:00
|
|
|
return 'horizontalDash'
|
2023-08-28 20:31:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getNextSearchParams(currentSort: string, newSort: string) {
|
|
|
|
if (currentSort === null || !currentSort)
|
|
|
|
return { sort_by: newSort + (newSort !== 'modified' ? DESC : '') }
|
|
|
|
if (currentSort.includes(newSort) && !currentSort.includes(DESC))
|
|
|
|
return { sort_by: '' }
|
|
|
|
return {
|
|
|
|
sort_by: newSort + (currentSort.includes(DESC) ? '' : DESC),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getSortFunction(sortBy: string) {
|
2024-04-25 00:13:09 -07:00
|
|
|
const sortByName = (a: Project, b: Project) => {
|
2023-08-28 20:31:49 -04:00
|
|
|
if (a.name && b.name) {
|
|
|
|
return sortBy.includes('desc')
|
|
|
|
? a.name.localeCompare(b.name)
|
|
|
|
: b.name.localeCompare(a.name)
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2024-04-25 00:13:09 -07:00
|
|
|
const sortByModified = (a: Project, b: Project) => {
|
|
|
|
if (a.metadata?.modified && b.metadata?.modified) {
|
|
|
|
const aDate = new Date(a.metadata.modified)
|
|
|
|
const bDate = new Date(b.metadata.modified)
|
2023-08-28 20:31:49 -04:00
|
|
|
return !sortBy || sortBy.includes('desc')
|
2024-04-25 00:13:09 -07:00
|
|
|
? bDate.getTime() - aDate.getTime()
|
|
|
|
: aDate.getTime() - bDate.getTime()
|
2023-08-28 20:31:49 -04:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sortBy?.includes('name')) {
|
|
|
|
return sortByName
|
|
|
|
} else {
|
|
|
|
return sortByModified
|
|
|
|
}
|
|
|
|
}
|