Skip to content

Commit 0e64e57

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

File tree

14 files changed

+342
-55
lines changed

14 files changed

+342
-55
lines changed

.changeset/brave-kiwis-explode.md

+15
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

+5-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ function App() {
3535
// Querying Moves and Position models that has at least [account.address] as key
3636
KeysClause(
3737
[ModelsMapping.Moves, ModelsMapping.Position],
38-
[addAddressPadding(account?.address ?? "0")],
38+
[
39+
account?.address
40+
? addAddressPadding(account.address)
41+
: undefined,
42+
],
3943
"FixedLen"
4044
).build()
4145
)

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

-2
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

+1-1
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/experimental/index.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@ import { SchemaType, SDKConfig, StandardizedQueryResult } from "../types";
33
import { parseEntities } from "../parseEntities";
44
import { parseHistoricalEvents } from "../parseHistoricalEvents";
55
import { intoEntityKeysClause } from "../convertClauseToEntityKeysClause";
6+
import { defaultClientConfig } from "..";
67

78
export type ToriiSubscriptionCallback<T extends SchemaType> = (response: {
89
data?: StandardizedQueryResult<T> | StandardizedQueryResult<T>[];
910
error?: Error;
1011
}) => void;
1112

1213
export async function init<T extends SchemaType>(options: SDKConfig) {
13-
const client = await torii.createClient(options.client);
14+
const clientConfig = {
15+
...defaultClientConfig,
16+
...options.client,
17+
} as torii.ClientConfig;
18+
19+
const client = await torii.createClient(clientConfig);
1420

1521
return {
1622
getEntities: async (query: torii.Query) => {

packages/sdk/src/index.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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,
@@ -14,7 +14,7 @@ import {
1414
import { intoEntityKeysClause } from "./convertClauseToEntityKeysClause";
1515
import { parseEntities } from "./parseEntities";
1616
import { parseHistoricalEvents } from "./parseHistoricalEvents";
17-
import { ToriiQueryBuilder } from "./toriiQueryBuilder";
17+
import type { ToriiQueryBuilder } from "./toriiQueryBuilder";
1818
import { generateTypedData } from "./generateTypedData";
1919

2020
export * from "./types";
@@ -34,6 +34,11 @@ export async function createClient(
3434
return await torii.createClient(config);
3535
}
3636

37+
export const defaultClientConfig: Partial<torii.ClientConfig> = {
38+
toriiUrl: "http://localhost:8080",
39+
relayUrl: "/ip4/127.0.0.1/tcp/9090",
40+
};
41+
3742
/**
3843
* Initializes the SDK with the provided configuration and schema.
3944
*
@@ -43,7 +48,11 @@ export async function createClient(
4348
export async function init<T extends SchemaType>(
4449
options: SDKConfig
4550
): Promise<SDK<T>> {
46-
const client = await createClient(options.client);
51+
const clientConfig = {
52+
...defaultClientConfig,
53+
...options.client,
54+
} as torii.ClientConfig;
55+
const client = await createClient(clientConfig);
4756

4857
return {
4958
client,
@@ -137,6 +146,7 @@ export async function init<T extends SchemaType>(
137146
const parsedData = historical
138147
? parseHistoricalEvents<T>(data)
139148
: parseEntities<T>(data);
149+
140150
callback({
141151
data: parsedData as ToriiResponse<
142152
T,

packages/sdk/src/parseEntities.ts

+2-1
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

+26
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)