Skip to content

Commit

Permalink
add recent chain to Connect
Browse files Browse the repository at this point in the history
  • Loading branch information
Ross Bulat committed Mar 7, 2024
1 parent 2a07a36 commit 19b3ff1
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/contexts/Api/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,10 @@ export const ApiProvider = ({ children }: { children: ReactNode }) => {
if (tab?.chain && tab.autoConnect) {
const { id, provider } = tab.chain;

const endpoint = NetworkDirectory[id].providers[provider];
ApiController.instantiate(tab.id, id, endpoint);
const endpoint = NetworkDirectory[id]?.providers[provider];
if (endpoint) {
ApiController.instantiate(tab.id, id, endpoint);
}
}
});
}, []);
Expand Down
6 changes: 2 additions & 4 deletions src/contexts/Tabs/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const defaultTabsContext: TabsContextInterface = {
redirectCounter: 0,
incrementRedirectCounter: () => {},
connectTab: (tabId, chainId, endpoint) => {},
getStoredChain: (tabId) => undefined,
};

export const DEFAULT_TAB_WIDTH_PX = 145;
Expand Down Expand Up @@ -65,10 +66,7 @@ export const defaultTabs: Tabs = [
},
{
id: 4,
chain: {
id: 'westend',
provider: 'IBP-GeoDNS1',
},
chain: undefined,
name: 'Westend Relay Chain',
autoConnect: false,
},
Expand Down
13 changes: 13 additions & 0 deletions src/contexts/Tabs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ export const TabsProvider = ({ children }: { children: ReactNode }) => {
const getChainTab = (chainId: ChainId) =>
tabs.find((tab) => tab.chain?.id === chainId);

// Gets the previously connected to chain, if present.
const getStoredChain = (tabId: number) => {
const tab = tabs.find(({ id }) => id === tabId);

if (!tab?.chain?.id) {
return undefined;
}

return { id: tab.chain.id, chain: NetworkDirectory[tab.chain.id] };
};

// Get the largest id from a list of tabs.
const getLargestId = (list: Tabs) =>
[...list].sort((a, b) => b.id - a.id)?.[0].id || 0;
Expand Down Expand Up @@ -167,6 +178,7 @@ export const TabsProvider = ({ children }: { children: ReactNode }) => {
tab.id === tabId
? {
...tab,
// Auto rename the tab here if the setting is turned on.
name: autoTabNaming ? getAutoTabName(chainId) : tab.name,
chain: { id: chainId, provider },
}
Expand Down Expand Up @@ -204,6 +216,7 @@ export const TabsProvider = ({ children }: { children: ReactNode }) => {
connectTab,
redirectCounter,
incrementRedirectCounter,
getStoredChain,
instantiatedIds: instantiatedIds.current,
}}
>
Expand Down
5 changes: 4 additions & 1 deletion src/contexts/Tabs/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2024 @rossbulat/console authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import type { ChainId } from 'config/networks';
import type { ChainId, NetworkDirectoryItem } from 'config/networks';
import type { Dispatch, SetStateAction } from 'react';

export type Tabs = Tab[];
Expand Down Expand Up @@ -42,4 +42,7 @@ export interface TabsContextInterface {
redirectCounter: number;
incrementRedirectCounter: () => void;
connectTab: (tabId: number, chainId: ChainId, endpoint: string) => void;
getStoredChain: (
tabId: number
) => { id: ChainId; chain: NetworkDirectoryItem } | undefined;
}
12 changes: 10 additions & 2 deletions src/screens/Default/Connect/ChainList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import { useTags } from 'contexts/Tags';
import type { TagItem } from 'contexts/Tags/types';

export const ChainList = () => {
const { activeTabId } = useTabs();
const { getTagsForChain } = useTags();
const { activeTabId, getStoredChain } = useTabs();
const { getAppliedTags, getSearchTerm } = useChainFilter();
const tabStoredChain = getStoredChain(activeTabId);

// NOTE: Currently naively filering simple chain list.
// NOTE: Currently naively filtering simple chain list.

const results = NetworkDirectory;

Expand All @@ -35,6 +36,13 @@ export const ChainList = () => {
// Filter chains based on search term.
const searchTerm = getSearchTerm(activeTabId);

// Remove the currently stored chain from results if it exists.
if (tabStoredChain) {
filtered = Object.fromEntries(
Object.entries(filtered).filter(([key]) => key !== tabStoredChain.id)
);
}

if (searchTerm !== '') {
filtered = Object.fromEntries(
Object.entries(filtered).filter(([, { name }]) =>
Expand Down
26 changes: 26 additions & 0 deletions src/screens/Default/Connect/RecentChain.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2024 @rossbulat/console authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import { useTabs } from 'contexts/Tabs';
import { ChainListWrapper, Separator } from './Wrappers';
import { ChainListItem } from './ChainListItem';
import type { ChainId } from 'config/networks';

export const RecentChain = () => {
const { activeTabId, getStoredChain, getActiveTab } = useTabs();
const activeTab = getActiveTab();

const result = getStoredChain(activeTabId);

if (!result || !activeTab?.chain?.id) {
return null;
}

return (
<ChainListWrapper>
<Separator />
<h4>Recently Connected</h4>
<ChainListItem chainId={result.id as ChainId} name={result.chain.name} />
</ChainListWrapper>
);
};
2 changes: 2 additions & 0 deletions src/screens/Default/Connect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { ChainList } from './ChainList';
import { AutoConnect } from '../AutoConnect';
import { SearchChain } from './SearchChain';
import { PageContentWrapper } from 'library/Page/Wrapper';
import { RecentChain } from './RecentChain';

export const Connect = () => (
<PageContentWrapper>
<AutoConnect />
<SearchChain />
<RecentChain />
<ChainList />
</PageContentWrapper>
);

0 comments on commit 19b3ff1

Please sign in to comment.