Skip to content

Commit

Permalink
Merge pull request #16 from nabla-studio/DavideSegullo/feat-auto_sugg…
Browse files Browse the repository at this point in the history
…estion

Davide segullo/feat auto suggestion
  • Loading branch information
DavideSegullo authored Nov 10, 2023
2 parents 5e5cbba + f7fde06 commit 446327f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 7 deletions.
5 changes: 0 additions & 5 deletions examples/nextjs/components/button.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use client';

import { bitsong, bitsongAssetList } from '@nabla-studio/chain-registry';
import { useConfig, useConnect } from '@quirks/react';
import { suggestChains } from '@quirks/store';

export const Button = () => {
const { wallets } = useConfig();
Expand All @@ -16,9 +14,6 @@ export const Button = () => {
<div key={wallet.options.name}>
<button
onClick={async () => {
await suggestChains(wallet.options.name, [
{ chain: bitsong, assetList: bitsongAssetList, name: 'bitsong' },
]);
await connect(wallet.options.name);
}}
>
Expand Down
35 changes: 34 additions & 1 deletion packages/store/src/slices/connect.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { assertIsDefined, createInvalidWalletName } from '@quirks/core';
import {
type SuggestChain,
assertIsDefined,
createInvalidWalletName,
} from '@quirks/core';
import {
ConnectionStates,
type AppState,
Expand All @@ -8,12 +12,16 @@ import {
type ConnectState,
} from '../types';
import type { StateCreator } from 'zustand/vanilla';
import { suggestChains } from '../utils';

export const connectInitialState: ConnectState = {
walletName: undefined,
wallet: undefined,
status: ConnectionStates.DISCONNECTED,
reconnectionStatus: ReconnectionStates.IDLE,
options: {
autoSuggestions: true,
},
};

export const createConnectSlice: StateCreator<
Expand Down Expand Up @@ -66,6 +74,23 @@ export const createConnectSlice: StateCreator<
}
}
},
suggestChains: async (walletName) => {
const wallet = get().wallets.find((el) => el.options.name === walletName);

if (wallet) {
const chains: SuggestChain[] = get().chains.map((chain) => ({
chain,
name: chain.chain_name,
assetList: get().assetsLists.find(
(list) => list.chain_name === chain.chain_name,
),
}));

console.log(chains);

return suggestChains(wallet.options.name, chains);
}
},
connect: async (walletName) => {
try {
const wallet = get().wallets.find((el) => el.options.name === walletName);
Expand All @@ -76,6 +101,10 @@ export const createConnectSlice: StateCreator<

set(() => ({ walletName, status: ConnectionStates.WAITING }));

if (get().options.autoSuggestions) {
await get().suggestChains(walletName);
}

await wallet.enable(get().chains.map((el) => el.chain_id));

await get().setWallet(wallet);
Expand All @@ -99,6 +128,10 @@ export const createConnectSlice: StateCreator<

set(() => ({ reconnectionStatus: ReconnectionStates.WAITING }));

if (get().options.autoSuggestions) {
await get().suggestChains(walletName);
}

await wallet.enable(get().chains.map((el) => el.chain_id));

await get().setWallet(wallet);
Expand Down
12 changes: 11 additions & 1 deletion packages/store/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export const createConfig = (config: Config) => {
chains,
assetsLists,
autoConnect = true,
autoSuggestions = true,
persistOptions = defaultPersistOptions,
signOptions,
signerOptions,
Expand All @@ -91,6 +92,14 @@ export const createConfig = (config: Config) => {
signerOptions,
};

const connectOverrideInitialState = {
...connectInitialState,
options: {
...connectInitialState.options,
autoSuggestions,
},
};

store = createStore(
subscribeWithSelector(
persist(
Expand All @@ -101,12 +110,13 @@ export const createConfig = (config: Config) => {
assetsLists,
...createConnectSlice(...props),
...createAccountSlice(...props),
...connectOverrideInitialState,
...createSignSlice(...props),
...signOverrideInitialState,
reset: () => {
props[0]({
...configInitialState,
...connectInitialState,
...connectOverrideInitialState,
...accountInitialState,
...signOverrideInitialState,
wallets,
Expand Down
6 changes: 6 additions & 0 deletions packages/store/src/types/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ export const ReconnectionStates = {
export type ReconnectionState =
(typeof ReconnectionStates)[keyof typeof ReconnectionStates];

export interface ConnectOptions {
autoSuggestions: boolean;
}

export interface ConnectState {
walletName?: string;
wallet?: Wallet;
status: ConnectionState;
reconnectionStatus: ReconnectionState;
options: ConnectOptions;
}

export interface ConnectActions {
Expand All @@ -33,6 +38,7 @@ export interface ConnectActions {
connect: (walletName: string) => void;
reconnect: (walletName: string) => void;
disconnect: () => void;
suggestChains: (walletName: string) => Promise<void>;
}

export type ConnectSlice = ConnectState & ConnectActions;
6 changes: 6 additions & 0 deletions packages/store/src/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ export interface Config {
* @default true
*/
autoConnect?: boolean;
/**
* Automatically suggest chains using the provided ones.
*
* @default true
*/
autoSuggestions?: boolean;
/**
* Specify custom sign option
*
Expand Down

0 comments on commit 446327f

Please sign in to comment.