-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f71833
commit 2f78852
Showing
20 changed files
with
1,209 additions
and
264 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
"@dojoengine/sdk": minor | ||
"template-vite-ts": minor | ||
"@dojoengine/core": minor | ||
"@dojoengine/create-burner": minor | ||
"@dojoengine/create-dojo": minor | ||
"@dojoengine/predeployed-connector": minor | ||
"@dojoengine/react": minor | ||
"@dojoengine/state": minor | ||
"@dojoengine/torii-client": minor | ||
"@dojoengine/torii-wasm": minor | ||
"@dojoengine/utils": minor | ||
"@dojoengine/utils-wasm": minor | ||
--- | ||
|
||
feat: add reusable sdk react hooks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { KeysClause, ToriiQueryBuilder } from "@dojoengine/sdk"; | ||
import { useEntityId, useEventQuery, useModel } from "@dojoengine/sdk/react"; | ||
import { useAccount } from "@starknet-react/core"; | ||
import { addAddressPadding } from "starknet"; | ||
import { ModelsMapping } from "./typescript/models.gen"; | ||
|
||
export function Events() { | ||
const { account } = useAccount(); | ||
const entityId = useEntityId(account?.address ?? "0"); | ||
useEventQuery( | ||
new ToriiQueryBuilder() | ||
.withClause( | ||
KeysClause( | ||
[], | ||
[addAddressPadding(account?.address ?? "0")], | ||
"VariableLen" | ||
).build() | ||
) | ||
.includeHashedKeys() | ||
); | ||
const moved = useModel(entityId, ModelsMapping.Moved); | ||
if (!account) { | ||
return ( | ||
<div className="mt-6"> | ||
<h2 className="text-white">Please connect your wallet</h2> | ||
</div> | ||
); | ||
} | ||
return ( | ||
<div className="mt-6"> | ||
<h2 className="text-white"> | ||
Player Last Movement : {moved && moved.direction}{" "} | ||
</h2> | ||
|
||
{/* {events.map((e: ParsedEntity<SchemaType>, key) => { | ||
return <Event event={e} key={key} />; | ||
})} */} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"root":["./src/app.tsx","./src/historical-events.tsx","./src/main.tsx","./src/starknet-provider.tsx","./src/usesystemcalls.ts","./src/vite-env.d.ts","./src/wallet-account.tsx","./src/typescript/contracts.gen.ts","./src/typescript/models.gen.ts"],"version":"5.7.3"} | ||
{"root":["./src/app.tsx","./src/events.tsx","./src/historical-events.tsx","./src/main.tsx","./src/starknet-provider.tsx","./src/usesystemcalls.ts","./src/vite-env.d.ts","./src/wallet-account.tsx","./src/typescript/contracts.gen.ts","./src/typescript/models.gen.ts"],"version":"5.7.3"} |
72 changes: 15 additions & 57 deletions
72
examples/example-vite-react-sql/src/hooks/usePlayerActions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,20 @@ | ||
import { useEffect, useMemo } from "react"; | ||
|
||
import { KeysClause, ParsedEntity, ToriiQueryBuilder } from "@dojoengine/sdk"; | ||
import { useDojoSDK } from "@dojoengine/sdk/react"; | ||
import { getEntityIdFromKeys } from "@dojoengine/utils"; | ||
import { ModelsMapping, SchemaType } from "@/typescript/models.gen"; | ||
import { KeysClause, ToriiQueryBuilder } from "@dojoengine/sdk"; | ||
import { useEntityId, useEntityQuery } from "@dojoengine/sdk/react"; | ||
import { ModelsMapping, type SchemaType } from "@/typescript/models.gen"; | ||
import { addAddressPadding } from "starknet"; | ||
|
||
export function usePlayerActions(address: string | undefined) { | ||
const { sdk, useDojoStore } = useDojoSDK(); | ||
const state = useDojoStore((state) => state); | ||
|
||
const entityId = useMemo(() => { | ||
if (address) { | ||
return getEntityIdFromKeys([BigInt(address)]); | ||
} | ||
return BigInt(0); | ||
}, [address]); | ||
|
||
useEffect(() => { | ||
let unsubscribe: (() => void) | undefined; | ||
|
||
const subscribe = async (address: string) => { | ||
const [entities, subscription] = await sdk.subscribeEntityQuery({ | ||
query: new ToriiQueryBuilder() | ||
.withClause( | ||
KeysClause( | ||
[ModelsMapping.Moves, ModelsMapping.Position], | ||
[addAddressPadding(address)], | ||
"VariableLen" | ||
).build() | ||
) | ||
.includeHashedKeys(), | ||
callback: ({ error, data }) => { | ||
if (error) { | ||
console.error("Error setting up entity sync:", error); | ||
} else if ( | ||
data && | ||
(data[0] as ParsedEntity<SchemaType>).entityId !== "0x0" | ||
) { | ||
state.updateEntity(data[0] as ParsedEntity<SchemaType>); | ||
} | ||
}, | ||
}); | ||
state.setEntities(entities); | ||
|
||
unsubscribe = () => subscription.cancel(); | ||
}; | ||
|
||
if (address) { | ||
subscribe(address); | ||
} | ||
|
||
return () => { | ||
if (unsubscribe) { | ||
unsubscribe(); | ||
} | ||
}; | ||
}, [sdk, address]); | ||
|
||
const entityId = useEntityId(address ?? "0"); | ||
useEntityQuery<SchemaType>( | ||
new ToriiQueryBuilder() | ||
.withClause( | ||
KeysClause( | ||
[ModelsMapping.Moves, ModelsMapping.Position], | ||
[addAddressPadding(address ?? "0")], | ||
"VariableLen" | ||
).build() | ||
) | ||
.includeHashedKeys() | ||
); | ||
return entityId; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.