|
| 1 | +<template> |
| 2 | + <component :is="chatListComponent" :chatItems="sortedChatItems"></component> |
| 3 | +</template> |
| 4 | + |
| 5 | +<script setup lang="ts"> |
| 6 | +import { DefaultChatList, ColorfulChatList } from "#components"; |
| 7 | +import { encodeFormatString } from "~/lib/utils"; |
| 8 | +import type { ChatItem } from "~/lib/interfaces"; |
| 9 | +
|
| 10 | +const props = defineProps<{ |
| 11 | + theme: string | null; |
| 12 | + maxChatSize: number | null; |
| 13 | + chzzkChannelId: string | null; |
| 14 | + twitchChannel: string | null; |
| 15 | + youtubeHandle: string | null; |
| 16 | + isUseOpenDcconSelector: boolean | null; |
| 17 | +}>(); |
| 18 | +const chatListComponent = computed(() => { |
| 19 | + switch (props.theme) { |
| 20 | + case "colorful": |
| 21 | + return ColorfulChatList; |
| 22 | + case "default": |
| 23 | + default: |
| 24 | + return DefaultChatList; |
| 25 | + } |
| 26 | +}); |
| 27 | +
|
| 28 | +const maxChatSize = computed(() => { |
| 29 | + if (props.maxChatSize === null) { |
| 30 | + return 100; |
| 31 | + } |
| 32 | + return props.maxChatSize; |
| 33 | +}); |
| 34 | +
|
| 35 | +const { chatItems: chzzkChatItems } = useChzzk( |
| 36 | + props.chzzkChannelId, |
| 37 | + maxChatSize |
| 38 | +); |
| 39 | +
|
| 40 | +const { chatItems: twitchChatItems } = useTwitch( |
| 41 | + props.twitchChannel, |
| 42 | + maxChatSize |
| 43 | +); |
| 44 | +
|
| 45 | +const { chatItems: youtubeLiveChatItems } = useYoutubeLive( |
| 46 | + props.youtubeHandle, |
| 47 | + maxChatSize |
| 48 | +); |
| 49 | +
|
| 50 | +const { stickerItems } = useOpenDcconSelector( |
| 51 | + props.isUseOpenDcconSelector ? props.twitchChannel : null |
| 52 | +); |
| 53 | +
|
| 54 | +function handleStickers(chat: ChatItem) { |
| 55 | + const stickers: { [key: string]: string } = {}; |
| 56 | + for (const stickerItem of stickerItems.value) { |
| 57 | + if (chat.message.includes(`~${stickerItem.id}`)) { |
| 58 | + stickers[`~${stickerItem.id}`] = stickerItem.url; |
| 59 | + } |
| 60 | + } |
| 61 | + return stickers; |
| 62 | +} |
| 63 | +
|
| 64 | +const sortedChatItems = computed(() => { |
| 65 | + return [ |
| 66 | + ...chzzkChatItems.value, |
| 67 | + ...twitchChatItems.value, |
| 68 | + ...youtubeLiveChatItems.value, |
| 69 | + ] |
| 70 | + .sort((a, b) => a.timestamp - b.timestamp) |
| 71 | + .slice(-maxChatSize.value) |
| 72 | + .map((chat) => { |
| 73 | + const stickers = handleStickers(chat); |
| 74 | +
|
| 75 | + const newChat = { ...chat, extra: { ...chat.extra, stickers } }; |
| 76 | +
|
| 77 | + const encodeTargets = { |
| 78 | + ...newChat.extra.emojis, |
| 79 | + ...newChat.extra.stickers, |
| 80 | + }; |
| 81 | + const { message: newMessage, targets: newTargets } = encodeFormatString( |
| 82 | + newChat.message, |
| 83 | + Object.keys(encodeTargets) |
| 84 | + ); |
| 85 | +
|
| 86 | + const newEmojis: { [key: string]: string } = {}; |
| 87 | + if (newChat.extra.emojis) { |
| 88 | + const oldEmojis = newChat.extra.emojis; |
| 89 | + Object.keys(oldEmojis).forEach((key) => { |
| 90 | + if (key in newTargets) { |
| 91 | + newEmojis[newTargets[key]] = oldEmojis[key]; |
| 92 | + } |
| 93 | + }); |
| 94 | + } |
| 95 | +
|
| 96 | + const newStickers: { [key: string]: string } = {}; |
| 97 | + if (newChat.extra.stickers) { |
| 98 | + const oldStickers = newChat.extra.stickers; |
| 99 | + Object.keys(oldStickers).forEach((key) => { |
| 100 | + if (key in newTargets) { |
| 101 | + newStickers[newTargets[key]] = oldStickers[key]; |
| 102 | + } |
| 103 | + }); |
| 104 | + } |
| 105 | +
|
| 106 | + return { |
| 107 | + ...newChat, |
| 108 | + message: newMessage, |
| 109 | + extra: { |
| 110 | + ...newChat.extra, |
| 111 | + emojis: newEmojis, |
| 112 | + stickers: newStickers, |
| 113 | + }, |
| 114 | + }; |
| 115 | + }); |
| 116 | +}); |
| 117 | +</script> |
0 commit comments