-
Notifications
You must be signed in to change notification settings - Fork 21
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 #178 from xmtp/user/alexrisch/hook-setup
Hook Setup
- Loading branch information
Showing
13 changed files
with
187 additions
and
77 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
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
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,36 @@ | ||
import * as React from 'react' | ||
|
||
import { Client } from '../lib/Client' | ||
|
||
export interface XmtpContextValue { | ||
/** | ||
* The XMTP client instance | ||
*/ | ||
client: Client<any> | null | ||
/** | ||
* Set the XMTP client instance | ||
*/ | ||
setClient: React.Dispatch<React.SetStateAction<Client<any> | null>> | ||
} | ||
|
||
export const XmtpContext = React.createContext<XmtpContextValue>({ | ||
client: null, | ||
setClient: () => {}, | ||
}) | ||
interface Props { | ||
children: React.ReactNode | ||
client?: Client<any> | ||
} | ||
export const XmtpProvider: React.FC<Props> = ({ | ||
children, | ||
client: initialClient, | ||
}) => { | ||
const [client, setClient] = React.useState<Client<any> | null>( | ||
initialClient ?? null | ||
) | ||
const context = React.useMemo( | ||
() => ({ client, setClient }), | ||
[client, setClient] | ||
) | ||
return <XmtpContext.Provider value={context}>{children}</XmtpContext.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 @@ | ||
export * from './XmtpContext' |
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,2 @@ | ||
export * from './useXmtp' | ||
export * from './useClient' |
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,80 @@ | ||
import { Signer } from 'ethers' | ||
import { useCallback, useRef, useState } from 'react' | ||
|
||
import { useXmtp } from './useXmtp' | ||
import { Client, ClientOptions } from '../lib/Client' | ||
|
||
interface InitializeClientOptions { | ||
signer: Signer | ||
options?: ClientOptions | ||
} | ||
|
||
export const useClient = (onError?: (e: Error) => void) => { | ||
const [isLoading, setIsLoading] = useState(false) | ||
const [error, setError] = useState<Error | null>(null) | ||
// client is initializing | ||
const initializingRef = useRef(false) | ||
|
||
const { client, setClient } = useXmtp() | ||
/** | ||
* Initialize an XMTP client | ||
*/ | ||
const initialize = useCallback( | ||
async ({ options, signer }: InitializeClientOptions) => { | ||
// only initialize a client if one doesn't already exist | ||
if (!client && signer) { | ||
// if the client is already initializing, don't do anything | ||
if (initializingRef.current) { | ||
return undefined | ||
} | ||
|
||
// flag the client as initializing | ||
initializingRef.current = true | ||
|
||
// reset error state | ||
setError(null) | ||
// reset loading state | ||
setIsLoading(true) | ||
|
||
let xmtpClient: Client<any> | ||
|
||
try { | ||
// create a new XMTP client with the provided keys, or a wallet | ||
xmtpClient = await Client.create(signer ?? null, { | ||
...options, | ||
}) | ||
setClient(xmtpClient) | ||
} catch (e) { | ||
setClient(null) | ||
setError(e as Error) | ||
onError?.(e as Error) | ||
// re-throw error for upstream consumption | ||
throw e | ||
} | ||
|
||
setIsLoading(false) | ||
|
||
return xmtpClient | ||
} | ||
return client | ||
}, | ||
[client, onError, setClient] | ||
) | ||
|
||
/** | ||
* Disconnect the XMTP client | ||
*/ | ||
const disconnect = useCallback(async () => { | ||
if (client) { | ||
setClient(null) | ||
} | ||
}, [client, setClient]) | ||
|
||
return { | ||
client, | ||
error, | ||
initialize, | ||
disconnect, | ||
isLoading, | ||
} | ||
} |
Oops, something went wrong.