Skip to content
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

Add requiredFilters param to useScaffoldEventHistory hook #621

Merged
merged 4 commits into from
Nov 26, 2023

Conversation

damianmarti
Copy link
Collaborator

Description

Add requiredFilters param to useScaffoldEventHistory hook to avoid getting wrong events when setting a filter that is not yet populated with the right value.

If one of the requiredFilters is not set, the hook returns an error.

For example, if I get the filter value from the router query, the first value is undefined, and the hook will return all the events, without this filter applied.

To fix it, I can use a useEffect hook, something like this:

const { roomHash } = router.query as { roomHash: 0x${string} };

const [roomHashFilter, setRoomHashFilter] = useState<string>("aa");

  useEffect(() => {
    if (roomHash) {
      setRoomHashFilter(roomHash);
    }
  }, [roomHash]);

  const {
    data: roomJoinEvents,
    isLoading: isLoadingRoomCreateEvents,
    error: errorReadingRoomCreateEvents,
  } = useScaffoldEventHistory({
    contractName: "BettingRoom",
    eventName: "RoomJoin",
    fromBlock: 1n,
    filters: { roomHash: roomHashFilter },
    watch: true,
  });

But if I use the requieredFilters params I only need the useScaffoldEventHistory hook:

const { roomHash } = router.query as { roomHash: 0x${string} };

  const {
    data: roomJoinEvents,
    isLoading: isLoadingRoomCreateEvents,
    error: errorReadingRoomCreateEvents,
  } = useScaffoldEventHistory({
    contractName: "BettingRoom",
    eventName: "RoomJoin",
    fromBlock: 1n,
    filters: { roomHash: roomHash },
    watch: true,
    requiredFilters: ["roomHash"],
  });

Or maybe there is another better option that I'm missing :-)

Additional Information

Your ENS/address: damianmarti.eth

@rin-st
Copy link
Member

rin-st commented Nov 22, 2023

what about adding enabled flag like for example here (default true)? So if that flag is false then return undefined or empty array.

why it can be undefined btw?

so your hook will look like

const { roomHash } = router.query as { roomHash: 0x${string} };

  const {
    data: roomJoinEvents,
    isLoading: isLoadingRoomCreateEvents,
    error: errorReadingRoomCreateEvents,
  } = useScaffoldEventHistory({
    contractName: "BettingRoom",
    eventName: "RoomJoin",
    fromBlock: 1n,
    filters: { roomHash: roomHash },
    watch: true,
    enabled: Boolean(roomHash)
  });

@damianmarti
Copy link
Collaborator Author

router.query

Thanks for the feedback!

I think this is a good option too! Not sure what is better ;-)

roomHash is undefined before it's hydrated with the router query data.

@sverps
Copy link
Collaborator

sverps commented Nov 23, 2023

I like the enabled flag, because it follows the way that wagmi hooks do it, so should be somewhat familiar to devs. It also allows for more flexibility in the condition to trigger the hook.

@damianmarti
Copy link
Collaborator Author

@rin-st @sverps Changed to use enabled flag!

@damianmarti damianmarti merged commit c3344b3 into main Nov 26, 2023
1 check passed
@damianmarti damianmarti deleted the event-history-hook-required-filters branch November 26, 2023 21:59
@carletex
Copy link
Member

Thanks @damianmarti for making the hooks better.

And thanks all for the review!!!

@github-actions github-actions bot mentioned this pull request Dec 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants