From 34ad32430a22c9e50334d90e50128657a56a09c2 Mon Sep 17 00:00:00 2001 From: mantikoros Date: Mon, 29 Aug 2022 01:18:52 -0500 Subject: [PATCH] twitch panel logic --- web/components/twitch-panel.tsx | 148 +++++++++++++------------------- web/pages/profile.tsx | 2 +- 2 files changed, 62 insertions(+), 88 deletions(-) diff --git a/web/components/twitch-panel.tsx b/web/components/twitch-panel.tsx index 369fd514..f3988265 100644 --- a/web/components/twitch-panel.tsx +++ b/web/components/twitch-panel.tsx @@ -3,120 +3,94 @@ import React, { useState } from 'react' import toast from 'react-hot-toast' import { copyToClipboard } from 'web/lib/util/copy' -import { initLinkTwitchAccount } from 'web/lib/twitch/link-twitch-account' +import { linkTwitchAccount } from 'web/lib/twitch/link-twitch-account' import { LinkIcon } from '@heroicons/react/solid' -import { User, PrivateUser } from 'common/user' +import { track } from 'web/lib/service/analytics' +import { Button } from './button' +import { LoadingIndicator } from './loading-indicator' +import { Row } from './layout/row' +import { usePrivateUser, useUser } from 'web/hooks/use-user' -export function TwitchPanel(props: { - auth: { user: User; privateUser: PrivateUser } -}) { - const { user, privateUser } = props.auth +export function TwitchPanel() { + const user = useUser() + const privateUser = usePrivateUser() - const [twitchToken, setTwitchToken] = useState('') - const [twitchLoading, setTwitchLoading] = useState(false) - const [twitchLinkError, setTwitchLinkError] = useState('') - const [controlToken, setControlToken] = useState( - undefined - ) - - const linkTwitchAccount = async () => { - if (!privateUser.apiKey) return // TODO: handle missing API key - try { - setTwitchLoading(true) - const [twitchAuthURL, linkSuccessPromise] = await initLinkTwitchAccount( - privateUser.id, - privateUser.apiKey - ) - window.open(twitchAuthURL) - const data = await linkSuccessPromise - setTwitchToken(data.twitchName) - setControlToken(data.controlToken) - } catch (e) { - console.error(e) - toast.error('Failed to link Twitch account: ' + (e as Object).toString()) - } finally { - setTwitchLoading(false) - } - } + const twitchName = privateUser?.twitchInfo?.twitchName + const twitchToken = privateUser?.twitchInfo?.controlToken const linkIcon =