|
|
@ -1,12 +1,13 @@ |
|
|
|
import rfdc from 'rfdc' |
|
|
|
import rfdc from 'rfdc' |
|
|
|
import type { UsersSortType } from '~/lib' |
|
|
|
import type { UsersSortType } from '~/lib' |
|
|
|
import { useGlobal } from '#imports' |
|
|
|
import { useGlobal } from '#imports' |
|
|
|
|
|
|
|
import { extractRolesObj, ProjectRoles, OrgUserRoles, WorkspaceUserRoles } from 'nocodb-sdk' |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Hook for managing user sorts and sort configurations. |
|
|
|
* Hook for managing user sorts and sort configurations. |
|
|
|
* @returns An object containing reactive values and functions related to user sorts. |
|
|
|
* @returns An object containing reactive values and functions related to user sorts. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
export function useUserSorts() { |
|
|
|
export function useUserSorts(roleType: 'Workspace' | 'Org' | 'Project') { |
|
|
|
const clone = rfdc() |
|
|
|
const clone = rfdc() |
|
|
|
|
|
|
|
|
|
|
|
const { user } = useGlobal() |
|
|
|
const { user } = useGlobal() |
|
|
@ -68,7 +69,7 @@ export function useUserSorts() { |
|
|
|
} |
|
|
|
} |
|
|
|
return sort |
|
|
|
return sort |
|
|
|
}) |
|
|
|
}) |
|
|
|
.filter((sort) => sort.field !== newSortConfig.field), // For now it is only single level of sorting so remove another sort field
|
|
|
|
.filter((sort) => sort.field === newSortConfig.field), // For now it is only single level of sorting so remove another sort field
|
|
|
|
] |
|
|
|
] |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
// Add a new sort configuration
|
|
|
|
// Add a new sort configuration
|
|
|
@ -112,18 +113,55 @@ export function useUserSorts() { |
|
|
|
* @template T - The type of objects in the input array. |
|
|
|
* @template T - The type of objects in the input array. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
function handleGetSortsData<T extends Record<string, any>>(data: T[], sortsConfig: UsersSortType[] = sorts.value): T[] { |
|
|
|
function handleGetSortsData<T extends Record<string, any>>(data: T[], sortsConfig: UsersSortType[] = sorts.value): T[] { |
|
|
|
const sortedData = clone(data).sort((a, b) => { |
|
|
|
let userRoleOrder: string[] = [] |
|
|
|
|
|
|
|
if (roleType === 'Workspace') { |
|
|
|
|
|
|
|
userRoleOrder = Object.values(WorkspaceUserRoles) |
|
|
|
|
|
|
|
} else if (roleType === 'Org') { |
|
|
|
|
|
|
|
userRoleOrder = Object.values(OrgUserRoles) |
|
|
|
|
|
|
|
} else if (roleType === 'Project') { |
|
|
|
|
|
|
|
userRoleOrder = Object.values(ProjectRoles) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
data = clone(data) |
|
|
|
|
|
|
|
// let superUserIndex = data.findIndex((user) => user?.roles?.includes('super'))
|
|
|
|
|
|
|
|
// let superUser = superUserIndex !== -1 ? data.splice(superUserIndex, 1) : null
|
|
|
|
|
|
|
|
// console.log('super', superUser)
|
|
|
|
|
|
|
|
const sortedData = data.sort((a, b) => { |
|
|
|
let sortCondition = 0 |
|
|
|
let sortCondition = 0 |
|
|
|
|
|
|
|
|
|
|
|
for (const { field, direction } of sortsConfig) { |
|
|
|
for (const { field, direction } of sortsConfig) { |
|
|
|
if (!a[field]) continue |
|
|
|
if (!a[field]) continue |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (field === 'roles') { |
|
|
|
|
|
|
|
for (const role of userRoleOrder) { |
|
|
|
|
|
|
|
const indexA = a?.roles?.split(',')?.indexOf(role) ?? -1 |
|
|
|
|
|
|
|
const indexB = b?.roles?.split(',')?.indexOf(role) ?? -1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if (indexA === -1) {
|
|
|
|
|
|
|
|
// sortCondition = sortCondition || direction === 'asc' ? 1 : -1 // Role A is missing, so it should come last
|
|
|
|
|
|
|
|
// break
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if (indexB === -1) {
|
|
|
|
|
|
|
|
// sortCondition = sortCondition || direction === 'asc' ? -1 : 1 // Role B is missing, so it should come last
|
|
|
|
|
|
|
|
// break
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (direction === 'asc') { |
|
|
|
|
|
|
|
sortCondition = sortCondition || indexA - indexB |
|
|
|
|
|
|
|
break |
|
|
|
|
|
|
|
} else if (direction === 'desc') { |
|
|
|
|
|
|
|
sortCondition = sortCondition || indexB - indexA |
|
|
|
|
|
|
|
break |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
if (direction === 'asc') { |
|
|
|
if (direction === 'asc') { |
|
|
|
sortCondition = sortCondition || a[field]?.localeCompare(b[field]) |
|
|
|
sortCondition = sortCondition || a[field]?.localeCompare(b[field]) |
|
|
|
} else if (direction === 'desc') { |
|
|
|
} else if (direction === 'desc') { |
|
|
|
sortCondition = sortCondition || b[field]?.localeCompare(a[field]) |
|
|
|
sortCondition = sortCondition || b[field]?.localeCompare(a[field]) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return sortCondition |
|
|
|
return sortCondition |
|
|
|
}) |
|
|
|
}) |
|
|
|