import * as React from "react"; import {useContext, useState} from "react"; import {Registry} from "tc-shared/events"; import { PrivateConversationInfo, SideHeaderChannelState, SideHeaderEvents, SideHeaderPingInfo, SideHeaderServerInfo, SideHeaderState } from "tc-shared/ui/frames/side/HeaderDefinitions"; import {Translatable, VariadicTranslatable} from "tc-shared/ui/react-elements/i18n"; import {RemoteIconRenderer} from "tc-shared/ui/react-elements/Icon"; import {getIconManager} from "tc-shared/file/Icons"; const StateContext = React.createContext(undefined); const EventsContext = React.createContext>(undefined); const cssStyle = require("./HeaderRenderer.scss"); const Block = (props: { children: [React.ReactElement, React.ReactElement], target: "left" | "right" }) => (
{props.children[0]}
{props.children[1]}
) const ChannelStateRenderer = (props: { info: SideHeaderChannelState }) => { if(props.info.state === "not-connected") { return
Not connected
; } else { let limit; if(props.info.channelMaxUser === -1) { limit = Unlimited } else { limit = props.info.channelMaxUser; } let icon; if(props.info.channelIcon.iconId !== 0) { const remoteIcon = getIconManager().resolveIcon(props.info.channelIcon.iconId, props.info.channelIcon.serverUniqueId, props.info.channelIcon.handlerId); icon = ; } return (
{icon}{props.info.channelName}
{props.info.channelUserCount} / {limit}
); } } const BlockChannelState = (props: { mode: "voice" | "text" }) => { const events = useContext(EventsContext); const [ info, setInfo ] = useState(() => { events.fire("query_current_channel_state", { mode: props.mode }); return { state: "not-connected" }; }); events.reactUse("notify_current_channel_state", event => event.mode === props.mode && setInfo(event.state)); let title; if(props.mode === "voice") { title = You're talking in Channel; } else { title = You're chatting in Channel; } return ( {title} ); } const ServerInfoRenderer = React.memo((props: { info: SideHeaderServerInfo | undefined }) => { if(!props.info) { return
Not connected
; } let icon; if(props.info.icon.iconId !== 0) { const remoteIcon = getIconManager().resolveIcon(props.info.icon.iconId, props.info.icon.serverUniqueId, props.info.icon.handlerId); icon = ; } return (
{icon}{props.info.name}
); }); const BlockServerState = () => { const events = useContext(EventsContext); const [ info, setInfo ] = useState(() => { events.fire("query_server_info"); return undefined; }); events.reactUse("notify_server_info", event => setInfo(event.info)); return ( You're chatting on Server ); } const BlockPing = () => { const events = useContext(EventsContext); const [ pingInfo, setPingInfo ] = useState(() => { events.fire("query_ping"); return undefined; }); events.reactUse("notify_ping", event => setPingInfo(event.ping)); let value, title; if(!pingInfo) { value = (
Not connected
); } else { let pingClass; if(pingInfo.native <= 30) { pingClass = cssStyle.veryGood; } else if(pingInfo.native <= 50) { pingClass = cssStyle.good; } else if(pingInfo.native <= 90) { pingClass = cssStyle.medium; } else if(pingInfo.native <= 200) { pingClass = cssStyle.poor; } else { pingClass = cssStyle.veryPoor; } if(pingInfo.javaScript === undefined) { title = tra("Ping: {}ms", pingInfo.native.toFixed(3)); } else { title = tra("Native: {}ms\nJavascript: {}ms", pingInfo.native.toFixed(3), pingInfo.javaScript.toFixed(3)); } value =
{pingInfo.native.toFixed(0)}ms
; } return ( Your Ping {value} ); }; const BlockPrivateChats = (props: { asButton: boolean }) => { const events = useContext(EventsContext); const [ info, setInfo ] = useState(() => { events.fire("query_private_conversations"); return { unread: 0, open: 0 }; }); events.reactUse("notify_private_conversations", event => setInfo(event.info)); let body; if(info.open === 0) { body = No conversations; } else if(info.open === 1) { body = One conversation; } else { body = {info.open}; } let title; if(info.unread === 0) { title = Private Chats; } else { title = ( Private Chats
{info.unread}
) } return ( {title}
props.asButton && events.fire("action_open_conversation")}> {body}
); } const BlockButtonSwitchToChannelChat = () => { const events = useContext(EventsContext); return ( <> 
events.fire("action_switch_channel_chat")}> Switch to channel chat
) } const BlockButtonOpenConversation = () => { const events = useContext(EventsContext); return ( <> 
events.fire("action_open_conversation")}> Open conversation
) } const BlockButtonBotManage = () => { const events = useContext(EventsContext); return ( <> 
events.fire("action_bot_manage")}> Manage bot
) } const BlockButtonBotSongAdd = () => { const events = useContext(EventsContext); return ( <> 
events.fire("action_bot_add_song")}> Add song
) } const BlockTopLeft = () => ; const BlockTopRight = () => ; const BlockBottomLeft = () => { const state = useContext(StateContext); switch (state.state) { case "conversation": if(state.mode === "private") { return ; } else if(state.mode === "server") { return } else { return ; } case "music-bot": return ; case "none": case "client": default: return null; } } const BlockBottomRight = () => { const events = useContext(EventsContext); const state = useContext(StateContext); const [ ownClient, setOwnClient ] = useState(() => { events.fire("query_client_info_own_client"); return false; }); events.reactUse("notify_client_info_own_client", event => setOwnClient(event.isOwnClient)); switch (state.state) { case "client": if(ownClient) { return null; } else { return ; } case "conversation": return ; case "music-bot": return ; case "none": default: return null; } } export const SideHeaderRenderer = React.memo((props: { events: Registry, state: SideHeaderState }) => { return (
); })