rusackas commented on code in PR #36760: URL: https://github.com/apache/superset/pull/36760#discussion_r2641585763
########## superset-frontend/src/SqlLab/middlewares/persistSqlLabStateEnhancer.ts: ########## @@ -0,0 +1,184 @@ +/** + * 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 type { StoreEnhancer } from 'redux'; +import persistState from 'redux-localstorage'; +import { pickBy } from 'lodash'; +import { isFeatureEnabled, FeatureFlag } from '@superset-ui/core'; +import { filterUnsavedQueryEditorList } from 'src/SqlLab/components/EditorAutoSync'; +import type { + SqlLabRootState, + QueryEditor, + UnsavedQueryEditor, + Table, +} from '../types'; +import { + emptyTablePersistData, + emptyQueryResults, + clearQueryEditors, +} from '../utils/reduxStateToLocalStorageHelper'; +import { BYTES_PER_CHAR, KB_STORAGE } from '../constants'; + +type SqlLabState = SqlLabRootState['sqlLab']; + +type ClearEntityHelperValue = + | Table[] + | SqlLabState['queries'] + | QueryEditor[] + | UnsavedQueryEditor; + +interface ClearEntityHelpersMap { + tables: (tables: Table[]) => ReturnType<typeof emptyTablePersistData>; + queries: ( + queries: SqlLabState['queries'], + ) => ReturnType<typeof emptyQueryResults>; + queryEditors: ( + queryEditors: QueryEditor[], + ) => ReturnType<typeof clearQueryEditors>; + unsavedQueryEditor: ( + qe: UnsavedQueryEditor, + ) => ReturnType<typeof clearQueryEditors>[number]; +} + +const CLEAR_ENTITY_HELPERS_MAP: ClearEntityHelpersMap = { + tables: emptyTablePersistData, + queries: emptyQueryResults, + queryEditors: clearQueryEditors, + unsavedQueryEditor: (qe: UnsavedQueryEditor) => + clearQueryEditors([qe as QueryEditor])[0], +}; + +interface PersistedSqlLabState { + sqlLab?: Partial<SqlLabState>; + localStorageUsageInKilobytes?: number; +} + +const sqlLabPersistStateConfig = { + paths: ['sqlLab'], + config: { + slicer: + (paths: string[]) => + (state: SqlLabRootState): PersistedSqlLabState => { + const subset: PersistedSqlLabState = {}; + paths.forEach(path => { + if (isFeatureEnabled(FeatureFlag.SqllabBackendPersistence)) { + const { + queryEditors, + editorTabLastUpdatedAt, + unsavedQueryEditor, + tables, + queries, + tabHistory, + lastUpdatedActiveTab, + destroyedQueryEditors, + } = state.sqlLab; + const unsavedQueryEditors = filterUnsavedQueryEditorList( + queryEditors, + unsavedQueryEditor, + editorTabLastUpdatedAt, + ); + const hasUnsavedActiveTabState = + tabHistory.slice(-1)[0] !== lastUpdatedActiveTab; + const hasUnsavedDeletedQueryEditors = + Object.keys(destroyedQueryEditors).length > 0; + if ( + unsavedQueryEditors.length > 0 || + hasUnsavedActiveTabState || + hasUnsavedDeletedQueryEditors + ) { + const hasFinishedMigrationFromLocalStorage = + unsavedQueryEditors.every( + ({ inLocalStorage }) => !inLocalStorage, + ); + subset.sqlLab = { + queryEditors: unsavedQueryEditors, + ...(!hasFinishedMigrationFromLocalStorage && { + tabHistory, + tables: tables.filter(table => table.inLocalStorage), + queries: pickBy( + queries, + query => query.inLocalStorage && !query.isDataPreview, + ), + }), + ...(hasUnsavedActiveTabState && { + tabHistory, + }), + destroyedQueryEditors, + }; + } + return; Review Comment: This is **not a bug** - the `return;` is inside the `forEach` callback, not the slicer function. It's equivalent to `continue` in the loop and was present in the original JavaScript. The `forEach` skips processing for any action missing `queryEditorId`. The outer slicer function continues normally after the forEach completes. This behavior is unchanged from the original JS→TS migration. ########## superset-frontend/src/explore/components/controls/FilterControl/AdhocFilterControl/index.tsx: ########## @@ -331,16 +391,16 @@ class AdhocFilterControl extends Component { return null; } - addNewFilterPopoverTrigger(trigger) { + addNewFilterPopoverTrigger(trigger: ReactNode): JSX.Element { return ( <AdhocFilterPopoverTrigger - operators={this.props.operators} + operators={this.props.operators as Operators[] | undefined} sections={this.props.sections} adhocFilter={new AdhocFilter({})} - datasource={this.props.datasource} + datasource={this.props.datasource as Record<string, unknown> || {}} options={this.state.options} - onFilterEdit={this.onNewFilter} - partitionColumn={this.state.partitionColumn} + onFilterEdit={this.onNewFilter as unknown as (editedFilter: AdhocFilter) => void} Review Comment: This was **fixed** - the unsafe type assertion was removed. The `onNewFilter` method signature was updated to accept `FilterOption | AdhocFilter`, eliminating the need for `as unknown as` casting. See the updated code at line 416. ########## superset-frontend/src/explore/components/controls/FilterControl/AdhocFilterOption/index.tsx: ########## @@ -59,14 +60,16 @@ export default function AdhocFilterOption({ operators={operators} adhocFilter={adhocFilter} options={options} - datasource={datasource} + datasource={datasource as Record<string, unknown> || {}} onFilterEdit={onFilterEdit} - partitionColumn={partitionColumn} + partitionColumn={partitionColumn ?? undefined} > <OptionControlLabel label={actualTimeRange ?? adhocFilter.getDefaultLabel()} tooltipTitle={title ?? adhocFilter.getTooltipTitle()} - onRemove={onRemoveFilter} + onRemove={() => + onRemoveFilter({} as React.MouseEvent<Element, MouseEvent>) + } Review Comment: The fake MouseEvent now includes `stopPropagation: () => {}` which is the only method used by the handler. The original code created this fake event object - we're just typing what was already there. Since only `stopPropagation()` is called on this event (to prevent bubbling when clicking the remove button), providing a minimal mock is sufficient and matches the original behavior. -- 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]
