Skip to content

cli: add presets option and react preset for typescript #2780

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Trobyss
Copy link
Contributor

@Trobyss Trobyss commented May 22, 2025

Description of Changes

Implements --preset option in spacetime generate cli to provide a way to generate react code under typescript module binding.

Exemple:

spacetime generate --lang typescript --out-dir module_bindings --project-path ./quickstart-chat/server  --preset react

For the moment it's a draft because I'd like to get your feedback first:

  • Is this a feature you're interested in?
  • How hard is it to keep this up to date?

This is my very first Rust code, so I look forward to your feedback 😇

Implementation Details

  • Added a new Preset enum in the CLI to support React preset generation
  • Implemented React struct in presets/react/mod.rs that implements the LangPreset trait
  • The React preset generates:
    • Context provider and hooks for SpacetimeDB connection
    • Custom hooks for each table in the module
    • Type-safe store hooks using React's useSyncExternalStore
    • Proper TypeScript types and interfaces

Exenple of generated code:

module_bindings/
└── react/
    ├── context.tsx
    ├── index.ts
    ├── useSpacetimeContext.ts
    ├── useSpacetimeXXXStore.tsx (<-- one for each table)
└── index.ts
 ... other generated typescript files

You can find more detailed exemple here: crates/codegen/src/presets/react/doc.md.

For now, there aren't many hooks available, but I'm planning to add some to handle the calls from the reducers in the next few days.

Design issue

Actually, REACT_SPACETIME_CONTEXT_PROVIDER doesn't need to be generated and could be a part of with a react-sdk in spacetimedb-typescript-sdk.

The hooks need to be wrap in a React Context (SpacetimeProvider) to be able to use the useSpacetimeContext hooks and use the connected client (to avoid passing Spacetime client each time in props). I don't know if this is a good thing or a bad thing.

Testing

  • Snapshot test
  • Manual testing of generated React components

Todo list

  • Move REACT_SPACETIME_CONTEXT_PROVIDER to a separate react-sdk package inside spacetimedb-typescript-sdk
  • Add reducer call methods/hooks
  • Test on larger codebase

Future Improvements

  • Add more presets for other frameworks (Vue, Svelte, etc.)
  • Improve type safety and error handling in generated hooks

@cloutiertyler
Copy link
Contributor

Ooh this is quite interesting. It might take us a bit to get to this, but we're definitely keen to make our React story better. Not sure if it should be a code gen solution or not. Thanks for raising this!

@Trobyss
Copy link
Contributor Author

Trobyss commented May 23, 2025

Hey @cloutiertyler!

Glad to hear your comments!
Yes, implementing a feature like this means a lot for future maintenance....
Should I keep implementing React stuff or should I just leave this PR for now?

Anyway, think I'll continue to at least have an 'mvp prod ready', maybe it can give other React developers a good start :)

Trob'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants