5180 fichiers 38.5 Mo 0 téléchargements 259 vues mis à jour il y a 5 heures ui
nitro-v36-with-renderer-v36 / src / hooks / mentions / useMentionMessages.ts
useMentionMessages.ts 2.8 Ko Télécharger le fichier
import { MentionListItem, MentionReceivedEvent, MentionsListEvent, MentionsPageEvent } from '@octane/renderer';
import { useCallback, useEffect } from 'react';
import { GetConfigurationValue, GetRoomSession, IMentionEntry, PlaySound } from '../../api';
import { useMessageEvent } from '../events';
import { useNotification } from '../notification/useNotification';
import { requestMentions } from './mentionsRequests';
import { addMention, appendMentions, isMentionsPanelOpen, setMentions } from './mentionsStore';

// Dedicated mention chime served from nitro-assets/sounds/<sample>.mp3.
const MENTION_SOUND_SAMPLE = 'mentions_notification';

const toEntry = (mention: MentionListItem, read: boolean): IMentionEntry => ({
    mentionId: mention.mentionId,
    senderId: mention.senderId,
    senderUsername: mention.senderUsername,
    senderFigure: mention.senderFigure,
    roomId: mention.roomId,
    roomName: mention.roomName,
    message: mention.message,
    mentionType: mention.mentionType,
    timestamp: mention.timestamp,
    read
});

export const useMentionMessages = (): void => {
    const { showMentionBubble } = useNotification();

    const onMentionsList = useCallback((event: MentionsListEvent) => {
        const parser = event.getParser();

        setMentions(
            parser.mentions.map((mention) => toEntry(mention, mention.read)),
            parser.hasMore
        );
    }, []);

    const onMentionsPage = useCallback((event: MentionsPageEvent) => {
        const parser = event.getParser();

        appendMentions(
            parser.mentions.map((mention) => toEntry(mention, mention.read)),
            parser.hasMore
        );
    }, []);

    const onMentionReceived = useCallback(
        (event: MentionReceivedEvent) => {
            if (!GetConfigurationValue<boolean>('mentions_ui.enabled', true)) return;

            const entry = toEntry(event.getParser().mention, false);

            addMention(entry);

            // The panel shows it as it lands, so there is nothing to announce.
            if (isMentionsPanelOpen()) return;

            const inThatRoom = entry.roomId > 0 && GetRoomSession()?.roomId === entry.roomId;

            if (GetConfigurationValue<boolean>('mentions_ui.sound', true)) PlaySound(MENTION_SOUND_SAMPLE);

            // In the room itself the highlighted chat line is the notice; elsewhere a toast says where it came from.
            if (!inThatRoom) showMentionBubble(entry);
        },
        [showMentionBubble]
    );

    useMessageEvent<MentionsListEvent>(MentionsListEvent, onMentionsList);
    useMessageEvent<MentionsPageEvent>(MentionsPageEvent, onMentionsPage);
    useMessageEvent<MentionReceivedEvent>(MentionReceivedEvent, onMentionReceived);

    useEffect(() => {
        if (!GetConfigurationValue<boolean>('mentions_ui.enabled', true)) return;

        requestMentions();
    }, []);
};
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