import { Link, Redirect, Router, useLocation } from '@reach/router'; import React, { useEffect, useRef, useState } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import { useTranslation } from 'react-i18next'; import { RootState } from '../store'; import { createWSClient } from '../store/api/reducer'; import Home from './pages/Home'; import HTTPPage from './pages/HTTP'; import TwitchPage from './pages/twitch/Main'; import StulbePage from './pages/stulbe/Main'; import LoyaltyPage from './pages/loyalty/Main'; import DebugPage from './pages/Debug'; import LoyaltySettingPage from './pages/loyalty/Settings'; import LoyaltyRewardsPage from './pages/loyalty/Rewards'; import LoyaltyUserListPage from './pages/loyalty/UserList'; import LoyaltyGoalsPage from './pages/loyalty/Goals'; import LoyaltyRedeemQueuePage from './pages/loyalty/Queue'; import TwitchSettingsPage from './pages/twitch/APISettings'; import TwitchBotSettingsPage from './pages/twitch/BotSettings'; import TwitchBotCommandsPage from './pages/twitch/Commands'; import StulbeConfigPage from './pages/stulbe/Config'; import StulbeWebhooksPage from './pages/stulbe/Webhook'; import TwitchBotTimersPage from './pages/twitch/Timers'; import { ConnectionStatus } from '../store/api/types'; import Field from './components/Field'; import TwitchBotAlertsPage from './pages/twitch/Alerts'; interface RouteItem { name?: string; route: string; subroutes?: RouteItem[]; } const menu: RouteItem[] = [ { route: '/' }, { route: '/http' }, { route: '/twitch', subroutes: [ { route: '/twitch/settings' }, { route: '/twitch/bot/settings' }, { route: '/twitch/bot/commands' }, { route: '/twitch/bot/timers' }, { route: '/twitch/bot/alerts' }, ], }, { route: '/loyalty', subroutes: [ { route: '/loyalty/settings' }, { route: '/loyalty/users' }, { route: '/loyalty/queue' }, { route: '/loyalty/rewards' }, { route: '/loyalty/goals' }, ], }, { route: '/stulbe', subroutes: [{ route: '/stulbe/config' }, { route: '/stulbe/webhooks' }], }, ]; function AuthModal(): React.ReactElement { const { t } = useTranslation(); const [password, setPassword] = useState(''); const inputRef = useRef(); useEffect(() => { if (inputRef?.current) { inputRef.current.focus(); } }); const submit = () => { localStorage.setItem('password', password); window.location.reload(); }; return (

{t('auth.header')}

{t('auth.message')} setPassword(ev.target.value)} onKeyUp={(ev) => { if (ev.key === 'Enter' || ev.code === 'Enter') { submit(); } }} />
); } export default function App(): React.ReactElement { const loc = useLocation(); const { t } = useTranslation(); const client = useSelector((state: RootState) => state.api.client); const connected = useSelector( (state: RootState) => state.api.connectionStatus, ); const dispatch = useDispatch(); // Create WS client useEffect(() => { if (!client) { dispatch( createWSClient({ address: process.env.NODE_ENV === 'development' ? 'ws://localhost:4337/ws' : `ws://${loc.host}/ws`, password: localStorage.password, }), ); } }, []); if (connected === ConnectionStatus.AuthenticationNeeded) { return ; } if (!client) { return
{t('system.loading')}
; } const basepath = '/ui/'; const routeItem = ({ route, name, subroutes }: RouteItem) => (
  • { const active = isCurrent || (subroutes && isPartiallyCurrent); return { className: active ? 'is-active' : '', }; }} to={`${basepath}${route}`.replace(/\/\//gi, '/')} > {name ?? t(`pages.${route}`)} {subroutes ? (
      {subroutes.map(routeItem)}
    ) : null}
  • ); return (
    {connected !== ConnectionStatus.Connected ? (
    {t('system.connection-lost')}
    ) : null}
    ); }