Skip to content

Commit

Permalink
fix: hidden and highlighted datastreams persist correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
ssjagad authored and corteggiano committed Jan 31, 2024
1 parent 7cc590d commit 5a85bb7
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ const LineScatterChartWidgetComponent: React.FC<LineScatterChartWidget> = (
return (
<WidgetTile widget={widget} removeable title={title}>
<Chart
id={widget.id}
queries={queries}
viewport={viewport}
gestures={readOnly}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import useDataStore from '../../../store';
// Recommended usage from zustand https://docs.pmnd.rs/zustand/previous-versions/zustand-v3-create-context
export const ChartStoreContext = createContext<
ReturnType<typeof createChartStore>
>(createChartStore());
>(createChartStore({ chartId: 'contextStore' }));

export const ChartStoreProvider = ({
id,
Expand Down
11 changes: 8 additions & 3 deletions packages/react-components/src/components/chart/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ import { devtools, persist } from 'zustand/middleware';
import { createMultiYAxisSlice, MultiYAxisState } from './multiYAxis';
import { createDataStreamsSlice, DataStreamsState } from './contextDataStreams';

export type StateData = DataStreamsState & MultiYAxisState;
export const createChartStore = () =>
export type StateData = DataStreamsState &
MultiYAxisState & { chartId: string };

export const createChartStore = (
initProps: Partial<StateData> & Required<Pick<Partial<StateData>, 'chartId'>>
) =>
create<StateData>()(
devtools(
persist(
(...args) => ({
...initProps,
...createMultiYAxisSlice(...args),
...createDataStreamsSlice(...args),
}),
{ name: 'chartStore' }
{ name: `chart-store-${initProps?.chartId}` }
)
)
);
2 changes: 1 addition & 1 deletion packages/react-components/src/store/chartStoreSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface ChartStoreState extends ChartStoreData {
export const createChartStoresSlice: StateCreator<ChartStoreState> = (set) => ({
chartStores: {},
addChart: (id) => {
const store = createChartStore();
const store = createChartStore({ chartId: id }); //repopulate chart store from local storage
set((state) => ({
chartStores: {
...state.chartStores,
Expand Down
19 changes: 13 additions & 6 deletions packages/react-components/src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { create } from 'zustand';
import { devtools } from 'zustand/middleware';
import { devtools, persist } from 'zustand/middleware';
import { createTrendCursorsSlice, TrendCursorsState } from './trendCusorSlice';
import { ConfigState, createConfigSlice, Flags } from './config';
import { ChartStoreState, createChartStoresSlice } from './chartStoreSlice';

export type StateData = TrendCursorsState & ConfigState & ChartStoreState;
const useDataStore = create<StateData>()(
devtools((...args) => ({
...createTrendCursorsSlice(...args),
...createConfigSlice(...args),
...createChartStoresSlice(...args),
}))
devtools(
persist(
(...args) => ({
...createTrendCursorsSlice(...args),
...createConfigSlice(...args),
...createChartStoresSlice(...args),
}),
{
name: 'global-store',
}
)
)
);

export default useDataStore;
Expand Down

0 comments on commit 5a85bb7

Please sign in to comment.