-
Notifications
You must be signed in to change notification settings - Fork 14k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Persist SQL Lab autocomplete setting across tabs and visits #17708
feat: Persist SQL Lab autocomplete setting across tabs and visits #17708
Conversation
489bdb9
to
e1c1d19
Compare
e1c1d19
to
60f2ef6
Compare
cfad894
to
6580525
Compare
if (value === null) { | ||
return defaultValue; | ||
} | ||
return JSON.parse(value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was broken in a very subtle way before. If the value in localStorage was falsey (0, false, '', etc.), then this function would return the defaultValue.
unfortunately, localStorage doesn't have a hasItem
function, so the best thing we can do is see if getItem
returns null (https://developer.mozilla.org/en-US/docs/Web/API/Storage/getItem#return_value). Isn't JS fun!
/testenv up |
@etr2460 Ephemeral environment spinning up at http://34.219.206.85:8080. Credentials are |
Codecov Report
@@ Coverage Diff @@
## master #17708 +/- ##
==========================================
- Coverage 67.16% 67.16% -0.01%
==========================================
Files 1608 1608
Lines 64798 64801 +3
Branches 6851 6851
==========================================
+ Hits 43520 43521 +1
- Misses 19419 19421 +2
Partials 1859 1859
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
@@ -65,3 +65,6 @@ export const SQL_FUNCTIONS_AUTOCOMPLETE_SCORE = 90; | |||
export const SCHEMA_AUTOCOMPLETE_SCORE = 60; | |||
export const TABLE_AUTOCOMPLETE_SCORE = 55; | |||
export const COLUMN_AUTOCOMPLETE_SCORE = 50; | |||
|
|||
// LocalStorage keys | |||
export const SQL_LAB_AUTOCOMPLETE_SETTING_KEY = 'isSQLLabAutocompleteEnabled'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be SQL_LAB_AUTOCOMPLETE_ENABLED_SETTING_KEY
?
SQL_LAB_AUTOCOMPLETE_SETTING_KEY, | ||
!prevState.autocompleteEnabled, | ||
); | ||
return { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is a return necessary when it wasn’t previously?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does previously. An arrow function with just expression will use the value of that expression as the return value.
@@ -65,3 +65,6 @@ export const SQL_FUNCTIONS_AUTOCOMPLETE_SCORE = 90; | |||
export const SCHEMA_AUTOCOMPLETE_SCORE = 60; | |||
export const TABLE_AUTOCOMPLETE_SCORE = 55; | |||
export const COLUMN_AUTOCOMPLETE_SCORE = 50; | |||
|
|||
// LocalStorage keys | |||
export const SQL_LAB_AUTOCOMPLETE_SETTING_KEY = 'isSQLLabAutocompleteEnabled'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be better if we keep all local storage keys as an enum
and move into localStorageHelpers.js
? The keys are global so it makes sense to keep them in a centralized place.
The values can even be typed:
/**
* Local storage manager
*/
export function safeJsonParse(jsonString: string | null) {
if (jsonString === null) return null;
try {
return JSON.parse(jsonString);
} catch {
return null;
}
}
export type StorageValues = {
key1: Type1,
key2: Type2,
};
export type StorageKey = keyof StorageValues;
export const defaultStorageValues: StorageValues = {
key1: value1,
key2: value2
};
export function entries() {
Object.values(Storage).forEach(key => {
return [key, getItem(key)];
});
}
export function getItem<K extends StorageKey>(key: K): StorageValues[K] {
return safeJsonParse(localStorage.getItem(key)) ?? defaultStorageValues[key];
}
export function setItem(key: StorageKey, value: string | number | object) {
return localStorage.setItem(key, JSON.stringify(value));
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, that was my original thought, but i realized the existing keys that are used don't do that yet. maybe i'll make a PR first to centralize all the keys, then i'll rebase this one on it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to implement this now, but probably won't add default values here, if only because i think sometimes the caller might want to se a different default
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
will rebase on #17832 before merging |
6580525
to
24c3d9a
Compare
Ephemeral environment shutdown and build artifacts deleted. |
SUMMARY
Follows the pattern used for the filter box migration and the welcome page settings to make the SQL Lab autocomplete setting persistent across SQL Lab tabs and Superset sessions/visits
TESTING INSTRUCTIONS
Go to SQL Lab, see autocomplete is on by default. If you disable it and switch tabs, see it disabled. If you reload the page, see it still disabled. Reenable it, reload the page, and see it still enabled
ADDITIONAL INFORMATION
to: @michael-s-molina @graceguo-supercat @ktmud @rusackas