Skip to content

Commit

Permalink
Refactor after review #575
Browse files Browse the repository at this point in the history
  • Loading branch information
caebr committed Jul 17, 2023
1 parent 6e5b6d7 commit 45c1bed
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 28 deletions.
35 changes: 25 additions & 10 deletions src/components/Portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { getHash, getScopeFromUrl, updateHash } from "../utils/routing";
import { getCurrentAccessToken } from "../utils/storage";
import { settings } from "../settings";
import { getInitialLocale } from "../utils/locale";
import { getNavigationItemKey } from "../utils/navigation.ts";
import { getNavigationItemByAppPath } from "../utils/navigation.ts";

const oAuthClient = createOAuthClient();

Expand Down Expand Up @@ -129,28 +129,43 @@ export class Portal extends LitElement {
switch (data.type) {
case "bkdAppPushState": {
const url = data.args[2];
const hash = getHash(url);
updateHash(hash, false);
const itemKey = getNavigationItemKey(portalState.navigation, hash);
if (itemKey && itemKey !== portalState.navigationItemKey) {
portalState.actualAppPath = hash;
portalState.navigationItemKey = itemKey;
}
this.updateUrlAndNavigationState(url, false);
break;
}
case "bkdAppReplaceState": {
const url = data.args[2];
updateHash(getHash(url), true);
this.updateUrlAndNavigationState(url, true);
break;
}
case "bkdAppHashChange": {
const { url } = data;
updateHash(getHash(url));
this.updateUrlAndNavigationState(url, false);
break;
}
}
};

/**
* When navigating between modules of the same app, be sure to update the
* portal URL and navigation state
*/
private updateUrlAndNavigationState(url: string, replaceHash: boolean) {
const hash = getHash(url);
updateHash(hash, replaceHash);

const navigationItem = getNavigationItemByAppPath(
portalState.navigation,
hash
);
if (
navigationItem?.item?.key &&
navigationItem.item.key !== portalState.navigationItemKey
) {
portalState.actualAppPath = hash;
portalState.navigationItemKey = navigationItem.item.key;
}
}

/**
* Update the iframe's appPath whenever the user changes the hash in
* the portal URL
Expand Down
43 changes: 25 additions & 18 deletions src/utils/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,38 @@ export function getNavigationItem(
navigation: Navigation,
itemKey: string
): { item: NavigationItem; group: NavigationGroup | null } {
let item = ungroupedNavigationItems.find(({ key }) => key === itemKey);
if (item) return { item, group: null };

for (const group of navigation) {
item = group.items.find(({ key }) => key === itemKey);
if (item) return { item, group };
}

return { item: settings.navigationHome, group: null };
const navigationItem = findNavigationItem(
navigation,
({ key }) => key === itemKey
);
return navigationItem
? navigationItem
: { item: settings.navigationHome, group: null };
}

export function getNavigationItemKey(
/**
* Returns navigation item (and its group) with given app path.
*/
export function getNavigationItemByAppPath(
navigation: Navigation,
appPath: string
): string | null {
let key = ungroupedNavigationItems.find(
): { item: NavigationItem | null; group: NavigationGroup | null } | null {
return findNavigationItem(
navigation,
(item) => item.appPath !== "#/" && appPath.startsWith(item.appPath)
)?.key;
if (key) return key;
);
}

function findNavigationItem(
navigation: Navigation,
callback: (item: NavigationItem) => boolean
): { item: NavigationItem; group: NavigationGroup | null } | null {
let item = ungroupedNavigationItems.find((item) => callback(item));
if (item) return { item, group: null };

for (const group of navigation) {
key = group.items.find(
(item) => item.appPath !== "#/" && appPath.startsWith(item.appPath)
)?.key;
if (key) return key;
item = group.items.find((item) => callback(item));
if (item) return { item, group };
}

return null;
Expand Down

0 comments on commit 45c1bed

Please sign in to comment.