-
Notifications
You must be signed in to change notification settings - Fork 53
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
feat: sdk reusable query hooks #402
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
❓ Verification inconclusive
Avoid using @ts-ignore without explanation
Using
@ts-ignore
suppresses TypeScript errors without addressing the underlying issue. This can lead to unexpected runtime errors and makes it harder to maintain the codebase in the long term.Consider either:
Alternatively, investigate and fix the actual type issue:
🏁 Script executed:
Length of output: 214
Below is a revised review comment. Note that the previous command encountered an error with the file type filter; please re-run the search manually (or use the snippet below) to confirm that no additional instances require fixing. For example:
Action Required: Annotate the Suppressed TypeScript Error in __root.tsx
File & Location:
examples/example-vite-react-sql/src/routes/__root.tsx
at line 24Current Code:
Recommended Revision:
Next Steps:
Either document why the suppression is needed (for example, a known issue with
Outlet
types in the current version of@tanstack/react-router
) or resolve the underlying typing conflict.Please manually verify there are no other occurrences of unexplained
@ts-ignore
comments that might hide type errors.