Skip to content
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

Docs for React SDK production release #544

Merged
merged 8 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 85 additions & 31 deletions docs/build/authentication.md

Large diffs are not rendered by default.

125 changes: 106 additions & 19 deletions docs/build/conversations.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,43 @@ let canAliceMessageBob = try await client.canMessage(bobClient.address)
<TabItem value="react" label="React" attributes={{className: "react_tab"}}>

```tsx
const { canMessage } = useCanMessage();
if (await canMessage("0x3F11b27F323b62B159D2642964fa27C46C841897")) {
//Create conversation
}
import { useCanMessage } from "@xmtp/react-sdk";

export const CanMessage: React.FC = () => {
const [peerAddress, setPeerAddress] = useState("");
const [isOnNetwork, setIsOnNetwork] = useState(false);
const [isLoading, setIsLoading] = useState(false);

const { canMessage } = useCanMessage();

const handleAddressChange = useCallback((e: React.KeyboardEvent<HTMLInputElement>) => {
setPeerAddress(e.target.value);
}, []);

const handleCheckAddress = useCallback(async (e: FormEvent) => {
e.preventDefault();
if (isValidAddress(peerAddress)) {
setIsLoading(true);
setIsOnNetwork(await canMessage(peerAddress));
setIsLoading(false);
} else {
setIsOnNetwork(false);
}
};
void checkAddress();
}, [peerAddress]);

return (
<form onSubmit={handleCheckAddress}>
<input
name="addressInput"
type="text"
onChange={handleAddressChange}
disabled={isLoading}
/>
</form>
);
};
```

</TabItem>
Expand Down Expand Up @@ -117,11 +150,59 @@ val newConversation =
<TabItem value="react" label="React" attributes={{className: "react_tab"}}>

```tsx
const startConversation = useStartConversation();
const convv = await startConversation(
"0x3F11b27F323b62B159D2642964fa27C46C841897",
"hi",
);
import { isValidAddress, useStartConversation } from "@xmtp/react-sdk";
import { useCallback, useState } from "react";

export const StartConversation: React.FC = () => {
const [peerAddress, setPeerAddress] = useState("");
const [message, setMessage] = useState("");
const [isLoading, setIsLoading] = useState(false);

const startConversation = useStartConversation();

const handleAddressChange = useCallback(
(e: ChangeEvent<HTMLInputElement>) => {
setPeerAddress(e.target.value);
},
[],
);

const handleMessageChange = useCallback(
(e: ChangeEvent<HTMLInputElement>) => {
setMessage(e.target.value);
},
[],
);

const handleStartConversation = useCallback(
async (e: React.FormEvent) => {
e.preventDefault();
if (peerAddress && message) {
setIsLoading(true);
const conversation = await startConversation(peerAddress, message);
setIsLoading(false);
}
},
[message, peerAddress, startConversation],
);

return (
<form onSubmit={handleStartConversation}>
<input
name="addressInput"
type="text"
onChange={handleAddressChange}
disabled={isLoading}
/>
<input
name="messageInput"
type="text"
onChange={handleMessageChange}
disabled={isLoading || !isValidAddress(peerAddress)}
/>
</form>
);
};
```

</TabItem>
Expand Down Expand Up @@ -199,16 +280,22 @@ for (conversation in allConversations) {
<TabItem value="react" label="React" attributes={{className: "react_tab"}}>

```tsx
const { conversations, error, isLoading } = useConversations();

{
conversations.map((conversation, index) => (
<div key={index}>
{conversation?.peerAddress}-
{JSON.stringify(conversation.context?.metadata)}
</div>
));
}
export const ListConversations: React.FC = () => {
const { initialize } = useClient();
const { conversations, error, isLoading } = useConversations();

if (error) {
return "An error occurred while loading conversations";
}

if (isLoading) {
return "Loading conversations...";
}

return (
...
);
};
jhaaaa marked this conversation as resolved.
Show resolved Hide resolved
```

</TabItem>
Expand Down
Binary file added docs/build/img/local-first-arch.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading