From 2e83c9b41055c967a4cf76eadad2a19f7345dd52 Mon Sep 17 00:00:00 2001 From: Ramesh Mane <101566080+rameshmane7218@users.noreply.github.com> Date: Tue, 2 Jan 2024 08:15:24 +0000 Subject: [PATCH] feat: added object validator helper function --- packages/nc-gui/composables/useUserSorts.ts | 19 +++++++---- .../nc-gui/helpers/parsers/parserHelpers.ts | 33 +++++++++++++++++++ 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/packages/nc-gui/composables/useUserSorts.ts b/packages/nc-gui/composables/useUserSorts.ts index 8d59793a71..aa09f0dccc 100644 --- a/packages/nc-gui/composables/useUserSorts.ts +++ b/packages/nc-gui/composables/useUserSorts.ts @@ -2,6 +2,7 @@ import rfdc from 'rfdc' import { OrgUserRoles, ProjectRoles, WorkspaceUserRoles } from 'nocodb-sdk' import type { UsersSortType } from '~/lib' import { useGlobal } from '#imports' +import { isValidObjectType } from '~/helpers/parsers/parserHelpers' /** * Hook for managing user sorts and sort configurations. @@ -41,11 +42,15 @@ export function useUserSorts(roleType: 'Workspace' | 'Org' | 'Project') { // Retrieve sort configuration from local storage const storedConfig = localStorage.getItem(userSortConfigKey) - const sortConfig = storedConfig ? JSON.parse(storedConfig) : {} - sorts.value = sortConfig + const sortConfig = storedConfig ? JSON.parse(storedConfig) : ({} as Record) + if (isValidObjectType(sortConfig, {} as Record)) { + sorts.value = sortConfig - // Load user-specific sort configurations or default configurations - sorts.value = user.value?.id ? sortConfig[user.value.id] || {} : sortConfig[defaultUserId] || {} + // Load user-specific sort configurations or default configurations + sorts.value = user.value?.id ? sortConfig[user.value.id] || {} : sortConfig[defaultUserId] || {} + } else { + sorts.value = {} + } } catch (error) { console.error('Error while retrieving sort configuration from local storage:', error) // Set sorts to an empty obj in case of an error @@ -135,10 +140,10 @@ export function useUserSorts(roleType: 'Workspace' | 'Org' | 'Project') { }) if (superUser && superUser.length) { - if (sortsConfig.direction === 'asc') { - sortedData = [superUser[0], ...sortedData] - } else { + if (sortsConfig.direction === 'desc') { sortedData = [...sortedData, superUser[0]] + } else { + sortedData = [superUser[0], ...sortedData] } } diff --git a/packages/nc-gui/helpers/parsers/parserHelpers.ts b/packages/nc-gui/helpers/parsers/parserHelpers.ts index b629fe4192..f2c391c020 100644 --- a/packages/nc-gui/helpers/parsers/parserHelpers.ts +++ b/packages/nc-gui/helpers/parsers/parserHelpers.ts @@ -225,3 +225,36 @@ export const extractNextDefaultName = (namesData: string[], defaultName: string, ? `${defaultName}${splitOperator}${extractedSortedNumbers[extractedSortedNumbers.length - 1] + 1}` : `${defaultName}${splitOperator}1` } + +/** + * Validates whether an object matches the expected type. + * + * @param obj - The object to be validated. + * @param expectedType - The expected type of the object. + * @returns `true` if the object matches the expected type, `false` otherwise. + * + * @template T - The type representing the expected structure of the object. + * + * @example + * // Define a type representing the expected structure. + * interface MyType { + * id: number; + * name: string; + * } + * + * // Object to validate + * const objToValidate = { + * id: 1, + * name: 'John Doe', + * }; + * + * // Check if the object matches the expected type. + * const isValid = isValidObjectType(objToValidate, {} as MyType); + * console.log(isValid); // Should print true + */ +export const isValidObjectType = >(obj: Record, expectedType: T): boolean => { + // Check if the obj has the expected keys and values + return Object.entries(expectedType).every(([key, expectedValue]) => { + return Object.prototype.hasOwnProperty.call(obj, key) && typeof obj[key] === typeof expectedValue + }) +}