|
|
@ -2,7 +2,6 @@ import rfdc from 'rfdc' |
|
|
|
import { OrgUserRoles, ProjectRoles, WorkspaceUserRoles } from 'nocodb-sdk' |
|
|
|
import { OrgUserRoles, ProjectRoles, WorkspaceUserRoles } from 'nocodb-sdk' |
|
|
|
import type { UsersSortType } from '~/lib' |
|
|
|
import type { UsersSortType } from '~/lib' |
|
|
|
import { useGlobal } from '#imports' |
|
|
|
import { useGlobal } from '#imports' |
|
|
|
import { isValidObjectType } from '~/helpers/parsers/parserHelpers' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Hook for managing user sorts and sort configurations. |
|
|
|
* Hook for managing user sorts and sort configurations. |
|
|
@ -43,16 +42,17 @@ export function useUserSorts(roleType: 'Workspace' | 'Org' | 'Project') { |
|
|
|
const storedConfig = localStorage.getItem(userSortConfigKey) |
|
|
|
const storedConfig = localStorage.getItem(userSortConfigKey) |
|
|
|
|
|
|
|
|
|
|
|
const sortConfig = storedConfig ? JSON.parse(storedConfig) : ({} as Record<string, UsersSortType>) |
|
|
|
const sortConfig = storedConfig ? JSON.parse(storedConfig) : ({} as Record<string, UsersSortType>) |
|
|
|
if (isValidObjectType(sortConfig, {} as Record<string, UsersSortType>)) { |
|
|
|
|
|
|
|
sorts.value = sortConfig |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (sortConfig && isValidSortConfig(sortConfig)) { |
|
|
|
|
|
|
|
sorts.value = sortConfig |
|
|
|
// Load user-specific sort configurations or default configurations
|
|
|
|
// Load user-specific sort configurations or default configurations
|
|
|
|
sorts.value = user.value?.id ? sortConfig[user.value.id] || {} : sortConfig[defaultUserId] || {} |
|
|
|
sorts.value = user.value?.id ? sortConfig[user.value.id] || {} : sortConfig[defaultUserId] || {} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
|
|
|
|
// remove sortConfig from localStorage if it's invalid
|
|
|
|
|
|
|
|
localStorage.removeItem(userSortConfigKey) |
|
|
|
sorts.value = {} |
|
|
|
sorts.value = {} |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (error) { |
|
|
|
} catch { |
|
|
|
console.error('Error while retrieving sort configuration from local storage:', error) |
|
|
|
|
|
|
|
// Set sorts to an empty obj in case of an error
|
|
|
|
// Set sorts to an empty obj in case of an error
|
|
|
|
sorts.value = {} |
|
|
|
sorts.value = {} |
|
|
|
} |
|
|
|
} |
|
|
@ -150,5 +150,26 @@ export function useUserSorts(roleType: 'Workspace' | 'Org' | 'Project') { |
|
|
|
return sortedData |
|
|
|
return sortedData |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Checks if the provided sort configuration has the expected structure. |
|
|
|
|
|
|
|
* @param sortConfig - The sort configuration to validate. |
|
|
|
|
|
|
|
* @param expectedStructure - The expected structure for the sort configuration. |
|
|
|
|
|
|
|
* Defaults to { field: 'email', direction: 'asc' }. |
|
|
|
|
|
|
|
* @returns `true` if the sort configuration is valid, otherwise `false`. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
function isValidSortConfig( |
|
|
|
|
|
|
|
sortConfig: Record<string, any>, |
|
|
|
|
|
|
|
expectedStructure: UsersSortType = { field: 'email', direction: 'asc' }, |
|
|
|
|
|
|
|
): boolean { |
|
|
|
|
|
|
|
// Check if the sortConfig has the expected keys
|
|
|
|
|
|
|
|
for (const key in sortConfig) { |
|
|
|
|
|
|
|
const isValidConfig = Object.keys(sortConfig[key]).every((key) => |
|
|
|
|
|
|
|
Object.prototype.hasOwnProperty.call(expectedStructure, key), |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
if (!isValidConfig) return false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return true |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return { sorts, sortDirection, loadSorts, saveOrUpdate, handleGetSortsData } |
|
|
|
return { sorts, sortDirection, loadSorts, saveOrUpdate, handleGetSortsData } |
|
|
|
} |
|
|
|
} |
|
|
|