Skip to content

Commit e71507e

Browse files
committed
feat: add useModels hook
1 parent 80bee03 commit e71507e

File tree

11 files changed

+336
-52
lines changed

11 files changed

+336
-52
lines changed

.changeset/brave-kiwis-explode.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
"@dojoengine/utils": patch
3+
"@dojoengine/sdk": patch
4+
"@dojoengine/core": patch
5+
"@dojoengine/create-burner": patch
6+
"@dojoengine/create-dojo": patch
7+
"@dojoengine/predeployed-connector": patch
8+
"@dojoengine/react": patch
9+
"@dojoengine/state": patch
10+
"@dojoengine/torii-client": patch
11+
"@dojoengine/torii-wasm": patch
12+
"@dojoengine/utils-wasm": patch
13+
---
14+
15+
fix: cairo option and enum ignore in zustand merge

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
useEntityId,
1111
useEntityQuery,
1212
useModel,
13+
useModels,
1314
} from "@dojoengine/sdk/react";
1415
import { addAddressPadding, CairoCustomEnum } from "starknet";
1516
import { Events } from "./events.tsx";
@@ -35,7 +36,11 @@ function App() {
3536
// Querying Moves and Position models that has at least [account.address] as key
3637
KeysClause(
3738
[ModelsMapping.Moves, ModelsMapping.Position],
38-
[addAddressPadding(account?.address ?? "0")],
39+
[
40+
account?.address
41+
? addAddressPadding(account.address)
42+
: undefined,
43+
],
3944
"FixedLen"
4045
).build()
4146
)

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import StarknetProvider from "./starknet-provider.tsx";
2222
async function main() {
2323
const sdk = await init<SchemaType>({
2424
client: {
25-
toriiUrl: dojoConfig.toriiUrl,
26-
relayUrl: dojoConfig.relayUrl,
2725
worldAddress: dojoConfig.manifest.world.address,
2826
},
2927
domain: {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"format:check": "turbo format:check",
1212
"release": "pnpm build && pnpm -F './packages/**' publish -r --force --no-git-checks",
1313
"release:dry-run": "pnpm -F './packages/**' publish -r --force --dry-run",
14-
"dev": "turbo dev --concurrency 15"
14+
"dev": "turbo watch dev --concurrency 15"
1515
},
1616
"devDependencies": {
1717
"@commitlint/cli": "^18.6.1",

packages/sdk/src/index.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import * as torii from "@dojoengine/torii-client";
2-
import { Account, Signature, StarknetDomain, TypedData } from "starknet";
2+
import type { Account, Signature, StarknetDomain, TypedData } from "starknet";
33

4-
import {
4+
import type {
55
GetParams,
66
SchemaType,
77
SDK,
8+
SDKClientConfig,
89
SDKConfig,
910
SubscribeParams,
1011
SubscribeResponse,
@@ -14,7 +15,7 @@ import {
1415
import { intoEntityKeysClause } from "./convertClauseToEntityKeysClause";
1516
import { parseEntities } from "./parseEntities";
1617
import { parseHistoricalEvents } from "./parseHistoricalEvents";
17-
import { ToriiQueryBuilder } from "./toriiQueryBuilder";
18+
import type { ToriiQueryBuilder } from "./toriiQueryBuilder";
1819
import { generateTypedData } from "./generateTypedData";
1920

2021
export * from "./types";
@@ -34,6 +35,11 @@ export async function createClient(
3435
return await torii.createClient(config);
3536
}
3637

38+
const defaultClientConfig: SDKClientConfig = {
39+
toriiUrl: "http://localhost:8080",
40+
relayUrl: "/ip4/127.0.0.1/tcp/9090",
41+
};
42+
3743
/**
3844
* Initializes the SDK with the provided configuration and schema.
3945
*
@@ -43,7 +49,11 @@ export async function createClient(
4349
export async function init<T extends SchemaType>(
4450
options: SDKConfig
4551
): Promise<SDK<T>> {
46-
const client = await createClient(options.client);
52+
const clientConfig = {
53+
...defaultClientConfig,
54+
...options.client,
55+
} as torii.ClientConfig;
56+
const client = await createClient(clientConfig);
4757

4858
return {
4959
client,
@@ -137,6 +147,7 @@ export async function init<T extends SchemaType>(
137147
const parsedData = historical
138148
? parseHistoricalEvents<T>(data)
139149
: parseEntities<T>(data);
150+
140151
callback({
141152
data: parsedData as ToriiResponse<
142153
T,

packages/sdk/src/parseEntities.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ function parseValue(value: torii.Ty): any {
7676
CairoOptionVariant.Some,
7777
parseValue((value.value as torii.EnumValue).value)
7878
);
79-
} else if ("None" === (value.value as torii.EnumValue).option) {
79+
}
80+
if ("None" === (value.value as torii.EnumValue).option) {
8081
return new CairoOption(CairoOptionVariant.None);
8182
}
8283

packages/sdk/src/react/hooks.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,31 @@ export function useModel<
6363
return modelData;
6464
}
6565

66+
/**
67+
* Custom hook to retrieve all entities that have a specific model.
68+
*
69+
* @param model - The model to retrieve, specified as a string in the format "namespace-modelName".
70+
* @returns The model structure if found, otherwise undefined.
71+
*/
72+
export function useModels<
73+
N extends keyof SchemaType,
74+
M extends keyof SchemaType[N] & string,
75+
Client extends (...args: any) => any,
76+
Schema extends SchemaType
77+
>(model: `${N}-${M}`): { [entityId: string]: SchemaType[N][M] | undefined } {
78+
const [namespace, modelName] = model.split("-") as [N, M];
79+
const { useDojoStore } =
80+
useContext<DojoContextType<Client, Schema>>(DojoContext);
81+
82+
const modelData = useDojoStore((state) =>
83+
state.getEntitiesByModel(namespace, modelName).map((entity) => ({
84+
[entity.entityId]: entity.models?.[namespace]?.[modelName],
85+
}))
86+
) as unknown as { [entityId: string]: SchemaType[N][M] | undefined };
87+
88+
return modelData;
89+
}
90+
6691
/**
6792
* Hook that exposes sdk features.
6893
*
@@ -223,6 +248,7 @@ export function useEntityQuery<Schema extends SchemaType>(
223248
processInitialData: (data) => state.mergeEntities(data),
224249
processUpdateData: (data) => {
225250
const entity = data.pop();
251+
226252
if (entity && entity.entityId !== "0x0") {
227253
state.updateEntity(entity);
228254
}

0 commit comments

Comments
 (0)