nimeshnayaju / y-presence

Simple react hooks to manage multiplayer presence using Yjs
MIT License
177 stars 4 forks source link

Feature request: filter current user #6

Closed seleckis closed 11 months ago

seleckis commented 11 months ago

Thanks for the library, it is great. Also would be nice to have an ability to return only other users from useUsers. Maybe useUsers should have optional options argument for selector function and equality function, where in addition excludeSelf could be presented. What do you think?

Example:

const otherUsers = useUsers(awareness, {
  selector: (state) => state,
  excludeSelf: true,
});
nimeshnayaju commented 11 months ago

Thank you! I like the idea of being able to retrieve other users (users except self) though I'd like to spend some time thinking about the best API for the purpose. I quite like the addition of a new argument that you suggested. I have also thought of exporting a new hook called useOthers, but the implementation would be quite simple and would be some version of the following:

const others = useUsers(awareness, (users) => Array.from(users.entries()).filter(([id]) => id !== awareness.clientID))

What are your thoughts?

seleckis commented 11 months ago

Thanks for the quick answer. I like it more than my previous suggestion, because then I thought that I need to filter awareness also by page. In my case I use Next.js where I can take pathname and add to awareness, to say this user is on this page. So I do:

  const pathname = usePathname();
  useEffect(() => {
    setAwareness("pathname", pathname);
  }, [key, value]);

and then in certain component:

const pathname = usePathname();
const others = useUsers(awareness, (users) => 
    Array.from(users).filter(([id, value]) => id !== awareness.clientID && value.pathname === pathname)
  );

In this case I get other users than me on the current page. Well, now I don't know if it is necessary to add this useOthers hook in your lib. Perhaps this can be left as a workaround.