1 import axios from 'axios';
2 import React from 'react';
4 import Leaderboard from './Leaderboard';
7 const API_BASE = 'https://alttp.localhorst.tv/twitch/guessing-game-leaderboard';
10 const [channelId, setChannelId] = React.useState('');
11 const [leaderboard, setLeaderboard] = React.useState({});
12 const [theme, setTheme] = React.useState('light');
14 React.useEffect(() => {
15 window.Twitch.ext.onAuthorized((authCallback) => {
16 setChannelId(authCallback.channelId);
18 window.Twitch.ext.onContext((context) => {
20 setTheme(context.theme);
25 const fetchLeaderboard = React.useCallback(async (id) => {
27 const rsp = await axios.get(`${API_BASE}/${id}/gtbk`);
28 setLeaderboard(rsp.data);
34 React.useEffect(() => {
36 fetchLeaderboard(channelId);
37 const interval = window.setInterval(() => {
38 fetchLeaderboard(channelId);
41 window.clearInterval(interval);
49 return <div>loading</div>;
52 return <div className={`theme-${theme}`}>
54 <Leaderboard entries={leaderboard.all} />