-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Missing for now: - actions / mutations (need spec) - defaultOptions (can be super useful for actions, its a good place to add toasts) - example usage in playground - tests
- Loading branch information
1 parent
54853c1
commit 26e373a
Showing
24 changed files
with
498 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Outlet } from 'react-router-dom' | ||
|
||
export const RootLayout = () => { | ||
return ( | ||
<div className="flex min-h-screen w-full flex-col"> | ||
<header className="border-b border-zinc-200 px-4 py-3"> | ||
<h1 className="text-2xl font-bold">River Playground</h1> | ||
</header> | ||
<div className="flex h-full flex-1 flex-col bg-zinc-50 px-4 pt-8"> | ||
<Outlet /> | ||
</div> | ||
</div> | ||
) | ||
} |
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,14 +1,88 @@ | ||
import { Outlet } from 'react-router-dom' | ||
import { useAccount, useConnect } from 'wagmi' | ||
import { makeRiverConfig } from '@river-build/sdk' | ||
import { useRiverConnection, useSyncValue } from '@river-build/react-sdk' | ||
import { Button } from '@/components/ui/button' | ||
import { useEthersSigner } from '@/utils/viem-to-ethers' | ||
|
||
export const ConnectRoute = () => { | ||
const { isConnected } = useAccount() | ||
|
||
return ( | ||
<div className="flex flex-col gap-6"> | ||
{isConnected ? <ConnectRiver /> : <ChainConnectButton />} | ||
</div> | ||
) | ||
} | ||
|
||
const ChainConnectButton = () => { | ||
const { connector: activeConnector } = useAccount() | ||
const { connect, connectors, error, isLoading, pendingConnector } = useConnect() | ||
|
||
return ( | ||
<div> | ||
{connectors.map((connector) => ( | ||
<Button | ||
disabled={!connector.ready} | ||
key={connector.id} | ||
onClick={() => connect({ connector })} | ||
> | ||
{activeConnector?.id === connector.id | ||
? `Connected - ${connector.name}` | ||
: connector.name} | ||
{isLoading && pendingConnector?.id === connector.id && ' (connecting)'} | ||
</Button> | ||
))} | ||
{error && <div>{error.message}</div>} | ||
</div> | ||
) | ||
} | ||
|
||
const riverConfig = makeRiverConfig('gamma') | ||
|
||
const ConnectRiver = () => { | ||
const signer = useEthersSigner() | ||
const { connect, disconnect, isConnecting, isConnected } = useRiverConnection() | ||
|
||
export const RootLayout = () => { | ||
return ( | ||
<div className="flex min-h-screen w-full flex-col"> | ||
<header className="border-b border-zinc-200 px-4 py-3"> | ||
<h1 className="text-2xl font-bold">River Playground</h1> | ||
</header> | ||
<div className="flex h-full flex-1 flex-col bg-zinc-50 px-4 pt-8"> | ||
<Outlet /> | ||
<> | ||
<div> | ||
<Button | ||
onClick={() => { | ||
if (isConnected) { | ||
disconnect() | ||
} else { | ||
if (signer) { | ||
connect(signer, { riverConfig }) | ||
} | ||
} | ||
}} | ||
> | ||
{isConnected ? 'Disconnect' : isConnecting ? 'Connecting...' : 'Connect'} | ||
</Button> | ||
</div> | ||
{isConnected ? ( | ||
<> | ||
<h2 className="text-lg font-semibold">Connected to Sync Agent</h2> | ||
<ConnectedContent /> | ||
</> | ||
) : ( | ||
<h2 className="text-lg font-semibold">Not Connected</h2> | ||
)} | ||
</> | ||
) | ||
} | ||
|
||
const ConnectedContent = () => { | ||
const { data: nodeUrls } = useSyncValue((s) => s.riverStreamNodeUrls, { | ||
onUpdate: (data) => console.log('onUpdate', data), | ||
onError: (error) => console.error('onError', error), | ||
onSaved: (data) => console.log('onSaved', data), | ||
}) | ||
return ( | ||
<div className="max-w-xl rounded-sm border border-zinc-200 bg-zinc-100 p-2"> | ||
<pre className="overflow-auto whitespace-pre-wrap"> | ||
{JSON.stringify(nodeUrls, null, 2)} | ||
</pre> | ||
</div> | ||
) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import * as React from 'react' | ||
import { type WalletClient, useWalletClient } from 'wagmi' | ||
import { providers } from 'ethers' | ||
|
||
export function walletClientToSigner(walletClient: WalletClient) { | ||
const { account, chain, transport } = walletClient | ||
const network = { | ||
chainId: chain.id, | ||
name: chain.name, | ||
ensAddress: chain.contracts?.ensRegistry?.address, | ||
} | ||
const provider = new providers.Web3Provider(transport, network) | ||
const signer = provider.getSigner(account.address) | ||
return signer | ||
} | ||
|
||
/** Hook to convert a viem Wallet Client to an ethers.js Signer. */ | ||
export function useEthersSigner({ chainId }: { chainId?: number } = {}) { | ||
const { data: walletClient } = useWalletClient({ chainId }) | ||
return React.useMemo( | ||
() => (walletClient ? walletClientToSigner(walletClient) : undefined), | ||
[walletClient], | ||
) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use client' | ||
import type { SyncAgent } from '@river-build/sdk' | ||
import { useEffect, useState } from 'react' | ||
import { RiverSyncContext } from './internals/RiverSyncContext' | ||
|
||
type RiverSyncProviderProps = { | ||
syncAgent?: SyncAgent | ||
children?: React.ReactNode | ||
} | ||
|
||
export const RiverSyncProvider = (props: RiverSyncProviderProps) => { | ||
const [syncAgent, setSyncAgent] = useState(() => props.syncAgent) | ||
|
||
useEffect(() => { | ||
if (syncAgent) { | ||
syncAgent.start() | ||
} | ||
}, [syncAgent]) | ||
|
||
return ( | ||
<RiverSyncContext.Provider | ||
value={{ | ||
syncAgent, | ||
setSyncAgent, | ||
}} | ||
> | ||
{props.children} | ||
</RiverSyncContext.Provider> | ||
) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/// This file can be used on server side to create a River Client | ||
/// We don't want a 'use client' directive here | ||
import { SyncAgent, type SyncAgentConfig, makeSignerContext } from '@river-build/sdk' | ||
import { ethers } from 'ethers' | ||
|
||
export const connectRiver = async ( | ||
signer: ethers.Signer, | ||
config: Omit<SyncAgentConfig, 'context'>, | ||
): Promise<SyncAgent> => { | ||
const delegateWallet = ethers.Wallet.createRandom() | ||
const signerContext = await makeSignerContext(signer, delegateWallet) | ||
return new SyncAgent({ context: signerContext, ...config }) | ||
} |
This file was deleted.
Oops, something went wrong.
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 +1,10 @@ | ||
export { hello } from './context' | ||
/************************************************************************** | ||
* This file can be auto generated by 🏕️ scripts/generate_sdk_index.sh 🏕️ * | ||
**************************************************************************/ | ||
export * from './RiverSyncProvider' | ||
export * from './connectRiver' | ||
export * from './useObservable' | ||
export * from './useRiverConnection' | ||
export * from './useSyncAgent' | ||
export * from './useSyncValue' | ||
export * from './utils' |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use client' | ||
import { SyncAgent } from '@river-build/sdk' | ||
import { createContext } from 'react' | ||
|
||
type RiverSyncContextType = { | ||
syncAgent: SyncAgent | undefined | ||
setSyncAgent: (syncAgent: SyncAgent | undefined) => void | ||
} | ||
export const RiverSyncContext = createContext<RiverSyncContextType | undefined>(undefined) |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
'use client' | ||
import { useContext } from 'react' | ||
import { RiverSyncContext } from './RiverSyncContext' | ||
export const useRiverSync = () => useContext(RiverSyncContext) |
Oops, something went wrong.