forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: consolidate sqllab store into SPA store (apache#25088)
- Loading branch information
1 parent
eeecd59
commit 846c79e
Showing
7 changed files
with
152 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
superset-frontend/src/SqlLab/middlewares/persistSqlLabStateEnhancer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* 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. | ||
*/ | ||
// TODO: requires redux-localstorage > 1.0 for typescript support | ||
import persistState from 'redux-localstorage'; | ||
import { | ||
emptyTablePersistData, | ||
emptyQueryResults, | ||
clearQueryEditors, | ||
} from '../utils/reduxStateToLocalStorageHelper'; | ||
import { BYTES_PER_CHAR, KB_STORAGE } from '../constants'; | ||
|
||
const CLEAR_ENTITY_HELPERS_MAP = { | ||
tables: emptyTablePersistData, | ||
queries: emptyQueryResults, | ||
queryEditors: clearQueryEditors, | ||
unsavedQueryEditor: qe => clearQueryEditors([qe])[0], | ||
}; | ||
|
||
const sqlLabPersistStateConfig = { | ||
paths: ['sqlLab'], | ||
config: { | ||
slicer: paths => state => { | ||
const subset = {}; | ||
paths.forEach(path => { | ||
// this line is used to remove old data from browser localStorage. | ||
// we used to persist all redux state into localStorage, but | ||
// it caused configurations passed from server-side got override. | ||
// see PR 6257 for details | ||
delete state[path].common; // eslint-disable-line no-param-reassign | ||
if (path === 'sqlLab') { | ||
subset[path] = Object.fromEntries( | ||
Object.entries(state[path]).map(([key, value]) => [ | ||
key, | ||
CLEAR_ENTITY_HELPERS_MAP[key]?.(value) ?? value, | ||
]), | ||
); | ||
} | ||
}); | ||
|
||
const data = JSON.stringify(subset); | ||
// 2 digit precision | ||
const currentSize = | ||
Math.round(((data.length * BYTES_PER_CHAR) / KB_STORAGE) * 100) / 100; | ||
if (state.localStorageUsageInKilobytes !== currentSize) { | ||
state.localStorageUsageInKilobytes = currentSize; // eslint-disable-line no-param-reassign | ||
} | ||
|
||
return subset; | ||
}, | ||
merge: (initialState, persistedState = {}) => { | ||
const result = { | ||
...initialState, | ||
...persistedState, | ||
sqlLab: { | ||
...(persistedState?.sqlLab || {}), | ||
// Overwrite initialState over persistedState for sqlLab | ||
// since a logic in getInitialState overrides the value from persistedState | ||
...initialState.sqlLab, | ||
}, | ||
}; | ||
return result; | ||
}, | ||
}, | ||
}; | ||
|
||
export const persistSqlLabStateEnhancer = persistState( | ||
sqlLabPersistStateConfig.paths, | ||
sqlLabPersistStateConfig.config, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters