-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
react hooks #1687
base: main
Are you sure you want to change the base?
react hooks #1687
Conversation
|
b586a78
to
b5d8002
Compare
b5d8002
to
704552b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now
If I were to write the examples for this package, I'd write smt like this:
Wrap the app in the Penumbra context provider
import { PenumbraContextProvider } from '@penumbra-zone/react';
import App from './app.tsx';
export const Main = () => {
return (
<React.StrictMode>
<PenumbraContextProvider>
<App />
</PenumbraContextProvider>
</React.StrictMode>
);
};
const rootElement = document.getElementById('root') as HTMLDivElement;
createRoot(rootElement).render(<Main />);
Get the Penumbra connection state
import { usePenumbra } from '@penumbra-zone/react';
const Component = () => {
const { connected, connect } = usePenumbra();
if (!connected) {
return (
<button type="button" onClick={connect} />
);
}
return (
<button type="button" onClick={disconnect} />
);
};
Request blockchain data by calling the queries
import { usePenumbraQuery } from '@penumbra-zone/react';
import { ViewService } from '@penumbra-zone/protobuf';
import { bech32mAddress } from '@penumbra-zone/bech32m/penumbra';
const Component = () => {
const viewService = usePenumbraQuery(ViewService);
const { data, error, isPending } = viewService.getAddressByIndex({ account: 0 });
if (isPending) {
return <p>loading...</p>;
}
return <p>You account address is {bech32mAddress(data)}</p>;
}
Or request blockchain data without wrappers
import { usePenumbraService } from '@penumbra-zone/react';
import { ViewService } from '@penumbra-zone/protobuf';
const viewService = usePenumbraService(ViewService);
const response = await viewService.getAddressByIndex({ account: 0 });
Proposal
Having the idea of the current implementation state, I think of more ways of using the React package:
- Firstly, we need hooks to represent static methods of
PenumbraClient
:getProviders
,getProviderManifest
, and the rest. It could be, for instance, theuseProviders
hook. It might wrap the provider list into the react query. - Queries with simplified namings:
useBalances
,useAccount
, etc. It will use theusePenumbraQuery
under the hook but at least the names will be clear. - Something to simplify the creation of a transaction. It might also be a naming issue but for me personally it is extremely hard to compose a transaction. This can also go into the
client
package, not only for react.
@turbocrime @grod220 wdyt?
console.log('providerConnected', providerConnected); | ||
|
||
return ( | ||
<penumbraContext.Provider value={providerConnected ? penumbra : penumbra}> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should it be like this?
<penumbraContext.Provider value={providerConnected ? penumbra : penumbra}> | |
<penumbraContext.Provider value={penumbra}> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably the value of the context should be a reactive object similar to PenumbraClient
but without static methods, service
, onConnectionStateChange
and others non-needed functions replaced by hooks
parts of minifront slices that might be helpful in developing hooks
there's plenty more, these are just some suggestions. anything in the minifront slices that assembles a plan request or a transaction request could contribute to a provided querier. |
this contains some sketches for a new react package.
context-based, which there was some interest in moving away from. but
the
usePenumbraQuery
hook is the interesting part, as it wraps aPromiseClient
withuseQuery
, so that callers may automatically consume penumbra requests as react queries. streams are consumed withArray.fromAsync
so it is not suitable for long-streaming requests.