Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions superset-frontend/src/explore/components/ExploreViewContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,20 @@ function ExploreViewContainer(props) {
);
}

function onResize(evt) {
const { x } = evt;
if (x > 0) {
localStorage.setItem('explore_sidebar_widths', x);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You might want to check that localStorage is available or catch eventual errors. It might be disabled and will throw an exception in that case.

}
}

if (props.standalone) {
return renderChartContainer();
}

const exploreSidebarWidth =
localStorage.getItem('explore_sidebar_widths') || 320;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same as my other comment


return (
<Styles id="explore-container" height={height}>
<Global
Expand Down Expand Up @@ -404,7 +414,8 @@ function ExploreViewContainer(props) {
/>
)}
<Resizable
defaultSize={{ width: 300 }}
onResize={onResize}
defaultSize={{ width: exploreSidebarWidth }}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think the dataset pane and the controls pane should have separate widths. You might want to create a pair of small getter/setter functions to handle saving and reading both widths in one key safely.

enum StorageKey  {
   sidebarWidth = "explore_sidebar_widths"
}

function getSidebarWidths() {
  try {
     return localStorage.getItem(StorageKey.siderbarWidth).split(":");
  } catch {}
  return [300, 320]
}

function setSidebarWidths(widths: number[]) {
  try {
    localStorage.setItem(StorageKey.siderbarWidth, widths.join(":"));
  } catch {}
}

@rusackas rusackas Jan 20, 2021

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I noticed SQL Lab is using localstorage without a try/catch safety net. This PR makes me wonder if we should make an abstracted helper method, kind of like the "logging" method from @superset-ui/core. These methods, maybe setStoredValue and getStoredValue, could try local storage, then try setting a cookie, and take an optional callback for the catch block. I don't think we need to get that crazy with this PR, but wonder if folks think it's an idea worth ticketing separately.

@ktmud ktmud Jan 20, 2021

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I always do that in my other projects. Here's an example file with typing for storage keys & values:

import { User } from 'graphql/job.graphql';

/**
 * Local storage manager
 */
export function safeJsonParse(jsonString: string | null) {
  if (jsonString === null) return null;
  try {
    return JSON.parse(jsonString);
  } catch {
    return null;
  }
}

export type StorageKey = 'persistedStates' | 'hasWelcomed';

export type StorageValues = {
  persistedStates: Record<string, any> | null;
  hasWelcomed: boolean;
};

export const defaultStorageValues = {
  userIdMap: null,
  persistedStates: null,
  hasWelcomed: false,
};

export function entries() {
  Object.values(Storage).forEach(key => {
    return [key, getItem(key)];
  });
}

export function getItem<K extends StorageKey>(key: K): StorageValues[K] | null {
  return safeJsonParse(localStorage.getItem(key)) ?? defaultStorageValues[key];
}

export function setItem(key: StorageKey, value: string | number | object) {
  return localStorage.setItem(key, JSON.stringify(value));
}

minWidth={300}
maxWidth="33%"
enable={{ right: true }}
Expand Down Expand Up @@ -456,7 +467,8 @@ function ExploreViewContainer(props) {
</div>
) : null}
<Resizable
defaultSize={{ width: 320 }}
onResize={onResize}
defaultSize={{ width: exploreSidebarWidth }}
minWidth={320}
maxWidth="33%"
enable={{ right: true }}
Expand Down