Skip to content

Commit aea6e32

Browse files
cloutiertylerbfops
andcommitted
[release/typescript]: Fixed the react router issue and added a new test app to test for it (#3428)
This PR fixes #3359. Additionally I have add a new test app to test for this issue and I changed a perl script to be portable on windows. The issue was that `useTable` had it's own `isActive` state that it was incorrectly maintaining, when it should just have been using the connections `isActive` state directly. This fixes a critical bug and should be pushed ASAP. None, fixes a critical bug. 2, quite straightforward fix after figuring it out. - [x] I added a test app and tested the fix in my browser. --------- Co-authored-by: Zeke Foppa <[email protected]>
1 parent 6727594 commit aea6e32

35 files changed

+3005
-1442
lines changed

Cargo.lock

Lines changed: 1321 additions & 1418 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ members = [
4949
"tools/upgrade-version",
5050
"tools/license-check",
5151
"crates/bindings-typescript/test-app/server",
52+
"crates/bindings-typescript/test-react-router-app/server",
5253
]
5354
default-members = ["crates/cli", "crates/standalone", "crates/update"]
5455
# cargo feature graph resolver. v3 is default in edition2024 but workspace

crates/bindings-typescript/src/react/useTable.ts

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,6 @@ export function useTable<
292292
| undefined;
293293
}
294294
const [subscribeApplied, setSubscribeApplied] = useState(false);
295-
const [isActive, setIsActive] = useState(false);
296295
let spacetime: DbConnection | undefined;
297296
try {
298297
spacetime = useSpacetimeDB<DbConnection>();
@@ -329,27 +328,7 @@ export function useTable<
329328
}, [client, tableName, whereKey, subscribeApplied]);
330329

331330
useEffect(() => {
332-
const onConnect = () => {
333-
setIsActive(client.isActive);
334-
};
335-
const onDisconnect = () => {
336-
setIsActive(client.isActive);
337-
};
338-
const onConnectError = () => {
339-
setIsActive(client.isActive);
340-
};
341-
client['on']('connect', onConnect);
342-
client['on']('disconnect', onDisconnect);
343-
client['on']('connectError', onConnectError);
344-
return () => {
345-
client['off']('connect', onConnect);
346-
client['off']('disconnect', onDisconnect);
347-
client['off']('connectError', onConnectError);
348-
};
349-
}, [client]);
350-
351-
useEffect(() => {
352-
if (isActive) {
331+
if (client.isActive) {
353332
const cancel = client
354333
.subscriptionBuilder()
355334
.onApplied(() => {
@@ -360,7 +339,7 @@ export function useTable<
360339
cancel.unsubscribe();
361340
};
362341
}
363-
}, [query, isActive, client]);
342+
}, [query, client.isActive, client]);
364343

