Improve Performances of FE by reducing first print queries (#2623)

This commit is contained in:
Charles Bochet
2023-11-21 22:47:49 +01:00
committed by GitHub
parent c74bde28b8
commit 77733f2bc8
26 changed files with 304 additions and 331 deletions

View File

@ -1,10 +1,8 @@
import { useState } from 'react';
import { useApolloClient } from '@apollo/client';
import { OnDragEndResponder } from '@hello-pangea/dnd';
import { useRecoilCallback, useRecoilState, useRecoilValue } from 'recoil';
import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState';
import { Company } from '@/companies/types/Company';
import { Favorite } from '@/favorites/types/Favorite';
import { mapFavorites } from '@/favorites/utils/mapFavorites';
import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem';
@ -35,55 +33,7 @@ export const useFavorites = ({
const apolloClient = useApolloClient();
const [allCompanies, setAllCompanies] = useState<
Record<string, { name: string; domainName?: string }>
>({});
const [allPeople, setAllPeople] = useState<
Record<string, { firstName: string; lastName: string; avatarUrl?: string }>
>({});
// This is only temporary and will be refactored once we have main identifiers
const { loading: companiesLoading } = useFindManyObjectRecords({
objectNamePlural: 'companies',
onCompleted: async (
data: PaginatedObjectTypeResults<Required<Company>>,
) => {
setAllCompanies(
data.edges.reduce(
(acc, { node: company }) => ({
...acc,
[company.id]: {
name: company.name,
domainName: company.domainName,
},
}),
{},
),
);
},
});
const { loading: peopleLoading } = useFindManyObjectRecords({
objectNamePlural: 'people',
onCompleted: async (data) => {
setAllPeople(
data.edges.reduce(
(acc, { node: person }) => ({
...acc,
[person.id]: {
firstName: person.firstName,
lastName: person.lastName,
avatarUrl: person.avatarUrl,
},
}),
{},
),
);
},
});
useFindManyObjectRecords({
skip: companiesLoading || peopleLoading,
objectNamePlural: 'favorites',
onCompleted: useRecoilCallback(
({ snapshot, set }) =>
@ -92,17 +42,13 @@ export const useFavorites = ({
const queriedFavorites = mapFavorites(
data.edges.map((edge) => edge.node),
{
...allCompanies,
...allPeople,
},
);
if (!isDeeplyEqual(favorites, queriedFavorites)) {
set(favoritesState, queriedFavorites);
}
},
[allCompanies, allPeople],
[],
),
});
@ -125,25 +71,20 @@ export const useFavorites = ({
},
});
const createdFavorite = result?.data?.createFavoriteV2;
const createdFavorite = result?.data?.createFavorite;
const newFavorite = {
...additionalData,
...createdFavorite,
};
const newFavoritesMapped = mapFavorites([newFavorite], {
...allCompanies,
...allPeople,
});
const newFavoritesMapped = mapFavorites([newFavorite]);
if (createdFavorite) {
set(favoritesState, [...favorites, ...newFavoritesMapped]);
}
},
[
allCompanies,
allPeople,
apolloClient,
createOneMutation,
currentWorkspaceMember,