diff --git a/examples/iframe-playground/packages/client/src/App.tsx b/examples/iframe-playground/packages/client/src/App.tsx index d9561073..22c163d9 100755 --- a/examples/iframe-playground/packages/client/src/App.tsx +++ b/examples/iframe-playground/packages/client/src/App.tsx @@ -29,7 +29,7 @@ import * as S from './AppStyles'; import SafeAreas from './pages/SafeAreas'; import ThermalStates from './pages/ThermalStates'; import ActivityChannel from './pages/ActivityChannel'; -import Instance from './pages/Instance'; +import ActivityParticipants from './pages/ActivityParticipants'; // Add contexts here export default function App(): React.ReactElement { @@ -161,10 +161,10 @@ const routes: Record = { name: 'Window Size Tracker', component: WindowSizeTracker, }, - instance: { - path: '/instance', - name: 'Instance Debug', - component: Instance, + activityParticipants: { + path: '/activity-participants', + name: 'Activity Participants', + component: ActivityParticipants, }, }; diff --git a/examples/iframe-playground/packages/client/src/pages/ActivityParticipants.tsx b/examples/iframe-playground/packages/client/src/pages/ActivityParticipants.tsx new file mode 100644 index 00000000..d10b8b15 --- /dev/null +++ b/examples/iframe-playground/packages/client/src/pages/ActivityParticipants.tsx @@ -0,0 +1,64 @@ +import * as React from 'react'; +import discordSdk from '../discordSdk'; +import {EventPayloadData} from '@discord/embedded-app-sdk'; + +export default function ActivityParticipants() { + const [participants, setParticipants] = React.useState< + EventPayloadData<'ACTIVITY_INSTANCE_PARTICIPANTS_UPDATE'>['participants'] + >([]); + const [speakingParticipants, setSpeakingParticipants] = React.useState([]); // Array of user ids who are currently speaking + + React.useEffect(() => { + const updateParticipants = (res: EventPayloadData<'ACTIVITY_INSTANCE_PARTICIPANTS_UPDATE'>) => { + setParticipants(res.participants); + }; + discordSdk.commands.getInstanceConnectedParticipants().then(updateParticipants); + discordSdk.subscribe('ACTIVITY_INSTANCE_PARTICIPANTS_UPDATE', updateParticipants); + + return () => { + discordSdk.unsubscribe('ACTIVITY_INSTANCE_PARTICIPANTS_UPDATE', updateParticipants); + }; + }, []); + + React.useEffect(() => { + const addSpeakingParticipants = (res: EventPayloadData<'SPEAKING_START'>) => { + setSpeakingParticipants((s) => [...s, res.user_id]); + }; + const removeSpeakingParticipants = (res: EventPayloadData<'SPEAKING_STOP'>) => { + setSpeakingParticipants((speakingParticipants) => + speakingParticipants.filter((speakingParticipant) => speakingParticipant !== res.user_id) + ); + }; + discordSdk.subscribe('SPEAKING_START', addSpeakingParticipants, {channel_id: discordSdk.channelId}); + discordSdk.subscribe('SPEAKING_STOP', removeSpeakingParticipants, {channel_id: discordSdk.channelId}); + + return () => { + discordSdk.unsubscribe('SPEAKING_START', addSpeakingParticipants, {channel_id: discordSdk.channelId}); + discordSdk.unsubscribe('SPEAKING_STOP', removeSpeakingParticipants, {channel_id: discordSdk.channelId}); + }; + }, []); + return ( +
+

Tracking instance participants and their speaking state

+
+

This example tracks who is participating in the activity and whether or not they are speaking.

+
+
+
+ Username +
+
+ Speaking Status +
+ {participants.map((user) => { + return ( + +
{user.nickname ?? user.global_name}
+
{speakingParticipants.some((s) => s === user.id) ? 'Speaking' : 'Not Speaking'}
+
+ ); + })} +
+
+ ); +} diff --git a/examples/iframe-playground/packages/client/src/pages/Instance.tsx b/examples/iframe-playground/packages/client/src/pages/Instance.tsx deleted file mode 100644 index 01f25e55..00000000 --- a/examples/iframe-playground/packages/client/src/pages/Instance.tsx +++ /dev/null @@ -1,30 +0,0 @@ -import {useState, useEffect} from 'react'; -import discordSdk from '../discordSdk'; -import {Events, EventPayloadData} from '@discord/embedded-app-sdk'; - -type UpdateEvent = EventPayloadData<'ACTIVITY_INSTANCE_PARTICIPANTS_UPDATE'>; - -export default function Instance() { - const [participants, setParticipants] = useState([]); - useEffect(() => { - const updateParticipants = (res: UpdateEvent) => { - setParticipants(res.participants); - }; - discordSdk.commands.getInstanceConnectedParticipants().then(updateParticipants); - discordSdk.subscribe(Events.ACTIVITY_INSTANCE_PARTICIPANTS_UPDATE, updateParticipants); - - return () => { - discordSdk.unsubscribe(Events.ACTIVITY_INSTANCE_PARTICIPANTS_UPDATE, updateParticipants); - }; - }, []); - return ( -
- Participants: -
    - {participants.map((user) => { - return
  • {user.nickname ?? user.global_name}
  • ; - })} -
-
- ); -}