365344
const subscribe = useCallback(
366345
(onStoreChange: () => void) => {

crates/bindings-typescript/test-app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"format": "prettier . --write --ignore-path ../../../.prettierignore",
1313
"lint": "eslint . && prettier . --check --ignore-path ../../../.prettierignore",
1414
"preview": "vite preview",
15-
"generate": "cargo build -p spacetimedb-standalone && cargo run -p spacetimedb-cli generate --lang typescript --out-dir src/module_bindings --project-path server && prettier --write src/module_bindings && find src/module_bindings -type f -exec perl -pi -e 's#spacetimedb#../../../src/index#g' {} + && prettier --write src/module_bindings",
15+
"generate": "cargo build -p spacetimedb-standalone && cargo run -p spacetimedb-cli generate --lang typescript --out-dir src/module_bindings --project-path server && prettier --write src/module_bindings && node ./replace-spacetimedb.js && prettier --write src/module_bindings",
1616
"spacetime:generate": "spacetime generate --lang typescript --out-dir src/module_bindings --project-path server",
1717
"spacetime:start": "spacetime start server",
1818
"spacetime:publish:local": "spacetime publish game --project-path server --server local",
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Cross-platform replacement script
5+
* Equivalent to:
6+
* find src/module_bindings -type f -exec perl -pi -e 's#spacetimedb#../../../src/index#g' {}
7+
*/
8+
9+
import fs from 'fs';
10+
import path from 'path';
11+
12+
const ROOT = path.resolve('src/module_bindings');
13+
const SEARCH = /spacetimedb/g;
14+
const REPLACEMENT = '../../../src/index';
15+
16+
function replaceInFile(filePath) {
17+
try {
18+
let content = fs.readFileSync(filePath, 'utf8');
19+
if (SEARCH.test(content)) {
20+
const updated = content.replace(SEARCH, REPLACEMENT);
21+
fs.writeFileSync(filePath, updated, 'utf8');
22+
console.log(`✔ Updated: ${filePath}`);
23+
}
24+
} catch (err) {
25+
console.error(`✖ Error processing ${filePath}:`, err);
26+
}
27+
}
28+
29+
function walkDir(dir) {
30+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
31+
const fullPath = path.join(dir, entry.name);
32+
if (entry.isDirectory()) walkDir(fullPath);
33+
else if (entry.isFile()) replaceInFile(fullPath);
34+
}
35+
}
36+
37+
walkDir(ROOT);
38+
console.log('✅ Replacement complete.');
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# @clockworklabs/test-app
2+
3+
## 0.0.1
4+
5+
### Patch Changes
6+
7+
- Updated dependencies [[`cf7b7d8`](https://github.com/clockworklabs/spacetimedb-typescript-sdk/commit/cf7b7d89a1547fb3863f6641f5b2eb40a27c05d8), [`941cf4e`](https://github.com/clockworklabs/spacetimedb-typescript-sdk/commit/941cf4eba6b7df934d74696b373b89cc62764673), [`a501f5c`](https://github.com/clockworklabs/spacetimedb-typescript-sdk/commit/a501f5ccf9a0a926eb4f345ddeb01ffcb872d67e), [`9032269`](https://github.com/clockworklabs/spacetimedb-typescript-sdk/commit/9032269004d4dae587c39ccd85da0a32fb9a0114), [`6547882`](https://github.com/clockworklabs/spacetimedb-typescript-sdk/commit/6547882bb28ed9a1ca436335745e9997328026ff), [`5d7304b`](https://github.com/clockworklabs/spacetimedb-typescript-sdk/commit/5d7304bd3e05dd7a032cfb7069aab97b881f0179)]:
8+
- @clockworklabs/spacetimedb-sdk@1.2.0
9+
10+
## 0.0.3-rc1.0
11+
12+
### Patch Changes
13+
14+
- Updated dependencies [[`cf7b7d8`](https://github.com/clockworklabs/spacetimedb-typescript-sdk/commit/cf7b7d89a1547fb3863f6641f5b2eb40a27c05d8), [`a501f5c`](https://github.com/clockworklabs/spacetimedb-typescript-sdk/commit/a501f5ccf9a0a926eb4f345ddeb01ffcb872d67e), [`9032269`](https://github.com/clockworklabs/spacetimedb-typescript-sdk/commit/9032269004d4dae587c39ccd85da0a32fb9a0114), [`6547882`](https://github.com/clockworklabs/spacetimedb-typescript-sdk/commit/6547882bb28ed9a1ca436335745e9997328026ff), [`5d7304b`](https://github.com/clockworklabs/spacetimedb-typescript-sdk/commit/5d7304bd3e05dd7a032cfb7069aab97b881f0179)]:
15+
- @clockworklabs/spacetimedb-sdk@1.0.0-rc1.0
16+
17+
## 0.0.2
18+
19+
### Patch Changes
20+
21+
- Updated dependencies [[`2f6c82c`](https://github.com/clockworklabs/spacetimedb-typescript-sdk/commit/2f6c82c724b9f9407c7bedee13252ca8ffab8f7d), [`b9db9b6`](https://github.com/clockworklabs/spacetimedb-typescript-sdk/commit/b9db9b6e46d8c98b29327d97c12c07b7a2fc96bf), [`79c278b`](https://github.com/clockworklabs/spacetimedb-typescript-sdk/commit/79c278be71b2dfd82106ada983fd81d395b1d912)]:
22+
- @clockworklabs/spacetimedb-sdk@0.12.1
23+
24+
## 0.0.1
25+
26+
### Patch Changes
27+
28+
- Updated dependencies [[`5adb557`](https://github.com/clockworklabs/spacetimedb-typescript-sdk/commit/5adb55776c81d0760cf0268df0fa5dee600f0ef8), [`ab1f463`](https://github.com/clockworklabs/spacetimedb-typescript-sdk/commit/ab1f463d7da6e530a6cd47e2433141bfd16addd1), [`b8c944c`](https://github.com/clockworklabs/spacetimedb-typescript-sdk/commit/b8c944cd23d3b53c72131803a775127bf0a95213), [`17227c0`](https://github.com/clockworklabs/spacetimedb-typescript-sdk/commit/17227c0f65def3a9d5e767756ccf46777210041a)]:
29+
- @clockworklabs/spacetimedb-sdk@0.12.0
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# React + TypeScript + Vite
2+
3+
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
4+
5+
Currently, two official plugins are available:
6+
7+
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
8+
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
9+
10+
## Expanding the ESLint configuration
11+
12+
If you are developing a production application, we recommend updating the configuration to enable type aware lint rules:
13+
14+
- Configure the top-level `parserOptions` property like this:
15+
16+
```js
17+
export default {
18+
// other rules...
19+
parserOptions: {
20+
ecmaVersion: 'latest',
21+
sourceType: 'module',
22+
project: ['./tsconfig.json', './tsconfig.node.json', './tsconfig.app.json'],
23+
tsconfigRootDir: __dirname,
24+
},
25+
};
26+
```
27+
28+
- Replace `plugin:@typescript-eslint/recommended` to `plugin:@typescript-eslint/recommended-type-checked` or `plugin:@typescript-eslint/strict-type-checked`
29+
- Optionally add `plugin:@typescript-eslint/stylistic-type-checked`
30+
- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and add `plugin:react/recommended` & `plugin:react/jsx-runtime` to the `extends` list
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Vite + React + TS</title>
8+
</head>
9+
<body>
10+
<div id="root"></div>
11+
<script type="module" src="/src/main.tsx"></script>
12+
</body>
13+
</html>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "@clockworklabs/test-app",
3+
"private": true,
4+
"version": "0.0.1",
5+
"type": "module",
6+
"files": [
7+
"src"
8+
],
9+
"scripts": {
10+
"dev": "vite",
11+
"build": "tsc -b && vite build",
12+
"format": "prettier . --write --ignore-path ../../../.prettierignore",
13+
"lint": "eslint . && prettier . --check --ignore-path ../../../.prettierignore",
14+
"preview": "vite preview",
15+
"generate": "cargo build -p spacetimedb-standalone && cargo run -p spacetimedb-cli generate --lang typescript --out-dir src/module_bindings --project-path server && prettier --write src/module_bindings && node ./replace-spacetimedb.js && prettier --write src/module_bindings",
16+
"spacetime:generate": "spacetime generate --lang typescript --out-dir src/module_bindings --project-path server",
17+
"spacetime:start": "spacetime start server",
18+
"spacetime:publish:local": "spacetime publish game --project-path server --server local",
19+
"spacetime:publish": "spacetime publish game --project-path server --server testnet"
20+
},
21+
"dependencies": {
22+
"react": "^18.3.1",
23+
"react-dom": "^18.3.1",
24+
"react-router-dom": "^7.9.4"
25+
},
26+
"devDependencies": {
27+
"@types/react": "^18.3.3",
28+
"@types/react-dom": "^18.3.0",
29+
"@vitejs/plugin-react": "^4.3.1",
30+
"typescript": "^5.2.2",
31+
"vite": "^7.1.5"
32+
}
33+
}

0 commit comments

Comments
 (0)