Refactor Views by cleaning the code, relying on apolloCache and improving performances (#4516)

* Wip refactoring view

* Post merge conflicts

* Fix review

* Add create view capability

* Fix create object missing view

* Fix tests
This commit is contained in:
Charles Bochet
2024-03-20 14:21:58 +01:00
committed by GitHub
parent 20e14cb455
commit cfb0cce9b8
392 changed files with 3474 additions and 4410 deletions
@@ -0,0 +1,48 @@
import { useCallback } from 'react';
import { useApolloClient } from '@apollo/client';
import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem';
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { ObjectRecordEdge } from '@/object-record/types/ObjectRecordEdge';
import { GraphQLView } from '@/views/types/GraphQLView';
import { isUndefinedOrNull } from '~/utils/isUndefinedOrNull';
export const useGetViewFromCache = () => {
const client = useApolloClient();
const cache = client.cache;
const { getRecordFromCache } = useObjectMetadataItem({
objectNameSingular: CoreObjectNameSingular.View,
});
const getViewFromCache = useCallback(
async (viewId: string) => {
// Todo Fix typing once we have figured out record connections
const viewWithConnections = getRecordFromCache<any>(viewId, cache);
if (isUndefinedOrNull(viewWithConnections)) {
return;
}
const view = {
...viewWithConnections,
viewFilters: viewWithConnections.viewFilters?.edges.map(
(edge: ObjectRecordEdge) => edge.node,
),
viewSorts: viewWithConnections.viewSorts?.edges.map(
(edge: ObjectRecordEdge) => edge.node,
),
viewFields: viewWithConnections.viewFields?.edges.map(
(edge: ObjectRecordEdge) => edge.node,
),
} as GraphQLView;
return view;
},
[cache, getRecordFromCache],
);
return {
getViewFromCache,
};
};