Skip to content

Commit 914db3d

Browse files
Fix issue #315: add comments in example-vite-react-sdk
1 parent 9aa8f33 commit 914db3d

File tree

7 files changed

+94
-4
lines changed

7 files changed

+94
-4
lines changed

examples/example-vite-react-sdk/src/App.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,17 @@ import { useDojo } from "./useDojo.tsx";
88
import useModel from "./useModel.tsx";
99
import { useSystemCalls } from "./useSystemCalls.ts";
1010

11+
/**
12+
* Global store for managing Dojo game state.
13+
*/
1114
export const useDojoStore = createDojoStore<Schema>();
1215

16+
/**
17+
* Main application component that provides game functionality and UI.
18+
* Handles entity subscriptions, state management, and user interactions.
19+
*
20+
* @param props.sdk - The Dojo SDK instance configured with the game schema
21+
*/
1322
function App({ sdk }: { sdk: SDK<Schema> }) {
1423
const {
1524
account,

examples/example-vite-react-sdk/src/DojoContext.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,30 @@ import { dojoConfig } from "../dojoConfig";
99
import { DojoProvider } from "@dojoengine/core";
1010
import { client } from "./contracts.gen";
1111

12+
/**
13+
* Interface defining the shape of the Dojo context.
14+
*/
1215
interface DojoContextType {
16+
/** The master account used for administrative operations */
1317
masterAccount: Account;
18+
/** The Dojo client instance */
1419
client: ReturnType<typeof client>;
20+
/** The current burner account information */
1521
account: BurnerAccount;
1622
}
1723

24+
/**
25+
* React context for sharing Dojo-related data throughout the application.
26+
*/
1827
export const DojoContext = createContext<DojoContextType | null>(null);
1928

29+
/**
30+
* Provider component that makes Dojo context available to child components.
31+
*
32+
* @param props.children - Child components that will have access to the Dojo context
33+
* @param props.burnerManager - Instance of BurnerManager for handling burner accounts
34+
* @throws {Error} If DojoProvider is used more than once in the component tree
35+
*/
2036
export const DojoContextProvider = ({
2137
children,
2238
burnerManager,

examples/example-vite-react-sdk/src/bindings.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,46 @@
1+
/**
2+
* Interface representing a player's movement capabilities and state.
3+
*/
14
interface Moves {
5+
/** Order of fields in the model */
26
fieldOrder: string[];
7+
/** Player identifier */
38
player: string;
9+
/** Number of moves remaining */
410
remaining: number;
11+
/** Last direction moved */
512
last_direction: Direction;
13+
/** Whether the player can currently move */
614
can_move: boolean;
715
}
816

17+
/**
18+
* Interface representing available movement directions for a player.
19+
*/
920
interface DirectionsAvailable {
21+
/** Order of fields in the model */
1022
fieldOrder: string[];
23+
/** Player identifier */
1124
player: string;
25+
/** List of available directions */
1226
directions: Direction[];
1327
}
1428

29+
/**
30+
* Interface representing a player's position in the game world.
31+
*/
1532
interface Position {
33+
/** Order of fields in the model */
1634
fieldOrder: string[];
35+
/** Player identifier */
1736
player: string;
37+
/** 2D vector representing position */
1838
vec: Vec2;
1939
}
2040

41+
/**
42+
* Enum representing possible movement directions.
43+
*/
2144
enum Direction {
2245
None = "0",
2346
Left = "1",
@@ -26,11 +49,19 @@ enum Direction {
2649
Down = "4",
2750
}
2851

52+
/**
53+
* Interface representing a 2D vector.
54+
*/
2955
interface Vec2 {
56+
/** X coordinate */
3057
x: number;
58+
/** Y coordinate */
3159
y: number;
3260
}
3361

62+
/**
63+
* Type representing the complete schema of game models.
64+
*/
3465
type Schema = {
3566
dojo_starter: {
3667
Moves: Moves;
@@ -39,6 +70,9 @@ type Schema = {
3970
};
4071
};
4172

73+
/**
74+
* Enum representing model identifiers in the format "namespace-modelName".
75+
*/
4276
enum Models {
4377
Moves = "dojo_starter-Moves",
4478
DirectionsAvailable = "dojo_starter-DirectionsAvailable",

examples/example-vite-react-sdk/src/main.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ import { dojoConfig } from "../dojoConfig.ts";
1010
import { DojoContextProvider } from "./DojoContext.tsx";
1111
import { setupBurnerManager } from "@dojoengine/create-burner";
1212

13+
/**
14+
* Initializes and bootstraps the Dojo application.
15+
* Sets up the SDK, burner manager, and renders the root component.
16+
*
17+
* @throws {Error} If initialization fails
18+
*/
1319
async function main() {
1420
const sdk = await init<Schema>(
1521
{

examples/example-vite-react-sdk/src/useDojo.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import { useContext } from "react";
22
import { DojoContext } from "./DojoContext";
33

4+
/**
5+
* Custom hook to access the Dojo context and account information.
6+
* Must be used within a DojoProvider component.
7+
*
8+
* @returns An object containing:
9+
* - setup: The Dojo setup configuration
10+
* - account: The current account information
11+
* @throws {Error} If used outside of a DojoProvider context
12+
*/
413
export const useDojo = () => {
514
const context = useContext(DojoContext);
615

examples/example-vite-react-sdk/src/useModel.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import { Schema } from "./bindings";
88
* @param model - The model to retrieve, specified as a string in the format "namespace-modelName".
99
* @returns The model structure if found, otherwise undefined.
1010
*/
11-
function useModel<N extends keyof Schema, M extends keyof Schema[N] & string>(
12-
entityId: string,
13-
model: `${N}-${M}`
14-
): Schema[N][M] | undefined {
11+
function useModel<
12+
N extends keyof Schema,
13+
M extends keyof Schema[N] & string
14+
>(entityId: string, model: `${N}-${M}`): Schema[N][M] | undefined {
1515
const [namespace, modelName] = model.split("-") as [N, M];
1616

1717
// Select only the specific model data for the given entityId

examples/example-vite-react-sdk/src/useSystemCalls.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ import { useDojoStore } from "./App";
33
import { useDojo } from "./useDojo";
44
import { v4 as uuidv4 } from "uuid";
55

6+
/**
7+
* Custom hook to handle system calls and state management in the Dojo application.
8+
* Provides functionality for spawning entities and managing optimistic updates.
9+
*
10+
* @returns An object containing system call functions:
11+
* - spawn: Function to spawn a new entity with initial moves
12+
*/
613
export const useSystemCalls = () => {
714
const state = useDojoStore((state) => state);
815

@@ -11,10 +18,19 @@ export const useSystemCalls = () => {
1118
account: { account },
1219
} = useDojo();
1320

21+
/**
22+
* Generates a unique entity ID based on the current account address.
23+
* @returns {string} The generated entity ID
24+
*/
1425
const generateEntityId = () => {
1526
return getEntityIdFromKeys([BigInt(account?.address)]);
1627
};
1728

29+
/**
30+
* Spawns a new entity with initial moves and handles optimistic updates.
31+
* @returns {Promise<void>}
32+
* @throws {Error} If the spawn action fails
33+
*/
1834
const spawn = async () => {
1935
// Generate a unique entity ID
2036
const entityId = generateEntityId();

0 commit comments

Comments
 (0)