Skip to content

Commit f6c017d

Browse files
committed
Tutorial fixes
1 parent e3769d2 commit f6c017d

3 files changed

Lines changed: 43 additions & 8 deletions

File tree

docs/docs/sdks/typescript/quickstart.md

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ Replace the entire contents of `client/src/App.tsx` with the following:
4949
```tsx
5050
import React, { useEffect, useState } from 'react';
5151
import { DbConnection, Message, User } from './module_bindings';
52+
import {
53+
useSpacetimeDB,
54+
useTable,
55+
where,
56+
eq,
57+
} from '@clockworklabs/spacetimedb-sdk/react';
58+
import { Identity, Timestamp } from '@clockworklabs/spacetimedb-sdk';
5259
import './App.css';
5360

5461
export type PrettyMessage = {
@@ -67,6 +74,7 @@ function App() {
6774
const prettyMessages: PrettyMessage[] = [];
6875
const onlineUsers: User[] = [];
6976
const offlineUsers: User[] = [];
77+
const users = [...onlineUsers, ...offlineUsers];
7078

7179
const name = '';
7280

@@ -76,7 +84,7 @@ function App() {
7684
// TODO: Call `setName` reducer
7785
};
7886

79-
const onMessageSubmit = (e: React.FormEvent<HTMLFormElement>) => {
87+
const onSubmitMessage = (e: React.FormEvent<HTMLFormElement>) => {
8088
e.preventDefault();
8189
setNewMessage('');
8290
// TODO: Call `sendMessage` reducer
@@ -187,7 +195,7 @@ return (
187195
</div>
188196
<div className="new-message">
189197
<form
190-
onSubmit={onMessageSubmit}
198+
onSubmit={onSubmitMessage}
191199
style={{
192200
display: 'flex',
193201
flexDirection: 'column',
@@ -514,7 +522,7 @@ const onConnectError = (_ctx: ErrorContext, err: Error) => {
514522
const connectionBuilder = DbConnection.builder()
515523
.withUri('ws://localhost:3000')
516524
.withModuleName('quickstart-chat')
517-
.withToken(localStorage.getItem('auth_token') || '')
525+
.withToken(localStorage.getItem('auth_token') || undefined)
518526
.onConnect(onConnect)
519527
.onDisconnect(onDisconnect)
520528
.onConnectError(onConnectError);
@@ -534,7 +542,7 @@ We are also using `localStorage` to store our SpacetimeDB credentials. This way,
534542

535543
If you chose a different name for your database, replace `quickstart-chat` with that name, or republish your module as `quickstart-chat`.
536544

537-
In the `onConnect` function we are also subscribing to the `message` and `user` tables. When we subscribe, SpacetimeDB will run our subscription queries and store the result in a local "client cache". This cache will be updated in real-time as the data in the table changes on the server.
545+
Our React hooks will subscribe to the data in SpacetimeDB. When we subscribe, SpacetimeDB will run our subscription queries and store the result in a local "client cache". This cache will be updated in real-time as the data in the table changes on the server.
538546

539547
We pass our connection configuration directly to the `SpacetimeDBProvider` which will manage our connection to SpacetimeDB.
540548

@@ -628,7 +636,7 @@ Modify the `onSubmitNewName` callback by adding a call to the `setName` reducer:
628636
Next modify the `onSubmitMessage` callback by adding a call to the `sendMessage` reducer:
629637

630638
```tsx
631-
const onMessageSubmit = (e: React.FormEvent<HTMLFormElement>) => {
639+
const onSubmitMessage = (e: React.FormEvent<HTMLFormElement>) => {
632640
e.preventDefault();
633641
setNewMessage("");
634642
conn.reducers.sendMessage(newMessage);
@@ -703,6 +711,33 @@ These callbacks will be called any time the state of the `useTable` result chang
703711

704712
Here we post a system message saying a new user has connected if the user is being added to the `user` table and they're online, or if an existing user's online status is being set to "online".
705713

714+
Next, let's add the system messages to our list of `Message`s so they can be interleaved with the chat messages. Modify `prettyMessages` to concat the `systemMessages` as well:
715+
716+
```tsx
717+
const prettyMessages: PrettyMessage[] = Array.from(messages)
718+
.concat(systemMessages)
719+
.sort((a, b) => (a.sent.toDate() > b.sent.toDate() ? 1 : -1))
720+
.map(message => {
721+
const user = users.find(
722+
u => u.identity.toHexString() === message.sender.toHexString()
723+
);
724+
return {
725+
senderName: user?.name || message.sender.toHexString().substring(0, 8),
726+
text: message.text,
727+
sent: message.sent,
728+
kind: Identity.zero().isEqual(message.sender) ? 'system' : 'user',
729+
};
730+
});
731+
```
732+
733+
Finally, let's also subscribe to offline users so we can show offline users in the side bar as well. Replace `const offlineUsers: User[] = [];` with:
734+
735+
```tsx
736+
const { rows: offlineUsers } = useTable<DbConnection, User>(
737+
'user',
738+
where(eq('online', false))
739+
);
740+
706741
## Conclusion
707742

708743
Congratulations! You've built a simple chat app with SpacetimeDB. You can find the full source code for the client we've created in this quickstart tutorial [here](https://github.com/clockworklabs/SpacetimeDB/tree/master/sdks/typescript/examples/quickstart-chat).

sdks/typescript/examples/quickstart-chat/src/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function App() {
104104
conn.reducers.setName(newName);
105105
};
106106

107-
const onMessageSubmit = (e: React.FormEvent<HTMLFormElement>) => {
107+
const onSubmitMessage = (e: React.FormEvent<HTMLFormElement>) => {
108108
e.preventDefault();
109109
setNewMessage('');
110110
conn.reducers.sendMessage(newMessage);
@@ -214,7 +214,7 @@ function App() {
214214
</div>
215215
<div className="new-message">
216216
<form
217-
onSubmit={onMessageSubmit}
217+
onSubmit={onSubmitMessage}
218218
style={{
219219
display: 'flex',
220220
flexDirection: 'column',

sdks/typescript/examples/quickstart-chat/src/main.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const onConnectError = (_ctx: ErrorContext, err: Error) => {
2828
const connectionBuilder = DbConnection.builder()
2929
.withUri('ws://localhost:3000')
3030
.withModuleName('quickstart-chat')
31-
.withToken(localStorage.getItem('auth_token') || '')
31+
.withToken(localStorage.getItem('auth_token') || undefined)
3232
.onConnect(onConnect)
3333
.onDisconnect(onDisconnect)
3434
.onConnectError(onConnectError);

0 commit comments

Comments
 (0)