2022-11-18 14:46:50 +00:00
|
|
|
import { CircleIcon } from '@radix-ui/react-icons';
|
2021-12-15 09:28:17 +00:00
|
|
|
import React from 'react';
|
2022-01-22 12:05:24 +00:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2022-12-20 13:02:27 +00:00
|
|
|
import { useLiveKey } from '~/lib/react';
|
2023-01-09 15:31:27 +00:00
|
|
|
import {
|
|
|
|
EventSubNotification,
|
|
|
|
EventSubNotificationType,
|
|
|
|
unwrapEvent,
|
|
|
|
} from '~/lib/eventSub';
|
2022-12-20 14:42:27 +00:00
|
|
|
import { PageContainer, SectionHeader, styled, TextBlock } from '../theme';
|
2022-01-22 12:05:24 +00:00
|
|
|
import WIPNotice from '../components/utils/WIPNotice';
|
2022-11-18 19:28:13 +00:00
|
|
|
import BrowserLink from '../components/BrowserLink';
|
2023-01-09 15:31:27 +00:00
|
|
|
import Scrollbar from '../components/utils/Scrollbar';
|
2022-01-22 12:05:24 +00:00
|
|
|
|
|
|
|
interface StreamInfo {
|
|
|
|
id: string;
|
|
|
|
user_name: string;
|
|
|
|
user_login: string;
|
|
|
|
game_name: string;
|
|
|
|
title: string;
|
|
|
|
viewer_count: number;
|
|
|
|
started_at: string;
|
|
|
|
language: string;
|
|
|
|
thumbnail_url: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const StreamBlock = styled('div', {
|
|
|
|
display: 'grid',
|
|
|
|
gap: '1rem',
|
|
|
|
gridTemplateColumns: '160px 1fr',
|
|
|
|
});
|
|
|
|
const StreamTitle = styled('h3', {
|
|
|
|
gridRow: 1,
|
|
|
|
gridColumn: 2,
|
|
|
|
fontWeight: 400,
|
|
|
|
margin: 0,
|
|
|
|
marginTop: '0.5rem',
|
|
|
|
});
|
|
|
|
const StreamInfo = styled('div', {
|
|
|
|
gridRow: 2,
|
|
|
|
gridColumn: 2,
|
|
|
|
fontWeight: 'bold',
|
|
|
|
margin: 0,
|
|
|
|
marginBottom: '0.5rem',
|
|
|
|
});
|
|
|
|
const LiveIndicator = styled('div', {
|
|
|
|
gridRow: '1/3',
|
|
|
|
gridColumn: 1,
|
|
|
|
display: 'flex',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center',
|
|
|
|
fontWeight: 'bold',
|
|
|
|
zIndex: 2,
|
|
|
|
backgroundSize: 'cover',
|
|
|
|
backgroundPosition: 'center',
|
|
|
|
});
|
2022-11-18 19:28:13 +00:00
|
|
|
const Darken = styled(BrowserLink, {
|
2022-01-22 12:05:24 +00:00
|
|
|
flex: 1,
|
|
|
|
display: 'flex',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center',
|
|
|
|
background: 'rgba(0,0,0,0.5)',
|
|
|
|
width: '100%',
|
|
|
|
height: '100%',
|
|
|
|
gap: '0.5rem',
|
|
|
|
color: '$red11 !important',
|
|
|
|
textDecoration: 'none !important',
|
|
|
|
transition: 'all 0.2s ease-in-out',
|
|
|
|
'&:hover': {
|
|
|
|
opacity: 0.5,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-01-09 15:31:27 +00:00
|
|
|
const EventListContainer = styled('div', {
|
|
|
|
display: 'flex',
|
|
|
|
flexDirection: 'column',
|
|
|
|
gap: '5px',
|
|
|
|
});
|
|
|
|
|
|
|
|
const TwitchEventContainer = styled('div', {
|
|
|
|
background: '$gray3',
|
|
|
|
padding: '8px',
|
|
|
|
borderRadius: '5px',
|
|
|
|
});
|
|
|
|
|
|
|
|
const supportedMessages: EventSubNotificationType[] = [
|
|
|
|
EventSubNotificationType.Followed,
|
|
|
|
];
|
|
|
|
|
|
|
|
function TwitchEvent({ data }: { data: EventSubNotification }) {
|
|
|
|
let content: JSX.Element | string;
|
|
|
|
const message = unwrapEvent(data);
|
|
|
|
switch (message.type) {
|
|
|
|
case EventSubNotificationType.Followed: {
|
|
|
|
content = `${message.event.user_name} followed you!`;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
content = <small>{message.type}</small>;
|
|
|
|
}
|
|
|
|
return <TwitchEventContainer>{content}</TwitchEventContainer>;
|
|
|
|
}
|
|
|
|
|
|
|
|
function TwitchEventLog({ events }: { events: EventSubNotification[] }) {
|
|
|
|
// TODO Include a note specifying that it's not ALL events!!
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<SectionHeader>Latest events</SectionHeader>
|
|
|
|
<Scrollbar vertical={true} viewport={{ maxHeight: '200px' }}>
|
|
|
|
<EventListContainer>
|
|
|
|
{events
|
|
|
|
.filter((ev) => supportedMessages.includes(ev.subscription.type))
|
|
|
|
.map((ev) => (
|
|
|
|
<TwitchEvent
|
|
|
|
key={`${ev.subscription.id}-${ev.subscription.created_at}`}
|
|
|
|
data={ev}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</EventListContainer>
|
|
|
|
</Scrollbar>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function TwitchStreamStatus({ info }: { info: StreamInfo }) {
|
|
|
|
const { t } = useTranslation();
|
|
|
|
return (
|
|
|
|
<StreamBlock>
|
|
|
|
<LiveIndicator
|
|
|
|
css={{
|
|
|
|
backgroundImage: `url(${info.thumbnail_url
|
|
|
|
.replace('{width}', '160')
|
|
|
|
.replace('{height}', '90')})`,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Darken target="_blank" href={`https://twitch.tv/${info.user_login}`}>
|
|
|
|
<CircleIcon /> {t('pages.dashboard.live')}
|
|
|
|
</Darken>
|
|
|
|
</LiveIndicator>
|
|
|
|
<StreamTitle>{info.title}</StreamTitle>
|
|
|
|
<StreamInfo>
|
|
|
|
{info.game_name} -{' '}
|
|
|
|
{t('pages.dashboard.x-viewers', {
|
|
|
|
count: info.viewer_count,
|
|
|
|
})}
|
|
|
|
</StreamInfo>
|
|
|
|
</StreamBlock>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-22 12:05:24 +00:00
|
|
|
function TwitchSection() {
|
|
|
|
const { t } = useTranslation();
|
|
|
|
const twitchInfo = useLiveKey<StreamInfo[]>('twitch/stream-info');
|
2022-11-18 14:46:50 +00:00
|
|
|
// const twitchActivity = useLiveKey<StreamInfo[]>('twitch/chat-activity');
|
2023-01-09 15:31:27 +00:00
|
|
|
const twitchEvents = useLiveKey<EventSubNotification[]>(
|
|
|
|
'twitch/eventsub-history',
|
|
|
|
);
|
|
|
|
console.log(twitchEvents);
|
2022-01-22 12:05:24 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2023-01-09 15:31:27 +00:00
|
|
|
<SectionHeader spacing="none">
|
|
|
|
{t('pages.dashboard.twitch-status')}
|
|
|
|
</SectionHeader>
|
2022-01-22 12:05:24 +00:00
|
|
|
{twitchInfo && twitchInfo.length > 0 ? (
|
2023-01-09 15:31:27 +00:00
|
|
|
<TwitchStreamStatus info={twitchInfo[0]} />
|
2022-01-22 12:05:24 +00:00
|
|
|
) : (
|
2022-12-20 14:42:27 +00:00
|
|
|
<TextBlock>{t('pages.dashboard.not-live')}</TextBlock>
|
2022-01-22 12:05:24 +00:00
|
|
|
)}
|
2023-01-09 15:31:27 +00:00
|
|
|
{twitchEvents ? <TwitchEventLog events={twitchEvents} /> : null}
|
2022-01-22 12:05:24 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
2021-12-15 09:28:17 +00:00
|
|
|
|
|
|
|
export default function Dashboard(): React.ReactElement {
|
2022-01-22 12:05:24 +00:00
|
|
|
return (
|
|
|
|
<PageContainer>
|
|
|
|
<TwitchSection />
|
|
|
|
<WIPNotice />
|
|
|
|
</PageContainer>
|
|
|
|
);
|
2021-12-15 09:28:17 +00:00
|
|
|
}
|