Skip to content

Commit

Permalink
add getTabApi
Browse files Browse the repository at this point in the history
  • Loading branch information
Ross Bulat committed Mar 5, 2024
1 parent 40b669b commit a28fa29
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/contexts/Api/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright 2024 @rossbulat/console authors & contributors
// SPDX-License-Identifier: GPL-3.0-only
/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function */

import type { ApiContextInterface } from './types';

export const defaultApiContext: ApiContextInterface = {
isReady: false,
getTabApi: () => undefined,
};
35 changes: 26 additions & 9 deletions src/contexts/Api/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,34 @@ import type { ReactNode } from 'react';
import { createContext, useContext } from 'react';
import { defaultApiContext } from './defaults';
import type { ApiContextInterface } from './types';
import { ApiController } from 'controllers/ApiController';
import { useTabs } from 'contexts/Tabs';

export const Api = createContext<ApiContextInterface>(defaultApiContext);

export const useApi = () => useContext(Api);

export const ApiProvider = ({ children }: { children: ReactNode }) => (
<Api.Provider
value={{
isReady: false,
}}
>
{children}
</Api.Provider>
);
export const ApiProvider = ({ children }: { children: ReactNode }) => {
const { getActiveTab } = useTabs();

// Gets the Api instance of the active tab, if present.
const getTabApi = () => {
const chainId = getActiveTab()?.chainId;
if (chainId) {
return ApiController.instances[chainId];
}
};

return (
<Api.Provider
value={{
isReady: false,
// TODO: apiStatus,
// TODO: setRpcEndpoint,
getTabApi,
}}
>
{children}
</Api.Provider>
);
};
3 changes: 3 additions & 0 deletions src/contexts/Api/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright 2024 @rossbulat/console authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import type { Api } from 'model/Api';

export interface ApiContextInterface {
isReady: boolean;
getTabApi: () => Api | undefined;
}
8 changes: 4 additions & 4 deletions src/contexts/Tabs/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,27 @@ export const TAB_TRANSITION_DURATION_MS = 300;
export const defaultTabs: Tabs = [
{
id: 1,
chain: 'polkadot-relay-chain',
chainId: 'polkadot-relay-chain',
name: 'Polkadot Relay',
autoConnect: true,
},
{
id: 2,
chain: 'kusama-relay-chain',
chainId: 'kusama-relay-chain',
name: 'Kusama Relay',
autoConnect: true,
},
{
id: 3,
chain: 'westend-relay-chain',
chainId: 'westend-relay-chain',
name: 'Westend Relay Long Name',
autoConnect: true,
},
];

export const defaultEemptyTab: Tab = {
id: -1,
chain: undefined,
chainId: undefined,
name: '',
autoConnect: true,
};
2 changes: 1 addition & 1 deletion src/contexts/Tabs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const TabsProvider = ({ children }: { children: ReactNode }) => {
...tabs,
{
id: newTabId,
chain: undefined,
chainId: undefined,
name: 'New Tab',
autoConnect,
},
Expand Down
2 changes: 1 addition & 1 deletion src/contexts/Tabs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type Tabs = Tab[];

export interface Tab {
id: number;
chain: ChainId | undefined;
chainId: ChainId | undefined;
name: string;
autoConnect: boolean;
}
Expand Down

0 comments on commit a28fa29

Please sign in to comment.