From 3aa37f394558fc82d7f934b9d8cc3ea9c4a29b90 Mon Sep 17 00:00:00 2001 From: Bill Date: Wed, 17 Aug 2022 19:13:32 -0400 Subject: [PATCH] Move respect to the session hook --- .../infostand/InfoStandWidgetPetView.tsx | 58 +++++-------------- .../menu/AvatarInfoWidgetAvatarView.tsx | 17 ++---- .../menu/AvatarInfoWidgetOwnPetView.tsx | 40 ++++--------- .../menu/AvatarInfoWidgetPetView.tsx | 38 ++++-------- src/hooks/index.ts | 1 - src/hooks/pets/index.ts | 1 - src/hooks/pets/usePets.ts | 16 ----- src/hooks/session/useSessionInfo.ts | 22 ++++++- 8 files changed, 63 insertions(+), 130 deletions(-) delete mode 100644 src/hooks/pets/index.ts delete mode 100644 src/hooks/pets/usePets.ts diff --git a/src/components/room/widgets/avatar-info/infostand/InfoStandWidgetPetView.tsx b/src/components/room/widgets/avatar-info/infostand/InfoStandWidgetPetView.tsx index 5d803b33..aa23e036 100644 --- a/src/components/room/widgets/avatar-info/infostand/InfoStandWidgetPetView.tsx +++ b/src/components/room/widgets/avatar-info/infostand/InfoStandWidgetPetView.tsx @@ -1,9 +1,9 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { PetRespectComposer, PetType } from '@nitrots/nitro-renderer'; -import { FC, useEffect } from 'react'; -import { AvatarInfoPet, CreateLinkEvent, GetConfiguration, GetSessionDataManager, LocalizeText, SendMessageComposer } from '../../../../../api'; +import { FC } from 'react'; +import { AvatarInfoPet, CreateLinkEvent, GetConfiguration, LocalizeText, SendMessageComposer } from '../../../../../api'; import { Base, Button, Column, Flex, LayoutPetImageView, Text, UserProfileIconView } from '../../../../../common'; -import { usePets, useRoom } from '../../../../../hooks'; +import { useRoom, useSessionInfo } from '../../../../../hooks'; interface InfoStandWidgetPetViewProps { @@ -15,16 +15,10 @@ export const InfoStandWidgetPetView: FC = props => { const { avatarInfo = null, onClose = null } = props; const { roomSession = null } = useRoom(); - const { petRespect, changePetRespect } = usePets(); + const { petRespectRemaining = 0, respectPet = null } = useSessionInfo(); if(!avatarInfo) return null; - useEffect(() => - { - changePetRespect(avatarInfo.respectsPetLeft); - - }, [ avatarInfo ]); - const processButtonAction = (action: string) => { let hideMenu = true; @@ -34,17 +28,9 @@ export const InfoStandWidgetPetView: FC = props => switch (action) { case 'respect': - let newRespectsLeftChange = 0; + respectPet(avatarInfo.id); - changePetRespect(prevValue => - { - newRespectsLeftChange = (prevValue - 1); - - return newRespectsLeftChange; - }); - - GetSessionDataManager().givePetRespect(avatarInfo.id); - if(newRespectsLeftChange > 0) hideMenu = false; + if((petRespectRemaining - 1) >= 1) hideMenu = false; break; case 'buyfood': CreateLinkEvent('catalog/open/' + GetConfiguration('catalog.links')['pets.buy_saddle']); @@ -66,12 +52,6 @@ export const InfoStandWidgetPetView: FC = props => if(hideMenu) onClose(); } - useEffect(() => - { - changePetRespect(avatarInfo.respectsPetLeft); - - }, [ avatarInfo ]); - return ( @@ -140,36 +120,30 @@ export const InfoStandWidgetPetView: FC = props => - { avatarInfo.petType !== PetType.MONSTERPLANT && + { (avatarInfo.petType !== PetType.MONSTERPLANT) && - } - { avatarInfo.isOwner && avatarInfo.petType !== PetType.MONSTERPLANT && + } + { avatarInfo.isOwner && (avatarInfo.petType !== PetType.MONSTERPLANT) && - } + } { !avatarInfo.dead && ((avatarInfo.energy / avatarInfo.maximumEnergy) < 0.98) && avatarInfo.petType === PetType.MONSTERPLANT && - } + } { roomSession?.isRoomOwner && avatarInfo.petType === PetType.MONSTERPLANT && - } + } { avatarInfo.isOwner && - } - { (petRespect > 0) && avatarInfo.petType !== PetType.MONSTERPLANT && + } + { (petRespectRemaining > 0) && (avatarInfo.petType !== PetType.MONSTERPLANT) && - } + { LocalizeText('infostand.button.petrespect', [ 'count' ], [ petRespectRemaining.toString() ]) } + } ); diff --git a/src/components/room/widgets/avatar-info/menu/AvatarInfoWidgetAvatarView.tsx b/src/components/room/widgets/avatar-info/menu/AvatarInfoWidgetAvatarView.tsx index 3a4185aa..9556888e 100644 --- a/src/components/room/widgets/avatar-info/menu/AvatarInfoWidgetAvatarView.tsx +++ b/src/components/room/widgets/avatar-info/menu/AvatarInfoWidgetAvatarView.tsx @@ -3,7 +3,7 @@ import { RoomControllerLevel, RoomObjectCategory, RoomObjectVariable, RoomUnitGi import { FC, useEffect, useMemo, useState } from 'react'; import { AvatarInfoUser, CreateLinkEvent, DispatchUiEvent, GetOwnRoomObject, GetSessionDataManager, GetUserProfile, LocalizeText, MessengerFriend, ReportType, RoomWidgetUpdateChatInputContentEvent, SendMessageComposer } from '../../../../../api'; import { Base, Flex } from '../../../../../common'; -import { useFriends, useHelp, useRoom } from '../../../../../hooks'; +import { useFriends, useHelp, useRoom, useSessionInfo } from '../../../../../hooks'; import { ContextMenuHeaderView } from '../../context-menu/ContextMenuHeaderView'; import { ContextMenuListItemView } from '../../context-menu/ContextMenuListItemView'; import { ContextMenuView } from '../../context-menu/ContextMenuView'; @@ -26,10 +26,10 @@ export const AvatarInfoWidgetAvatarView: FC = p { const { avatarInfo = null, onClose = null } = props; const [ mode, setMode ] = useState(MODE_NORMAL); - const [ respectsLeft, setRespectsLeft ] = useState(0); const { canRequestFriend = null } = useFriends(); const { report = null } = useHelp(); const { roomSession = null } = useRoom(); + const { userRespectRemaining = 0, respectUser = null } = useSessionInfo(); const isShowGiveRights = useMemo(() => { @@ -113,13 +113,9 @@ export const AvatarInfoWidgetAvatarView: FC = p setMode(MODE_RELATIONSHIP); break; case 'respect': { - let newRespectsLeft = (respectsLeft - 1); - - setRespectsLeft(newRespectsLeft); + respectUser(avatarInfo.webID); - GetSessionDataManager().giveRespect(avatarInfo.webID); - - if(newRespectsLeft > 0) hideMenu = false; + if((userRespectRemaining - 1) >= 1) hideMenu = false; break; } case 'ignore': @@ -203,7 +199,6 @@ export const AvatarInfoWidgetAvatarView: FC = p useEffect(() => { setMode(MODE_NORMAL); - setRespectsLeft(avatarInfo.respectLeft); }, [ avatarInfo ]); return ( @@ -223,9 +218,9 @@ export const AvatarInfoWidgetAvatarView: FC = p processAction('whisper') }> { LocalizeText('infostand.button.whisper') } - { (respectsLeft > 0) && + { (userRespectRemaining > 0) && processAction('respect') }> - { LocalizeText('infostand.button.respect', [ 'count' ], [ respectsLeft.toString() ]) } + { LocalizeText('infostand.button.respect', [ 'count' ], [ userRespectRemaining.toString() ]) } } { !canRequestFriend(avatarInfo.webID) && processAction('relationship') }> diff --git a/src/components/room/widgets/avatar-info/menu/AvatarInfoWidgetOwnPetView.tsx b/src/components/room/widgets/avatar-info/menu/AvatarInfoWidgetOwnPetView.tsx index 763a7a00..af889c12 100644 --- a/src/components/room/widgets/avatar-info/menu/AvatarInfoWidgetOwnPetView.tsx +++ b/src/components/room/widgets/avatar-info/menu/AvatarInfoWidgetOwnPetView.tsx @@ -1,7 +1,7 @@ import { PetRespectComposer, PetType, RoomObjectCategory, RoomObjectType, RoomObjectVariable, RoomUnitGiveHandItemPetComposer } from '@nitrots/nitro-renderer'; import { FC, useEffect, useMemo, useState } from 'react'; -import { AvatarInfoPet, CreateLinkEvent, GetConfiguration, GetOwnRoomObject, GetSessionDataManager, LocalizeText, SendMessageComposer } from '../../../../../api'; -import { usePets, useRoom } from '../../../../../hooks'; +import { AvatarInfoPet, CreateLinkEvent, GetConfiguration, GetOwnRoomObject, LocalizeText, SendMessageComposer } from '../../../../../api'; +import { useRoom, useSessionInfo } from '../../../../../hooks'; import { ContextMenuHeaderView } from '../../context-menu/ContextMenuHeaderView'; import { ContextMenuListItemView } from '../../context-menu/ContextMenuListItemView'; import { ContextMenuView } from '../../context-menu/ContextMenuView'; @@ -22,13 +22,7 @@ export const AvatarInfoWidgetOwnPetView: FC = p const { avatarInfo = null, onClose = null } = props; const [ mode, setMode ] = useState(MODE_NORMAL); const { roomSession = null } = useRoom(); - const { petRespect, changePetRespect } = usePets(); - - useEffect(() => - { - changePetRespect(avatarInfo.respectsPetLeft); - - }, [ avatarInfo ]); + const { petRespectRemaining = 0, respectPet = null } = useSessionInfo(); const canGiveHandItem = useMemo(() => { @@ -55,18 +49,9 @@ export const AvatarInfoWidgetOwnPetView: FC = p switch(name) { case 'respect': - let newRespectsLeftChange = 0; + respectPet(avatarInfo.id); - changePetRespect(prevValue => - { - newRespectsLeftChange = (prevValue - 1); - - return newRespectsLeftChange; - }); - - GetSessionDataManager().givePetRespect(avatarInfo.id); - - if(newRespectsLeftChange > 0) hideMenu = false; + if((petRespectRemaining - 1) >= 1) hideMenu = false; break; case 'treat': SendMessageComposer(new PetRespectComposer(avatarInfo.id)); @@ -137,9 +122,6 @@ export const AvatarInfoWidgetOwnPetView: FC = p return MODE_NORMAL; }); - - changePetRespect(avatarInfo.respectsPetLeft); - }, [ avatarInfo ]); return ( @@ -149,9 +131,9 @@ export const AvatarInfoWidgetOwnPetView: FC = p { (mode === MODE_NORMAL) && <> - { (petRespect > 0) && + { (petRespectRemaining > 0) && processAction('respect') }> - { LocalizeText('infostand.button.petrespect', [ 'count' ], [ petRespect.toString() ]) } + { LocalizeText('infostand.button.petrespect', [ 'count' ], [ petRespectRemaining.toString() ]) } } processAction('train') }> { LocalizeText('infostand.button.train') } @@ -177,9 +159,9 @@ export const AvatarInfoWidgetOwnPetView: FC = p { LocalizeText('infostand.button.toggle_riding_permission') } - { (petRespect > 0) && + { (petRespectRemaining > 0) && processAction('respect') }> - { LocalizeText('infostand.button.petrespect', [ 'count' ], [ petRespect.toString() ]) } + { LocalizeText('infostand.button.petrespect', [ 'count' ], [ petRespectRemaining.toString() ]) } } processAction('train') }> { LocalizeText('infostand.button.train') } @@ -196,9 +178,9 @@ export const AvatarInfoWidgetOwnPetView: FC = p processAction('dismount') }> { LocalizeText('infostand.button.dismount') } - { (petRespect > 0) && + { (petRespectRemaining > 0) && processAction('respect') }> - { LocalizeText('infostand.button.petrespect', [ 'count' ], [ petRespect.toString() ]) } + { LocalizeText('infostand.button.petrespect', [ 'count' ], [ petRespectRemaining.toString() ]) } } } { (mode === MODE_MONSTER_PLANT) && diff --git a/src/components/room/widgets/avatar-info/menu/AvatarInfoWidgetPetView.tsx b/src/components/room/widgets/avatar-info/menu/AvatarInfoWidgetPetView.tsx index 29090b49..4219ace9 100644 --- a/src/components/room/widgets/avatar-info/menu/AvatarInfoWidgetPetView.tsx +++ b/src/components/room/widgets/avatar-info/menu/AvatarInfoWidgetPetView.tsx @@ -1,7 +1,7 @@ import { PetRespectComposer, PetType, RoomControllerLevel, RoomObjectCategory, RoomObjectType, RoomObjectVariable, RoomUnitGiveHandItemPetComposer } from '@nitrots/nitro-renderer'; import { FC, useEffect, useMemo, useState } from 'react'; import { AvatarInfoPet, GetOwnRoomObject, GetSessionDataManager, LocalizeText, SendMessageComposer } from '../../../../../api'; -import { usePets, useRoom } from '../../../../../hooks'; +import { useRoom, useSessionInfo } from '../../../../../hooks'; import { ContextMenuHeaderView } from '../../context-menu/ContextMenuHeaderView'; import { ContextMenuListItemView } from '../../context-menu/ContextMenuListItemView'; import { ContextMenuView } from '../../context-menu/ContextMenuView'; @@ -22,13 +22,7 @@ export const AvatarInfoWidgetPetView: FC = props = const { avatarInfo = null, onClose = null } = props; const [ mode, setMode ] = useState(MODE_NORMAL); const { roomSession = null } = useRoom(); - const { petRespect, changePetRespect } = usePets(); - - useEffect(() => - { - changePetRespect(avatarInfo.respectsPetLeft); - - }, [ avatarInfo ]); + const { petRespectRemaining = 0, respectPet = null } = useSessionInfo(); const canPickUp = useMemo(() => { @@ -60,18 +54,9 @@ export const AvatarInfoWidgetPetView: FC = props = switch(name) { case 'respect': - let newRespectsLeftChange = 0; + respectPet(avatarInfo.id); - changePetRespect(prevValue => - { - newRespectsLeftChange = (prevValue - 1); - - return newRespectsLeftChange; - }); - - GetSessionDataManager().givePetRespect(avatarInfo.id); - - if(newRespectsLeftChange > 0) hideMenu = false; + if((petRespectRemaining - 1) >= 1) hideMenu = false; break; case 'treat': SendMessageComposer(new PetRespectComposer(avatarInfo.id)); @@ -104,9 +89,6 @@ export const AvatarInfoWidgetPetView: FC = props = return MODE_NORMAL; }); - - changePetRespect(avatarInfo.respectsPetLeft); - }, [ avatarInfo ]); return ( @@ -114,9 +96,9 @@ export const AvatarInfoWidgetPetView: FC = props = { avatarInfo.name } - { (mode === MODE_NORMAL) && (petRespect > 0) && + { (mode === MODE_NORMAL) && (petRespectRemaining > 0) && processAction('respect') }> - { LocalizeText('infostand.button.petrespect', [ 'count' ], [ petRespect.toString() ]) } + { LocalizeText('infostand.button.petrespect', [ 'count' ], [ petRespectRemaining.toString() ]) } } { (mode === MODE_SADDLED_UP) && <> @@ -124,9 +106,9 @@ export const AvatarInfoWidgetPetView: FC = props = processAction('mount') }> { LocalizeText('infostand.button.mount') } } - { (petRespect > 0) && + { (petRespectRemaining > 0) && processAction('respect') }> - { LocalizeText('infostand.button.petrespect', [ 'count' ], [ petRespect.toString() ]) } + { LocalizeText('infostand.button.petrespect', [ 'count' ], [ petRespectRemaining.toString() ]) } } } { (mode === MODE_RIDING) && @@ -134,9 +116,9 @@ export const AvatarInfoWidgetPetView: FC = props = processAction('dismount') }> { LocalizeText('infostand.button.dismount') } - { (petRespect > 0) && + { (petRespectRemaining > 0) && processAction('respect') }> - { LocalizeText('infostand.button.petrespect', [ 'count' ], [ petRespect.toString() ]) } + { LocalizeText('infostand.button.petrespect', [ 'count' ], [ petRespectRemaining.toString() ]) } } } { (mode === MODE_MONSTER_PLANT) && !avatarInfo.dead && ((avatarInfo.energy / avatarInfo.maximumEnergy) < 0.98) && diff --git a/src/hooks/index.ts b/src/hooks/index.ts index 98a0d049..91a23d23 100644 --- a/src/hooks/index.ts +++ b/src/hooks/index.ts @@ -12,7 +12,6 @@ export * from './inventory'; export * from './mod-tools'; export * from './navigator'; export * from './notification'; -export * from './pets'; export * from './purse'; export * from './rooms'; export * from './session'; diff --git a/src/hooks/pets/index.ts b/src/hooks/pets/index.ts deleted file mode 100644 index 055b581e..00000000 --- a/src/hooks/pets/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './usePets'; diff --git a/src/hooks/pets/usePets.ts b/src/hooks/pets/usePets.ts deleted file mode 100644 index 6453458e..00000000 --- a/src/hooks/pets/usePets.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { useState } from 'react'; -import { useBetween } from 'use-between'; - -const usePetsState = () => -{ - const [ petRespect, setPetRespect ] = useState(0); - - const changePetRespect = (respects: React.SetStateAction) => - { - setPetRespect(respects); - } - - return { petRespect, setPetRespect, changePetRespect }; -} - -export const usePets = () => useBetween(usePetsState); diff --git a/src/hooks/session/useSessionInfo.ts b/src/hooks/session/useSessionInfo.ts index faff5dca..911e4d0c 100644 --- a/src/hooks/session/useSessionInfo.ts +++ b/src/hooks/session/useSessionInfo.ts @@ -1,7 +1,7 @@ import { FigureUpdateEvent, RoomUnitChatStyleComposer, UserInfoDataParser, UserInfoEvent, UserSettingsEvent } from '@nitrots/nitro-renderer'; import { useState } from 'react'; import { useBetween } from 'use-between'; -import { SendMessageComposer } from '../../api'; +import { GetSessionDataManager, SendMessageComposer } from '../../api'; import { useMessageEvent } from '../events'; const useSessionInfoState = () => @@ -9,6 +9,8 @@ const useSessionInfoState = () => const [ userInfo, setUserInfo ] = useState(null); const [ userFigure, setUserFigure ] = useState(null); const [ chatStyleId, setChatStyleId ] = useState(0); + const [ userRespectRemaining, setUserRespectRemaining ] = useState(0); + const [ petRespectRemaining, setPetRespectRemaining ] = useState(0); const updateChatStyleId = (styleId: number) => { @@ -17,12 +19,28 @@ const useSessionInfoState = () => SendMessageComposer(new RoomUnitChatStyleComposer(styleId)); } + const respectUser = (userId: number) => + { + GetSessionDataManager().giveRespect(userId); + + setUserRespectRemaining(GetSessionDataManager().respectsLeft); + } + + const respectPet = (petId: number) => + { + GetSessionDataManager().givePetRespect(petId); + + setPetRespectRemaining(GetSessionDataManager().respectsPetLeft); + } + useMessageEvent(UserInfoEvent, event => { const parser = event.getParser(); setUserInfo(parser.userInfo); setUserFigure(parser.userInfo.figure); + setUserRespectRemaining(parser.userInfo.respectsRemaining); + setPetRespectRemaining(parser.userInfo.respectsPetRemaining); }); useMessageEvent(FigureUpdateEvent, event => @@ -39,7 +57,7 @@ const useSessionInfoState = () => setChatStyleId(parser.chatType); }); - return { userInfo, userFigure, chatStyleId, updateChatStyleId }; + return { userInfo, userFigure, chatStyleId, userRespectRemaining, petRespectRemaining, respectUser, respectPet, updateChatStyleId }; } export const useSessionInfo = () => useBetween(useSessionInfoState);