From 050c80609b35d39db5153770c55eed52714fd320 Mon Sep 17 00:00:00 2001 From: mantikoros Date: Fri, 26 Aug 2022 11:35:32 -0500 Subject: [PATCH] manually merge in Phil's code --- web/components/twitch-panel.tsx | 122 +++++++++++++++++++++++++ web/hooks/use-redirect-after-signup.ts | 21 ++++- web/lib/twitch/link-twitch-account.ts | 25 +++++ web/pages/profile.tsx | 3 + 4 files changed, 168 insertions(+), 3 deletions(-) create mode 100644 web/components/twitch-panel.tsx create mode 100644 web/lib/twitch/link-twitch-account.ts diff --git a/web/components/twitch-panel.tsx b/web/components/twitch-panel.tsx new file mode 100644 index 00000000..369fd514 --- /dev/null +++ b/web/components/twitch-panel.tsx @@ -0,0 +1,122 @@ +import clsx from 'clsx' +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 { LinkIcon } from '@heroicons/react/solid' +import { User, PrivateUser } from 'common/user' + +export function TwitchPanel(props: { + auth: { user: User; privateUser: PrivateUser } +}) { + const { user, privateUser } = props.auth + + 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 linkIcon =