5180 fichiers 38.5 Mo 0 téléchargements 234 vues mis à jour il y a 5 heures ui
nitro-v36-with-renderer-v36 / src / hooks / hotel-view / useCommunityGoalProgress.ts
useCommunityGoalProgress.ts 1.7 Ko Télécharger le fichier
import {
    CommunityGoalData,
    CommunityGoalProgressMessageEvent,
    CommunityGoalVoteMessageComposer,
    CommunityGoalVoteMessageEvent,
    GetCommunityGoalProgressMessageComposer
} from '@octane/renderer';
import { useCallback, useEffect, useState } from 'react';
import { SendMessageComposer } from '../../api';
import { useMessageEvent } from '../events';

/**
 * CommunityGoalWidget (+ VsMode, VsModeWithVoting): the server's community goal
 * (GetCommunityGoalProgress 1145 -> CommunityGoalProgress 2525) and its vote
 * (CommunityGoalVote 3536 -> acknowledgement 1435, which hides the vote buttons).
 */
export const useCommunityGoalProgress = () => {
    const [goal, setGoal] = useState<CommunityGoalData>(null);
    const [receivedAt, setReceivedAt] = useState(0);
    const [voted, setVoted] = useState(false);

    useMessageEvent<CommunityGoalProgressMessageEvent>(CommunityGoalProgressMessageEvent, (event) => {
        const data = event.getParser()?.data ?? null;

        setGoal(data);
        setReceivedAt(Date.now());
    });

    useMessageEvent<CommunityGoalVoteMessageEvent>(CommunityGoalVoteMessageEvent, (event) => {
        if (event.getParser()?.acknowledged) setVoted(true);
    });

    const requestProgress = useCallback(() => SendMessageComposer(new GetCommunityGoalProgressMessageComposer()), []);

    /** The vote buttons hide on click, before the server answers. */
    const vote = useCallback((option: number) => {
        setVoted(true);
        SendMessageComposer(new CommunityGoalVoteMessageComposer(option));
    }, []);

    useEffect(() => {
        requestProgress();
    }, [requestProgress]);

    return { goal, receivedAt, voted, vote, requestProgress };
};
15Sujets
1Dépôts
20Membres
2En ligne
Profile
Inscrit
Dernière visite
Messages
0
Votes positifs
0
Votes négatifs
0

Nous utilisons des cookies pour assurer le bon fonctionnement du site et améliorer votre expérience. en savoir plus

Autoriser