-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #308 from omnisat/feat/state-selectors
New Feature: Add option to pass a state selector for less re-renders
- Loading branch information
Showing
9 changed files
with
249 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,55 @@ | ||
import { | ||
ContentType, | ||
MAINNET, | ||
createStores, | ||
LaserEyesClient, | ||
LaserEyesStoreType, | ||
NetworkType, | ||
ProviderType, | ||
} from '@omnisat/lasereyes-core' | ||
import { createContext } from 'react' | ||
import { LaserEyesContextType } from './types' | ||
import { MapStore, WritableAtom } from 'nanostores' | ||
|
||
export const initialContext = { | ||
hasUnisat: false, | ||
hasXverse: false, | ||
hasOyl: false, | ||
hasMagicEden: false, | ||
hasOkx: false, | ||
hasOrange: false, | ||
hasOpNet: false, | ||
hasLeather: false, | ||
hasPhantom: false, | ||
hasSparrow: false, | ||
hasWizz: false, | ||
isInitializing: true, | ||
connected: false, | ||
isConnecting: false, | ||
publicKey: '', | ||
address: '', | ||
paymentAddress: '', | ||
paymentPublicKey: '', | ||
balance: undefined, | ||
network: MAINNET as NetworkType, | ||
library: null, | ||
provider: null, | ||
accounts: [], | ||
connect: async (_network: ProviderType) => { }, | ||
disconnect: () => { }, | ||
requestAccounts: async () => [], | ||
getNetwork: async () => MAINNET, | ||
switchNetwork: async (_network: NetworkType) => { }, | ||
getPublicKey: async () => '', | ||
const { $store, $network } = createStores() | ||
export const defaultMethods = { | ||
connect: async () => {}, | ||
disconnect: () => {}, | ||
getBalance: async () => '', | ||
getInscriptions: async () => [], | ||
sendBTC: async (_to: string, _amount: number) => '', | ||
signMessage: async (_message: string) => '', | ||
signPsbt: async (_tx: string) => { | ||
return { | ||
signedPsbtHex: '', | ||
signedPsbtBase64: '', | ||
txId: '', | ||
} | ||
}, | ||
pushPsbt: async (_tx: string) => { | ||
return '' | ||
}, | ||
inscribe: async (_content: any, _: ContentType) => '', | ||
isCreatingCommit: false, | ||
isInscribing: false, | ||
getNetwork: async () => '', | ||
getPublicKey: async () => '', | ||
pushPsbt: async () => '', | ||
signMessage: async () => '', | ||
requestAccounts: async () => [], | ||
sendBTC: async () => '', | ||
signPsbt: async () => ({ | ||
signedPsbtBase64: '', | ||
signedPsbtHex: '', | ||
}), | ||
switchNetwork: async () => {}, | ||
inscribe: async () => '', | ||
} | ||
|
||
export const LaserEyesContext = | ||
createContext<LaserEyesContextType>(initialContext) | ||
export const LaserEyesStoreContext = createContext<{ | ||
$store: MapStore<LaserEyesStoreType> | ||
$network: WritableAtom<NetworkType> | ||
client: LaserEyesClient | null | ||
methods: Pick< | ||
LaserEyesContextType, | ||
| 'switchNetwork' | ||
| 'signPsbt' | ||
| 'signMessage' | ||
| 'requestAccounts' | ||
| 'sendBTC' | ||
| 'inscribe' | ||
| 'getPublicKey' | ||
| 'getNetwork' | ||
| 'pushPsbt' | ||
| 'getInscriptions' | ||
| 'getBalance' | ||
| 'disconnect' | ||
| 'connect' | ||
> | ||
}>({ | ||
$store, | ||
$network, | ||
client: null, | ||
methods: defaultMethods, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,60 @@ | ||
import { useContext } from 'react' | ||
import { LaserEyesContext } from './context' | ||
import { useContext, useMemo } from 'react' | ||
import { LaserEyesStoreContext } from './context' | ||
import { LaserEyesContextType } from './types' | ||
import { computed, keepMount, onNotify } from 'nanostores' | ||
import { useStore } from '@nanostores/react' | ||
import { compareValues } from '../utils/comparison' | ||
|
||
export const useLaserEyes = (): LaserEyesContextType => { | ||
return useContext(LaserEyesContext) | ||
export function useLaserEyes(): LaserEyesContextType | ||
export function useLaserEyes<T>(selector: (x: LaserEyesContextType) => T): T | ||
export function useLaserEyes<T>( | ||
selector?: (x: LaserEyesContextType) => T | ||
): T | LaserEyesContextType { | ||
const { $network, $store, methods } = useContext(LaserEyesStoreContext) | ||
|
||
const $computedStore = useMemo(() => { | ||
const computedStore = computed([$store, $network], (store, network) => { | ||
const value = { | ||
paymentAddress: store.paymentAddress, | ||
address: store.address, | ||
publicKey: store.publicKey, | ||
paymentPublicKey: store.paymentPublicKey, | ||
library: {}, | ||
network, | ||
accounts: store.accounts, | ||
balance: Number(store.balance), | ||
connected: store.connected, | ||
isConnecting: store.isConnecting, | ||
isInitializing: store.isInitializing, | ||
provider: store.provider, | ||
hasLeather: store.hasProvider.leather ?? false, | ||
hasMagicEden: store.hasProvider['magic-eden'] ?? false, | ||
hasOkx: store.hasProvider.okx ?? false, | ||
hasOyl: store.hasProvider.oyl ?? false, | ||
hasOrange: store.hasProvider.orange ?? false, | ||
hasOpNet: store.hasProvider.op_net ?? false, | ||
hasPhantom: store.hasProvider.phantom ?? false, | ||
hasUnisat: store.hasProvider.unisat ?? false, | ||
hasSparrow: store.hasProvider.sparrow ?? false, | ||
hasWizz: store.hasProvider.wizz ?? false, | ||
hasXverse: store.hasProvider.xverse ?? false, | ||
...methods, | ||
} | ||
if (typeof selector === 'function') { | ||
return selector(value) | ||
} | ||
return value | ||
}) | ||
|
||
keepMount(computedStore) | ||
onNotify(computedStore, ({ oldValue, abort }) => { | ||
if (compareValues(oldValue, computedStore.value)) { | ||
abort() | ||
} | ||
}) | ||
|
||
return computedStore | ||
}, [$network, $store, selector, methods]) | ||
|
||
return useStore($computedStore) | ||
} |
Oops, something went wrong.