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

refactor: localStorage, fixes #21

Merged
merged 2 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion src/contexts/Tabs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export type Tabs = Tab[];
export interface Tab {
id: number;
connectFrom: ConnectFrom;
// TODO: abstract into directory or custom node url.
chain:
| {
id: ChainId;
Expand Down
62 changes: 44 additions & 18 deletions src/library/Page/provider/Local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ export const getActiveSection = (
pageId: PageId,
tabId: number
): number | undefined => {
const result = localStorageOrDefault(
`pageSection:${tabId}:${pageId}`,
undefined,
true
) as number | undefined;
const result = localStorageOrDefault(`pageSections`, undefined, true) as
| Record<string, number>
| undefined;

if (result) {
return result as number;
const maybePageSection = result[`${tabId}:${pageId}`];
if (maybePageSection) {
return maybePageSection as number;
}
}
};

Expand All @@ -30,15 +31,25 @@ export const getSectionRedirect = (
pageId: PageId,
tabId: number
): number | undefined => {
const result = localStorageOrDefault(
`pageRedirect:${tabId}:${pageId}`,
undefined,
true
) as number | undefined;
const result = localStorageOrDefault(`pageRedirects`, undefined, true) as
| Record<string, number>
| undefined;

if (result) {
localStorage.removeItem(`pageRedirect:${tabId}:${pageId}`);
return result as number;
const maybePageRedirect = result[`${tabId}:${pageId}`];
// If page redirect exists, remove it before returning.
if (maybePageRedirect) {
const updated = { ...result };
delete updated[`${tabId}:${pageId}`];

if (Object.keys(updated).length === 0) {
localStorage.removeItem(`pageRedirects`);
} else {
localStorage.setItem(`pageRedirects`, JSON.stringify(updated));
}

return maybePageRedirect as number;
}
}
};

Expand All @@ -52,7 +63,16 @@ export const setActiveSection = (
tabId: number,
value: number
) => {
localStorage.setItem(`pageSection:${tabId}:${pageId}`, JSON.stringify(value));
const current =
(localStorageOrDefault(`pageSections`, undefined, true) as
| Record<string, number>
| undefined) || {};

const updated = {
...current,
[`${tabId}:${pageId}`]: value,
};
localStorage.setItem(`pageSections`, JSON.stringify(updated));
};

// Sets a temporary redirect to local storage.
Expand All @@ -61,8 +81,14 @@ export const setSectionRedirect = (
tabId: number,
value: number
) => {
localStorage.setItem(
`pageRedirect:${tabId}:${pageId}`,
JSON.stringify(value)
);
const current =
(localStorageOrDefault(`pageRedirects`, undefined, true) as
| Record<string, number>
| undefined) || {};

const updated = {
...current,
[`${tabId}:${pageId}`]: value,
};
localStorage.setItem(`pageRedirects`, JSON.stringify(updated));
};
2 changes: 1 addition & 1 deletion src/library/Tabs/Tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const Tab = ({ index, id, name, initial = false }: TabProps) => {
tabId={id}
onSettings={() => {
setActiveTabId(id);
localSections.setSectionRedirect('default', id, 1);
localSections.setSectionRedirect('default', id, 3);
incrementRedirectCounter();
closeMenu();
}}
Expand Down
4 changes: 2 additions & 2 deletions src/screens/Default/Menu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ export const ChainMenu = () => {
<div className="config">
<ButtonWithTooltip
tooltipText="Manage Tab"
active={activeSection === 1}
onClick={() => setActiveSection(1, false)}
active={activeSection === 3}
onClick={() => setActiveSection(3, false)}
icon={faBarsProgress}
disabled={false}
iconTransform="shrink-1"
Expand Down
6 changes: 3 additions & 3 deletions src/screens/Default/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export const Default = () => {
return (
<>
{activeSection === 0 && firstSection}
{activeSection === 2 && <ChainState />}
{activeSection === 3 && <Extrinsics />}
{activeSection === 1 && <ManageTab />}
{activeSection === 1 && <ChainState />}
{activeSection === 2 && <Extrinsics />}
{activeSection === 3 && <ManageTab />}
</>
);
};
1 change: 0 additions & 1 deletion src/screens/Settings/WorkspaceSettings/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import type { AnyJson } from '@w3ux/utils/types';

// The supported localStorage keys for import and export.
// TODO: Reformat local pageSection keys to host in one `pageSections` key.
const SUPPORTED_WORKSPACE_LOCAL_STORAGE_KEYS = [
'activeTabs',
'activeTabId',
Comment on lines 4 to 9
Copy link

Choose a reason for hiding this comment

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

📝 NOTE
This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [14-43]

The exportWorkspace function correctly fetches all keys from localStorage, accumulates their values if they exist, and then exports them as a JSON file. This function is well-implemented with proper error handling and a fallback mechanism in case of JSON parsing errors. However, it's crucial to ensure that the function is called in a context where it does not violate any security policies, such as Content Security Policy (CSP), which might restrict the use of data URIs or dynamic element creation. Additionally, consider providing feedback to the user in case of failure to export the workspace settings, as the current implementation silently fails without notifying the user.

To enhance user experience and error handling, consider implementing a notification or logging mechanism to inform the user when the export operation fails.

Expand Down