2020-12-31 01:56:51 -05:00
|
|
|
import React, { useState, useEffect } from "react";
|
|
|
|
|
|
|
|
const PartyContext = React.createContext();
|
|
|
|
|
|
|
|
export function PartyProvider({ session, children }) {
|
|
|
|
const [partyState, setPartyState] = useState({});
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
function handleSocketPartyState(partyState) {
|
|
|
|
if (partyState) {
|
|
|
|
const { [session.id]: _, ...otherMembersState } = partyState;
|
|
|
|
setPartyState(otherMembersState);
|
|
|
|
} else {
|
|
|
|
setPartyState({});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-31 20:44:56 -05:00
|
|
|
session.socket?.on("party_state", handleSocketPartyState);
|
2020-12-31 01:56:51 -05:00
|
|
|
|
|
|
|
return () => {
|
2020-12-31 20:44:56 -05:00
|
|
|
session.socket?.off("party_state", handleSocketPartyState);
|
2020-12-31 01:56:51 -05:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<PartyContext.Provider value={partyState}>{children}</PartyContext.Provider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default PartyContext;
|