Fix performances on view loading (#14859)
## Context We are experiencing bad performance on Twenty. One of the root cause hypothesis is that computing `currentUser.currentWorkspace.views` is CPU consuming. Without views, the GetCurrentUser response is ~1000 lines. With its ~10000 lines. As graphql is going through all fields recursively this can be quite heavy on CPU. We had a similar issues on ObjectMetadataItems 2 years ago and came with storing the response in redis. Note: I thought there was also a cache in RAM but this is not the case, so to invalidate the cache we can just empty redis. ## How - Extract getting all views from GetCurrentUser and update frontend to perform both queries - Add views to cached graphql operations - invalidate the cache manually on view or related core entities update / create / delete / destroy ## Tests I have tested a lot on v1
This commit is contained in:
+35
-24
@@ -22,6 +22,7 @@ import { AppPath, type ObjectPermissions } from 'twenty-shared/types';
|
||||
import { isDefined } from 'twenty-shared/utils';
|
||||
import {
|
||||
type WorkspaceMember,
|
||||
useFindAllCoreViewsQuery,
|
||||
useGetCurrentUserQuery,
|
||||
} from '~/generated-metadata/graphql';
|
||||
import { dateLocaleState } from '~/localization/states/dateLocaleState';
|
||||
@@ -29,7 +30,7 @@ import { dynamicActivate } from '~/utils/i18n/dynamicActivate';
|
||||
import { isDeeplyEqual } from '~/utils/isDeeplyEqual';
|
||||
import { isMatchingLocation } from '~/utils/isMatchingLocation';
|
||||
|
||||
export const UserProviderEffect = () => {
|
||||
export const UserAndViewsProviderEffect = () => {
|
||||
const location = useLocation();
|
||||
|
||||
const [isCurrentUserLoaded, setIsCurrentUserLoaded] = useRecoilState(
|
||||
@@ -82,53 +83,57 @@ export const UserProviderEffect = () => {
|
||||
currentWorkspaceDeletedMembersState,
|
||||
);
|
||||
|
||||
const { data: queryData, loading: queryLoading } = useGetCurrentUserQuery({
|
||||
skip:
|
||||
!isLoggedIn ||
|
||||
isCurrentUserLoaded ||
|
||||
isMatchingLocation(location, AppPath.Verify) ||
|
||||
isMatchingLocation(location, AppPath.VerifyEmail),
|
||||
const shouldSkip =
|
||||
!isLoggedIn ||
|
||||
isCurrentUserLoaded ||
|
||||
isMatchingLocation(location, AppPath.Verify) ||
|
||||
isMatchingLocation(location, AppPath.VerifyEmail);
|
||||
|
||||
const { data: userQueryData, loading: userQueryLoading } =
|
||||
useGetCurrentUserQuery({
|
||||
skip: shouldSkip,
|
||||
});
|
||||
|
||||
const { data: queryDataCoreViews } = useFindAllCoreViewsQuery({
|
||||
skip: shouldSkip,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (!queryLoading) {
|
||||
if (!userQueryLoading) {
|
||||
setIsCurrentUserLoaded(true);
|
||||
}
|
||||
|
||||
if (!isDefined(queryData?.currentUser)) return;
|
||||
if (!isDefined(userQueryData?.currentUser)) return;
|
||||
|
||||
setCurrentUser(queryData.currentUser);
|
||||
setCurrentUser(userQueryData.currentUser);
|
||||
|
||||
if (isDefined(queryData.currentUser.currentWorkspace)) {
|
||||
if (isDefined(userQueryData.currentUser.currentWorkspace)) {
|
||||
setCurrentWorkspace({
|
||||
...queryData.currentUser.currentWorkspace,
|
||||
defaultRole: queryData.currentUser.currentWorkspace.defaultRole ?? null,
|
||||
...userQueryData.currentUser.currentWorkspace,
|
||||
defaultRole:
|
||||
userQueryData.currentUser.currentWorkspace.defaultRole ?? null,
|
||||
defaultAgent:
|
||||
queryData.currentUser.currentWorkspace.defaultAgent ?? null,
|
||||
userQueryData.currentUser.currentWorkspace.defaultAgent ?? null,
|
||||
});
|
||||
}
|
||||
|
||||
if (isDefined(queryData.currentUser.currentUserWorkspace)) {
|
||||
if (isDefined(userQueryData.currentUser.currentUserWorkspace)) {
|
||||
setCurrentUserWorkspace({
|
||||
...queryData.currentUser.currentUserWorkspace,
|
||||
...userQueryData.currentUser.currentUserWorkspace,
|
||||
objectsPermissions:
|
||||
(queryData.currentUser.currentUserWorkspace
|
||||
(userQueryData.currentUser.currentUserWorkspace
|
||||
.objectsPermissions as Array<
|
||||
ObjectPermissions & { objectMetadataId: string }
|
||||
>) ?? [],
|
||||
});
|
||||
}
|
||||
|
||||
if (isDefined(queryData.currentUser?.currentWorkspace?.views)) {
|
||||
setCoreViews(queryData.currentUser.currentWorkspace.views);
|
||||
}
|
||||
|
||||
const {
|
||||
workspaceMember,
|
||||
workspaceMembers,
|
||||
deletedWorkspaceMembers,
|
||||
availableWorkspaces,
|
||||
} = queryData.currentUser;
|
||||
} = userQueryData.currentUser;
|
||||
|
||||
const affectDefaultValuesOnEmptyWorkspaceMemberFields = (
|
||||
workspaceMember: WorkspaceMember,
|
||||
@@ -168,8 +173,8 @@ export const UserProviderEffect = () => {
|
||||
setAvailableWorkspaces(availableWorkspaces);
|
||||
}
|
||||
}, [
|
||||
queryLoading,
|
||||
queryData?.currentUser,
|
||||
userQueryLoading,
|
||||
userQueryData?.currentUser,
|
||||
setCurrentUser,
|
||||
setCurrentUserWorkspace,
|
||||
setCurrentWorkspaceMembers,
|
||||
@@ -183,5 +188,11 @@ export const UserProviderEffect = () => {
|
||||
setCoreViews,
|
||||
]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isDefined(queryDataCoreViews?.getCoreViews)) return;
|
||||
|
||||
setCoreViews(queryDataCoreViews.getCoreViews);
|
||||
}, [queryDataCoreViews?.getCoreViews, setCoreViews]);
|
||||
|
||||
return null;
|
||||
};
|
||||
Reference in New Issue
Block a user