From 0fc346fda8dd0be0399f75d6f02e825ae97ce7a4 Mon Sep 17 00:00:00 2001 From: Phil Date: Fri, 16 Sep 2022 13:45:46 +0100 Subject: [PATCH] Add/remove bot from channel working. --- web/pages/twitch.tsx | 124 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 104 insertions(+), 20 deletions(-) diff --git a/web/pages/twitch.tsx b/web/pages/twitch.tsx index 6aa47821..bd745a91 100644 --- a/web/pages/twitch.tsx +++ b/web/pages/twitch.tsx @@ -17,9 +17,16 @@ import { Title } from 'web/components/title' import { useSaveReferral } from 'web/hooks/use-save-referral' import { useTracking } from 'web/hooks/use-tracking' import { usePrivateUser, useUser } from 'web/hooks/use-user' -import { firebaseLogin, getUserAndPrivateUser } from 'web/lib/firebase/users' +import { + firebaseLogin, + getUserAndPrivateUser, + updatePrivateUser, +} from 'web/lib/firebase/users' import { track } from 'web/lib/service/analytics' -import { linkTwitchAccountRedirect } from 'web/lib/twitch/link-twitch-account' +import { + linkTwitchAccountRedirect, + updateBotEnabledForUser, +} from 'web/lib/twitch/link-twitch-account' import { copyToClipboard } from 'web/lib/util/copy' function ButtonGetStarted(props: { @@ -189,21 +196,24 @@ function BotSetupStep(props: { stepNum: number buttonName?: string buttonOnClick?: MouseEventHandler + overrideButton?: ReactNode children: ReactNode }) { - const { stepNum, buttonName, buttonOnClick, children } = props + const { stepNum, buttonName, buttonOnClick, overrideButton, children } = props return ( - {buttonName && ( + {(overrideButton || buttonName) && ( <> - + {overrideButton ?? ( + + )} )} @@ -215,6 +225,81 @@ function BotSetupStep(props: { ) } +function BotConnectButton(props: { + privateUser: PrivateUser | null | undefined +}) { + const { privateUser } = props + const [loading, setLoading] = useState(false) + + const updateBotConnected = (connected: boolean) => async () => { + if (!privateUser) return + const twitchInfo = privateUser.twitchInfo + if (!twitchInfo) return + + const error = connected + ? 'Failed to add bot to your channel' + : 'Failed to remove bot from your channel' + const success = connected + ? 'Added bot to your channel' + : 'Removed bot from your channel' + + setLoading(true) + toast.promise( + updateBotEnabledForUser(privateUser, connected) + .then(() => + updatePrivateUser(privateUser.id, { + twitchInfo: { ...twitchInfo, botEnabled: connected }, + }) + ) + .finally(() => setLoading(false)), + { loading: 'Updating bot settings...', error, success }, + { + loading: { + className: '!max-w-sm', + }, + success: { + className: + '!bg-primary !transition-all !duration-500 !text-white !max-w-sm', + }, + error: { + className: + '!bg-red-400 !transition-all !duration-500 !text-white !max-w-sm', + }, + } + ) + } + + return ( + <> + {privateUser?.twitchInfo?.botEnabled ? ( + + ) : ( + + )} + + ) +} + function SetUpBot(props: { user?: User | null privateUser?: PrivateUser | null @@ -223,18 +308,17 @@ function SetUpBot(props: { const twitchLinked = privateUser?.twitchInfo?.twitchName const controlToken = privateUser?.twitchInfo?.controlToken - const linkIcon =