From 71a0eee1959cef5576fc6419af8bf9ba7bc7baf1 Mon Sep 17 00:00:00 2001 From: simoleo89 Date: Mon, 18 May 2026 21:31:36 +0200 Subject: [PATCH] refactor(hooks/session): migrate useSessionInfo to useUserDataSnapshot MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace the local useState mirror of userFigure / userRespectRemaining / petRespectRemaining (driven by useMessageEvent + useMessageEvent + manual setUser after giveRespect) with a single useUserDataSnapshot() read. Why this works: SessionDataManager already invalidates its snapshot on every state change that mattered to the old hook — UserInfoEvent handler (line 142), FigureUpdateEvent listener (line 117), giveRespect / givePetRespect (lines 540/551). The snapshot's respectsLeft / respectsPetLeft map directly to the parser fields respectsRemaining / respectsPetRemaining the old code mirrored. Net result: 3 useState declarations + 2 useMessageEvent subscriptions removed; respectUser / respectPet become trivial pass-throughs (no post-call setState because the manager's invalidate dispatches the event for us). UserSettingsEvent stays on useMessageEvent — chatStyleId is not in the snapshot. Also drops the deprecated `userInfo: UserInfoDataParser` field from the return shape — no in-tree consumer reads it (verified via grep across src/), it was carried as legacy clutter. Consumers unchanged: ToolbarView, HcCenterView, ChatInputView, AvatarInfoPetTrainingPanelView, InfoStandWidgetPetView, AvatarInfoWidget {Avatar,Pet,OwnPet}View. All destructure individual fields, not the deprecated userInfo. Verification: yarn typecheck clean, yarn test 203/203. --- src/hooks/session/useSessionInfo.ts | 57 +++++++++++------------------ 1 file changed, 21 insertions(+), 36 deletions(-) diff --git a/src/hooks/session/useSessionInfo.ts b/src/hooks/session/useSessionInfo.ts index 9959698..bb2cd11 100644 --- a/src/hooks/session/useSessionInfo.ts +++ b/src/hooks/session/useSessionInfo.ts @@ -1,16 +1,21 @@ -import { FigureUpdateEvent, GetSessionDataManager, RoomUnitChatStyleComposer, UserInfoDataParser, UserInfoEvent, UserSettingsEvent } from '@nitrots/nitro-renderer'; +import { GetSessionDataManager, RoomUnitChatStyleComposer, UserSettingsEvent } from '@nitrots/nitro-renderer'; import { useState } from 'react'; import { useBetween } from 'use-between'; import { SendMessageComposer } from '../../api'; import { useMessageEvent } from '../events'; +import { useUserDataSnapshot } from './useSessionSnapshots'; const useSessionInfoState = () => { - const [ userInfo, setUserInfo ] = useState(null); - const [ userFigure, setUserFigure ] = useState(null); + // figure / respectsLeft / respectsPetLeft come from the renderer's + // referentially-stable IUserDataSnapshot. The snapshot is invalidated + // (and re-derived on next read) inside SessionDataManager whenever + // the underlying state changes: UserInfoEvent + FigureUpdateEvent + + // giveRespect/givePetRespect all call invalidateUserDataSnapshot(). + // So we no longer need to mirror those fields into local useState. + const userData = useUserDataSnapshot(); + const [ chatStyleId, setChatStyleId ] = useState(0); - const [ userRespectRemaining, setUserRespectRemaining ] = useState(0); - const [ petRespectRemaining, setPetRespectRemaining ] = useState(0); const updateChatStyleId = (styleId: number) => { @@ -19,36 +24,8 @@ 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 => - { - const parser = event.getParser(); - - setUserFigure(parser.figure); - }); + const respectUser = (userId: number) => GetSessionDataManager().giveRespect(userId); + const respectPet = (petId: number) => GetSessionDataManager().givePetRespect(petId); useMessageEvent(UserSettingsEvent, event => { @@ -57,7 +34,15 @@ const useSessionInfoState = () => setChatStyleId(parser.chatType); }); - return { userInfo, userFigure, chatStyleId, userRespectRemaining, petRespectRemaining, respectUser, respectPet, updateChatStyleId }; + return { + userFigure: userData.figure, + chatStyleId, + userRespectRemaining: userData.respectsLeft, + petRespectRemaining: userData.respectsPetLeft, + respectUser, + respectPet, + updateChatStyleId + }; }; export const useSessionInfo = () => useBetween(useSessionInfoState);