Skip to content

Commit

Permalink
Use new @mirohq/websdk-react-hooks lib (#234)
Browse files Browse the repository at this point in the history
* Use new @mirohq/websdk-react-hooks lib

* Remove unused imports
  • Loading branch information
fredcido authored Dec 11, 2023
1 parent 8b47832 commit 02e9a81
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 82 deletions.
2 changes: 2 additions & 0 deletions examples/breakout-rooms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
},
"dependencies": {
"@mirohq/design-system": "^0.18.2",
"@mirohq/websdk-react-hooks": "^0.0.3",
"@mirohq/websdk-types": "2.11.0",
"@stitches/react": "^1.2.8",
"classnames": "^2.3.2",
"mirotone": "5",
Expand Down
6 changes: 5 additions & 1 deletion examples/breakout-rooms/src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"use client";

import * as React from "react";
import { MiroProvider } from "@mirohq/websdk-react-hooks";

import { useBreakout } from "./hooks";
import { BreakoutManager } from "./components/BreakoutManager";
import { WaitingRoom } from "./components/WaitingRoom/WaitingRoom";
Expand All @@ -14,7 +16,9 @@ const App: React.FC = () => {

return (
<ErrorBoundary>
{areYouReady ? <BreakoutManager /> : <WaitingRoom />}
<MiroProvider>
{areYouReady ? <BreakoutManager /> : <WaitingRoom />}
</MiroProvider>
</ErrorBoundary>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@ import * as React from "react";

import { Participant, Room } from "../../types";
import { Frame, Json, OnlineUserInfo } from "@mirohq/websdk-types";
import {
useBreakout,
useFeatureCheck,
useOnlineUsers,
useSelectedItems,
useTimer,
} from "../../hooks";
import { useSelectedItems, useOnlineUsers } from "@mirohq/websdk-react-hooks";
import { useBreakout, useFeatureCheck, useTimer } from "../../hooks";
import { formatDisplayTime, isUser } from "../../utils";
import { DEFAULT_TIME } from "../Timer/Timer";
import { Button } from "@mirohq/design-system";
Expand All @@ -29,22 +24,15 @@ export const BreakoutManager: React.FC = () => {
});
const [selectedRoom, setSelectedRoom] = React.useState<Room>();
const [duration, setTimerDuration] = React.useState<number>();
const [currentTime, setCurrentTime] = React.useState<number>(0);
const canUseTimer = useFeatureCheck("timer");

const onTimerStop = React.useCallback(() => {
service.endSession();
}, [breakout?.id]);

const onTick = React.useCallback(
(timestamp: number) => setCurrentTime(timestamp),
[],
);

const timer = useTimer({
duration: duration ?? DEFAULT_TIME,
onStop: onTimerStop,
onTick,
});

const participantIds = rooms
Expand All @@ -54,7 +42,11 @@ export const BreakoutManager: React.FC = () => {
.join("-");

const unassignedUsers = React.useMemo(() => {
return onlineUsers.filter((user) =>
if (onlineUsers.status !== "success") {
return [];
}

return onlineUsers.result.filter((user) =>
rooms.every((room) =>
room.participants.every((participant) => participant.id !== user.id),
),
Expand All @@ -63,11 +55,15 @@ export const BreakoutManager: React.FC = () => {

React.useEffect(() => {
const handleSelectionUpdate = async () => {
if (!selectedItems.length || !selectedRoom) {
if (
selectedItems.status !== "success" ||
!selectedItems.result.length ||
!selectedRoom
) {
return;
}

const [frame] = selectedItems;
const [frame] = selectedItems.result;

if (frame) {
await service.setRoomTarget(selectedRoom, frame.id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ export const RoomConfig: React.FunctionComponent<Props> = ({
</IconButton>
</DropdownMenu.Trigger>
<DropdownMenu.Content>
{unassignedUsers.length < 1 && room.participants.length < 1 && (
<DropdownMenu.Item disabled>
No more users left to assign
</DropdownMenu.Item>
)}
{unassignedUsers.length ? (
<div className="list">
<DropdownMenu.Item disabled>
Expand Down
68 changes: 12 additions & 56 deletions examples/breakout-rooms/src/hooks.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import * as React from "react";

import {
Item,
OnlineUserInfo,
SelectionUpdateEvent,
TimerEvent,
UserInfo,
Session,
Expand All @@ -14,7 +12,6 @@ import {
Breakout,
Participant,
Room,
SelectItemsOpts,
TimerOpts,
TimerState,
UserSessionEvent,
Expand Down Expand Up @@ -42,55 +39,6 @@ export const useCurrentUser = () => {
return userInfo;
};

export const useOnlineUsers = () => {
const [onlineUsers, setOnlineUsers] = React.useState<OnlineUserInfo[]>([]);

React.useEffect(() => {
const fetch = async () => {
const users = await miro.board.getOnlineUsers();
setOnlineUsers(users);
};

miro.board.ui.on("online_users:update", fetch);

fetch();

return () => {
miro.board.ui.off("online_users:update", fetch);
};
}, []);

return onlineUsers;
};

export const useSelectedItems = <T extends Item = Item>(
opts?: SelectItemsOpts,
) => {
const [items, setItems] = React.useState<T[]>([]);

React.useEffect(() => {
const subscribe = () => {
const handleSelectionUpdate = async (event: SelectionUpdateEvent) => {
let items = event.items as T[];
if (opts?.predicate) {
items = items.filter(opts.predicate);
}
setItems(items);
};

miro.board.ui.on("selection:update", handleSelectionUpdate);

return () => {
miro.board.ui.off("selection:update", handleSelectionUpdate);
};
};

return subscribe();
}, []);

return items;
};

export const useBreakout = () => {
const [breakout, setBreakout] = React.useState<Breakout>();
const currentUser = useCurrentUser();
Expand Down Expand Up @@ -322,9 +270,7 @@ export const useBreakout = () => {
return;
}

if (participant.id === currentUser?.id) {
await miro.board.viewport.zoomTo(frame);
} else {
if (participant.id !== currentUser?.id) {
await miro.board.collaboration.zoomTo(participant, frame);
}
};
Expand Down Expand Up @@ -410,11 +356,21 @@ export const useBreakout = () => {

if (myself) {
await session.join();
const frame = await miro.board.get({
type: "frame",
id: room.targetId,
});
await miro.board.viewport.zoomTo(frame);
}
await session.invite(everyoneElse);

room.participants.map((participant) =>
updateParticipant(room, participant, { state: "Invitation Pending" }),
updateParticipant(room, participant, {
state:
currentUser?.id !== participant.id
? "Invitation Pending"
: "In Session",
}),
);

session.on("user-joined", handleUserJoined);
Expand Down
4 changes: 0 additions & 4 deletions examples/breakout-rooms/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ export type TimerOpts = {
onStart?: () => void;
};

export type SelectItemsOpts = {
predicate?: (items: Item) => boolean;
};

export type UserSessionEvent = {
userId: string;
sessionId: string;
Expand Down
27 changes: 23 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1064,10 +1064,17 @@
node-fetch "^2.6.9"
typedoc "0.23.24"

"@mirohq/[email protected]":
version "2.10.0"
resolved "https://registry.yarnpkg.com/@mirohq/websdk-types/-/websdk-types-2.10.0.tgz#acb7335957be62290ae651ff6c23b29b24608f7f"
integrity sha512-JMBxUBZW1Pz/NeOuoXhpPGp7DcmkIOICpoSramhNpx5OFnzLed8ECZjQt13axIRpMqQ6IKcGQGGRmOh9zYIHhw==
"@mirohq/websdk-react-hooks@^0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@mirohq/websdk-react-hooks/-/websdk-react-hooks-0.0.3.tgz#0a912abc273b78e115872d4dac31b83b54207f2a"
integrity sha512-pIambSGRVa+tb9v1hkj6h47ZfqEqS9NXMkvUvO70Rjxkq6s1v0c2equKapkU6JE5YdliYSDzsOjgmyd1cI5FCQ==
dependencies:
"@react-hookz/web" "^23.1.0"

"@mirohq/[email protected]":
version "2.11.0"
resolved "https://registry.yarnpkg.com/@mirohq/websdk-types/-/websdk-types-2.11.0.tgz#b72113d71c6f6b3a9fe0dd66ad6bf664a3bbe22e"
integrity sha512-LTy8dUJurKL+GwNIl1ba20XbV7yc1t8irhBXUN/vDJR1G1JlMvpqR6UXghUvEAGee9XGLjxGNWtwI8iNbMbPAQ==
dependencies:
typescript ">=4.6.3 || ~5"

Expand Down Expand Up @@ -2255,6 +2262,18 @@
"@react-types/shared" "^3.22.0"
"@swc/helpers" "^0.5.0"

"@react-hookz/deep-equal@^1.0.4":
version "1.0.4"
resolved "https://registry.yarnpkg.com/@react-hookz/deep-equal/-/deep-equal-1.0.4.tgz#68a71f36cbc88724b3ce6f4036183778b6e7f282"
integrity sha512-N56fTrAPUDz/R423pag+n6TXWbvlBZDtTehaGFjK0InmN+V2OFWLE/WmORhmn6Ce7dlwH5+tQN1LJFw3ngTJVg==

"@react-hookz/web@^23.1.0":
version "23.1.0"
resolved "https://registry.yarnpkg.com/@react-hookz/web/-/web-23.1.0.tgz#4e9bf133c56519924b4c2988aca20d09387f5e0a"
integrity sha512-fvbURdsa1ukttbLR1ASE/XmqXP09vZ1PiCYppYeR1sNMzCrdkG0iBnjxniFSVjJ8gIw2fRs6nqMTbeBz2uAkuA==
dependencies:
"@react-hookz/deep-equal" "^1.0.4"

"@react-stately/calendar@^3.4.2":
version "3.4.2"
resolved "https://registry.yarnpkg.com/@react-stately/calendar/-/calendar-3.4.2.tgz#7dd55cd2f0689bd0a5825326507dcb6b3d7f3d05"
Expand Down

2 comments on commit 02e9a81

@vercel
Copy link

@vercel vercel bot commented on 02e9a81 Dec 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

webhooks-manager – ./examples/webhooks-manager

webhooks-manager-git-main-miro-web.vercel.app
webhooks-manager-miro-web.vercel.app
webhooks-manager-sepia.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 02e9a81 Dec 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

app-examples-wordle – ./examples/wordle

app-examples-wordle-anthonyroux.vercel.app
app-examples-wordle-git-main-anthonyroux.vercel.app
app-examples-wordle.vercel.app

Please sign in to comment.