actionNavigate.tsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import React from "react";
  2. import { Avatar } from "../components/Avatar";
  3. import { register } from "./register";
  4. import { getClientColors, getClientInitials } from "../clients";
  5. import { Collaborator } from "../types";
  6. import { normalizeScroll } from "../scene";
  7. export const actionGoToCollaborator = register({
  8. name: "goToCollaborator",
  9. perform: (_elements, appState, value) => {
  10. const point = value as Collaborator["pointer"];
  11. if (!point) {
  12. return { appState, commitToHistory: false };
  13. }
  14. return {
  15. appState: {
  16. ...appState,
  17. scrollX: normalizeScroll(appState.width / 2 - point.x),
  18. scrollY: normalizeScroll(appState.height / 2 - point.y),
  19. // Close mobile menu
  20. openMenu: appState.openMenu === "canvas" ? null : appState.openMenu,
  21. },
  22. commitToHistory: false,
  23. };
  24. },
  25. PanelComponent: ({ appState, updateData, id }) => {
  26. const clientId = id;
  27. if (!clientId) {
  28. return null;
  29. }
  30. const collaborator = appState.collaborators.get(clientId);
  31. if (!collaborator) {
  32. return null;
  33. }
  34. const { background } = getClientColors(clientId);
  35. const shortName = getClientInitials(collaborator.username);
  36. return (
  37. <Avatar
  38. color={background}
  39. onClick={() => updateData(collaborator.pointer)}
  40. >
  41. {shortName}
  42. </Avatar>
  43. );
  44. },
  45. });