Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Add state-change handling to usePresence #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 33 additions & 26 deletions lib/src/hooks/use-presence.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
import { useState, useEffect, useCallback, useMemo } from "react";
import { HereNowParameters, HereNowResponse } from "pubnub";
import { usePubNub } from "pubnub-react";
import cloneDeep from "lodash.clonedeep";
import { useState, useEffect, useCallback, useMemo } from 'react';
import { HereNowParameters, HereNowResponse } from 'pubnub';
import { usePubNub } from 'pubnub-react';
import cloneDeep from 'lodash.clonedeep';

type ChannelsOccupancy = HereNowResponse["channels"];
type ChannelsOccupancy = HereNowResponse['channels'];
type HookReturnValue = [ChannelsOccupancy, () => Promise<void>, number, Error];

export const usePresence = ({
channels,
channelGroups,
includeUUIDs,
includeState,
}: HereNowParameters = {}): HookReturnValue => {
export const usePresence = ({ channels, channelGroups, includeUUIDs, includeState }: HereNowParameters = {}): HookReturnValue => {
const pubnub = usePubNub();

const [presence, setPresence] = useState<ChannelsOccupancy>({});
const [error, setError] = useState<Error>();

const presenceValues = Object.values(presence);
const total = presenceValues.map((ch) => ch.occupancy).reduce((prev, cur) => prev + cur, 0);
const total = presenceValues.map(ch => ch.occupancy).reduce((prev, cur) => prev + cur, 0);

const options = useMemo(() => ({ channels, channelGroups, includeUUIDs, includeState }), [
channels,
channelGroups,
includeUUIDs,
includeState,
includeState
]);

const command = useCallback(async () => {
Expand All @@ -37,37 +32,49 @@ export const usePresence = ({
}, [pubnub, options]);

const handlePresence = useCallback(
(event) => {
setPresence((presence) => {
const presenceClone = cloneDeep(presence);
event => {
setPresence(pres => {
const presenceClone = cloneDeep(pres);
if (!presenceClone[event.channel]) presenceClone[event.channel] = {};
const channel = presenceClone[event.channel];

if (event.action === "join") {
if (!channel.hasOwnProperty("occupants")) channel.occupants = [];
if (event.action === 'join') {
if (!Object.prototype.hasOwnProperty.call(channel, 'occupants')) channel.occupants = [];
channel.occupancy = event.occupancy;

if (
options.includeUUIDs !== false &&
channel.hasOwnProperty("occupants") &&
!channel.occupants.find((u) => u.uuid == event.uuid)
Object.prototype.hasOwnProperty.call(channel, 'occupants') &&
!channel.occupants.find(u => u.uuid === event.uuid)
) {
const { state, uuid } = event;
channel.occupants.push({ state, uuid });
}
}

if (["leave", "timeout"].includes(event.action)) {
if (['leave', 'timeout'].includes(event.action)) {
channel.occupancy = event.occupancy;

if (
options.includeUUIDs !== false &&
channel.hasOwnProperty("occupants") &&
channel.occupants.find((u) => u.uuid == event.uuid)
Object.prototype.hasOwnProperty.call(channel, 'occupants') &&
channel.occupants.find(u => u.uuid === event.uuid)
) {
presenceClone[event.channel].occupants = channel.occupants.filter(
(u) => u.uuid !== event.uuid
);
presenceClone[event.channel].occupants = channel.occupants.filter(u => u.uuid !== event.uuid);
}
}

if (event.action === 'state-change') {
channel.occupancy = event.occupancy;
if (
options.includeUUIDs !== false &&
Object.prototype.hasOwnProperty.call(channel, 'occupants') &&
channel.occupants.find(u => u.uuid === event.uuid)
) {
const { state, uuid } = event;
const updatedUser = channel.occupants.find(u => u.uuid === uuid);
updatedUser.state = state;
channel.occupants[uuid] = updatedUser;
}
}

Expand Down