import { useState } from 'react'; import { mutate } from 'swr'; import { SERVICE_LABELS } from '@musicshare/shared'; import { SignInError, openSignupPage, signInWithService } from '@/lib/connect'; import { Button } from '../ui'; import { ErrorText, SetupActions, SetupHeader, type SetupProps } from './common'; type Provider = 'plex' | 'lastfm'; /** * Sign in to Plex, or to Last.fm for apps that can send plays there. * (Spotify has its own SpotifySetup: via Last.fm or ListenBrainz.) */ export function ServiceSignInSetup({ service, appName, onDone, skipLabel, }: SetupProps & { service: Provider; appName?: string }) { const [done, setDone] = useState(false); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const run = async () => { setBusy(true); setError(null); try { if ((await signInWithService(service)) === 'connected') { await mutate('/connections'); setDone(true); } } catch (err) { setError(err instanceof SignInError ? err.message : `Couldn't connect ${SERVICE_LABELS[service]}. Try again.`); } setBusy(false); }; if (done) { return ( <> ); } if (service === 'lastfm') { return ( <> {error}