11import React , { FC , useCallback , useEffect , useMemo } from 'react'
2+ import cloneDeep from 'lodash/cloneDeep'
23import { useHistory } from 'react-router-dom'
34import CreateFlagModal from 'components/modals/create-feature'
45import Constants from 'common/constants'
@@ -87,8 +88,9 @@ const FeaturesPage: FC<FeaturesPageProps> = ({
8788 getEnvironment,
8889 project,
8990 } = useProjectEnvironments ( projectId )
90- const { data, error, isFetching, isLoading, refetch } =
91+ const { currentData , data, error, isFetching, isLoading, refetch } =
9192 useFeatureListWithApiKey ( effectiveFilters , page , environmentId , projectId )
93+ const isDataStale = ! ! data && ! currentData
9294
9395 // Backward compatibility: Populate ProjectStore for legacy components (CreateFlag)
9496 // TODO: Remove this when CreateFlag is migrated to RTK Query
@@ -98,31 +100,21 @@ const FeaturesPage: FC<FeaturesPageProps> = ({
98100 }
99101 } , [ projectId ] )
100102
101- // Backward compatibility: Populate FeatureListStore for legacy components (CreateFlag modal)
102- // Must pass current filters/search/page so FeatureListStore contains the same features
103- // that RTK Query displays. Otherwise editing features will crash because they're not in the store.
104- // TODO: Remove this when CreateFlag is migrated to RTK Query
105103 useEffect ( ( ) => {
106- if ( projectId && environmentId ) {
107- AppActions . getFeatures (
108- projectId ,
109- environmentId ,
110- true ,
111- effectiveFilters . search ,
112- effectiveFilters . sort ,
113- page ,
114- {
115- group_owners : effectiveFilters . group_owners ?. join ( ',' ) || undefined ,
116- is_archived : effectiveFilters . showArchived ,
117- is_enabled : effectiveFilters . is_enabled ,
118- owners : effectiveFilters . owners ?. join ( ',' ) || undefined ,
119- tag_strategy : effectiveFilters . tag_strategy ,
120- tags : effectiveFilters . tags ?. join ( ',' ) || undefined ,
121- value_search : effectiveFilters . value_search ,
122- } ,
123- )
104+ if ( data && environmentId ) {
105+ // TODO: Remove this when CreateFlag is migrated to RTK Query
106+ // This currently avoids duplicate api calls
107+ FeatureListStore . envId = environmentId
108+ FeatureListStore . projectId = projectId
109+ FeatureListStore . environmentId = environmentId
110+ FeatureListStore . model = {
111+ features : cloneDeep ( data . results ) ,
112+ keyedEnvironmentFeatures : cloneDeep ( data . environmentStates ) ,
113+ }
114+ FeatureListStore . paging = { ...data . pagination }
115+ FeatureListStore . loaded ( )
124116 }
125- } , [ projectId , environmentId , page , effectiveFilters ] )
117+ } , [ data , environmentId , projectId ] )
126118
127119 // Force re-fetch when legacy Flux store updates features
128120 // TODO: Remove when all feature mutations use RTK Query
@@ -317,7 +309,7 @@ const FeaturesPage: FC<FeaturesPageProps> = ({
317309 id = 'features-list'
318310 renderSearchWithNoResults
319311 itemHeight = { 65 }
320- isLoading = { isLoading }
312+ isLoading = { isLoading || isDataStale }
321313 paging = { paging }
322314 header = { renderHeader ( ) }
323315 nextPage = { handleNextPage }
0 commit comments