Skip to content

Commit

Permalink
Fix initial experiment state namespace bug (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
jescalada authored Apr 1, 2024
1 parent 582ed30 commit e6946ab
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/src/pages/Experiment/useExperimentState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { IExperimentData } from 'modules/core/api/experimentsApi';

import experimentEngine from './ExperimentStore';

function useExperimentState(experimentId: string) {
function useExperimentState(experimentId?: string) {
const { current: engine } = React.useRef(experimentEngine);
const experimentState: IResourceState<IExperimentData> =
engine.experimentState((state) => state);
Expand All @@ -21,7 +21,12 @@ function useExperimentState(experimentId: string) {
}, []);

React.useEffect(() => {
engine.fetchExperimentData(experimentId as any);
if (experimentId) {
engine.fetchExperimentData(experimentId as any);
} else {
// Fetch the default experiment in the namespace
engine.fetchExperimentsData();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [experimentId]);

Expand Down
19 changes: 18 additions & 1 deletion src/src/pages/Metrics/components/MetricsBar/MetricsBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import ConfirmModal from 'components/ConfirmModal/ConfirmModal';

import { DOCUMENTATIONS } from 'config/references';

import { IResourceState } from 'modules/core/utils/createResource';
import { IExperimentData } from 'modules/core/api/experimentsApi';

import createExperimentEngine from 'pages/Dashboard/components/ExploreSection/ExperimentsCard/ExperimentsStore';
import ExperimentBar from 'pages/Experiment/components/ExperimentBar';
import useExperimentState from 'pages/Experiment/useExperimentState';

Expand Down Expand Up @@ -41,9 +45,22 @@ function MetricsBar({

const route = useRouteMatch<any>();

const { current: experimentsEngine } = React.useRef(createExperimentEngine);

const experimentsStore: IResourceState<IExperimentData[]> =
experimentsEngine.experimentsState((state) => state);

React.useEffect(() => {
experimentsEngine.fetchExperiments();
return () => {
experimentsEngine.destroy();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

// Fetch all experiments along with default
const { experimentState, experimentsState, getExperimentsData } =
useExperimentState('0');
useExperimentState(experimentsStore.data?.[0]?.id);

const { data: experimentData, loading: isExperimentLoading } =
experimentState;
Expand Down

0 comments on commit e6946ab

Please sign in to comment.