import { Label, Modal, TextInput, Button } from 'flowbite-react' import generator, { MegalodonInterface, detector } from 'megalodon' import { useState } from 'react' import { db } from '@/db' type NewProps = { opened: boolean close: () => void } export default function New(props: NewProps) { const [sns, setSNS] = useState<'mastodon' | 'pleroma' | 'firefish' | 'friendica' | null>(null) const [domain, setDomain] = useState('') const [client, setClient] = useState() const [clientId, setClientId] = useState() const [clientSecret, setClientSecret] = useState() const close = () => { setSNS(null) setDomain('') setClient(undefined) setClientId(undefined) setClientSecret(undefined) props.close() } const checkDomain = async () => { const input = document.getElementById('domain') as HTMLInputElement setDomain(input.value) const url = `https://${input.value}` const sns = await detector(url) setSNS(sns) const client = generator(sns, url) setClient(client) const appData = await client.registerApp('Whalebird', {}) setClientId(appData.client_id) setClientSecret(appData.client_secret) global.ipc.invoke('open-browser', appData.url) } const authorize = async () => { const input = document.getElementById('authorization') as HTMLInputElement if (!client || !clientId || !clientSecret) return const tokenData = await client.fetchAccessToken(clientId, clientSecret, input.value) if (!sns) return const cli = generator(sns, `https://${domain}`, tokenData.access_token, 'Whalebird') const acct = await cli.verifyAccountCredentials() await db.accounts.add({ username: acct.data.username, account_id: acct.data.id, avatar: acct.data.avatar, client_id: clientId, client_secret: clientSecret, access_token: tokenData.access_token, refresh_token: tokenData.refresh_token, url: `https://${domain}`, domain: domain, sns: sns }) close() } return ( <> Add account
{sns === null && ( <>
{' '} )} {sns && ( <>
{' '} )}
) }