qf-jonathan commented on code in PR #37488: URL: https://github.com/apache/superset/pull/37488#discussion_r2844245608
########## superset-frontend/src/explore/reducers/undoableExploreReducer.ts: ########## @@ -0,0 +1,38 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { Reducer } from '@reduxjs/toolkit'; +import undoable, { includeAction, StateWithHistory } from 'redux-undo'; +import { UNDO_LIMIT } from 'src/dashboard/util/constants'; +import { SET_FIELD_VALUE, UPDATE_CHART_TITLE } from '../actions/exploreActions'; +import { ExploreState } from 'src/explore/types'; +import exploreReducer from './exploreReducer'; + +export type UndoableExploreState = StateWithHistory<ExploreState>; + +const undoableReducer: Reducer<UndoableExploreState> = undoable( + exploreReducer, + { + // +1 because length of history seems max out at limit - 1 + // +1 again so we can detect if we've exceeded the limit + limit: UNDO_LIMIT + 2, + filter: includeAction([SET_FIELD_VALUE, UPDATE_CHART_TITLE]), Review Comment: I tried using `clearHistoryType: HYDRATE_EXPLORE`, but it introduced a race condition (Cannot read properties of undefined (reading 'id')). `clearHistoryType` clears history as soon as `HYDRATE_EXPLORE` is dispatched, during Redux initialization, before the chart is fully in the store. That means `mapStateToProps` may access `charts[slice_id]` while it's still `undefined`. Clearing history in `useComponentDidMount` avoids this. It runs after the component mounts and the Redux state is fully initialized, making the timing predictable and safe. We could add defensive checks in `mapStateToProps`, but that would just mask a timing issue and add unnecessary complexity. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
