Skip to content
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

Merged

Conversation

etr2460
Copy link
Member

@etr2460 etr2460 commented Dec 10, 2021

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

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

to: @michael-s-molina @graceguo-supercat @ktmud @rusackas

@etr2460 etr2460 force-pushed the erik-ritter--persist-sql-autocomplete-setting branch 2 times, most recently from 489bdb9 to e1c1d19 Compare December 10, 2021 05:02
@apache apache deleted a comment from github-actions bot Dec 10, 2021
@etr2460 etr2460 force-pushed the erik-ritter--persist-sql-autocomplete-setting branch from e1c1d19 to 60f2ef6 Compare December 10, 2021 05:45
@pull-request-size pull-request-size bot added size/M and removed size/S labels Dec 10, 2021
@apache apache deleted a comment from github-actions bot Dec 10, 2021
@etr2460 etr2460 force-pushed the erik-ritter--persist-sql-autocomplete-setting branch 2 times, most recently from cfad894 to 6580525 Compare December 10, 2021 05:51
Comment on lines 23 to 97
if (value === null) {
return defaultValue;
}
return JSON.parse(value);
Copy link
Member Author

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!

@etr2460
Copy link
Member Author

etr2460 commented Dec 10, 2021

/testenv up

@github-actions
Copy link
Contributor

@etr2460 Ephemeral environment spinning up at http://34.219.206.85:8080. Credentials are admin/admin. Please allow several minutes for bootstrapping and startup.

@codecov
Copy link

codecov bot commented Dec 10, 2021

Codecov Report

Merging #17708 (24c3d9a) into master (6edc183) will decrease coverage by 0.00%.
The diff coverage is 25.00%.

Impacted file tree graph

@@            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              
Flag Coverage Δ
javascript 53.87% <25.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...frontend/src/SqlLab/components/SqlEditor/index.jsx 51.76% <0.00%> (-0.62%) ⬇️
superset-frontend/src/utils/localStorageHelpers.ts 84.00% <100.00%> (+0.66%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6edc183...24c3d9a. Read the comment docs.

@@ -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';
Copy link
Member

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 {
Copy link
Member

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?

Copy link
Member

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';
Copy link
Member

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));
}

Copy link
Member Author

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

Copy link
Member Author

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

Copy link
Contributor

@riahk riahk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@etr2460
Copy link
Member Author

etr2460 commented Dec 20, 2021

will rebase on #17832 before merging

@etr2460 etr2460 force-pushed the erik-ritter--persist-sql-autocomplete-setting branch from 6580525 to 24c3d9a Compare December 20, 2021 22:12
@pull-request-size pull-request-size bot added size/S and removed size/M labels Dec 20, 2021
@etr2460 etr2460 merged commit 5b0aa27 into apache:master Dec 20, 2021
@github-actions
Copy link
Contributor

Ephemeral environment shutdown and build artifacts deleted.

shcoderAlex pushed a commit to casual-precision/superset that referenced this pull request Feb 7, 2022
bwang221 pushed a commit to casual-precision/superset that referenced this pull request Feb 10, 2022
@mistercrunch mistercrunch added 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 1.5.0 labels Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels size/S 🚢 1.5.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants