Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upcoming: [DI-21322] - Retain Resource selections during expand / collapse of filter button #11068

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@linode/manager": Upcoming Features
---

Retain resource selection while expand or collapse the filter button ([#11068](https://github.com/linode/manager/pull/11068))
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { dashboardFactory } from 'src/factories';
import { databaseQueries } from 'src/queries/databases/databases';

import { RESOURCES } from './constants';
import { deepEqual } from './FilterBuilder';
import {
buildXFilter,
checkIfAllMandatoryFiltersAreSelected,
Expand Down Expand Up @@ -264,3 +265,39 @@ it('test constructAdditionalRequestFilters method', () => {
expect(result).toBeDefined();
expect(result.length).toEqual(0);
});

it('returns true for identical primitive values', () => {
expect(deepEqual(1, 1)).toBe(true);
expect(deepEqual('test', 'test')).toBe(true);
expect(deepEqual(true, true)).toBe(true);
});

it('returns false for different primitive values', () => {
expect(deepEqual(1, 2)).toBe(false);
expect(deepEqual('test', 'other')).toBe(false);
expect(deepEqual(true, false)).toBe(false);
});

it('returns true for identical objects', () => {
const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { a: 1, b: { c: 2 } };
expect(deepEqual(obj1, obj2)).toBe(true);
});

it('returns false for different objects', () => {
const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { a: 1, b: { c: 3 } };
expect(deepEqual(obj1, obj2)).toBe(false);
});

it('returns true for identical arrays', () => {
const arr1 = [1, 2, 3];
const arr2 = [1, 2, 3];
expect(deepEqual(arr1, arr2)).toBe(true);
});

it('returns false for different arrays', () => {
const arr1 = [1, 2, 3];
const arr2 = [1, 2, 4];
expect(deepEqual(arr1, arr2)).toBe(false);
});
67 changes: 67 additions & 0 deletions packages/manager/src/features/CloudPulse/Utils/FilterBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,70 @@ const getDependentFiltersByFilterKey = (
: configuration.filterKey
);
};


/**
* @param obj1 The first object to be compared
* @param obj2 The second object to be compared
* @returns True if, both are equal else false
*/
export const deepEqual = <T>(obj1: T, obj2: T): boolean => {
if (obj1 === obj2) {
return true; // Identical references or values
}

// If either is null or undefined, or they are not of object type, return false
if (
obj1 === null ||
obj2 === null ||
typeof obj1 !== 'object' ||
typeof obj2 !== 'object'
) {
return false;
}

// Handle array comparison separately
if (Array.isArray(obj1) && Array.isArray(obj2)) {
return compareArrays(obj1, obj2);
}

// Ensure both objects have the same number of keys
const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2);

if (keys1.length !== keys2.length) {
return false;
}

// Recursively check each key
for (const key of keys1) {
if (!(key in obj2)) {
return false;
}
// Recursive deep equal check
if (!deepEqual((obj1 as any)[key], (obj2 as any)[key])) {
return false;
}
}

return true;
};

/**
* @param arr1 Array for comparison
* @param arr2 Array for comparison
* @returns True if, both the arrays are equal, else false
*/
const compareArrays = <T>(arr1: T[], arr2: T[]): boolean => {
if (arr1.length !== arr2.length) {
return false;
}

for (let i = 0; i < arr1.length; i++) {
if (!deepEqual(arr1[i], arr2[i])) {
return false;
}
}

return true;
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { Autocomplete } from 'src/components/Autocomplete/Autocomplete';
import { useResourcesQuery } from 'src/queries/cloudpulse/resources';
import { themes } from 'src/utilities/theme';

import { deepEqual } from '../Utils/FilterBuilder';

import type { Filter, FilterValue } from '@linode/api-v4';

export interface CloudPulseResources {
Expand Down Expand Up @@ -129,7 +131,7 @@ function compareProps(
return false;
}
}
if (prevProps.xFilter !== nextProps.xFilter) {
if (!deepEqual(prevProps.xFilter, nextProps.xFilter)) {
return false;
}

Expand Down