Skip to content

Commit

Permalink
Merge pull request #22 from rossbulat/rb-import-reset-workspace
Browse files Browse the repository at this point in the history
feat: import & reset workspace functionality
  • Loading branch information
Ross Bulat authored Mar 10, 2024
2 parents 3b5644e + 614955d commit de213ea
Show file tree
Hide file tree
Showing 12 changed files with 521 additions and 223 deletions.
10 changes: 4 additions & 6 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Entry } from 'library/Entry';
import { Router } from 'Router';
import { ErrorBoundary } from 'react-error-boundary';
import { AppErrorBoundary } from 'library/ErrorBoundaries/AppErrorBoundary';
import * as integrityChecks from 'IntegrityChecks';
import { performLocalIntegrityChecks } from 'IntegrityChecks/Local';

// The currently supported pages.
export type PageId = 'default' | 'settings';
Expand All @@ -16,12 +16,10 @@ export const App = () => (
<HashRouter basename="/">
<ErrorBoundary
FallbackComponent={AppErrorBoundary}
onReset={() => {
onReset={() =>
// Check local storage for integrity & upate if necessary.
integrityChecks.checkLocalTabs();
integrityChecks.checkLocalTags();
integrityChecks.checkLocalChainFilter();
}}
performLocalIntegrityChecks()
}
>
<Router />
</ErrorBoundary>
Expand Down
198 changes: 0 additions & 198 deletions src/IntegrityChecks.ts

This file was deleted.

156 changes: 156 additions & 0 deletions src/IntegrityChecks/Local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// Copyright 2024 @rossbulat/console authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import {
performTabsCheck,
sanitizeAppliedTags,
sanitizeKeysForTabExistence,
sanitizeTags,
} from 'IntegrityChecks';
import * as localTabs from 'contexts/Tabs/Local';
import * as localTags from 'contexts/Tags/Local';
import * as localChainFilter from 'contexts/ChainFilter/Local';
import { defaultTabs } from 'contexts/Tabs/defaults';
import { defaultTags, defaultTagsConfig } from 'contexts/Tags/defaults';
import {
defaultAppliedTags,
defaultCustomNodeUrls,
defaultSearchTerms,
} from 'contexts/ChainFilter/defaults';
import type { RemoveOrSetInput } from './types';

// ------------------------------------------------------
// Tabs.
// ------------------------------------------------------

// Check local tabs data.
export const checkLocalTabs = () => {
// Use default tabs if activeTabs is empty.
const activeTabs = localTabs.getTabs() || defaultTabs;
const activeTabId = localTabs.getActiveTabId() || 0;
const activeTabIndex = localTabs.getActiveTabIndex() || 0;

const { activeTabsValid, activeTabIdValid, activeTabIndexValid } =
performTabsCheck({
activeTabs,
activeTabId,
activeTabIndex,
});

// Clear all tab data if active tabs are invalid.
if (!activeTabsValid) {
removeLocalStorageState();
}

// Clear activeTabId if it is not valid.
if (!activeTabIdValid) {
localStorage.removeItem('activeTabId');
}

// Clear `activeTabIndex` if not valid.
if (!activeTabIndexValid) {
localStorage.removeItem('activeTabIndex');
}
};

// ------------------------------------------------------
// Tags.
// ------------------------------------------------------

export const checkLocalTags = () => {
const tags = localTags.getTags() || defaultTags;
const tagsConfig = localTags.getTagsConfig() || defaultTagsConfig;

const { updated, result } = sanitizeTags({ tags, tagsConfig });

// If result is empty, clear it from local storage.
if (JSON.stringify(result) === '{}') {
localStorage.removeItem('tagsConfig');
} else {
// Update local storage if `tagsConfig` was updated.
if (updated) {
localTags.setTagsConfig(result);
}
}
};

// ------------------------------------------------------
// Chain Filter.
// ------------------------------------------------------

// Check existing local storage and clear up if there are errors.
export const checkLocalChainFilter = () => {
// Use default tabs if activeTabs is empty.
const activeTabs = localTabs.getTabs() || defaultTabs;
const tags = localTags.getTags() || defaultTags;
const searchTerms = localChainFilter.getSearchTerms() || defaultSearchTerms;
const customNodeUrls =
localChainFilter.getCustomNodeUrls() || defaultCustomNodeUrls;
const appliedTags = localChainFilter.getAppliedTags() || defaultAppliedTags;

// Check if tabs exist for each search term, and remove the entry otherwise.
removeOrSetLocalData(
'searchTerms',
sanitizeKeysForTabExistence(activeTabs, searchTerms)
);

// Check if tabs exist for each custom node url, and remove the entry otherwise.
removeOrSetLocalData(
'customNodeUrls',
sanitizeKeysForTabExistence(activeTabs, customNodeUrls)
);

// Check if tab index exists for each applied tag key, and that the corresponding tag entry also
// exists. Remove otherwise.
removeOrSetLocalData(
'appliedTags',
sanitizeAppliedTags({
activeTabs,
tags,
appliedTags,
})
);
};

// ------------------------------------------------------
// Utils.
// ------------------------------------------------------

// Remove or update local data depending on resulting updated state.
export const removeOrSetLocalData = <T>(
key: string,
{ updated, result }: RemoveOrSetInput<T>
) => {
if (!result || JSON.stringify(result) === '{}') {
localStorage.removeItem(key);
} else {
if (updated) {
localChainFilter.setCustomNodeUrls(result);
}
}
};

// Call all local integrity checks.
export const performLocalIntegrityChecks = () => {
checkLocalTabs();
checkLocalTags();
checkLocalChainFilter();
};

// Remove all local storage state tied to tabs config, or remove all workspace state entirely if
// `includeTags` is set to true.
export const removeLocalStorageState = (includeTags = false) => {
localStorage.removeItem('activeTabs');
localStorage.removeItem('activeTabId');
localStorage.removeItem('activeTabIndex');
localStorage.removeItem('searchTerms');
localStorage.removeItem('customNodeUrls');
localStorage.removeItem('appliedTags');
localStorage.removeItem('pageSections');
localStorage.removeItem('pageRedirects');

if (includeTags) {
localStorage.removeItem('tags');
localStorage.removeItem('tagsConfig');
}
};
Loading

0 comments on commit de213ea

Please sign in to comment.