From fac23126caf221f89739974443e59cab6d517c2f Mon Sep 17 00:00:00 2001 From: Lucas Bordeau Date: Wed, 4 Sep 2024 20:40:11 +0200 Subject: [PATCH 1/5] Fixed bug --- .../components/QueryParamsViewIdEffect.tsx | 40 ++++++++++++++++++- .../src/modules/views/components/ViewBar.tsx | 1 + .../modules/views/hooks/useGetCurrentView.ts | 1 + 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/packages/twenty-front/src/modules/views/components/QueryParamsViewIdEffect.tsx b/packages/twenty-front/src/modules/views/components/QueryParamsViewIdEffect.tsx index ddd25463b81c..446989619bbc 100644 --- a/packages/twenty-front/src/modules/views/components/QueryParamsViewIdEffect.tsx +++ b/packages/twenty-front/src/modules/views/components/QueryParamsViewIdEffect.tsx @@ -1,12 +1,16 @@ import { useLastVisitedObjectMetadataItem } from '@/navigation/hooks/useLastVisitedObjectMetadataItem'; import { useLastVisitedView } from '@/navigation/hooks/useLastVisitedView'; import { useFilteredObjectMetadataItems } from '@/object-metadata/hooks/useFilteredObjectMetadataItems'; +import { usePrefetchedData } from '@/prefetch/hooks/usePrefetchedData'; +import { PrefetchKey } from '@/prefetch/types/PrefetchKey'; import { useViewFromQueryParams } from '@/views/hooks/internal/useViewFromQueryParams'; import { useViewStates } from '@/views/hooks/internal/useViewStates'; import { useGetCurrentView } from '@/views/hooks/useGetCurrentView'; +import { useResetCurrentView } from '@/views/hooks/useResetCurrentView'; +import { View } from '@/views/types/View'; import { isUndefined } from '@sniptt/guards'; import { useEffect } from 'react'; -import { useRecoilState } from 'recoil'; +import { useRecoilState, useSetRecoilState } from 'recoil'; import { isDeeplyEqual } from '~/utils/isDeeplyEqual'; import { isDefined } from '~/utils/isDefined'; @@ -33,6 +37,30 @@ export const QueryParamsViewIdEffect = () => { lastVisitedObjectMetadataItemId, ); + const { records: views } = usePrefetchedData(PrefetchKey.AllViews); + + const { resetCurrentView } = useResetCurrentView(); + + const { unsavedToUpsertViewFiltersState, unsavedToUpsertViewSortsState } = + useViewStates(); + + const setUnsavedViewFilters = useSetRecoilState( + unsavedToUpsertViewFiltersState, + ); + + const setUnsavedViewSorts = useSetRecoilState(unsavedToUpsertViewSortsState); + + useEffect(() => { + if (isDefined(currentViewId)) { + const view = views.find((view) => view.id === currentViewId); + + if (isDefined(view)) { + setUnsavedViewFilters(view.viewFilters ?? []); + setUnsavedViewSorts(view.viewSorts ?? []); + } + } + }, [currentViewId, views, setUnsavedViewFilters, setUnsavedViewSorts]); + useEffect(() => { const indexView = viewsOnCurrentObject.find((view) => view.key === 'INDEX'); @@ -44,7 +72,9 @@ export const QueryParamsViewIdEffect = () => { viewId: lastVisitedViewId, }); } + setCurrentViewId(lastVisitedViewId); + return; } @@ -58,7 +88,9 @@ export const QueryParamsViewIdEffect = () => { viewId: viewIdQueryParam, }); } + setCurrentViewId(viewIdQueryParam); + return; } @@ -72,10 +104,15 @@ export const QueryParamsViewIdEffect = () => { viewId: indexView.id, }); } + setCurrentViewId(indexView.id); + return; } }, [ + views, + setUnsavedViewFilters, + setUnsavedViewSorts, currentViewId, getFiltersFromQueryParams, isLastVisitedObjectMetadataItemDifferent, @@ -87,6 +124,7 @@ export const QueryParamsViewIdEffect = () => { setLastVisitedView, viewIdQueryParam, viewsOnCurrentObject, + resetCurrentView, ]); return <>; diff --git a/packages/twenty-front/src/modules/views/components/ViewBar.tsx b/packages/twenty-front/src/modules/views/components/ViewBar.tsx index 3c600daec05f..123b990d3415 100644 --- a/packages/twenty-front/src/modules/views/components/ViewBar.tsx +++ b/packages/twenty-front/src/modules/views/components/ViewBar.tsx @@ -45,6 +45,7 @@ export const ViewBar = ({ return; } + // TODO: effects have dangerous cyclical dependencies that rapidly cause infinite loop return ( { const unsavedToUpsertViewFilters = useRecoilValue( unsavedToUpsertViewFiltersState, ); + const unsavedToUpsertViewSorts = useRecoilValue( unsavedToUpsertViewSortsState, ); From fdcbe607bf059c2520565a170207975dd9d5b6b3 Mon Sep 17 00:00:00 2001 From: Lucas Bordeau Date: Wed, 4 Sep 2024 20:40:42 +0200 Subject: [PATCH 2/5] Removed comment --- packages/twenty-front/src/modules/views/components/ViewBar.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/twenty-front/src/modules/views/components/ViewBar.tsx b/packages/twenty-front/src/modules/views/components/ViewBar.tsx index 123b990d3415..3c600daec05f 100644 --- a/packages/twenty-front/src/modules/views/components/ViewBar.tsx +++ b/packages/twenty-front/src/modules/views/components/ViewBar.tsx @@ -45,7 +45,6 @@ export const ViewBar = ({ return; } - // TODO: effects have dangerous cyclical dependencies that rapidly cause infinite loop return ( Date: Wed, 4 Sep 2024 21:40:16 +0200 Subject: [PATCH 3/5] Use reset instead --- .../components/QueryParamsViewIdEffect.tsx | 22 +++---------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/packages/twenty-front/src/modules/views/components/QueryParamsViewIdEffect.tsx b/packages/twenty-front/src/modules/views/components/QueryParamsViewIdEffect.tsx index 446989619bbc..0b322203c434 100644 --- a/packages/twenty-front/src/modules/views/components/QueryParamsViewIdEffect.tsx +++ b/packages/twenty-front/src/modules/views/components/QueryParamsViewIdEffect.tsx @@ -10,7 +10,7 @@ import { useResetCurrentView } from '@/views/hooks/useResetCurrentView'; import { View } from '@/views/types/View'; import { isUndefined } from '@sniptt/guards'; import { useEffect } from 'react'; -import { useRecoilState, useSetRecoilState } from 'recoil'; +import { useRecoilState } from 'recoil'; import { isDeeplyEqual } from '~/utils/isDeeplyEqual'; import { isDefined } from '~/utils/isDefined'; @@ -41,25 +41,11 @@ export const QueryParamsViewIdEffect = () => { const { resetCurrentView } = useResetCurrentView(); - const { unsavedToUpsertViewFiltersState, unsavedToUpsertViewSortsState } = - useViewStates(); - - const setUnsavedViewFilters = useSetRecoilState( - unsavedToUpsertViewFiltersState, - ); - - const setUnsavedViewSorts = useSetRecoilState(unsavedToUpsertViewSortsState); - useEffect(() => { if (isDefined(currentViewId)) { - const view = views.find((view) => view.id === currentViewId); - - if (isDefined(view)) { - setUnsavedViewFilters(view.viewFilters ?? []); - setUnsavedViewSorts(view.viewSorts ?? []); - } + resetCurrentView(); } - }, [currentViewId, views, setUnsavedViewFilters, setUnsavedViewSorts]); + }, [resetCurrentView, currentViewId]); useEffect(() => { const indexView = viewsOnCurrentObject.find((view) => view.key === 'INDEX'); @@ -111,8 +97,6 @@ export const QueryParamsViewIdEffect = () => { } }, [ views, - setUnsavedViewFilters, - setUnsavedViewSorts, currentViewId, getFiltersFromQueryParams, isLastVisitedObjectMetadataItemDifferent, From 694fe3a67baa0daf6d85096a2fd1145dc10660ec Mon Sep 17 00:00:00 2001 From: Lucas Bordeau Date: Wed, 4 Sep 2024 21:42:19 +0200 Subject: [PATCH 4/5] Fixes --- .../views/components/QueryParamsViewIdEffect.tsx | 11 ----------- .../src/modules/views/hooks/useGetCurrentView.ts | 1 - 2 files changed, 12 deletions(-) diff --git a/packages/twenty-front/src/modules/views/components/QueryParamsViewIdEffect.tsx b/packages/twenty-front/src/modules/views/components/QueryParamsViewIdEffect.tsx index 0b322203c434..0fefdc53d7cf 100644 --- a/packages/twenty-front/src/modules/views/components/QueryParamsViewIdEffect.tsx +++ b/packages/twenty-front/src/modules/views/components/QueryParamsViewIdEffect.tsx @@ -1,13 +1,10 @@ import { useLastVisitedObjectMetadataItem } from '@/navigation/hooks/useLastVisitedObjectMetadataItem'; import { useLastVisitedView } from '@/navigation/hooks/useLastVisitedView'; import { useFilteredObjectMetadataItems } from '@/object-metadata/hooks/useFilteredObjectMetadataItems'; -import { usePrefetchedData } from '@/prefetch/hooks/usePrefetchedData'; -import { PrefetchKey } from '@/prefetch/types/PrefetchKey'; import { useViewFromQueryParams } from '@/views/hooks/internal/useViewFromQueryParams'; import { useViewStates } from '@/views/hooks/internal/useViewStates'; import { useGetCurrentView } from '@/views/hooks/useGetCurrentView'; import { useResetCurrentView } from '@/views/hooks/useResetCurrentView'; -import { View } from '@/views/types/View'; import { isUndefined } from '@sniptt/guards'; import { useEffect } from 'react'; import { useRecoilState } from 'recoil'; @@ -37,8 +34,6 @@ export const QueryParamsViewIdEffect = () => { lastVisitedObjectMetadataItemId, ); - const { records: views } = usePrefetchedData(PrefetchKey.AllViews); - const { resetCurrentView } = useResetCurrentView(); useEffect(() => { @@ -74,9 +69,7 @@ export const QueryParamsViewIdEffect = () => { viewId: viewIdQueryParam, }); } - setCurrentViewId(viewIdQueryParam); - return; } @@ -90,13 +83,10 @@ export const QueryParamsViewIdEffect = () => { viewId: indexView.id, }); } - setCurrentViewId(indexView.id); - return; } }, [ - views, currentViewId, getFiltersFromQueryParams, isLastVisitedObjectMetadataItemDifferent, @@ -108,7 +98,6 @@ export const QueryParamsViewIdEffect = () => { setLastVisitedView, viewIdQueryParam, viewsOnCurrentObject, - resetCurrentView, ]); return <>; diff --git a/packages/twenty-front/src/modules/views/hooks/useGetCurrentView.ts b/packages/twenty-front/src/modules/views/hooks/useGetCurrentView.ts index 3853c4a1cbc8..70d3cd8160c1 100644 --- a/packages/twenty-front/src/modules/views/hooks/useGetCurrentView.ts +++ b/packages/twenty-front/src/modules/views/hooks/useGetCurrentView.ts @@ -58,7 +58,6 @@ export const useGetCurrentView = (viewBarComponentId?: string) => { const unsavedToUpsertViewFilters = useRecoilValue( unsavedToUpsertViewFiltersState, ); - const unsavedToUpsertViewSorts = useRecoilValue( unsavedToUpsertViewSortsState, ); From c7a4e19856a650fda1fd108c4b2b19c99351d761 Mon Sep 17 00:00:00 2001 From: Lucas Bordeau Date: Wed, 4 Sep 2024 21:42:46 +0200 Subject: [PATCH 5/5] Fixes --- .../src/modules/views/components/QueryParamsViewIdEffect.tsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/twenty-front/src/modules/views/components/QueryParamsViewIdEffect.tsx b/packages/twenty-front/src/modules/views/components/QueryParamsViewIdEffect.tsx index 0fefdc53d7cf..74d516a7f9ee 100644 --- a/packages/twenty-front/src/modules/views/components/QueryParamsViewIdEffect.tsx +++ b/packages/twenty-front/src/modules/views/components/QueryParamsViewIdEffect.tsx @@ -53,9 +53,7 @@ export const QueryParamsViewIdEffect = () => { viewId: lastVisitedViewId, }); } - setCurrentViewId(lastVisitedViewId); - return; }