import { useEffect, useState } from 'react' import { api, ApiError } from '../api/client' import { connectionModeDescription, connectionModeLabel, connectionReadyLabel, } from '../lib/connectionMode' import type { ThreadsAccountConnectionData } from '../types/api' import { useOnboarding } from '../onboarding/OnboardingContext' import { AcLink, Badge, ChoiceCard, ErrorText, SectionTitle, SuccessText } from './ui' type AccountConnectionModeProps = { accountId: string connectionsPath: string } export function AccountConnectionMode({ accountId, connectionsPath }: AccountConnectionModeProps) { const { refresh: refreshOnboarding } = useOnboarding() const [connection, setConnection] = useState(null) const [loading, setLoading] = useState(true) const [busy, setBusy] = useState(false) const [error, setError] = useState('') const [message, setMessage] = useState('') const load = async () => { if (!accountId) return setLoading(true) setError('') try { const data = await api.get( `/api/v1/threads-accounts/${encodeURIComponent(accountId)}/connection`, { auth: true }, ) setConnection(data) } catch (e) { setError(e instanceof ApiError ? e.message : '載入連線設定失敗') setConnection(null) } finally { setLoading(false) } } useEffect(() => { load().catch(() => undefined) }, [accountId]) const saveDevMode = async (devMode: boolean) => { if (!accountId) return setBusy(true) setError('') setMessage('') try { const data = await api.patch( `/api/v1/threads-accounts/${encodeURIComponent(accountId)}/connection`, { dev_mode: devMode }, { auth: true }, ) setConnection(data) setMessage(devMode ? '已切換為開發模式(爬蟲)' : '已切換為正式模式(API)') await refreshOnboarding() } catch (e) { setError(e instanceof ApiError ? e.message : '儲存失敗') } finally { setBusy(false) } } const prefs = connection?.prefs const devMode = !!prefs?.dev_mode return (
連線模式

決定此帳號走 Threads 官方 API,或本機爬蟲。OAuth、Chrome 同步等細節在{' '} 連線設定

{connection ? (
{connectionModeLabel(devMode)} {connectionReadyLabel(devMode, connection.browser_connected, connection.api_connected)}
) : null}
{loading ? (

載入連線模式…

) : prefs ? ( <>

{connectionModeDescription(devMode)}

saveDevMode(false)} /> saveDevMode(true)} />
) : (

無法載入連線設定。

)}
) }