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

Fix infinite loading on field settings #8938

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Expand Up @@ -16,10 +16,12 @@ export const ObjectMetadataItemsLoadEffect = () => {
const currentWorkspace = useRecoilValue(currentWorkspaceState);
const isLoggedIn = useIsLogged();

const { objectMetadataItems: newObjectMetadataItems } =
useFindManyObjectMetadataItems({
skip: !isLoggedIn,
});
const {
objectMetadataItems: newObjectMetadataItems,
loading: isObjectMetadataLoading,
} = useFindManyObjectMetadataItems({
skip: !isLoggedIn,
});

const updateObjectMetadataItems = useRecoilCallback(
({ set, snapshot }) =>
Expand All @@ -32,6 +34,7 @@ export const ObjectMetadataItemsLoadEffect = () => {
: newObjectMetadataItems;

if (
!isObjectMetadataLoading &&
!isDeeplyEqual(
snapshot.getLoadable(objectMetadataItemsState).getValue(),
toSetObjectMetadataItems,
Expand All @@ -40,7 +43,12 @@ export const ObjectMetadataItemsLoadEffect = () => {
set(objectMetadataItemsState, toSetObjectMetadataItems);
}
},
[currentUser, currentWorkspace?.activationStatus, newObjectMetadataItems],
[
currentUser,
currentWorkspace?.activationStatus,
isObjectMetadataLoading,
newObjectMetadataItems,
],
);

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import { FIND_MANY_OBJECT_METADATA_ITEMS } from '../graphql/queries';
import { currentWorkspaceState } from '@/auth/states/currentWorkspaceState';
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { useFindManyRecordsQuery } from '@/object-record/hooks/useFindManyRecordsQuery';
import { GET_CURRENT_USER } from '@/users/graphql/queries/getCurrentUser';
import { useSetRecoilState } from 'recoil';
import { isDefined } from 'twenty-ui';
import { useGetCurrentUserQuery } from '~/generated/graphql';
import { useApolloMetadataClient } from './useApolloMetadataClient';

export const useUpdateOneFieldMetadataItem = () => {
Expand All @@ -23,15 +22,7 @@ export const useUpdateOneFieldMetadataItem = () => {

const setCurrentWorkspace = useSetRecoilState(currentWorkspaceState);

const { refetch: refetchCurrentUser } = useGetCurrentUserQuery({
onCompleted: (data) => {
if (isDefined(data?.currentUser?.defaultWorkspace)) {
setCurrentWorkspace(data.currentUser.defaultWorkspace);
}
},
});

const { findManyRecordsQuery } = useFindManyRecordsQuery({
const { findManyRecordsQuery: findManyViewsQuery } = useFindManyRecordsQuery({
objectNameSingular: CoreObjectNameSingular.View,
recordGqlFields: {
id: true,
Expand Down Expand Up @@ -80,10 +71,11 @@ export const useUpdateOneFieldMetadataItem = () => {
refetchQueries: [getOperationName(FIND_MANY_OBJECT_METADATA_ITEMS) ?? ''],
});

await refetchCurrentUser();
const { data } = await apolloClient.query({ query: GET_CURRENT_USER });
setCurrentWorkspace(data?.currentUser?.defaultWorkspace);
Comment on lines +74 to +75
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: No error handling for failed user query or missing defaultWorkspace. Could cause runtime errors if query fails.


await apolloClient.query({
query: findManyRecordsQuery,
query: findManyViewsQuery,
variables: {
filter: {
objectMetadataId: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { WORKSPACE_MEMBER_QUERY_FRAGMENT } from '@/workspace-member/graphql/fragments/workspaceMemberQueryFragment';
import { gql } from '@apollo/client';

export const USER_QUERY_FRAGMENT = gql`
${WORKSPACE_MEMBER_QUERY_FRAGMENT}
fragment UserQueryFragment on User {
id
firstName
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// This query cannot be put in the graphQL folder because it cannot be generated by the graphQL codegen.
import { USER_QUERY_FRAGMENT } from '@/users/graphql/fragments/userQueryFragment';
import { gql } from '@apollo/client';

export const GET_CURRENT_USER = gql`
${USER_QUERY_FRAGMENT}
query GetCurrentUser {
currentUser {
...UserQueryFragment
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { gql } from '@apollo/client';

export const USER_QUERY_FRAGMENT = gql`
export const WORKSPACE_MEMBER_QUERY_FRAGMENT = gql`
fragment WorkspaceMemberQueryFragment on WorkspaceMember {
id
name {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { useApolloClient } from '@apollo/client';
import { zodResolver } from '@hookform/resolvers/zod';
import omit from 'lodash.omit';
import pick from 'lodash.pick';
import { useEffect, useState } from 'react';
import { useEffect } from 'react';
import { FormProvider, useForm } from 'react-hook-form';
import { useNavigate, useParams } from 'react-router-dom';
import {
Expand All @@ -21,7 +20,6 @@ import { useUpdateOneFieldMetadataItem } from '@/object-metadata/hooks/useUpdate
import { formatFieldMetadataItemInput } from '@/object-metadata/utils/formatFieldMetadataItemInput';
import { getFieldSlug } from '@/object-metadata/utils/getFieldSlug';
import { isLabelIdentifierField } from '@/object-metadata/utils/isLabelIdentifierField';
import { useFindManyRecordsQuery } from '@/object-record/hooks/useFindManyRecordsQuery';
import { RecordFieldValueSelectorContextProvider } from '@/object-record/record-store/contexts/RecordFieldValueSelectorContext';
import { SaveAndCancelButtons } from '@/settings/components/SaveAndCancelButtons/SaveAndCancelButtons';
import { SettingsPageContainer } from '@/settings/components/SettingsPageContainer';
Expand Down Expand Up @@ -49,7 +47,6 @@ type SettingsDataModelFieldEditFormValues = z.infer<
export const SettingsObjectFieldEdit = () => {
const navigate = useNavigate();
const { enqueueSnackBar } = useSnackBar();
const [isPersisting, setIsPersisting] = useState(false);

const { objectSlug = '', fieldSlug = '' } = useParams();
const { findObjectMetadataItemBySlug } = useFilteredObjectMetadataItems();
Expand All @@ -66,20 +63,6 @@ export const SettingsObjectFieldEdit = () => {
const getRelationMetadata = useGetRelationMetadata();
const { updateOneFieldMetadataItem } = useUpdateOneFieldMetadataItem();

const apolloClient = useApolloClient();

const { findManyRecordsQuery } = useFindManyRecordsQuery({
objectNameSingular: objectMetadataItem?.nameSingular || '',
});

const refetchRecords = async () => {
if (!objectMetadataItem) return;
await apolloClient.query({
query: findManyRecordsQuery,
fetchPolicy: 'network-only',
});
};

const formConfig = useForm<SettingsDataModelFieldEditFormValues>({
mode: 'onTouched',
resolver: zodResolver(settingsFieldFormSchema()),
Expand All @@ -93,11 +76,10 @@ export const SettingsObjectFieldEdit = () => {
});

useEffect(() => {
if (isPersisting) return;
if (!objectMetadataItem || !fieldMetadataItem) {
navigate(AppPath.NotFound);
}
}, [navigate, objectMetadataItem, fieldMetadataItem, isPersisting]);
}, [navigate, objectMetadataItem, fieldMetadataItem]);

const { isDirty, isValid, isSubmitting } = formConfig.formState;
const canSave = isDirty && isValid && !isSubmitting;
Expand Down Expand Up @@ -128,8 +110,6 @@ export const SettingsObjectFieldEdit = () => {
}) ?? {};

if (isDefined(relationFieldMetadataItem)) {
setIsPersisting(true);

await updateOneFieldMetadataItem({
objectMetadataId: objectMetadataItem.id,
fieldMetadataIdToUpdate: relationFieldMetadataItem.id,
Expand All @@ -146,24 +126,18 @@ export const SettingsObjectFieldEdit = () => {
Object.keys(otherDirtyFields),
);

setIsPersisting(true);
navigate(`/settings/objects/${objectSlug}`);

await updateOneFieldMetadataItem({
objectMetadataId: objectMetadataItem.id,
fieldMetadataIdToUpdate: fieldMetadataItem.id,
updatePayload: formattedInput,
});
}

Comment on lines +129 to 136
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Navigation happens before the async update operation completes. This could lead to race conditions where the user sees stale data if they quickly navigate back.

navigate(`/settings/objects/${objectSlug}`);

refetchRecords();
} catch (error) {
enqueueSnackBar((error as Error).message, {
variant: SnackBarVariant.Error,
});
} finally {
setIsPersisting(false);
}
};

Expand Down
Loading